Nodejs实现微信分账的示例代码

 5468

这篇文章主要介绍了Nodejs实现微信分账的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

公司的业务的场景需要用到微信分账的功能、对着官网文档调试了一下午才调通、记录下使用Nodejs微信分账的流程。

前提条件

在微信商户平台 产品中心->我的产品,支付扩展工具中 开通分账的功能

添加分账接收方。 这一步不设置的话回报一个*分账接收方关系不存在,请检查参数中每个接收方的关系。*错误

在商户平台获取商户id和secrect

需要将apiclient_cert.pem、 apiclient_key传到服务器某个目录下面

  1. // @router post -> share -> /common/payment/share
  2. async share() {
  3.     const { ctx } = this
  4.     const nonce_str = ctx.service.wx.randomStr()
  5.     // 商户id
  6.     const mch_id = '123456'
  7.     // x小程序appid
  8.     const appid = 'wx123456'
  9.     // 订单号
  10.     const out_order_no = '1609745196755nFvdMaYub2'
  11.     // 微信支付订单号
  12.     const transaction_id = '4200000801202101044301662433'
  13.     // 商户secrect
  14.     const key = '9813490da1ffb80afaa36f6f1265e490'
  15.     // 这一块的参数官网文档上有详细的说明
  16.     const params = {
  17.         appid,
  18.         mch_id,
  19.         nonce_str,
  20.         out_order_no,
  21.         receivers: `[{"account": "123qwe","amount": 1,"description": "description","type": "PERSONAL_OPENID"}]`,
  22.         sign_type: 'HMAC-SHA256',
  23.         transaction_id,
  24.     }
  25.     // 签名方式必须是HMAC-SHA256
  26.     const sign = ctx.service.wx.sign(params, key, 'HMAC-SHA256')
  27.     // xmlString
  28.     const formData = '<xml>
  29.         <appid>${appid}</appid>
  30.         <mch_id>${mch_id}</mch_id>
  31.         <nonce_str>${nonce_str}</nonce_str> 
  32.         <out_order_no>${out_order_no}</out_order_no>
  33.         <transaction_id>${transaction_id}</transaction_id>
  34.         <sign>${sign}</sign>
  35.         <sign_type>HMAC-SHA256</sign_type>
  36.         <receivers>${params.receivers}</receivers>
  37.     </xml>'
  38.     const res = await ctx.curl(
  39.         "https://api.mch.weixin.qq.com/secapi/pay/profitsharing",
  40.         {
  41.             // 需要使用证书apiclient_cert
  42.             cert: fs.readFileSync(path.join(__dirname,'../../../cert/apiclient_cert.pem')),
  43.             // 需要使用证书apiclient_key
  44.             key: fs.readFileSync(path.join(__dirname,'../../../cert/apiclient_key.pem')),
  45.             method: "post",
  46.             data: formData,
  47.         }
  48.     )
  49.     const datastring = res.data.toString()
  50.         xml2js.parseString(datastring, (err, result) => {
  51.         if (err) {
  52.             ctx.throw(422, err)
  53.         }
  54.         console.log(result)
  55.     })
  56. }   
  57. // randomStr
  58. // 生成随机字符串
  59. randomStr(len = 24) {
  60.     const str ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  61.     let result = '';
  62.     for (let i = 0; i < len; i++) {
  63.         result += str[Math.floor(Math.random() * str.length)];
  64.     }
  65.     return result;
  66. }
  67. // 签名
  68. // mchKey是商户secrect,否则签名不通过
  69. sign(data, mchKey, signType = 'MD5') {
  70.     const keys = [];
  71.     for (const key in data) {
  72.         if (data[key] !== undefined) {
  73.             keys.push(key);
  74.         }
  75.     }
  76.     // 字典排序=>key=value
  77.     const stringA = keys
  78.     .sort()
  79.     .map(key => `${key}=${decodeURIComponent(data[key])}`)
  80.     .join('&');
  81.     // 拼接商户key
  82.     const stringSignTemp = stringA + '&key=' + mchKey;
  83.     // 加密
  84.     let hash;
  85.     if (signType === 'MD5') {
  86.         hash = crypto.createHash('md5').update(stringSignTemp);
  87.     } else {
  88.         hash = crypto.createHmac('sha256', mchKey).update(stringSignTemp, 'utf8');
  89.     }
  90.  
  91.     const paySign = hash.digest('hex').toUpperCase();
  92.     return paySign;
  93. }

如果遇到签名不通过的问题。可以将你生成的formData放到接口签名校验工具进行逐步验证:


6007d496c0d03.jpg


分账接口其他常见问题


到此这篇关于Nodejs实现微信分账的示例代码的文章就介绍到这了,更多相关Nodejs 微信分账内容请关注站长图库。



TAG标签:
本文网址:https://www.zztuku.com/detail-8617.html
站长图库 - Nodejs实现微信分账的示例代码
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐