refactor: the whole paymanet service
This commit is contained in:
@@ -3,26 +3,32 @@ import { BadGatewayException, Injectable } from "@nestjs/common";
|
||||
import { PaymentMessage } from "../../../common/enums/message.enum";
|
||||
import { GatewayEnum } from "../enums/gateway.enum";
|
||||
import { ZarinpalGateway } from "../gateways/zarinpal.gateway";
|
||||
import { IPaymentGateway, IPaymentGatewayFactory } from "../interfaces/IPayment";
|
||||
import { GatewayType } from "../types/gateway.type";
|
||||
|
||||
@Injectable()
|
||||
export class PaymentGatewayFactory {
|
||||
constructor(private readonly ZarinpalGateway: ZarinpalGateway) {}
|
||||
export class PaymentGatewayFactory implements IPaymentGatewayFactory {
|
||||
private readonly gateways: Map<GatewayType, IPaymentGateway>;
|
||||
|
||||
//************************ *
|
||||
getPaymentGateway(provider: GatewayType) {
|
||||
switch (provider) {
|
||||
case "zarinpal":
|
||||
return this.ZarinpalGateway;
|
||||
|
||||
default:
|
||||
throw new BadGatewayException(PaymentMessage.PAYMENT_GATEWAY_NOT_FOUND);
|
||||
}
|
||||
constructor(private readonly zarinpalGateway: ZarinpalGateway) {
|
||||
this.gateways = new Map<GatewayType, IPaymentGateway>();
|
||||
this.initializeGateways();
|
||||
}
|
||||
|
||||
private initializeGateways(): void {
|
||||
this.gateways.set(GatewayEnum.ZARINPAL, this.zarinpalGateway);
|
||||
}
|
||||
|
||||
getPaymentGateway(provider: GatewayType): IPaymentGateway {
|
||||
const gateway = this.gateways.get(provider);
|
||||
if (!gateway) {
|
||||
throw new BadGatewayException(PaymentMessage.PAYMENT_GATEWAY_NOT_FOUND);
|
||||
}
|
||||
return gateway;
|
||||
}
|
||||
|
||||
//************************ */
|
||||
getAvailablePaymentGateways() {
|
||||
const gateways = [{ name: GatewayEnum.ZARINPAL }];
|
||||
const gateways = Array.from(this.gateways.keys()).map((name) => ({ name: name as GatewayEnum }));
|
||||
return { gateways };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user