|
1 |
| -import { Injectable } from '@nestjs/common'; |
2 |
| -// import { WebhookEvent } from '../../webhooks.decorators'; |
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { |
| 3 | + PaymentProcessedEventData, |
| 4 | + PaymentProcessedEventStatus, |
| 5 | + PaymentWebhookEvent, |
| 6 | +} from './payment.types'; |
| 7 | +import { WebhookEvent } from '../../webhooks.decorators'; |
| 8 | +import { PaymentsService } from 'src/shared/payments'; |
| 9 | +import { payment_status } from '@prisma/client'; |
| 10 | +import { PrismaService } from 'src/shared/global/prisma.service'; |
| 11 | +import { JsonObject } from '@prisma/client/runtime/library'; |
3 | 12 |
|
4 | 13 | @Injectable()
|
5 | 14 | export class PaymentHandler {
|
6 |
| - // @WebhookEvent(TrolleyWebhookEvent.paymentCreated) |
7 |
| - // async handlePaymentCreated(payload: any): Promise<any> { |
8 |
| - // // TODO: Build out logic for payment.created event |
9 |
| - // console.log('handling', TrolleyWebhookEvent.paymentCreated); |
10 |
| - // } |
| 15 | + private readonly logger = new Logger(PaymentHandler.name); |
| 16 | + |
| 17 | + constructor( |
| 18 | + private readonly prisma: PrismaService, |
| 19 | + private readonly paymentsService: PaymentsService, |
| 20 | + ) {} |
| 21 | + |
| 22 | + @WebhookEvent( |
| 23 | + PaymentWebhookEvent.processed, |
| 24 | + PaymentWebhookEvent.failed, |
| 25 | + PaymentWebhookEvent.returned, |
| 26 | + ) |
| 27 | + async handlePaymentProcessed( |
| 28 | + payload: PaymentProcessedEventData, |
| 29 | + ): Promise<any> { |
| 30 | + // TODO: remove slice-1 |
| 31 | + const winningIds = (payload.externalId ?? '').split(',').slice(0, -1); |
| 32 | + const externalTransactionId = payload.batch.id; |
| 33 | + |
| 34 | + if (!winningIds.length) { |
| 35 | + this.logger.error( |
| 36 | + `No valid winning IDs found in the externalId: ${payload.externalId}`, |
| 37 | + ); |
| 38 | + throw new Error('No valid winning IDs found in the externalId!'); |
| 39 | + } |
| 40 | + |
| 41 | + if (payload.status !== PaymentProcessedEventStatus.PROCESSED) { |
| 42 | + await this.updatePaymentStates( |
| 43 | + winningIds, |
| 44 | + externalTransactionId, |
| 45 | + payload.status.toUpperCase() as payment_status, |
| 46 | + payload.status.toUpperCase(), |
| 47 | + { failureMessage: payload.failureMessage }, |
| 48 | + ); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + await this.updatePaymentStates( |
| 54 | + winningIds, |
| 55 | + externalTransactionId, |
| 56 | + payment_status.PAID, |
| 57 | + 'PROCESSED', |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + private async updatePaymentStates( |
| 62 | + winningIds: string[], |
| 63 | + paymentId: string, |
| 64 | + processingState: payment_status, |
| 65 | + releaseState: string, |
| 66 | + metadata?: JsonObject, |
| 67 | + ): Promise<void> { |
| 68 | + try { |
| 69 | + await this.prisma.$transaction(async (tx) => { |
| 70 | + await this.paymentsService.updatePaymentProcessingState( |
| 71 | + winningIds, |
| 72 | + processingState, |
| 73 | + tx, |
| 74 | + ); |
| 75 | + |
| 76 | + await this.paymentsService.updatePaymentReleaseState( |
| 77 | + paymentId, |
| 78 | + releaseState, |
| 79 | + tx, |
| 80 | + metadata, |
| 81 | + ); |
| 82 | + }); |
| 83 | + } catch (error) { |
| 84 | + this.logger.error( |
| 85 | + `Failed to update payment states for paymentId: ${paymentId}, winnings: ${winningIds.join(',')}, error: ${error.message}`, |
| 86 | + error.stack, |
| 87 | + ); |
| 88 | + throw error; |
| 89 | + } |
| 90 | + } |
11 | 91 | }
|
0 commit comments