question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vercel Production API bodyParser (stripe webhooks)

See original GitHub issue

Hello,

When i have been building my application everything worked fine and no problems with stripe passing through webhooks to redwood API. Once Built to Vercel, the webhooks no longer work, and it’s critical they do work!

  if (event.httpMethod !== 'POST') {
    return { statusCode: 404 }
  }

  // Check signing signature
  const sig = event.headers['stripe-signature']
  let stripeEvent
  try {
    stripeEvent = stripe.webhooks.constructEvent(event.body, sig, webhookSecret)
  } catch (err) {
    // On error, log and return the error message
    // console.log(`Webhook Error message: ${err.message}`)
    return {
      statusCode: 400,
      body: `Event Error: ${err.message}`,
    }
  }

stripe.webhooks.constructEvent wants event.body to be passed into it unparsed, (works fine on dev) but when pushed to production on Vercel. It seems some kind of processing is happening to the event.body meaning that stripe will not accept it.

to what I understand is that stripe wants rawJSON but something is parsing that JSON.

this would be the solution if bodyParser was used with express

app.use(bodyParser.json({
    // Because Stripe needs the raw body, we compute it but only when hitting the Stripe callback URL.
    verify: function(req,res,buf) {
        var url = req.originalUrl;
        if (url.startsWith('/stripe-webhooks')) {
            req.rawBody = buf.toString()
        }
    }}));

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
kyrchacommented, Apr 22, 2021

I arrived in this issue through Google search and managed to get stripe webhooks working in vercel through this blog post https://maxkarlsson.dev/blog/2020/12/verify-stripe-webhook-signature-in-next-js-api-routes/.

1reaction
thedavidpricecommented, Apr 22, 2021

fwiw — this Issue is a good example of something that can make for a great how-to forum post as well.

https://community.redwoodjs.com

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webhooks API – Vercel Docs
Vercel Integrations allow you to subscribe to certain trigger-based events through webhooks. An example use-cases for webhooks might be cleaning up ...
Read more >
Subscribe to Stripe Webhooks Using Next.js API Routes
In this video, we create a new API route to handle webhook events from Stripe. We can call the stripe.webhooks.constructEvent function which returns...
Read more >
NextJS Stripe Webhook Raw Body - Stack Overflow
STRIPE_WEBHOOK_ENDPOINT_SECRET_NEW; // Stripe requires the raw body to construct the event. export const config = { api: { bodyParser: false, }, }; ...
Read more >
Processing payments with Stripe and webhooks - Jon Meyers
// pages/api/auth/hooks.js // other imports import initStripe from "stripe"; const stripe = initStripe(process ...
Read more >
Webhook Error: No Signatures Found Matching Only On Vercel
In the vercel example the product and price hooks are used where Stripe is the source of truth on products and pricing. Vercel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found