#Vanity - webhook

1 messages ยท Page 1 of 1 (latest)

frigid drum
#

What seems to be the issue?

woven atlas
#

so

#

basically i setup everything

#

(local)

#

cli

#

login

#

webhook endpoint (with signature checking)

#

and when i fire the event with stripe cli

#

i get a 400

#

as response

#

i'll send you some screenshots

frigid drum
#

Please don't

#

Screenshots are not useful

woven atlas
#

oh ok

frigid drum
#

What document are you following to configure your webhook listener function?

woven atlas
#

basically the one on the dashbord where you can create endpoints and local endpoints for testing

#

should i look to different documentations?

frigid drum
#

And be sure to review all the other related pages (you can see them in the left side navigation menu).

woven atlas
#

ok i'll try

#

ty

frigid drum
#

Happy to help. There's lots of good stuff in those docs but it can take some digging

woven atlas
#

was unable to find a solution

#

keeps saying that it gives me error because (maybe i'm not parsing request.body)

#

even tho i'm passing raw request.body

trail bloom
#

๐Ÿ‘‹ stepping in as Snufkin needs to step away

woven atlas
#

it's ok

trail bloom
#

Ah hello again. We were chatting yesterday about this

woven atlas
#

yeh

#

so requests works fine now

#

we have another problem ahah

#

when i fire the event with stripe cli

#

it gives me 400 saying that was unable to verify the request

#

even tho the endpointSecret is the correct one

#

and i'm passing raw body to the function

trail bloom
#

Are you using Express?

woven atlas
#

yes

trail bloom
#

Gotcha, so Express oftentimes messes with the raw Body

woven atlas
#

want a screenshot of what function looks like?

trail bloom
#

Are you using express.json()

#

?

woven atlas
#

i've this on the top

#

app.use(bodyParser.json());

trail bloom
#

Ah yeah that is the issue

#

Try this: app.use((req, res, next) => { if (req.originalUrl === '/webhook') { next(); // Do nothing with the body because I need it in a raw state. } else { bodyParser.json()(req, res, next); // ONLY do bodyParser.json() if the received request is NOT a WebHook from Stripe. } });

woven atlas
#

ok i'll try

#

i've also this

#

app.use(bodyParser.urlencoded({
extended: true
}));

#

can it be a problem?

trail bloom
#

Potentially, yes.

#

Anything that would mess with the raw body before the signature verification will make veification fail

woven atlas
#

i'll put it in the same way i did with the other one

trail bloom
#

๐Ÿ‘

#

Let me know

woven atlas
#

so

#

the problem is in this 2 functions

#

because i commented them

#

and everything works fine

#
app.use((req, res, next) => {
  if (req.originalUrl === '/webhook') {
    next(); 
  } else {
    bodyParser.json()(req, res, next);  
  }
});

app.use((req, res, next) => {
  if (req.originalUrl === '/webhook') {
    next(); 
  } else {
    bodyParser.urlencoded({
      extended: true
    })(req, res, next);  
  }
});```
trail bloom
#

Oh wait

#

You have bodyParser in your endpoint

#

Change that to express.raw

woven atlas
#

nono

#

i changed it

#
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
  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;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    default:
      // Unexpected event type
      console.log(`Unhandled event type ${event.type}.`);
  }

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

that's it now

#

the problem still in these two calls to express on the top of my server.js

trail bloom
#

Ah okay

#

Well bodyParser should be ignored then if you are doing app.use((req, res, next) => { if (req.originalUrl === '/webhook') { next(); } else { bodyParser.json()(req, res, next); } });

#

So that is odd

woven atlas
#

Yup

#

I could add everytime bodyparser manually to every endpoint except the correct one

#

But i've lot's of endpoints

trail bloom
#

Why do you need bodyParser here?

#

Oh

#

This is alongside your other endpoints

#

This isn't just your webhook code

woven atlas
#

Yep

trail bloom
#

I don't really know why it doesnt just ignore bodyParser though for your /webhook endpoint....

#

I know this has come up for a ton of people before though

woven atlas
#

Ok ty

trail bloom
#

There are a lot of potential solutions in there that you could try

woven atlas
#

I'll find out by myself but ty for the help

#

๐Ÿ™‚

trail bloom
#

๐Ÿ‘

woven atlas
#

it works

#

finally

#

tysm

#

wanna ask one last question

#

how can i verify if the payment was a test payment with a test payment card and if not

trail bloom
#

What did you do to fix it?

trail bloom
woven atlas
#

express.raw({type: 'application/json'})

#
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
  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;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    default:
      // Unexpected event type
      console.log(`Unhandled event type ${event.type}.`);
  }

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

for better understanding

#

tysm, love stripe support โค๏ธ