WeixinPay
A simple Wechat pay ruby gem, without unnecessary magic or wrapper.
copied from alipay .
Please read official document first: https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN.
Installation
Add this line to your Gemfile:
gem 'weixin_pay', :git => 'https://github.com/agideo/weixin_pay'
And then execute:
$ bundle
Usage
Config
Create config/initializers/weixin_pay.rb
and put following configurations into it.
WeixinPay.appid = 'YOUR_APPID'
WeixinPay.key = 'YOUR_KEY'
WeixinPay.mch_id = 'YOUR_MCH_ID'
Note: You should create your APIKEY (Link to 微信商户平台) first if you haven't, and pay attention that the length of the APIKEY should be 32.
APIs
TODO check required fields
micropay (提交刷卡支付)
WeixinPay supports REST.
order_code = SecureRandom.uuid.tr('-', '')
WeixinPay::Pay.micrpay({
attach: "微信刷卡支付",
body: "购买商品",
device_info: '88888888',
out_trade_no: order_code,
spbill_create_ip: '127.0.0.1',
total_fee: 1,
auth_code: 'xxxxxxxxxxxxxxxxxxx'
})
WeixinPay::Pay.micrpay params
will create an payment request and return a WeixinPay::Result instance(subclass of Hash) contains parsed result.
The result would be like this.
result = WeixinPay::Pay.micrpay({
...
...
})
result.raw
}
Return true if both return_code
and result_code
equal SUCCESS
result.success?
orderquery (查询订单)
WeixinPay supports REST.
WeixinPay::Pay.orderquery({
out_trade_no: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
})
WeixinPay::Pay.orderquery params
will create an payment request and return a WeixinPay::Result instance(subclass of Hash) contains parsed result.
The result would be like this.
result = WeixinPay::Pay.orderquery({
...
...
})
result.raw
{
"success":true,
"paid":true,
"out_trade_no":"b6c603e17874453fb513f7b46a5706e0",
"trade_state":"SUCCESS",
"errorobj":{
"result_code":"SUCCESS",
"err_code":null,
"err_code_des":null,
"return_msg":"OK"
}
}
Details link to Weixin API doc 提交刷卡支付API
TODO
Write test for WeixinPay gem
Contributing
Bug report or pull request are welcome.
Make a pull request
-
- Fork it Fork it ( https://github.com/agideo/weixin_pay )
-
- Create your feature branch (
git checkout -b my-new-feature
)
-
- Commit your changes (
git commit -am 'Add some feature'
)
-
- Push to the branch (
git push origin my-new-feature
)
-
- Create new Pull Request
Please write unit test with your code if necessary.
License
This project rocks and uses MIT-LICENSE.