No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

huangapple go评论62阅读模式
英文:

No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

问题

I was writing a stripe webhook that would be called when a payment would be completed. The problem is that when I try to check the signature, it keeps giving me this error no matter what:

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

This is my code:

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

  let event;
  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();
});

I already made sure to remove a global middleware but I keep having this error. What am I doing wrong?

英文:

I was writing a stripe webhook that would be called when a payment would be compleated. The problem is that when I try to check the signature I keeps giving me this error no matter what:

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

This is my code:

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();

}
);

I already made sure to remove a global middleware bat I keep having this error.
What I'm doing wrong?

答案1

得分: 0

  1. 请尝试使用 express.raw({type: 'application/json'}) 而不是 express.raw({type: '*/*'})
  2. 在将 payload 传递给 stripe.webhooks.constructEvent 时删除 JSON.stringify

请在进行上述两个更改后告诉我是否有效。

英文:

I noticed two differences between your code and the example code in Stripe's webhook endpoint builder

  1. Try use express.raw({type: 'application/json'}) instead of express.raw({type: '*/*'})
  2. Remove the JSON.stringify when passing payload to stripe.webhooks.constructEvent

Let me know if it works after making above two changes.

huangapple
  • 本文由 发表于 2023年5月14日 22:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定