#Mr.oggy

1 messages · Page 1 of 1 (latest)

placid vectorBOT
terse orchid
#

Can you share an example of the issue you're facing?

#

And the code you're using

pliant iron
#

async handleStripeWebhook(req: Request, res: Response) {
let event: any;
try {
event = this.stripe.webhooks.constructEvent(
req.body,
req.headers['stripe-signature'],
process.env.STRIPE_WEBHOOK_SECRET,
);
} catch (err) {
console.log(err);
console.log(⚠️ Webhook signature verification failed.);
console.log(
⚠️ Check the env file and enter the correct webhook secret.,
);

  return res.sendStatus(400);
}
const dataObject = event.data.object;
console.log(event.data);

// Handle the event
// Review important events for Billing webhooks
// https://stripe.com/docs/billing/webhooks
// Remove comment to see the various objects sent for this sample
switch (event.type) {
  case 'invoice.paid':
    // Used to provision services after the trial has ended.
    // The status of the invoice will show up as paid. Store the status in your
    // database to reference when a user accesses your service to avoid hitting rate limits.
    break;
  case 'invoice.payment_failed':
    // If the payment fails or the customer does not have a valid payment method,
    //  an invoice.payment_failed event is sent, the subscription becomes past_due.
    // Use this webhook to notify your user that their payment has
    // failed and to retrieve new card details.
    break;
  case 'customer.subscription.deleted':
    if (event.request != null) {
      // handle a subscription canceled by your request
      // from above.
    } else {
      // handle subscription canceled automatically based
      // upon your subscription settings.
    }
    break;
  default:
  // Unexpected event type
}

}

Learn to use webhooks to receive notifications of subscription activity.

#

here is controller

terse orchid
#

My guess is that NestJS is parsing req.body so its not the raw request payload (which is what constructEvent expects). This is a common issue when implementing webhook handlers with Node freameworks like this

pliant iron
#

I have add bodayParser.raw to /stripe/webhook route but noting happen

terse orchid
#

That doesn't look like the recommended NestJS example to me. You need to use the RawBodyRequest interface:

@Post()
handleStripeWebhook(@Req() req: RawBodyRequest<Request>) {
  const raw = req.rawBody; // returns a `Buffer`.
}
terse orchid
pliant iron
#

@Post('webhook')
handleStripeWebhook(
@Req() req: RawBodyRequest<Request>,
@Res() res: Response,
) {
return this.stripeService.handleStripeWebhook(req, res);
}
I have add RawBodyRequest<Request>) in both controller and service but nothing work

pliant iron
terse orchid
#

Ok, you'll need to elaborate on 'nothing working'. What does you NestFactory.create() function look like?

pliant iron
terse orchid
#

Ok, I think you need to pass these options to create():

rawBody: true,
bodyParser: true,
#

(I've not used NestJS so just following the official docs)

pliant iron
#

I try to add rawBody: true, bodyParser: true, console.log in my services is still showing nothing

#

async handleStripeWebhook(req: RawBodyRequest<Request>, res: Response) { let event: any; try { event = this.stripe.webhooks.constructEvent( req.rawBody, req.headers['stripe-signature'], process.env.STRIPE_WEBHOOK_SECRET, ); } catch (err) { console.log(err); console.log(⚠️ Webhook signature verification failed.); console.log( ⚠️ Check the env file and enter the correct webhook secret.`,
);

  return res.sendStatus(400);
}
const dataObject = event.data.object;
console.log(event.data);
switch (event.type) {
  case 'invoice.paid':
    // Used to provision services after the trial has ended.
    // The status of the invoice will show up as paid. Store the status in your
    // database to reference when a user accesses your service to avoid hitting rate limits.
    break;
  case 'invoice.payment_failed':
    // If the payment fails or the customer does not have a valid payment method,
    //  an invoice.payment_failed event is sent, the subscription becomes past_due.
    // Use this webhook to notify your user that their payment has
    // failed and to retrieve new card details.
    break;
  case 'customer.subscription.deleted':
    if (event.request != null) {
      // handle a subscription canceled by your request
      // from above.
    } else {
      // handle subscription canceled automatically based
      // upon your subscription settings.
    }
    break;
  default:
  // Unexpected event type
}

}
`

terse orchid
#

There's no need to paste both code and screenshots

#

Specifically which logs are firing? Is the constructEvent throwing?

pliant iron
#

console.log(event.data);

terse orchid
#

What if you just log event?

#

The fact that constructEvent doesn't throw would indicate you're passing the correct values

#

So it must be something else

pliant iron
#

Okay thanks

terse orchid
#

Did you try just logging event?

#

Do you have an example evt_xxx ID I can look at?