0

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/ and in my Stripe dashboard I get error saying that: Webhook Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing

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

Thanks!

seven
  • 1,183
  • 14
  • 33

1 Answers1

0

I got the same issue. Locally with the stripes-cli everything works fine. It seems that the lambda didn't hand over the raw body to the stripes.webhook.constructEvent.

Therefore my solution was to change the method signature to the following and use the event.body object.

exports.handler = async(event, context ) => {
    try {
        const stripeEvent = stripe.webhooks.constructEvent(
            event.body,
            event.headers['stripe-signature'],
            endpointSecret
        );....
F. Müller
  • 3,969
  • 8
  • 38
  • 49
seb
  • 71
  • 6