So I'm trying to make a website and I'm trying to implement stripe. However, I have run into an issue. Whenever I try to make a payment I get this error
StripeSignatureVerificationError: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
The thing is though that I am passing in the raw body. When I run it locally, everything works perfectly fine without any issues. But when I host it (using google cloud run) it doesn't seem to work. I've tried logging the body and it does indeed receive the raw body:
<Buffer 7b 0a 20 20 22 69 64 22 3a 20 22 65 76 74 5f 31 4d 51 57 53 74 44 77 6b 4c 62 73 37 55 68 76 72 52 66 43 70 58 67 4d 22 2c 0a 20 20 22 6f 62 6a 65 63 ... 3058 more bytes>
But I still get the error. This is my code for the function:
router.post("/orderupdate", catchAsync(async (req, res) => {
const payload = req.body
const sig = req.headers['stripe-signature']
let event
try {
event = stripe.webhooks.constructEvent(payload, sig, endpointSecret)
} catch (e) {
console.log(payload)
console.log(e)
throw new ExpressError("Webhook error!")
}
try {
if (event.type === "checkout.session.completed") {
...
}
} catch (e) {
console.log(e)
}
res.status(200).end()
}))
Any help would be much appreciated
CodePudding user response:
One common issue can be using the wrong Webhook secret. If you use Stripe CLI to listen to events locally for example, it creates a new secret for you that is different from the one you see in the Dashboard for this Webhook Endpoint. You need to make sure you use the correct secret based on the environment you're in.