refactor payment
This commit is contained in:
@@ -12,11 +12,15 @@ 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> {
|
||||||
this.gateways.set(GatewayProvider.Zarinpal, this.zarinPalGateway);
|
if (!this.gateways) {
|
||||||
this.gateways.set(GatewayProvider.SEP, this.sepGateway);
|
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<any> {
|
public async requestPayment(gatewayType: GatewayProvider, data: NewPaymentData): Promise<any> {
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user