Reputation:
So I´m trying to verify the transactions using Stripe and it needs the raw body to verify the request.
Stripe Docs on that topic: https://stripe.com/docs/webhooks/signatures
my code:
router.post(
"/stripe_webhook",
express.raw({ type: "application/json" }),
(request, response) => {
updateStripePaymentStatus(request, response);
}
);
exports.updateStripePaymentStatus = async (request, response) => {
const sig = request.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
console.log(err);
response.status(400).send(`Webhook Error: ${err.message}`);
}
//Handle the event
switch (event.type) {
case "payment_intent.succeeded":
const paymentIntent = event.data.object;
const doc = await FileModel.findOne({ paymentId: paymentIntent.id });
doc.paymentStatus = paymentIntent.status;
doc.save();
break;
default:
// Unexpected event type
console.log(`Unhandled event type ${event.type}.`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
};
Error: message: 'No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing',
The secret and and so on should work and all. But if I console.log the request.body it still returns a jason object instead of a string...
Any ideas what could be wrong here?
Upvotes: 3
Views: 2373
Reputation: 7354
The following worked for me
express.raw({ type: "application/json" })
--> express.raw()
stripe.webhooks.constructEvent(request.body, sig, endpointSecret)
--> stripe.webhooks.constructEvent(req["rawBody"], sig, endpointSecret)
Upvotes: 0
Reputation: 941
It could be a number of things. There's probably something in your stack that modifying the request body. Take a look at these issues for some clues: https://github.com/stripe/stripe-node/issues/341 and https://github.com/stripe/stripe-node/issues/356.
Upvotes: 1