refactor: the whole paymanet service

This commit is contained in:
mahyargdz
2025-05-03 16:30:22 +03:30
parent 4f54774400
commit 2e2154d745
30 changed files with 1231 additions and 396 deletions
@@ -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 };
}
}