#Mr.oggy
1 messages · Page 1 of 1 (latest)
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
}
}
here is controller
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
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
I have add bodayParser.raw to /stripe/webhook route but noting happen
https://stackoverflow.com/questions/54346465/access-raw-body-of-stripe-webhook-in-nest-js
base on this answer
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`.
}
You have app.use('/stripe/webhook') yet your post endpoint is just webhook
@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
my controller is stripe so i think it correct
Ok, you'll need to elaborate on 'nothing working'. What does you NestFactory.create() function look like?
here
Ok, I think you need to pass these options to create():
rawBody: true,
bodyParser: true,
Based on this SO answer: https://stackoverflow.com/a/76448770/15784650
(I've not used NestJS so just following the official docs)
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
}
}
`
There's no need to paste both code and screenshots
Specifically which logs are firing? Is the constructEvent throwing?
console.log(event.data);
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
Okay thanks