我按照这篇文章https://www.netlify.com/blog/2020/04/22/automate-order-fulfillment-w/stripe-webhooks-netlify-functions/并在我的 Stripe 仪表板我收到错误消息: Webhook 错误:未找到与有效负载的预期签名匹配的签名。您是否传递了从 Stripe 收到的原始请求正文?https://github.com/stripe/stripe-node#webhook-signing
现在我不确定用户是否收到确认电子邮件,Sendgrid 没有显示任何活动,但是当我之前测试此流程时它没有显示任何活动,尽管我收到了确认电子邮件。不幸的是,当时我在 Stripe 仪表板中按我的 webhook 活动详细信息重新发送,我不确定我是否应该重新发送这些信息,或者它们是否通过。谁能告诉我我的代码有什么问题?
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
exports.handler = async ({ body, headers }) => {
try {
const stripeEvent = stripe.webhooks.constructEvent(
body,
headers["stripe-signature"],
process.env.STRIPE_WEBHOOK_SECRET
);
if (stripeEvent.type === "charge.succeeded") {
const emailTo = stripeEvent.data.object.billing_details.email;
const msg = {
to: emailTo,
from: process.env.FROM_EMAIL_ADDRESS,
subject: `Thanks!`,
html: `elox`,
};
await sgMail.send(msg);
}
return {
statusCode: 200,
body: JSON.stringify({ received: true }),
};
} catch (err) {
console.log(`Stripe webhook failed with ${err}`);
return {
statusCode: 400,
body: `Webhook Error: ${err.message}`,
};
}
};
谢谢!