Currently working on setting up a checkout session using Stripe that triggers my webhook upon successful completion. The issue I am facing is an error message stating "error: 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". Below is a snippet of my code:
const stripeRouter = express.Router({ mergeParams: true });
const endPointSecret = 'Webook_Secret'
stripeRouter.post('/webhook',
express.raw({type: 'application/json'}),
async (req: express.Request, res: express.Response) => {
const stripe = await getStripe();
await getStripeSecretKey();
const sig = req.headers['stripe-signature'];
console.log(`signature: ${sig}`)
const payload = req.body;
console.log(`payload: ${payload}`)
let eventType;
let data;
if (endPointSecret) {
let event;
try {
event = stripe.webhooks.constructEvent(payload, sig, endPointSecret);
console.log(`$$$$$ Webhook Verification Successful`)
} catch (err) {
console.log(`$$$$$ Webhook signature verification failed`, err.message)
res.status(400).json({error: err.message, signature: req.body});
return;
}
eventType = event.type;
data = event.data;
console.log(`$$$ EVENT TYPE: ${eventType}`)
console.log(`$$$ DATA: ${data}`)
} else {
data = req.body.data.object;
eventType = req.body.type;
}
I have double-checked the webhook Signing Secret and it is accurate. Additionally, I attempted to use both express.raw() and bodyParser.raw(), but neither solved the problem.
In an effort to resolve the issue, I also implemented custom middleware as shown below, however, the error persists:
stripeRouter.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
if (req.originalUrl === '/webhook') {
next();`your text`
} else {
express.json()(req, res, next);
}
})