一个非官方的简单微信支付插件
A simple Wechat pay ruby gem, without unnecessary magic or wrapper. copied from alipay .
Please read official document first: https://pay.weixin.qq.com/wiki/doc/api/index.html.
Add this line to your Gemfile:
gem 'wx_pay'or development version
gem 'wx_pay', :github => 'jasl/wx_pay'And then execute:
$ bundleCreate config/initializers/wx_pay.rb and put following configurations into it.
…If you need to use sandbox mode.
(Please be aware, the WeChat has aborted the sandbox env already)
WxPay.appid = 'YOUR_APPID'
WxPay.mch_id = 'YOUR_MCH_ID' # required type is String, otherwise there will be cases where JS_PAY can pay but the APP cannot pay
WxPay.debug_mode = true # default is `true`
WxPay.sandbox_mode = true # default is `false`
result = WxPay::Service.get_sandbox_signkey
WxPay.key = result['sandbox_signkey']
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 MWEB, JSAPI, NATIVE and APP.
# required fields
params = {
body: '测试商品',
out_trade_no: 'test003',
total_fee: 1,
spbill_create_ip: '127.0.0.1',
notify_url: 'http://making.dev/notify',
trade_type: 'JSAPI', # could be "MWEB", ""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 "MWEB", the result would be like this.
r = WxPay::Service.invoke_unifiedorder params
# => {
# "return_code"=>"SUCCESS",
# "return_msg"=>"OK",
# "appid"=>"YOUR APPID",
# "mch_id"=>"YOUR MCH_ID",
# "nonce_str"=>"8RN7YfTZ3OUgWX5e",
# "sign"=>"623AE90C9679729DDD7407DC7A1151B2",
# "result_code"=>"SUCCESS",
# "prepay_id"=>"wx2014111104255143b7605afb0314593866",
# "mweb_url"=>"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx2016121516420242444321ca0631331346&package=1405458241",
# "trade_type"=>"MWEB"
# }If your trade type is "JSAPI", the result would be like this.
r = WxPay::Service.invoke_unifiedorder params
# => {
# "return_code"=>"SUCCESS",
# "return_msg"=>"OK",
# "appid"=>"YOUR APPID",
# "mch_id"=>"YOUR MCH_ID",
# "nonce_str"=>"8RN7YfTZ3OUgWX5e",
# "sign"=>"623AE90C9679729DDD7407DC7A1151B2",
# "result_code"=>"SUCCESS",
# "prepay_id"=>"wx2014111104255143b7605afb0314593866",
# "trade_type"=>"JSAPI"
# }"JSAPI" requires openid in params, in most cases I suggest you using omniauth with omniauth-wechat-oauth2 to resolve this, but
wx_payprovidesgenerate_authorize_urlandauthenticateto help you get Wechat authorization in simple case.
If your trade type is "NATIVE", the result would be like this.
r = WxPay::Service.invoke_unifiedorder params
# => {
# "return_code"=>"SUCCESS",
# "return_msg"=>"OK",
# "appid"=>"YOUR APPID",
# "mch_id"=>"YOUR MCH_ID",
# "nonce_str"=>"8RN7YfTZ3OUgWX5e",
# "sign"=>"623AE90C9679729DDD7407DC7A1151B2",
# "result_code"=>"SUCCESS",
# "prepay_id"=>"wx2014111104255143b7605afb0314593866",
# "code_url"=>"weixin://"
# "trade_type"=>"NATIVE"
# }Return true if both return_code and result_code equal SUCCESS
r.success? # => true…# required fields
params = {
prepayid: '1101000000140415649af9fc314aa427', # fetch by call invoke_unifiedorder with `trade_type` is `JSAPI`
noncestr: SecureRandom.hex(16),
}
# call generate_js_pay_req
r = WxPay::Service.generate_js_pay_req params
# {
# "appId": "wx020c5c792c8537de",
# "package": "prepay_id=wx20160902211806a11ccee7a20956539837",
# "nonceStr": "2vS5AJUD7uyaa5h9",
# "timeStamp": "1472822286",
# "signType": "MD5",
# "paySign": "A52433CB75CA8D58B67B2BB45A79AA01"
# }A simple example of processing notify for Rails Action Controller.
# config/routes.rb
post "notify" => "orders#notify"
# app/controllers/orders_controller.rb
def notify
result = Hash.from_xml(request.body.read)["xml"]
if WxPay::Sign.verify?(result)
# find your order and process the post-paid logic.
render :xml => {return_code: "SUCCESS"}.to_xml(root: 'xml', dasherize: false)
else
render :xml => {return_code: "FAIL", return_msg: "签名失败"}.to_xml(root: 'xml', dasherize: false)
end
endA simple example of processing notify for Grape v1.2.2 .
# Gemfile
gem 'multi_xml'
# config/routes.rb
mount WechatPay::Api => '/'
# app/api/wechat_pay/api.rb
module WechatPay
class Api 5, :level => :h ).to_img.resize(200, 200).save("public/uploads/qrcode/#{@order.id.to_s}_#{Time.now.to_i.to_s}.png")
@qrcode_url = "/uploads/qrcode/#{@order.id.to_s}_#{Time.now.to_i.to_s}.png"No documents yet, check lib/wx_pay/service.rb
All functions have third argument options,
you can pass appid, mch_id, key, apiclient_cert, apiclient_key as a hash.
For example
another_account = {appid: 'APPID', mch_id: 'MCH_ID', key: 'KEY'}.freeze
WxPay::Service.generate_app_pay_req params, another_account.dupBug report or pull request are welcome.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)Please write unit test with your code if necessary.
This project rocks and uses MIT-LICENSE.
暂无开放 Issues,或尚未同步最近议题。