I am trying to code a subscription system with stripe. My following code is mostly copied from the stripe docs, but it always shows me this error:
Webhook signature verification failed. Webhook payload must b
e provided as a string or a Buffer (https://nodejs.org/api/buffer.html) instance representing the _raw_ request body.Payload was provided as a parsed JavaScript object instead.
Signature verification is impossible without access to the original signed material.
Learn more about webhook signing and explore webhook integration
examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing
This is my code written in javascript:
app.post(
'/webhook',
express.raw({ type: 'application/json' }),
(request, response) => {
let event = request.body;
const endpointSecret = 'whsec_.....';
if (endpointSecret) {
const signature = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}
}
let subscription;
let status;
switch (event.type) {
case 'customer.subscription.created':
subscription = event.data.object;
status = subscription.status;
console.log(`Subscription status is ${status}.`);
break;
default:
console.log(`Unhandled event type ${event.type}.`);
}
response.send();
}
);
Does anyone know what I am doing wrong? I have literally been searching the internet for an hour now and cant find anything. Thanks in advance!
2
Answers
It seems like something in your app is parsing the incoming request to a different format than what the function expects (i.e. JSON). Do you have a middleware or parser configured in your app globally? If so, you’d likely want to disable it Or just add an exception where it doesn’t touch the incoming request body that’s going to
/webhook
route.It’s because
app.use(express.json());
code. Try to move webhook route beforeapp.use(express.json());
.