Nodejs实现微信分账的示例代码
导读:收集整理的这篇文章主要介绍了Nodejs实现微信分账的示例代码,觉得挺不错的,现在分享给大家,也给大家做个参考。 公司的业务的场景需要用到微信分账的功能、对着官网文档调试了一下午才调通、...
收集整理的这篇文章主要介绍了Nodejs实现微信分账的示例代码,觉得挺不错的,现在分享给大家,也给大家做个参考。 公司的业务的场景需要用到微信分账的功能、对着官网文档调试了一下午才调通、记录下使用Nodejs微信分账的流程。
前提条件
- 在微信商户平台 产品中心-> 我的产品,支付扩展工具中 开通分账的功能
- 添加分账接收方。 这一步不设置的话回报一个*分账接收方关系不存在,请检查参数中每个接收方的关系。*错误
- 在商户平台获取商户id和secrect
- 需要将apiclient_cert.PEm、 apiclient_key传到服务器某个目录下面
具体实现
// @router post -> share -> /common/payment/shareasync share() { const { ctx } = this const nonce_str = ctx.service.wx.randomStr() // 商户id const mch_id = '123456' // x小程序appid const appid = 'wx123456' // 订单号 const out_order_no = '1609745196755nFvdMaYub2' // 微信支付订单号 const transaction_id = '4200000801202101044301662433' // 商户secrect const key = '9813490da1ffb80afaa36f6f1265e490' // 这一块的参数官网文档上有详细的说明 const params = { appid, mch_id, nonce_str, out_order_no, receivers: `[{ "account": "123qwe","amount": 1,"description": "description","type": "PERSONAL_OPENID"} ]`, sign_type: 'HMAC-sHA256', transaction_id, } // 签名方式必须是HMAC-SHA256 const sign = ctx.service.wx.sign(params, key, 'HMAC-SHA256') // XMlString const formData = `xML> appid> ${ appid} /appid> mch_id> ${ mch_id} /mch_id> nonce_str> ${ nonce_str} /nonce_str> out_order_no> ${ out_order_no} /out_order_no> transaction_id> ${ transaction_id} /transaction_id> sign> ${ sign} /sign> sign_type> HMAC-SHA256/sign_type> receivers> ${ params.receivers} /receivers> /xml> ` const res = await ctx.curl( "https://api.mch.weixin.QQ.COM/secapi/pay/PRofITsharing", { // 需要使用证书apiclient_cert cert: fs.reaDFileSync(path.join(__dirname,'../../../cert/apiclient_cert.pem')), // 需要使用证书apiclient_key key: fs.readFileSync(path.join(__dirname,'../../../cert/apiclient_key.pem')), method: "post", data: formData, } ) const datastring = res.data.toString() xml2js.parseString(datastring, (err, result) => { if (err) { ctx.throw(422, err) } console.LOG(result) } )} // randomStr// 生成随机字符串randomStr(len = 24) { const str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let result = ''; for (let i = 0; i len; i++) { result += str[Math.floor(Math.random() * str.length)]; } return result; } // 签名// mchKey是商户secrect,否则签名不通过sign(data, mchKey, signType = 'MD5') { const keys = []; for (const key in data) { if (data[key] !== undefined) { keys.push(key); } } // 字典排序=> key=value const stringA = keys .sort() .map(key => `${ key} =${ decodeURIcomponent(data[key])} `) .join('& '); // 拼接商户key const stringSignTemp = stringA + '& key=' + mchKey; // 加密 let hash; if (signType === 'MD5') { hash = crypto.createHash('md5').update(stringSignTemp); } else { hash = crypto.createHmac('sha256', mchKey).update(stringSignTemp, 'utf8'); } const paySign = hash.digest('hex').toUpperCase(); return paySign; }
如果遇到签名不通过的问题。可以将你生成的formData放到接口签名校验工具进行逐步验证、
分账接口其他常见问题
到此这篇关于Nodejs实现微信分账的示例代码的文章就介绍到这了,更多相关Nodejs 微信分账内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章:- nodejs的错误处理过程记录
- nodejs环境快速操作mysql数据库的方法详解
- Nodejs实现定时爬虫的完整实例
- NodeJS和浏览器中this关键字的不同之处
- nodejs处理tcp连接的核心流程
- Nodejs 数组的队列以及forEach的应用详解
- 一文秒懂nodejs中的异步编程
- 在nodejs中创建child process的方法
- nodejs中使用worker_threads来创建新的线程的方法
- Nodejs 微信小程序消息推送的实现
- nodejs中的异步编程知识点详解
- nodejs+express最简易的连接数据库的方法
- windows如何把已安装的nodejs高版本降级为低版本(图文教程)
- NodeJS配置CORS实现过程详解
- 如何利用nodejs自动定时发送邮件提醒(超实用)
- nodeJs项目在阿里云的简单部署
- 如何利用nodejs实现命令行游戏
- 搞懂什么是Node.js原来这么简单
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Nodejs实现微信分账的示例代码
本文地址: https://pptw.com/jishu/594131.html