Files
dsc-api/src/modules/payments/factories/payment.factory.ts
T
2025-05-03 16:37:57 +03:30

35 lines
1.2 KiB
TypeScript
Executable File

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<GatewayType, IPaymentGateway>;
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 = Array.from(this.gateways.keys()).map((name) => ({ name: name as GatewayEnum }));
return { gateways };
}
}