0

我按照这篇文章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}`,
    };
  }
};

谢谢!

4

1 回答 1

0

我遇到了同样的问题。在本地使用 stripes-cli 一切正常。似乎 lambda 没有将原始身体交给stripes.webhook.constructEvent.

因此我的解决方案是将方法签名更改为以下并使用该event.body对象。

exports.handler = async(event, context ) => {
    try {
        const stripeEvent = stripe.webhooks.constructEvent(
            event.body,
            event.headers['stripe-signature'],
            endpointSecret
        );....
于 2022-01-31T23:55:46.037 回答