feat: add payment module and factory
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user