A unofficial jdpay ruby gem. Thanks for alipay and wx_pay
JdPay official document: http://payapi.jd.com/.
See Contributors page.Special thanks to all of them.
Add this line to your application's Gemfile:
gem 'jdpay'
or development version
gem 'jdpay', :github => 'genkin-he/jdpay'
And then execute:
$ bundle
require 'jd_pay'
# NOTE: 京东支付与京东付款码支付是两套账号密钥.
# 京东支付配置
JdPay.mch_id = 'YOUR_MERCHANT_NUM'
JdPay.des_key = 'YOUR_3DES_KEY'
JdPay.md5_key = 'YOUR_MD5_KEY'
JdPay.pri_key = 'YOUR_RSA_PRIVATE_KEY'
# 京东付款码支付配置
JdPay.qr_mch_id = 'YOUR_MERCHANT_NUM'
JdPay.qr_des_key = 'YOUR_3DES_KEY'
JdPay.qr_pri_key = 'YOUR_RSA_PRIVATE_KEY'
# jd's rsa public key, not merchant's.
# JdPay.pub_key = 'JDPAY_RSA_PUBLIC_KEY'
# JdPay.debug_mode = true # Enable parameter check. Default is true.
You can set default key, or pass a key directly to service method:
Service.uniorder({
tradeNum: 'TRADE_NUM',
}, {
mch_id: 'YOUR_MERCHANT_NUM',
des_key: 'YOUR_3DES_KEY',
md5_key: 'YOUR_MD5_KEY',
pri_key: 'YOUR_RSA_PRIVATE_KEY'
})
Check JdPay official document for detailed request params and return fields
商户系统先调用该接口在京东支付服务后台生成交易单,再按扫码、APP等不同场景发起支付
uniorder
JdPay::Service.uniorder(params, options = {})
# required fields
# [:tradeNum, :tradeName, :amount, :orderType, :notifyUrl, :userId]
params = {
tradeNum: '123456780',
tradeName: '测试商品',
amount: '1',
orderType: '0',
notifyUrl: 'http://making.dev/notify',
userId: '123456780'
}
result = JdPay::Service.uniorder(params)
# Used for app
signData = JdPay::Sign.md5_sign(result['jdpay']['orderId'], options)
支付请求接口提供给商户向京东支付服务发送支付请求数据集合,京东支付服务会根据请求数据验证商户身份,以及验证支付信息是否被篡改。验证通过后,京东支付服务会在当前页面弹出支付页面弹框。
pc_pay
# options
# need_redirect_url, Boolean, true: return a url, false: return a html, default: false
JdPay::Service.pc_pay(params, options = {})
# required keys
# [:tradeNum, :tradeName, :amount, :orderType, :notifyUrl, :callbackUrl, :userId]
params = {
tradeNum: '12345678',
tradeName: '测试商品',
amount: '1',
orderType: '0',
notifyUrl: 'http://making.dev/notify',
callbackUrl: 'http://frontend.com/return',
userId: "0000001"
}
JdPay::Service.pc_pay(params)
在线支付接口的手机版本,用法参照在线支付PC接口,不再赘述。
h5_pay
# options
# need_redirect_url, Boolean, true: return a url, false: return a html, default: false
JdPay::Service.h5_pay(params, options = {})
Due to unstable redirect url in JDPay's side, we manually organize the redirect url on our own. Please check method get_redirect_url for more details. If any unexpected response is met, please fill an issue.
交易查询接口是为了处理商户服务器长时间没有接收到支付结果的情况设计的。一般情况,支付结果会通过前端同步返回和京东支付服务器的异步通知发送到商户服务。但是为避免特殊情况商户服务器仍然没有接收到支付结果,这时候商户服务可以通过主动查询交易结果的接口查询支付状态。
query
JdPay::Service.query(params, options = {})
# required keys:
# cost trade query [:tradeNum, :tradeType]
# refund trade query [:tradeNum, :oTradeNum, :tradeType]
params = {
tradeNum: '12345678',
tradeType: '1',
oTradeNum: "12345677",
}
JdPay::Service.query(params)
退款申请接口提供给商户发起自动退款的能力。如果用户在商户系统下单支付以后发起退款,商户验证通过之后可以自动发起退款请求。同时京东支付商户管理后台提供手动退款的功能。
refund
JdPay::Service.refund(params, options = {})
# refund required keys [:tradeNum, :oTradeNum, :amount, :notifyUrl]
params = {
tradeNum: '12345678',
oTradeNum: "12345677",
amount: '1',
notifyUrl: 'http://making.dev/notify',
}
JdPay::Service.refund(params)
撤销申请接口提供给商户发起自动撤销的能力。对于未支付的订单撤销后不可再次支付,对于支付成功的订单则发起退款。
revoke
JdPay::Service.revoke(params, options = {})
# revoke required keys [:tradeNum, :oTradeNum, :amount, :notifyUrl]
params = {
tradeNum: '12345678',
oTradeNum: "12345677",
amount: '1',
}
JdPay::Service.revoke(params)
商户后台将收到的京东异步通知XML内容解密及验签.
notify_verify
JdPay::Service.notify_verify(x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1NzFlZDE5NTZhMzM5MjI1Mjk3NmVlMjYwODJjOWMyNjUyMDllMzE4ZTBmMTY0MDgzYTU0ODMyOTFmNTFhMTcyMGYwMTQwOTg1NjdkYjZkZGZhZjI5NzE1ZTA5YjFhNDczNjZkNTY3MDYwZDRmZmIzZjVjNjgyZTczZjFmNjJmY2YxYWE1NDAxZDk4NDE0NjdhZDE5OTMxZGZkZDdjNWI0YzZlNTkzNjU3OGRkZTA1MmNhM2JiZDhmYjg3NWE3NDY2NjA0YTU0M2ExOWNiZjQyNzlhM2UyMDc3MzI3YjI3NWFlYjQ3ZmU3NjMyZTAxN2E4ZmZkY2UyZjg3M2I2ZTcwYTE4Y2MzNWE3YjFiNmZjYzJkY2E4MjNkZGNmNmFhOWRlZWQzYjE3NTI5MGQ5NDZkMTQ5NmFhZjJiOGM0OGEwNzU3OGM4ODIzZTVhNTU2YjU1OTk5ZTJiMjEwYmYzMjFmN2M5MmU3Yzc2NjJiMzdmNmI1Y2JjNjFiYjllYjY0Zjc3NWMwMDVlNGY4YmVjNzhmZWNmZGFmNWJkNjc2ODY1ZTAwYzllODRkNjg2ZDNmZTBjOTJiOTYwMjlmMWIxYThjMzQzYmE3YzkxNTUzMDI4OTBjYzU0YTJiMGM2Yjg1ZTQ=</encrypt>
</jdpay>
EOF
JdPay::Service.notify_verify(xml_str)
用户支付成功后,京东支付系统根据支付接口中callbackUrl所传地址跳转到商户的支付成功页,并采用POST方式回传相应的订单参数。
verify_redirection
JdPay::Service.verify_redirection(params, options = {})
# In your controller:
begin
decrypted_params = JdPay::Service.verify_redirection(request.params, {})
rescue => _ex
render status: :bad_request
end
若商户用户与京东用户关联,下次支付时可跳过身份识别环境进行支付。此接口提供了用户关系查询功能。
user_relation
JdPay::Service.user_relation(params, options = {})
# user_relation required keys [:userId]
params = { userId: '12345678' }
JdPay::Service.user_relation(params)
解绑用户关系,解绑后,用户下一次支付,需重新通过手机等登录方式进行身份验证。
cancel_user
JdPay::Service.cancel_user(params, options = {})
# cancel_user required keys [:userId]
params = { userId: '12345678' }
JdPay::Service.cancel_user(params)
付款码支付接口用于客户使用付款码支付时,商户使用扫码枪完成一键下单并支付功能。在支付过程中对支付请求进行校验后,完成支付流程。
qrcode_pay
JdPay::QrService.qrcode_pay(params, options = {})
# required fields
# [:tradeNum, :tradeName, :amount, :device, :token]
params = {
tradeNum: '123456780',
tradeName: '测试商品',
amount: '1',
device: '0001',
token: '2563256985478547856'
}
JdPay::QrService.qrcode_pay(params)
JdPay::QrService.query(params, options = {})
# refer to:JdPay::Service.query(params, options = {})
JdPay::QrService.refund(params, options = {})
# refer to:JdPay::Service.refund(params, options = {})
JdPay::QrService.notify_verify(params, options = {})
# refer to:JdPay::Service.notify_verify(params, options = {})
Bug report or pull request are welcome.
- Fork it
- 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.
The gem is available as open source under the terms of the MIT License.
Donate to maintainer let him make this gem better.