refactor payment

This commit is contained in:
2026-01-03 18:15:14 +03:30
parent a65ad8b5ff
commit 0a1ef0f8f2
2 changed files with 12 additions and 6 deletions
+8 -3
View File
@@ -12,12 +12,16 @@ class PaymentGateway implements IPaymentGateway {
@inject(IOCTYPES.ZarinPalGateway) private zarinPalGateway: ZarinPalGateway; @inject(IOCTYPES.ZarinPalGateway) private zarinPalGateway: ZarinPalGateway;
@inject(IOCTYPES.SEPGateway) private sepGateway: SepGateway; @inject(IOCTYPES.SEPGateway) private sepGateway: SepGateway;
private readonly gateways: Map<GatewayProvider, IGateway> = new Map(); private gateways: Map<GatewayProvider, IGateway> | null = null;
constructor() { private initializeGateways(): Map<GatewayProvider, IGateway> {
if (!this.gateways) {
this.gateways = new Map();
this.gateways.set(GatewayProvider.Zarinpal, this.zarinPalGateway); this.gateways.set(GatewayProvider.Zarinpal, this.zarinPalGateway);
this.gateways.set(GatewayProvider.SEP, this.sepGateway); this.gateways.set(GatewayProvider.SEP, this.sepGateway);
} }
return this.gateways;
}
public async requestPayment(gatewayType: GatewayProvider, data: NewPaymentData): Promise<any> { public async requestPayment(gatewayType: GatewayProvider, data: NewPaymentData): Promise<any> {
const gateway = this.getGateway(gatewayType); const gateway = this.getGateway(gatewayType);
@@ -39,7 +43,8 @@ class PaymentGateway implements IPaymentGateway {
} }
private getGateway(gatewayType: GatewayProvider): IGateway { private getGateway(gatewayType: GatewayProvider): IGateway {
const gateway = this.gateways.get(gatewayType); const gateways = this.initializeGateways();
const gateway = gateways.get(gatewayType);
if (!gateway) { if (!gateway) {
throw new BadRequestError(`Payment provider ${gatewayType} is not supported`); throw new BadRequestError(`Payment provider ${gatewayType} is not supported`);
} }
@@ -196,9 +196,10 @@ class PaymentService {
// Verify payment through gateway // Verify payment through gateway
const verifyData = const verifyData =
gateway === GatewayProvider.SEP gateway === GatewayProvider.SEP
? { refNum: authority, amount: paymentData.totalPrice } ? { refNum, amount: paymentData.totalPrice }
: { authority, amount: paymentData.totalPrice }; : { authority, amount: paymentData.totalPrice };
const data = await this.paymentGateway.verifyPayment(gateway, verifyData); const data = await this.paymentGateway.verifyPayment(gateway, verifyData);
if (data.code === 100 || data.code === 101) { if (data.code === 100 || data.code === 101) {
await this.handleSellerNotify(order._id, user, session); await this.handleSellerNotify(order._id, user, session);
// For SEP gateway, save refNum as transaction_id; for Zarinpal, save ref_id // For SEP gateway, save refNum as transaction_id; for Zarinpal, save ref_id