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 implements IPaymentGatewayFactory { private readonly gateways: Map; constructor(private readonly zarinpalGateway: ZarinpalGateway) { this.gateways = new Map(); 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 = Array.from(this.gateways.keys()).map((name) => ({ name: name as GatewayEnum })); return { gateways }; } }