#dodo54
1 messages · Page 1 of 1 (latest)
It's mostly when your web app try to modify the raw data sent from Stripe. It's a common error tho
Here is the code in server.js:
app.use('/webhook', bodyParser.raw({type: "*/*"}))
app.use(bodyParser.json());
And my webhook URL:
router.post(
"/webhook",
bodyParser.raw({ type: 'application/json' }),(req, res) => {
const payload = req.body;
const sig = req.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(payload, sig, endpointSecret); //Here I am recieveing error
res.status(200).end();
} catch (e) {
console.log(e)
return res.status(400).send(`Webhook Error: ${e.message}`);
}
if (event.type === 'invoice.paid') {
const session = event.data.object;
sendEmailToMe(session.customer_email, session.customer_name);
// Fulfill the purchase...
fulfillOrder(session);
}
}
);
How to stop it?
I copied this answer but not working
bodyParse looks suspicious to me. Let's read through this issue and try out solutions there
Ok I am trying this:
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('/webhook')) {
req.rawBody = buf.toString()
}
}}));
And what should I change in this code:
router.post(
"/webhook",
bodyParser.raw({ type: 'application/json' }),(req, res) => {
const payload = req.body;
const sig = req.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(payload, sig, endpointSecret); //Here I am recieveing error
res.status(200).end();
} catch (e) {
console.log(e)
return res.status(400).send(`Webhook Error: ${e.message}`);
}
if (event.type === 'invoice.paid') {
const session = event.data.object;
sendEmailToMe(session.customer_email, session.customer_name);
// Fulfill the purchase...
fulfillOrder(session);
}
}
);
Um probably that's it and you can try sending webhook event again?
Okie could you try other suggestions on the thread as well?