I'm trying to test locally webhook stripe event, but it say :
Webhook signature verification failed.
So this is my webhook endpoint :
exports.stripeListenWebhook = (req, res) => {
let data
let eventType
const webhookSecret = 'whsec_VjORamXpVZs1j6mCV0eTyE7B2GI92'
if (webhookSecret) {
// Retrieve the event by verifying the signature using the raw body and secret.
let event
let signature = req.headers['stripe-signature']
console.log(req.headers)
try {
event = stripe.webhooks.constructEvent(req.body, signature, webhookSecret)
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`)
return res.sendStatus(400)
}
// Extract the object from the event.
data = event.data
eventType = event.type
} else {
// Webhook signing is recommended, but if the secret is not configured in `config.js`,
// retrieve the event data directly from the request body.
data = req.body.data
eventType = req.body.type
}
// Handle the event
switch (eventType) {
case 'checkout.session.completed':
const session = data.object
console.log(session)
// Then define and call a function to handle the event checkout.session.completed
break
default:
console.log(`Unhandled event type ${eventType}`)
}
res.json({ received: true })
}
My console.log(req.headers)
output after execute this command :
stripe trigger checkout.session.completed
{
host: 'localhost:8000',
'user-agent': 'Stripe/1.0 (+https://stripe.com/docs/webhooks)',
'content-length': '1923',
accept: '*/*; q=0.5, application/xml',
'cache-control': 'no-cache',
'content-type': 'application/json; charset=utf-8',
'stripe-signature': 't=1638211722,v1=08ed8a55af610fdb97d928c4ec068d19badfb82fe0a521aee7d8f8cfbe378d63,v0=aeebf964e3da2a19f9a533743d420804c168395bb25bf4789e04cfcd9f573d52',
'accept-encoding': 'gzip'
}
I follow the rules in the doc, and I'm using this configuration in my app.js :
const corsOptions = {
origin: [URLConfig.URL_CLIENT],
credentials: true,
methods: 'POST, PUT, OPTIONS, DELETE, GET, PATCH',
allowedHeaders: 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization',
}
app.use(cors(corsOptions))
app.use(cookieParser())
app.use(express.json())
app.use(
express.urlencoded({
extended: true,
})
)
Anyone have a issue for my problem ? I see that the body-parser is depracated so I used the express.json() instead