#tungtn1099

1 messages ยท Page 1 of 1 (latest)

iron remnantBOT
fleet helm
#

What is the issue you're facing?

high bridge
#

it tells me that im not passing the raw request i received from stripe when i cast an event

#

i made sure that the stripe secret key and webhook key are mine

#

its not logging any of these

#

oh wait, i reran the service and it does log 123

#

but same error not passing raw request

fleet helm
#

In your if condition, you didn't set the request to parse as raw.

For example.

express.raw({type: 'application/json'})(req, res, next);
high bridge
#
// Use JSON parser for all non-webhook routes
app.use(
  (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ): void => {
    if (req.originalUrl === '/webhook') {
      next();
    } else {
      express.json()(req, res, next);
    }
  }
);

app.post(
  '/webhook',
  // Stripe requires the raw body to construct the event
  express.raw({type: 'application/json'}),
  (req: express.Request, res: express.Response): void => {
    const sig = req.headers['stripe-signature']
      ? req.headers['stripe-signature']
      : '';
    let event: Stripe.Event;
    try {
      event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
    } catch (err) {
      // On error, log and return the error message
      console.log(`โŒ Error message: ${err.message}`);
      res.status(400).send(`Webhook Error: ${err.message}`);
      return;
    }

    // Successfully constructed event
    console.log(':white_check_mark: Success:', event.id);

    // Cast event data to Stripe object
    if (event.type === 'payment_intent.succeeded') {
      const stripeObject: Stripe.PaymentIntent = event.data
        .object as Stripe.PaymentIntent;
      console.log(`๐Ÿ’ฐ PaymentIntent status: ${stripeObject.status}`);
    } else if (event.type === 'charge.succeeded') {
      const charge = event.data.object as Stripe.Charge;
      console.log(`๐Ÿ’ต Charge id: ${charge.id}`);
    } else {
      console.warn(`๐Ÿคทโ€โ™€๏ธ Unhandled event type: ${event.type}`);
    }

    // Return a response to acknowledge receipt of the event
    res.json({received: true});
  }
);
#

i saw it in app.post()

fleet helm
#

ah i see! i wasn't aware it's set in another part of your code

#

How do you forward the event? Is it using CLI or endpoint set up in Dashboard?

high bridge
#

i used CLI

fleet helm
#

Do you get the webhookSecret from the CLI in the format of whsec_xxx?

high bridge
#

yes i do

fleet helm
#

How about the secret key initiating stripe client? Does the secret key belong to the same account as the webhook secret?

high bridge
#

it does, but i found the issue

#

i copy pasted the .env from my project into this one so the webhook key name is different ๐Ÿ˜‚

fleet helm
#

Ah! Great to hear that you found the issue!

high bridge
#

i have another question tho

fleet helm
#

What is the question?

high bridge
#
  (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ): void => {
    if (req.originalUrl === '/webhook') {
      next();
    } else {
      express.json()(req, res, next);
    }
  }
);```
is there any other way i can get the raw req.body
#

if i use this my swaggerUI just dies

#

wait, this one is more js question

#

thanks for your time ๐Ÿ˜…

fleet helm
#

No worries! I'm not familiar how Swagger UI takes in the request body. I'd recommend checking online/stackover flow that probably others might face similar issue