The company's business scenario requires the use of the WeChat account splitting function. I debugged it according to the official website documentation for an afternoon before it was finally working, and recorded the process of using Nodejs WeChat account splitting. Prerequisites
Specific implementation// @router post -> share -> /common/payment/share async share() { const { ctx } = this const nonce_str = ctx.service.wx.randomStr() //Merchant id const mch_id = '123456' // x applet appid const appid = 'wx123456' // Order number const out_order_no = '1609745196755nFvdMaYub2' // WeChat payment order number const transaction_id = '4200000801202101044301662433' //Merchant secrect const key = '9813490da1ffb80afaa36f6f1265e490' // The parameters of this block are described in detail in the official documentation 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, } // The signature method must be 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", { // Need to use the certificate apiclient_cert cert: fs.readFileSync(path.join(__dirname,'../../../cert/apiclient_cert.pem')), // Need to use the certificate 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 // Generate a random string 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; } // Signature // mchKey is the merchant's secrect, otherwise the signature will not pass sign(data, mchKey, signType = 'MD5') { const keys = []; for (const key in data) { if (data[key] !== undefined) { keys.push(key); } } // Dictionary sort => key=value const stringA = keys .sort() .map(key => `${key}=${decodeURIComponent(data[key])}`) .join('&'); // Concatenate merchant key const stringSignTemp = stringA + '&key=' + mchKey; // encrypt 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; } If you encounter the problem of signature failure. You can put the formData you generated into the interface signature verification tool for step-by-step verification. Other common problems with the account splitting interface This is the end of this article about the sample code for implementing WeChat account splitting with Nodejs. For more relevant content about WeChat account splitting with Nodejs, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 5.7.17 winx64 free installation version configuration method graphic tutorial
>>: A method of hiding processes under Linux and the pitfalls encountered
The explain command is the primary way to see how...
1. Create a new rtmp directory in the nginx sourc...
Table of contents 1 Difference 1.1 Space Occupanc...
Copy code The code is as follows: <span style=...
As shown below, if it were you, how would you ach...
This article introduces the sample code of CSS3 t...
Table of contents 1. What is the execution contex...
1. Create a Docker network docker network create ...
Summary: Configure nginx reverse proxy jira and i...
Preface nginx uses a multi-process model. When a ...
Table of contents 1. Function Binding 2. With par...
Problem description: structure: test has two fiel...
1. Introduction to Macvlan Before the emergence o...
Introduction to CentOS CentOS is an enterprise-cl...
I accidentally discovered a great artificial inte...