我参考api文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_8.shtml,想要添加分账接收方,请求参数按官方示例传入的,也检查了没错误,为什么总是报“请求参数错误”。
我使用的node开发,用的wechatpay-node-v3这个npm包。
const pay = new WxPay({
appid: config.appid,
mchid: config.mchid,
publicKey: fs.readFileSync('../apiclient_cert.pem'), // 公钥
privateKey: fs.readFileSync('../apiclient_key.pem'), // 秘钥
serial_no: config.serial_no
});
const params =
{
"appid": config.appid,
"type": "MERCHANT_ID",
"account": mchid1,
//"name": "hu89ohu89ohu89o",
"relation_type": "STORE",
"custom_relation": "代理商"
};
const nonce_str = Math.random().toString(36).substr(2, 15), // 随机字符串
timestamp = parseInt(+new Date() / 1000 + '').toString(), // 时间戳 秒
url = '/v3/profitsharing/receivers/add';
// 获取签名
const signature = pay.getSignature('POST', nonce_str, timestamp, url, params); // 如果是get 请求 则不需要params 参数拼接在url上 例如 /v3/pay/transactions/id/12177525012014?mchid=1230000109
// 获取头部authorization 参数
const authorization = pay.getAuthorization(nonce_str, timestamp, signature);
const result = await request
.post('https://api.mch.weixin.qq.com/v3/profitsharing/receivers/add')
.send(params)
.set({
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
Authorization: authorization,
'Accept-Encoding': 'gzip',
//"Wechatpay-Serial" : config.serial_no
});
console.log('添加分账方结果为:==========>', result.body);