#kirito
1 messages · Page 1 of 1 (latest)
apiVersion: "2022-11-15",
})
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET
// Stripe requires the raw body to construct the event.
export const config = {
api: {
bodyParser: false,
},
}
const cors = Cors({
allowMethods: [ "POST", "HEAD" ],
})```
if (req.method === "POST") {
const buf = await buffer(req)
const sig = req.headers["stripe-signature"]!
let event: Stripe.Event
try {
event = stripe.webhooks.constructEvent(buf.toString(), sig, webhookSecret)
} catch (err) {
const errorMessage = err instanceof Error ? err.message : "Unknown error"
// On error, log and return the error message.
if (err! instanceof Error) {
console.log(err)
}
console.log(`❌ Error message: ${errorMessage}`)
res.status(400).send(`Webhook Error: ${errorMessage}`)
return
}
// Successfully constructed event.
console.log("✅ Success:", event.id)
// Cast event data to Stripe object.
if (event.type === "invoice.paid") {
const dataObject = event.data.object as Stripe.Invoice````
something like that ?
Not sure what buffer(req) does. You can also pass the Buffer variable to constructEvent(), no need to cast to string.
I don't know the problem is that it was working before but just suddenly it did stop.
I found this repository that also uses Next , maybe it is helpful: https://github.com/dijonmusters/build-a-saas-with-next-js-supabase-and-stripe/blob/master/21-subscribe-to-stripe-webhooks-using-next-js-api-routes/pages/api/stripe-hooks.js
And you didn't do any changes?
yes
maybe an update to the api version :
from "2022-08-01"to "2022-11-15"
Also it work locally but not on test server
This might be the reason. Did it happen at the same time?
No
Maybe your test server configuration makes changes to the request body?
Emm how is that?
You might have an some kind of API proxy that intercepts the requests.
Where is your test server hosted?
On the cloud but it was working before