【问题标题】:Stripe Webhook error 400 with netlify functions带有 netlify 功能的 Stripe Webhook 错误 400
【发布时间】:2021-12-17 03:31:51
【问题描述】:

我已经按照这篇文章https://www.netlify.com/blog/2020/04/22/automate-order-fulfillment-w/stripe-webhooks-netlify-functions/ 使用 Netlify 功能设置了条带付款,并且在我的 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}`,
    };
  }
};

谢谢!

【问题讨论】:

    标签: javascript stripe-payments webhooks netlify-function


    【解决方案1】:

    我遇到了同样的问题。 在本地使用 stripes-cli 一切正常。 看来 lambda 并没有将 raw body 交给stripes.webhook.constructEvent.

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

    exports.handler = async(event, context ) => {
        try {
            const stripeEvent = stripe.webhooks.constructEvent(
                event.body,
                event.headers['stripe-signature'],
                endpointSecret
            );....
    

    【讨论】:

    • 这仍然行不通,因为 event.body 与往常一样是相同的解析数据。似乎 Netlify 不提供传入数据的原始版本。 (AWS Lambda 可以。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2020-10-24
    • 2021-11-28
    • 2019-05-18
    • 1970-01-01
    • 2021-01-26
    • 2020-10-14
    相关资源
    最近更新 更多