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.
Add this line to your Gemfile:
gem 'wx_pay'
or development version
gem 'wx_pay', :github => 'jasl/wx_pay'
And then execute:
$ bundle
Create config/initializers/wx_pay.rb
and put following configurations into it.
# required
WxPay.appid = 'YOUR_APPID'
WxPay.key = 'YOUR_KEY'
WxPay.mch_id = 'YOUR_MCH_ID'
WxPay.debug_mode = true # default is `true`
# cert, see https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3
# using PCKS12
WxPay.set_apiclient_by_pkcs12(File.read(pkcs12_filepath), pass)
# if you want to use `generate_authorize_req` and `authenticate`
WxPay.appsecret = 'YOUR_SECRET'
# optional - configurations for RestClient timeout, etc.
WxPay.extra_rest_client_options = {timeout: 2, open_timeout: 3}
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.
Check official document for detailed request params and return fields
WxPay supports both JSAPI, NATIVE and APP.
# required fields
params = {
body: '测试商品',
out_trade_no: 'test003',
total_fee: 1,
spbill_create_ip: '127.0.0.1',
notify_url: 'https://making.dev/notify',
trade_type: 'JSAPI', # could be "JSAPI", "NATIVE" or "APP",
openid: 'OPENID' # required when trade_type is `JSAPI`
}
WxPay::Service.invoke_unifiedorder params
will create an payment request and return a WxPay::Result instance(subclass of Hash) contains parsed result.
If your trade type is "JSAPI", the result would be like this.