Vercel Production API bodyParser (stripe webhooks)
See original GitHub issueHello,
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:
- Created 3 years ago
- Comments:13 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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/.
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