{"code":0,"msg":"虚拟支付签名","data":{"appid":"wx1333401d1bc09510","signData":"{\"offerId\":\"1450489069\",\"buyQuantity\":1,\"env\":1,\"currencyType\":\"CNY\",\"productId\":\"vvcard\",\"goodsPrice\":50,\"outTradeNo\":\"hanzar20263151119537425\",\"attach\":\"buy-vvcard\"}","paySig":"aba345c6b4e5e13a29f48903a17e74ba05befb224d65f514086277ffdcd0dad5","signature":"354770d4a1452e25a76c0811d81a92eb030b5063d9aa8af9043c2385a8156feb","mode":"short_series_goods"}}
后端是node.js
const signData = JSON.stringify({
offerId: "用了小程序的虚拟支付offerID",
buyQuantity: 1,
env: 1,
currencyType: "CNY",
productId: "vvcard",
goodsPrice: 50,
outTradeNo: this.orderNo(user.id),
attach: "buy-vvcard"
})
let paySig = this.calcPaySig("requestVirtualPayment", signData, "开发阶段用了沙箱key")
let lib = await db.table("libs").where({ unionid: user.unionid, appid: param.appid }).find();
if (!lib?.session_key) {
return this.fail(12, "error");
}
let signature = this.calcSignature(signData, lib.session_key);
return this.success({
appid: param.appid,
signData: signData,
paySig: paySig,
signature: signature,
mode: "short_series_goods"
}, "虚拟支付签名");
用的是官方文档提供的代码,哪里有问题?
hmacSha256Hex(key, msg) {
return crypto.createHmac('sha256', key).update(msg, 'utf8').digest('hex');
}
calcPaySig(uri, postBody, appKey) {
return this.hmacSha256Hex(appKey, `${uri}&${postBody}`);
}
calcSignature(postBody, sessionKey) {
return this.hmacSha256Hex(sessionKey, postBody);
}
可能的原因有很多,比如key 弄混了,signData 不一致
{errMsg: "requestVirtualPayment:fail SIGNATURE_INVALID", errno: -15005, errCode: -15005} 这是调用wx.requestVirtualPayment的报错; 求大佬指教