#mhiggie

1 messages ยท Page 1 of 1 (latest)

ivory nebulaBOT
wintry lily
#

Hi ๐Ÿ‘‹

A webhook registered on the Stripe dashboard will always need to point to a public URL. You can stream webhook events to your localhost server using the Stripe CLI and the command stripe listen
https://stripe.com/docs/cli/listen

torpid sandal
#

yes, so i am using a node express backend

#

i have my server running locally

#

i have opened two additional terminals within vscode

#

in the first additional terminal i ran stripe listen --forward-to http://localhost:5000

#

does the forward -to have to point to my server or the literal /webhook endpoint?

wintry lily
#

The literal endpoint

#

I run stripe listen --forward-to http://localhost:8000/webhook2/ for my test integration

torpid sandal
#

okay in my second additional terminal window i am running stripe trigger subscription_schedule.created

#

`router.post('/webhook', express.raw({type: 'application/json'}), async (request, response) => {
const sig = request.headers['stripe-signature'];
let event;

try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
console.log(event)
} catch (err) {
response.status(400).send(Webhook Error: ${err.message});
return;
}

// Handle the event
if (event.type === 'subscription_schedule.created') {
const subscriptionScheduleCreated = event.data.object;

// Then define and call a function to handle the event subscription_schedule.created
console.log('stripe subscription created hurray.')
console.log(subscriptionScheduleCreated)

} else {
console.log(Unhandled event type ${event.type});
}

res.sendStatus(200)
})`

#

And the error from try catch block is firing

wintry lily
#

Okay, do you have more information on what is triggering the 400 error?

#

Are you passing in the correct webhook secret?

torpid sandal
#

i think that is the issue

wintry lily
#

I would log the sig and endpointSecret values before you try to construct the event

torpid sandal
#

yes endPoint secret is not defined

#

thank you j

#

okay so how do i fix this issue

wintry lily
#

Well for localhost you copy the endpoint secret value that is written to the console when you run stripe listen and make it a const value in your webhook handler

torpid sandal
#

hah okay working great thank you

wintry lily
#

Great, happy to hear it ๐Ÿ™‚

torpid sandal
#

first timer with webhooks here

wintry lily
#

They do take some getting used to

torpid sandal
#

for that endpoint secret would i reference my production stripe key

wintry lily
torpid sandal
#

wait no, id probably need to create a new webhook env variable in production and reference that

wintry lily
#

Not either

#

Each webhook listener you register in the Stripe dashboard (or API) will generate it's own secret value

#

You will need to make that available in your hosting environment

torpid sandal
#

that makes sense thank you

#

then in each webhook point to that corresponding env variable from hosting

wintry lily
#

For node we see a lot of process.env.SECRET kind of stuff used to load the value from host configurations

torpid sandal
#

yup yup thanks again this was super helpful