chore: add direct payment for invoice

This commit is contained in:
Mahyargdz
2025-05-14 16:31:05 +03:30
parent eae5981ea1
commit 2ac29d1a91
9 changed files with 82 additions and 19 deletions
@@ -23,7 +23,7 @@ import { PaymentTransactionQueryDto } from "../DTO/payment-transaction-query.dto
import { SearchTransactionQueryDto } from "../DTO/search-transaction-query.dto";
import { UpdateBankAccountDto } from "../DTO/update-bankaccount.dto";
import { RejectDepositRequestDto } from "../DTO/update-deposit-request-status.dto";
import { VerifyQueryDto } from "../DTO/verify-payment.dto";
import { VerifyParamDto, VerifyQueryDto } from "../DTO/verify-payment.dto";
import { BankAccount } from "../entities/bank-account.entity";
import { DepositRequest } from "../entities/deposit-request.entity";
import { PaymentGateway } from "../entities/payment-gateway.entity";
@@ -32,7 +32,7 @@ import { DepositRequestStatus } from "../enums/deposit-request-status.enum";
import { PaymentStatus } from "../enums/payment-status.enum";
import { TransferType } from "../enums/payment-type.enum";
import { PaymentGatewayFactory } from "../factories/payment.factory";
import { IVerifyPayment } from "../interfaces/IPayment";
import { IProcessPaymentParams, IVerifyPayment } from "../interfaces/IPayment";
import { BankAccountsRepository } from "../repositories/bank-accounts.repository";
import { DepositRequestsRepository } from "../repositories/deposit-requests.repository";
import { PaymentGatewaysRepository } from "../repositories/payment-gateway.repository";
@@ -58,16 +58,22 @@ export class PaymentsService {
//===============================================
async chargeWalletWithGateway(chargeDto: GatewayDepositDto, userId: string) {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const { amount, gatewayId } = chargeDto;
await queryRunner.connect();
await queryRunner.startTransaction();
const { amount, gatewayId, invoiceId } = chargeDto;
const user = await this.getUserById(userId, queryRunner);
const paymentGateway = await this.getPaymentGatewayById(gatewayId, queryRunner);
const gatewayData = await this.processPayment(paymentGateway.name, amount, WalletMessage.DEPOSIT_WALLET_IPG, user.email, user.phone);
const gatewayData = await this.processPayment(paymentGateway.name, {
amount,
description: WalletMessage.DEPOSIT_WALLET_IPG,
email: user.email,
mobile: user.phone,
invoiceId,
});
const payment = await this.createGatewayPaymentForUser(user, amount, gatewayData.reference, paymentGateway.id, queryRunner);
@@ -113,21 +119,21 @@ export class PaymentsService {
}
//===============================================
async processPayment(provider: GatewayType, amount: number, description: string, email?: string, mobile?: string) {
async processPayment(provider: GatewayType, params: IProcessPaymentParams) {
const paymentGateway = this.gatewayFactory.getPaymentGateway(provider);
return paymentGateway.processPayment({ amount, description, email, mobile });
return paymentGateway.processPayment(params);
}
//===============================================
async verifyPayment(gateway: GatewayType, queryDto: VerifyQueryDto, rep: FRply) {
const frontUrl = this.buildFrontendRedirectUrl();
async verifyPayment(verifyParamDto: VerifyParamDto, queryDto: VerifyQueryDto, rep: FRply) {
const frontUrl = this.buildFrontendRedirectUrl(verifyParamDto.invoiceId);
const queryRunner = this.dataSource.createQueryRunner();
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const paymentGateway = this.gatewayFactory.getPaymentGateway(gateway);
const paymentGateway = this.gatewayFactory.getPaymentGateway(verifyParamDto.gateway);
const payment = await this.getPaymentByReference(queryDto.Authority, queryRunner);
if (payment.status === PaymentStatus.COMPLETED) throw new BadRequestException(PaymentMessage.VALIDATED_BEFORE);
@@ -409,9 +415,13 @@ export class PaymentsService {
//-private methods
//----------------------------------------
private buildFrontendRedirectUrl(): URL {
private buildFrontendRedirectUrl(invoiceId?: string): URL {
const frontUrl = new URL(this.configService.getOrThrow<string>("SITE_URL"));
frontUrl.pathname = "/payment";
if (invoiceId) {
frontUrl.pathname = "/receipts/" + invoiceId;
} else {
frontUrl.pathname = "/payment";
}
return frontUrl;
}