小程序云开发,接入微信支付,添加分账接收方
昨天调了一天,找不出原因的签名错误一直在发生!
微信支付接口签名校验工具
https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=20_1
使用签名校验工具,对一模一样的StringA原字符串进行编译,我生成的原sign值和签名校验工具生成新sign值总是不一样,不知该从和下手修改了。
//数据比对8:
原sign值:C714553B1E86EE6D6B190EA204890A4998F50325DD67B42D81B2C30DE9D7F380
新sign值:D8BE851C430B79BD374FC4D406944C8E175642A791A831ED23D453302D634EC9
求将以下StringA原字符串编译为签名校验工具同样结果的小程序示例代码,谢谢!
StringA:
appid=wx8c6e071527acdd96&mch_id=1374850001&nonce_str=762ZnhskWpfrysj0&receiver= { "account":"oBGH2RefE5Jj6T3IUP05sc2lgCcx", "name": "阿江", "relation_type": "USER", "type": "PERSONAL_OPENID" } &sign_type=HMAC-SHA256&key=693o12Kl341eZ81n1A5732a58Eb15cGF
非常感谢!
云开发-分账功能-获取HMAC-SHA256签名 搞定了,花了两天时间,终于解决了 const crypto = require('crypto') cloud.init() const db = cloud.database() //sha256算法-签名 function paysign({ ...args }) { let sa = [] for (let k in args) sa.push(k + '=' + args[k]) sa.push('key=' + key) return crypto.createHmac('sha256',key).update(sa.join('&')).digest('hex').toUpperCase() } exports.main = async (event, context) => { const appid = event.userInfo.appId const nonce_str = Math.random().toString(36).substr(2, 15) // const nonce_str = "jzjcgu3opt" const timeStamp = parseInt(Date.now() / 1000) + '' const out_order_no = "otn" + nonce_str + timeStamp // const out_order_no = "otnjzjcgu3opt1611509574" const transaction_id = "4200000807202101243055977824" //是否分账 const rece = [{ type: "MERCHANT_ID", account: "1516231761", amount: Number("10"), description: "分到商户" }, ] //分账户信息 const receivers = JSON.stringify(rece) let formData = "<xml>" formData += "<appid>" + appid + "</appid>" //appid formData += "<mch_id>" + mch_id + "</mch_id>" //商户号 formData += "<nonce_str>" + nonce_str + "</nonce_str>" //随机数 formData += "<out_order_no>" + out_order_no + "</out_order_no>" //分账商户订单号-随机生成 formData += "<receivers>" + receivers + "</receivers>" //分账接收方 formData += "<transaction_id>" + transaction_id + "</transaction_id>" //微信订单号 formData += "<sign>" + paysign({ appid, mch_id, nonce_str, out_order_no, receivers, transaction_id, }) + "</sign>" formData += "</xml>" return formData }
有段示例代码就好了,帮顶
看我的提问,贴上了代码。
我的代码(云函数中调用):
const CryptoJS = require('crypto-js') let partnerKey=693o12Kl341eZ81n1A5732a58Eb15cGF let StringA="appid=wx8c6e071527acdd96&mch_id=1374850001&nonce_str=762ZnhskWpfrysj0&receiver= { "account":"oBGH2RefE5Jj6T3IUP05sc2lgCcx", "name": "阿江", "relation_type": "USER", "type": "PERSONAL_OPENID" } &sign_type=HMAC-SHA256&key="+partnerKey StringA = CryptoJS.HmacSHA256(StringA, partnerKey) StringA = CryptoJS.enc.Hex.stringify(StringA) StringA = StringA.toUpperCase()