英文:
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
- 请尝试使用
express.raw({type: 'application/json'})
而不是express.raw({type: '*/*'})
。 - 在将 payload 传递给
stripe.webhooks.constructEvent
时删除JSON.stringify
。
请在进行上述两个更改后告诉我是否有效。
英文:
I noticed two differences between your code and the example code in Stripe's webhook endpoint builder
- Try use
express.raw({type: 'application/json'})
instead ofexpress.raw({type: '*/*'})
- Remove the
JSON.stringify
when passing payload tostripe.webhooks.constructEvent
Let me know if it works after making above two changes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论