feat: add payment module and factory

This commit is contained in:
mahyargdz
2025-02-03 15:41:53 +03:30
parent 8fb587f976
commit 31368610dd
48 changed files with 931 additions and 81 deletions
@@ -0,0 +1,42 @@
import { Injectable } from "@nestjs/common";
import { QueryRunner } from "typeorm";
import { User } from "../../users/entities/user.entity";
// import { PaymentMethod } from "../entities/payment-method.entity";
import { Payment } from "../entities/payment.entity";
import { PaymentGatewayFactory } from "../factories/payment.factory";
// import { PaymentsRepository } from "../repositories/payments.repository";
import { GatewayType } from "../types/gateway.type";
@Injectable()
export class PaymentsService {
constructor(
private readonly gatewayFactory: PaymentGatewayFactory,
// private readonly paymentsRepository: PaymentsRepository,
) {}
//*********************************** */
//*********************************** */
async processPayment(provider: GatewayType, amount: number, description: string, email?: string, mobile?: string) {
const paymentGateway = this.gatewayFactory.getPaymentGateway(provider);
return paymentGateway.processPayment({ amount, description, email, mobile });
}
//*********************************** */
//*********************************** */
async createPaymentForUser(user: User, amount: number, reference: string, queryRunner: QueryRunner) {
// const paymentMethod = queryRunner.manager.create(PaymentMethod, {});
const payment = queryRunner.manager.create(Payment, {
amount,
reference,
user,
});
await queryRunner.manager.save(Payment, payment);
return payment;
}
//*********************************** */
//*********************************** */
async getAvailableGateways() {
return this.gatewayFactory.getAvailablePaymentGateways();
}
}