#mhiggie-node-webhook
1 messages · Page 1 of 1 (latest)
router.post('/webhook', requireToken, (req, res) => {
let event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(⚠️ Webhook signature verification failed., err.message);
return response.sendStatus(400);
}
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
console.log(PaymentIntent for ${paymentIntent.amount} was successful!);
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent);
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
console.log("payment method", paymentMethod)
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached(paymentMethod);
break;
case 'charge.succeeded':
const objectThing = event.data.object;
console.log("payment success", objectThing)
break;
default:
// Unexpected event type
console.log(`Unhandled event type ${event.type}.`);
}
// Return a 200 response to acknowledge receipt of the event
res.status(200);
})
here's my route
above this route within the same file is my endpoint secret
const endpointSecret = 'whsec_6ff242a04..."
when listening for stripe events and checking out as a user in my localhost i get events
but something is up with my actual webhook
like the endpoint is not being hit at all
how did you configure the CLI though?
like did you make it properly forward the events to your localhost?
stripe listen --forward-to localhost:5000/webhook
gives me back my webhook secret which i then plug into my endpointSecret variable
okay so I think the first step here is to add logs to debug your code overall really
like you have tons of code right now, just remove all of it and do a simple route that just responds with "hello" and debug that
ah gotchya
like maybe you get the event and something else crashes, so I'd focus on debugging the route first
will do thank you
what are cli commands i can use to hit this route
without actually checking out and all that
okay mr. koopajah my endpoint is not being hit at all.
hmm
👋 koopajah is gonna head out soon, but I'm hopping in - let me catch up
cool cool, basically i am new to webhooks and trying to implement one for a successful custom checkout flow. When a payment.successful webhook handles additional logic..
dumb question but being a post route am i to hit this from my own frontend somewhere during checkout?
No - we (Stripe) hits that endpoint when it needs to send webhook events. It wouldn't be something you call from your frontend as part of the checkout process
yeah okay cool
When you say your endpoint isn't be hit at all - are you sure localhost:5000/webhook is the correct endpoint?
we don't need the secrete it's not relevant right now
like step 1: can you even hit that route yourself? Like does it work with curl?
router.post('/webhook', requireToken, (req, res) => { console.log("hello")
}
you wouldn't see anything here
the log appears where your Node.js server is running
likely in another terminal
yeah its not showing
Then ignore the CLI for a sec and debug this as you would any route on your own server
like can you curl it yourself?
what is requireToken for example?
yes, i can hit the route with postMan
requireToken is a custom auth in my project for protecting routes for certain types of users
ohhh
lol
i could see that being an issue
yeah usually with webhook endpoints you have to disable CSRF and similar things
yup my three updates are hittin
payment_intent created succeeded & charge.succeeded
ahhh silly mistake
thank you very much for the time ! happy friday to ya
happy to