Nodejs實(shí)現(xiàn)微信分賬的示例代碼
公司的業(yè)務(wù)的場景需要用到微信分賬的功能、對著官網(wǎng)文檔調(diào)試了一下午才調(diào)通、記錄下使用Nodejs微信分賬的流程。
前提條件
- 在微信商戶平臺 產(chǎn)品中心->我的產(chǎn)品,支付擴(kuò)展工具中 開通分賬的功能
- 添加分賬接收方。 這一步不設(shè)置的話回報一個*分賬接收方關(guān)系不存在,請檢查參數(shù)中每個接收方的關(guān)系。*錯誤
- 在商戶平臺獲取商戶id和secrect
- 需要將apiclient_cert.pem、 apiclient_key傳到服務(wù)器某個目錄下面
具體實(shí)現(xiàn)
// @router post -> share -> /common/payment/share async 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' // 這一塊的參數(shù)官網(wǎng)文檔上有詳細(xì)的說明 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 // 生成隨機(jī)字符串 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; }
如果遇到簽名不通過的問題??梢詫⒛闵傻膄ormData放到接口簽名校驗(yàn)工具進(jìn)行逐步驗(yàn)證、
到此這篇關(guān)于Nodejs實(shí)現(xiàn)微信分賬的示例代碼的文章就介紹到這了,更多相關(guān)Nodejs 微信分賬內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- nodejs的錯誤處理過程記錄
- nodejs環(huán)境快速操作mysql數(shù)據(jù)庫的方法詳解
- Nodejs實(shí)現(xiàn)定時爬蟲的完整實(shí)例
- NodeJS和瀏覽器中this關(guān)鍵字的不同之處
- nodejs處理tcp連接的核心流程
- Nodejs 數(shù)組的隊列以及forEach的應(yīng)用詳解
- 一文秒懂nodejs中的異步編程
- 在nodejs中創(chuàng)建child process的方法
- nodejs中使用worker_threads來創(chuàng)建新的線程的方法
- Nodejs 微信小程序消息推送的實(shí)現(xiàn)
- nodejs中的異步編程知識點(diǎn)詳解
- nodejs+express最簡易的連接數(shù)據(jù)庫的方法
- windows如何把已安裝的nodejs高版本降級為低版本(圖文教程)
- NodeJS配置CORS實(shí)現(xiàn)過程詳解
- 如何利用nodejs自動定時發(fā)送郵件提醒(超實(shí)用)
- nodeJs項(xiàng)目在阿里云的簡單部署
- 如何利用nodejs實(shí)現(xiàn)命令行游戲
- 搞懂什么是Node.js原來這么簡單
相關(guān)文章
Node.js通過身份證號驗(yàn)證年齡、出生日期與性別方法示例
最近工作中需要對身份證號的年齡、出生日期與性別進(jìn)行驗(yàn)證,所以這篇文章主要介紹了Node.js通過身份證號驗(yàn)證年齡、出生日期與性別的方法,在介紹完node.js的實(shí)現(xiàn)方法后又給大家分類的利用JS實(shí)現(xiàn)的方法,需要的朋友可以參考下。2017-03-03Node.js+jade+mongodb+mongoose實(shí)現(xiàn)爬蟲分離入庫與生成靜態(tài)文件的方法
下面小編就為大家?guī)硪黄狽ode.js+jade+mongodb+mongoose實(shí)現(xiàn)爬蟲分離入庫與生成靜態(tài)文件的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09從零開始學(xué)習(xí)Node.js系列教程四:多頁面實(shí)現(xiàn)的數(shù)學(xué)運(yùn)算示例
這篇文章主要介紹了Node.js多頁面實(shí)現(xiàn)的數(shù)學(xué)運(yùn)算,涉及nodejs請求響應(yīng)、數(shù)值傳遞、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04Node.js實(shí)現(xiàn)斷點(diǎn)續(xù)傳
上傳圖片、Excel等,畢竟幾M的大小可以很快就上傳到服務(wù)器。 針對于上傳視頻等大文件幾百M(fèi)或者幾G的大小,就需要等待比較長的時間。這就產(chǎn)生了對應(yīng)的解決方法,對于大文件上傳時的暫停、斷網(wǎng)、網(wǎng)絡(luò)較差的情況下, 使用切片+斷點(diǎn)續(xù)傳就能夠很好的應(yīng)對上述的情況2021-06-06