import { All, Body, Controller, HttpCode, HttpStatus, Logger, } from '@nestjs/common'; import { TelebirrWebhookService } from './providers/telebirr.service'; import { ApiOperation } from '@nestjs/swagger'; import { TelebirrDto } from './dto/telebirr.dto'; @Controller("payments/webhooks") export class WebhookController { constructor(private readonly telebirr: TelebirrWebhookService) { } private readonly logger = new Logger(WebhookController.name); @All('telebirr') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Telebirr payment notification callback (Ethiopia)', description: 'Webhook endpoint for Telebirr payment status updates. Used by Ethiopian passengers.' }) async receiveTelebirr(@Body() payload: TelebirrDto) { this.logger.log( `Telebirr webhook Called`, ); try { const verified = this.telebirr.verifyTelebirrNotification(payload) if (!verified) { throw new Error("not valid") } const merchantOrderId = payload.merch_order_id; if (merchantOrderId.startsWith("freight")) { await this.telebirr.handle(payload); } else if (merchantOrderId.startsWith("passagner")) { //todo: handle else where } } catch (err) { const message = err instanceof Error ? err.message : String(err); this.logger.error(`Telebirr webhook handler threw: ${message}`); } return { code: '0', message: 'OK' }; } }