javascript 未找到与负载的预期签名匹配的签名,您是否正在传递从Stripe收到的原始请求体?

hlswsv35  于 2023-05-16  发布在  Java
关注(0)|答案(1)|浏览(158)

我写了一个条纹webhook,将被称为当付款将完成。问题是,当我试图检查签名时,无论如何我都会给我这个错误:

{"type":"StripeSignatureVerificationError","raw":{"message":"No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

这是我的代码:

app.post("/webhook",express.raw({type: "*/*"}),async (request, response) => {
const payload = request.body;
const sig = request.headers["stripe-signature"];

let event;
// console.log(payload)
try {

  event = stripe.webhooks.constructEvent(
    JSON.stringify(payload),
    sig,
    process.env.WEBHOOK_SECRET
  );
} catch (err) {
  return response.status(400).send(`Webhook Error: ${err.message}`);
}

if (event.type === "checkout.session.completed") {
    // Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
    const sessionWithLineItems = await stripe.checkout.sessions.retrieve(
      event.data.object.id,
      {
        expand: ["line_items"],
      }
    );
    const lineItems = event;

    // Fulfill the purchase...
  }

response.status(200).end();

});
我已经确保删除了一个全局中间件,但我一直有这个错误。我做错了什么?

fdbelqdn

fdbelqdn1#

我注意到您的代码与Stripe的webhook endpoint builder中的示例代码之间有两个不同之处
1.尝试使用express.raw({type: 'application/json'})而不是express.raw({type: '*/*'})
1.将有效负载传递到stripe.webhooks.constructEvent时删除JSON.stringify
让我知道如果它的工作后,作出上述两个变化。

相关问题