From 0a1ef0f8f288bc6bed29933016ba782a6252da5b Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 3 Jan 2026 18:15:14 +0330 Subject: [PATCH] refactor payment --- src/modules/IPG/PaymentGateway.ts | 15 ++++++++++----- src/modules/payment/providers/payment.service.ts | 3 ++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/modules/IPG/PaymentGateway.ts b/src/modules/IPG/PaymentGateway.ts index 40a0c78..3021dcb 100644 --- a/src/modules/IPG/PaymentGateway.ts +++ b/src/modules/IPG/PaymentGateway.ts @@ -12,11 +12,15 @@ class PaymentGateway implements IPaymentGateway { @inject(IOCTYPES.ZarinPalGateway) private zarinPalGateway: ZarinPalGateway; @inject(IOCTYPES.SEPGateway) private sepGateway: SepGateway; - private readonly gateways: Map = new Map(); + private gateways: Map | null = null; - constructor() { - this.gateways.set(GatewayProvider.Zarinpal, this.zarinPalGateway); - this.gateways.set(GatewayProvider.SEP, this.sepGateway); + private initializeGateways(): Map { + if (!this.gateways) { + this.gateways = new Map(); + this.gateways.set(GatewayProvider.Zarinpal, this.zarinPalGateway); + this.gateways.set(GatewayProvider.SEP, this.sepGateway); + } + return this.gateways; } public async requestPayment(gatewayType: GatewayProvider, data: NewPaymentData): Promise { @@ -39,7 +43,8 @@ class PaymentGateway implements IPaymentGateway { } private getGateway(gatewayType: GatewayProvider): IGateway { - const gateway = this.gateways.get(gatewayType); + const gateways = this.initializeGateways(); + const gateway = gateways.get(gatewayType); if (!gateway) { throw new BadRequestError(`Payment provider ${gatewayType} is not supported`); } diff --git a/src/modules/payment/providers/payment.service.ts b/src/modules/payment/providers/payment.service.ts index 17b0443..b2a1227 100644 --- a/src/modules/payment/providers/payment.service.ts +++ b/src/modules/payment/providers/payment.service.ts @@ -196,9 +196,10 @@ class PaymentService { // Verify payment through gateway const verifyData = gateway === GatewayProvider.SEP - ? { refNum: authority, amount: paymentData.totalPrice } + ? { refNum, amount: paymentData.totalPrice } : { authority, amount: paymentData.totalPrice }; const data = await this.paymentGateway.verifyPayment(gateway, verifyData); + if (data.code === 100 || data.code === 101) { await this.handleSellerNotify(order._id, user, session); // For SEP gateway, save refNum as transaction_id; for Zarinpal, save ref_id