#krutarth-webhook-node

1 messages ยท Page 1 of 1 (latest)

dry parrotBOT
warm bronze
#

krutarth-webhook-node

#

Hey @hearty robin ! Webhooks are used to do reconciliation about things happening in your account. There's no link between a webhook endpoint and your frontend code

hearty robin
#

I don't clear understand what you just said. But above is the small usecase which i want to implement. how can i do that? basically displaying payment intent status in realtime. as we won't know when exactly did the user paid

warm bronze
#

I unfortunately don't understand what you are describing either

#

Let's say you have a PaymentIntent and you collect payment method details with PaymentElement, your code will render a UI to collect payment method details like card details, and then you call confirmPayment() when they are ready to pay

hearty robin
#

I read a bit about webhooks, please correct me if im wrong.
webhooks notifies regarding the events which occurs in the stripe and it is not actually related to frontend.
So, if above point is correct, Let me update my requirement. Using webhooks I want to track an event whenever a user makes a payment,

  1. can we use webhooks for tracking this. 2. if yes, please send a doc if it is available.
hybrid bronze
#

๐Ÿ‘‹ hopping in here since koopajah had to head out

#

If you want your backend server to be notified of when a payment has actually been completed then yes, webhooks would be one way of doing that

hearty robin
#

Unable to understand how exactly i can implement webhooks. Doc state step by step process to implement but Im unable to understand and apply for my use case. is there any resource other than this doc? I saw 1 min 24 second video present in the doc but it was complicated for me too.

hybrid bronze
#

What specifically do you not understand? Do you just not know which webhook events you should be listening for?

hearty robin
#

I know the event. but i cant understand Create a webhook endpoint as an HTTP endpoint (URL) on your local server. how exactly can i do that in my local system.
Handle requests from Stripe by parsing each event object and returning 2xx response status codes. how to handle request? what is 2xx response?

I cant understand Pretty much everything. there are more steps and they are ambigous as well.
I know of just the event which is payment_intent.succeeded but apart from it, how to implement, how to test, where to check test result. how to deploy or how exactly this looks like, im not able to understand

hybrid bronze
#

but if you're unsure of how to create a new endpoint on your server that accepts these events you may want to look into third-party solutions or hire a developer

hearty robin
#

I'm testing webhook in local env. somehow stuck in step 3. ran the 3rd command in terminal, it showed trigger succeded but. there is 404 error in stripe cli. I have below code in server.js for /webhook

const endpointSecret =
  "whsec_0612b3a6cb2b990fcb2cf46a8fa50d61a4e317c6bbc49c6fc33aed9a1b96c75e";

app.post(
  "/webhook",
  express.raw({ type: "application/json" }),
  (request, response) => {
    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;
    }

    // Handle the event
    switch (event.type) {
      case "payment_intent.succeeded":
        const paymentIntentSucceeded = event.data.object;
        // Then define and call a function to handle the event payment_intent.succeeded
        break;
      // ... handle other event types
      default:
        console.log(`Unhandled event type ${event.type}`);
    }

    // Return a 200 response to acknowledge receipt of the event
    response.send();
  }
);
dry parrotBOT
hearty robin
#

hi @queen patrol In summary, im trying to test webhooks locally. Facing issue in the step 3, shown in the first screenshot. More details are there in other screenshot and the code.

Only last message is sufficient to solve the query

#

There?

hybrid bronze
#

Sorry we're handing a bunch of folks in parallel

hearty robin
#

okay, no problem.

queen patrol
#

Hi

#

Catching up

#

Can you share a screenshot of the terminal tab where you're running your express server?

#

Want to see the output there

hearty robin
#

added logs to the code:

app.post(
  "/webhook",
  express.raw({ type: "application/json" }),
  (request, response) => {
    const sig = request.headers["stripe-signature"];

    let event;

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

    // Handle the event
    switch (event.type) {
      case "payment_intent.succeeded":
        const paymentIntentSucceeded = event.data.object;
        // Then define and call a function to handle the event payment_intent.succeeded
        break;
      // ... handle other event types
      default:
        console.log(`Unhandled event type ${event.type}`);
    }

    // Return a 200 response to acknowledge receipt of the event
    response.send();
  }
);

The response has changed from 404 to 400 right now.

queen patrol
#

So they're not 404s then

#

They're 400s

#

So the issue is a signature verification issue

#

You need to use the endpoint secret that was printed out by your stripe listen cli command

#

Is that the one you're using?

hearty robin
#

yes i rechecked the endpoint secret and the secret in the code which i sent and terminal screenshot which is following /webhook path is same.

queen patrol
#

You're not using any libraries that would mutate the inbound request body?

#

Just using our sample code exactly as it's shown?

hearty robin
#

im directly copy pasting the sample code

#

not any library which would mutate

queen patrol
#

Ok try something for me

#

Shut down that express server that's currently running

#

Literally click the download button to download the sample code.