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);
}