refactor payment
This commit is contained in:
@@ -12,12 +12,16 @@ class PaymentGateway implements IPaymentGateway {
|
||||
@inject(IOCTYPES.ZarinPalGateway) private zarinPalGateway: ZarinPalGateway;
|
||||
@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.SEP, this.sepGateway);
|
||||
}
|
||||
return this.gateways;
|
||||
}
|
||||
|
||||
public async requestPayment(gatewayType: GatewayProvider, data: NewPaymentData): Promise<any> {
|
||||
const gateway = this.getGateway(gatewayType);
|
||||
@@ -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`);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user