Method: WechatPay::Sign.generate_app_payment_params_from_prepay_id_and_appid

Defined in:
lib/wechat-pay/sign.rb

.generate_app_payment_params_from_prepay_id_and_appid(appid, prepay_id) ⇒ Object

Generate payment params with appid and prepay_id for invoking the wechat pay in app

Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_2_7.shtml

Take app for example

appid = 'appid for mobile'

params = {
  sp_appid: 'Your appid',
  sp_mchid: 'Your mchid',
  description: 'pay',
  out_trade_no: 'Order Number',
  amount: {
    total: 10
  },
  sub_mchid: 'Your sub mchid',
  notify_url: 'the url'
}
result = WechatPay::Ecommerce.invoke_transactions_in_app(params).body
# => { prepay_id => 'wx201410272009395522657a690389285100' }
prepay_id = result['prepay_id']
WechatPay::Sign.generate_app_payment_params_from_prepay_id_and_appid(appid, prepay_id)
# => params for invoking the wechat pay in app


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wechat-pay/sign.rb', line 43

def generate_app_payment_params_from_prepay_id_and_appid(appid, prepay_id)
  timestamp = Time.now.to_i.to_s
  noncestr = SecureRandom.hex
  string = build_app_paysign_string(appid, timestamp, noncestr, prepay_id)

  {
    appId: appid,
    partnerId: WechatPay.mch_id,
    timeStamp: timestamp,
    nonceStr: noncestr,
    prepayId: prepay_id,
    packageValue: 'Sign=WXPay',
    sign: sign_string(string)
  }.stringify_keys
end