简体   繁体   English

带有 netlify 功能的 Stripe Webhook 错误 400

[英]Stripe Webhook error 400 with netlify functions

I have set up payments with stripe using Netlify function by following this article https:\/\/www.netlify.com\/blog\/2020\/04\/22\/automate-order-fulfillment-w\/stripe-webhooks-netlify-functions\/<\/a> and in my Stripe dashboard I get error saying that: Webhook Error: No signatures found matching the expected signature for payload.<\/strong><\/em>我按照这篇文章https:\/\/www.netlify.com\/blog\/2020\/04\/22\/automate-order-fulfillment-w\/stripe-webhooks-netlify-functions\/<\/a>并在我的 Stripe 仪表板我收到错误消息: Webhook 错误:未找到与有效负载的预期签名匹配的签名。<\/strong><\/em> Are you passing the raw request body you received from Stripe?<\/strong><\/em>您是否传递了从 Stripe 收到的原始请求正文?<\/strong><\/em> https:\/\/github.com\/stripe\/stripe-node#webhook-signing<\/a><\/strong><\/em> https:\/\/github.com\/stripe\/stripe-node#webhook-signing<\/a><\/strong><\/em>

Now I am not sure if the user gets confirmation email, Sendgrid does not show any activity, however it has not shown any when I was testing this flow previously, and although I received confirmation email.现在我不确定用户是否收到确认电子邮件,Sendgrid 没有显示任何活动,但是当我之前测试此流程时它没有显示任何活动,尽管我收到了确认电子邮件。 Unfortunately back then I pressed resend by my webhook activity details in Stripe dashboard, and I am not sure if I should be resending those or do they go through.不幸的是,当时我在 Stripe 仪表板中按我的 webhook 活动详细信息重新发送,我不确定我是否应该重新发送这些信息,或者它们是否通过。 Would anyone be able to tell me what is wrong with my code?谁能告诉我我的代码有什么问题?

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}`,
    };
  }
};

I got the same issue.我遇到了同样的问题。 Locally with the stripes-cli everything works find.在本地使用 stripes-cli 可以找到所有工作。 It seams that the lambda didn't handover the raw body to the stripes.webhook.constructEvent.似乎 lambda 没有将原始主体移交给stripes.webhook.constructEvent.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM