#mhiggie-node-webhook

1 messages · Page 1 of 1 (latest)

ember skiff
#

Hey @upbeat girder what are your questions?

upbeat girder
#

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

ember skiff
#

how did you configure the CLI though?

#

like did you make it properly forward the events to your localhost?

upbeat girder
#

stripe listen --forward-to localhost:5000/webhook

#

gives me back my webhook secret which i then plug into my endpointSecret variable

ember skiff
#

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

upbeat girder
#

ah gotchya

ember skiff
#

like maybe you get the event and something else crashes, so I'd focus on debugging the route first

upbeat girder
#

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

raven sentinel
#

👋 koopajah is gonna head out soon, but I'm hopping in - let me catch up

upbeat girder
#

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?

raven sentinel
#

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

upbeat girder
#

yeah okay cool

raven sentinel
#

When you say your endpoint isn't be hit at all - are you sure localhost:5000/webhook is the correct endpoint?

upbeat girder
#

can i show u my webhook signing secret?

#

its included in a screenshot

ember skiff
#

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?

upbeat girder
#

router.post('/webhook', requireToken, (req, res) => { console.log("hello")
}

ember skiff
#

you wouldn't see anything here

#

the log appears where your Node.js server is running

#

likely in another terminal

upbeat girder
#

yeah its not showing

ember skiff
#

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?

upbeat girder
#

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

ember skiff
#

yeah usually with webhook endpoints you have to disable CSRF and similar things

upbeat girder
#

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

ember skiff
#

happy to