#shubhamjha
1 messages · Page 1 of 1 (latest)
Can you explain what part is giving you trouble?
Are you having trouble getting a payload at all, or issues with signature verification?
const { AuthDisabledRouter } = require('../../../shared/route-builder')
const config = require('../../../config')
const stripe = require('stripe')(config.STRIPE_KEY.STRIPE_SECRET_KEY)
let endpointSecret = config.STRIPE_KEY.WEBHOOK_KEY
let session = ''
const webhook = async (request, response) => {
console.log('hello')
const sig = request.headers['stripe-signature']
let event
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret)
} catch (err) {
response.status(400).send(Webhook Error: ${err.message})
return
}
switch (event.type) {
case 'invoice.upcoming':
session = event.data.object
const c = await stripe.customers.retrieve(session.customer)
console.log(c)
break
default:
console.log(`Unhandled event type ${event.type}`)
}
response.send()
}
const webhookRouter = new AuthDisabledRouter()
webhookRouter.post('/webhook-check', webhook)
module.exports = { webhookRouter }
this is my code
plase help me
The error you shared doesnt appare to have anything to do with the stripe client or the events, it appears to be having issues with your route configurations and accessing status which only appears on response.status that i can see
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret)
in this i will get the error
req.body can be in raw
but i dont know how to do that
You need to ensure the body is not mutated before passing into constructEvent
how can i pass raw body
or otherwise explicitly preserve the raw request body
how can i do that?
If you're parsing request body as json globally, you'll need to exclude that for your webhook route
i have get all the reqest body if i console it
or you can store a copy of it like this example:
?
https://github.com/stripe-samples/checkout-one-time-payments/blob/main/server/node/server.js#L22-L32
You can add a middle ware prior to your json parsing to eg save req.rawBody for later use in webhooks
But the implementation is going to depend on your integration
for example i have no idea what your AuthDisabledRouter does
or how webhookROuter is used later
i will deploye a router in that
However your router/app works, you need to preserve the raw body by either not mutating/parsing it, or making a preserved copy of it for later use