import { Controller, Post, Headers, HttpCode, HttpStatus, RawBody, Body } from '@nestjs/common';
import { StripeService } from 'src/stripe/stripe.service';
@Controller('webhook')
export class WebhookController {
constructor(private stripeService: StripeService) {}
@Post()
@HttpCode(HttpStatus.OK)
async handleWebhook(@Body() payload: any, @Headers('stripe-signature') signature: string) {
console.log('PAYLOAD ', payload, signature); // payload - undefined
const event = this.stripeService.constructEvent(payload, signature);
switch (event.type) {
case 'checkout.session.completed':
console.log('checkout.session.completed');
break;
case 'customer.subscription.updated':
console.log('customer.subscription.updated');
break;
case 'customer.subscription.deleted':
console.log('customer.subscription.deleted');
break;
default:
console.warn(`Unhandled event type ${event.type}`);
}
}
}
constructEvent(payload: any, signature: any) {
return this.stripe.webhooks.constructEvent(payload, signature, process.env.STRIPE_WEBHOOK_SECRET);
}