TheNorthMemory / wechatpay-axios-plugin

微信支付 WeChatPay OpenAPI v2&v3' SDK,以命令行方式与接口交互,play the openapi requests over command line
MIT License
243 stars 37 forks source link

请求「商家转账」接口,报"错误的签名,验签失败" #58

Closed taoliujun closed 7 months ago

taoliujun commented 7 months ago

版本:0.8.11

request:

==request {"appid":"***********","out_batch_no":"0000000019","batch_name":"提现0.1元","batch_remark":"向用户6转账0.1元","total_amount":10,"total_num":1,"transfer_detail_list":[{"out_detail_no":"0000000019","transfer_amount":10,"transfer_remark":"提现0.1元","openid":"**********"}]} { Authorization: 'WECHATPAY2-SHA256-RSA2048 mchid="********",serial_no="*******",timestamp="1709745440",nonce_str="5hMYYOOJpzdAfboMUB7QiPmfxmhVehVk",signature="****"',

response:

==response {"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_information":{"method":"POST","sign_message_length":71,"truncated_sign_message":"POST\n/v3/transfer/batches\n1709745440\n5hMYYOOJpzdAfboMUB7QiPmfxmhVehVk\n\n","url":"/v3/transfer/batches"}},"message":"错误的签名,验签失败"}

同样的request data和Authorization,我用curl请求了下,是正常的。但是用本js包请求,就是签名失败。

看起来sign_message_length: 71很诡异,签名体包括request data的长度,长度应该远远超过71呀?

TheNorthMemory commented 7 months ago

从返回的msg(ehVk\n\n","url")大致推断,post的载荷body有遗漏,请提供一下你的样例代码看看。

或者从 这里 对比一下请求示例,例如

wxpay.v3.transfer.batches.post({
  appid,
  out_batch_no,
  batch_name,
  batch_remark,
  total_amount,
  total_num,
  transfer_detail_list,
  transfer_scene_id,
})
taoliujun commented 7 months ago

样例代码:

const wxpay = new Wechatpay({
  mchid: WECHAT_SERVICE_APP_PAY_MCHID,
  serial: WECHAT_SERVICE_APP_PAY_API_NO,
  privateKey: apiPrivatePem,
  certs: {
    [WECHAT_SERVICE_APP_PAY_V3_NO]: v3Pem,
  },
});

wxpay.v3.transfer.batches
  .post({
    appid: WECHAT_MP_APP_ID,
  })
taoliujun commented 7 months ago

破案了。


Wechatpay.client.v3.defaults.transformRequest.push((data, headers) => {
  console.log("==request", data, headers);
  // 之前没有加return
  return data;
});