#yen6305
1 messages · Page 1 of 1 (latest)
My server.js code:
import initStripe from 'stripe'
import { buffer } from "micro";
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
export const config = {
api: {
bodyParser: false,
},
};
export default async function handler(req, res) {
const stripe = initStripe(process.env.STRIPE_SECRET_KEY);
// handler
const bodyBuffer = await buffer(req);
const signature = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(bodyBuffer, signature, webhookSecret);
} catch(error) {
console.log(bodyBuffer);
return res.status(400).send(`STWIPR STRIPE error: ${req.headers}`);
}
res.send({ received: true});
}
When I am logging signature it will return undefined
Apparently next.js changes the req, so I've used buffer from micro to get the raw body request
The output of the bodyBuffer is: `<Buffer 7b 22 65 76 65 6e 74 12 2a 22 23 68 61 72 27 65 2e 63 61 70 74 75 72 65 64 22 7d>'
If you log all the headers do you see anything?
Yeah, i see all the headers except for the 'stripe-signature' header
I can show you 1 sec.
I see, Not familiar with NextJs (sorry) but micro buffer only get you the bodyBuffer, but the req is still the NextJS modified version request, correct?
Yes, i believe so
Your colleague once send me this link: https://www.codedaily.io/tutorials/Stripe-Webhook-Verification-with-NextJS
So i followed all the steps
Did practically the same
But my stripe-signature header is still nowhere to be found
I deleted my file because it contains some personal data, but yeah
I think the micro buffer has nothing to do with the const signature = req.headers['stripe-signature']
the signature header should be created when a request has been made to this api route right?
I've also added my api endpoint in Stripe dashboard
Yeah It's strange
Sorry for throwing in another guide, but we have this for NextJS + Webhook: https://www.youtube.com/watch?v=nVvDr6MyEXE
Learn how to get a Next.js app up and running, install the Stripe libraries, configure your application securely, and create a webhook endpoint and handler using Next.js and Stripe.
Presenter
Paul Asjes - Developer Advocate at Stripe - https://twitter.com/paul_asjes
Resources
Next.js: https://nextjs.org
Table of contents
00:00 In...
It's a video from Stripe
Could you give it a look?