#shubhamjha

1 messages · Page 1 of 1 (latest)

kind warrenBOT
gleaming ivy
#

Can you explain what part is giving you trouble?

#

Are you having trouble getting a payload at all, or issues with signature verification?

woven jacinth
#

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

gleaming ivy
#

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

woven jacinth
#

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

gleaming ivy
#

You need to ensure the body is not mutated before passing into constructEvent

woven jacinth
#

how can i pass raw body

gleaming ivy
#

or otherwise explicitly preserve the raw request body

woven jacinth
#

how can i do that?

gleaming ivy
#

If you're parsing request body as json globally, you'll need to exclude that for your webhook route

woven jacinth
#

i have get all the reqest body if i console it

gleaming ivy
#

or you can store a copy of it like this example:

gleaming ivy
#

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

woven jacinth
#

i will deploye a router in that

gleaming ivy
#

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

woven jacinth
#

my code