收藏
回答

虚拟支付怎么下单?

{"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"
          }, "虚拟支付签名");

用的是官方文档提供的代码,哪里有问题?
/**
   * 生成HMAC-SHA256签名
   * @param {string} key 密钥
   * @param {string} msg 消息
   * @returns 签名
   */
  hmacSha256Hex(key, msg) {
    return crypto.createHmac('sha256', key).update(msg, 'utf8').digest('hex');
  }


  /**
   * 生成支付签名
   * @param {string} uri 接口路径
   * @param {string} postBody 请求体
   * @param {string} appKey 应用密钥
   * @returns 支付签名
   */
  calcPaySig(uri, postBody, appKey) {
    return this.hmacSha256Hex(appKey, `${uri}&${postBody}`);
  }


  /**
   * 生成用户签名
   * @param {string} postBody 请求体
   * @param {string} sessionKey 会话密钥
   * @returns 用户签名
   */
  calcSignature(postBody, sessionKey) {
    return this.hmacSha256Hex(sessionKey, postBody);
  }


最后一次编辑于  03-15
回答关注问题邀请回答
收藏

2 个回答

  • Memory (私信不回复)
    Memory (私信不回复)
    03-15

    可能的原因有很多,比如key 弄混了,signData 不一致

    03-15
    有用
    回复 2
  • 先锋兄弟[胜利]
    先锋兄弟[胜利]
    03-15
    {errMsg: "requestVirtualPayment:fail SIGNATURE_INVALID", errno: -15005, errCode: -15005}
    这是调用wx.requestVirtualPayment的报错;
    求大佬指教
    
    03-15
    有用
    回复
登录 后发表内容