chore: redirect user after payment gateway
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
FROM node:22-alpine AS base
|
FROM node:22-alpine AS base
|
||||||
|
|
||||||
RUN npm install -g corepack@latest
|
RUN npm install -g corepack@latest
|
||||||
RUN corepack enable && corepack prepare pnpm@latest --activate
|
RUN corepack enable && corepack prepare pnpm@9 --activate
|
||||||
|
|
||||||
# Install tzdata to support timezone settings
|
# Install tzdata to support timezone settings
|
||||||
RUN apk add --no-cache tzdata
|
RUN apk add --no-cache tzdata
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Body, Controller, Get, Param, Patch, Post, Query } from "@nestjs/common";
|
import { Body, Controller, Get, Param, Patch, Post, Query, Res } from "@nestjs/common";
|
||||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||||
|
import { FastifyReply } from "fastify";
|
||||||
|
|
||||||
import { CreateBankAccountDto } from "./DTO/create-bankaccount.dto";
|
import { CreateBankAccountDto } from "./DTO/create-bankaccount.dto";
|
||||||
import { GatewayDepositDto, TransferDepositDto } from "./DTO/deposit-wallet.dto";
|
import { GatewayDepositDto, TransferDepositDto } from "./DTO/deposit-wallet.dto";
|
||||||
@@ -87,8 +88,8 @@ export class PaymentsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get("verify/:gateway")
|
@Get("verify/:gateway")
|
||||||
verifyPayment(@Param() paramDto: VerifyParamDto, @Query() queryDto: VerifyQueryDto) {
|
verifyPayment(@Param() paramDto: VerifyParamDto, @Query() queryDto: VerifyQueryDto, @Res({ passthrough: true }) rep: FastifyReply) {
|
||||||
return this.paymentsService.verifyPayment(paramDto.gateway, queryDto);
|
return this.paymentsService.verifyPayment(paramDto.gateway, queryDto, rep);
|
||||||
}
|
}
|
||||||
|
|
||||||
///------------------- bank account -----------------------------
|
///------------------- bank account -----------------------------
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { InjectQueue } from "@nestjs/bullmq";
|
import { InjectQueue } from "@nestjs/bullmq";
|
||||||
import { BadRequestException, HttpException, Injectable, InternalServerErrorException } from "@nestjs/common";
|
import { BadRequestException, HttpException, HttpStatus, Injectable, InternalServerErrorException } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
import { Queue } from "bullmq";
|
import { Queue } from "bullmq";
|
||||||
// eslint-disable-next-line import/no-named-as-default
|
// eslint-disable-next-line import/no-named-as-default
|
||||||
import Decimal from "decimal.js";
|
import Decimal from "decimal.js";
|
||||||
|
import { FastifyReply } from "fastify";
|
||||||
import { DataSource, Not, QueryRunner } from "typeorm";
|
import { DataSource, Not, QueryRunner } from "typeorm";
|
||||||
|
|
||||||
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
||||||
@@ -36,6 +38,7 @@ import { GatewayType } from "../types/gateway.type";
|
|||||||
export class PaymentsService {
|
export class PaymentsService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectQueue(PAYMENT.PAYMENT_QUEUE_NAME) private readonly paymentQueue: Queue,
|
@InjectQueue(PAYMENT.PAYMENT_QUEUE_NAME) private readonly paymentQueue: Queue,
|
||||||
|
private readonly configService: ConfigService,
|
||||||
private readonly gatewayFactory: PaymentGatewayFactory,
|
private readonly gatewayFactory: PaymentGatewayFactory,
|
||||||
private readonly paymentGatewaysRepository: PaymentGatewaysRepository,
|
private readonly paymentGatewaysRepository: PaymentGatewaysRepository,
|
||||||
private readonly paymentsRepository: PaymentsRepository,
|
private readonly paymentsRepository: PaymentsRepository,
|
||||||
@@ -125,7 +128,10 @@ export class PaymentsService {
|
|||||||
|
|
||||||
//*********************************** */
|
//*********************************** */
|
||||||
//*********************************** */
|
//*********************************** */
|
||||||
async verifyPayment(gateway: GatewayType, queryDto: VerifyQueryDto) {
|
async verifyPayment(gateway: GatewayType, queryDto: VerifyQueryDto, rep: FastifyReply) {
|
||||||
|
const frontUrl = new URL(this.configService.getOrThrow<string>("SITE_URL"));
|
||||||
|
frontUrl.pathname = "/transactions";
|
||||||
|
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
@@ -135,7 +141,6 @@ export class PaymentsService {
|
|||||||
const payment = await queryRunner.manager.findOne(Payment, {
|
const payment = await queryRunner.manager.findOne(Payment, {
|
||||||
where: { reference: queryDto.Authority },
|
where: { reference: queryDto.Authority },
|
||||||
relations: ["user"],
|
relations: ["user"],
|
||||||
// lock: { mode: "pessimistic_write" },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!payment) throw new BadRequestException(PaymentMessage.PAYMENT_NOT_FOUND_WITH_REF);
|
if (!payment) throw new BadRequestException(PaymentMessage.PAYMENT_NOT_FOUND_WITH_REF);
|
||||||
@@ -144,37 +149,40 @@ export class PaymentsService {
|
|||||||
if (queryDto.Status !== "OK") {
|
if (queryDto.Status !== "OK") {
|
||||||
await this.handleFailedPayment(payment.id, queryRunner);
|
await this.handleFailedPayment(payment.id, queryRunner);
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
return { message: "nok" };
|
frontUrl.searchParams.append("status", PaymentStatus.FAILED);
|
||||||
|
frontUrl.searchParams.append("id", payment.id);
|
||||||
|
frontUrl.searchParams.append("date", payment.createdAt.toISOString());
|
||||||
|
frontUrl.searchParams.append("amount", payment.amount.toString());
|
||||||
|
|
||||||
|
return rep.status(HttpStatus.FOUND).redirect(frontUrl.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const verifyData = await paymentGateway.verifyPayment({ reference: queryDto.Authority, amount: payment.amount });
|
const verifyData = await paymentGateway.verifyPayment({ reference: queryDto.Authority, amount: payment.amount });
|
||||||
//
|
|
||||||
if (verifyData.code === 100) {
|
if (verifyData.code === 100) {
|
||||||
const transaction = await this.handleSuccessfulPayment(
|
await this.handleSuccessfulPayment(payment.id, payment.user.id, payment.amount, `${verifyData.ref_id}`, queryRunner);
|
||||||
payment.id,
|
|
||||||
payment.user.id,
|
|
||||||
payment.amount,
|
|
||||||
`${verifyData.ref_id}`,
|
|
||||||
queryRunner,
|
|
||||||
);
|
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
return {
|
frontUrl.searchParams.append("status", PaymentStatus.COMPLETED);
|
||||||
message: "success",
|
frontUrl.searchParams.append("id", payment.id);
|
||||||
transaction,
|
frontUrl.searchParams.append("date", payment.createdAt.toISOString());
|
||||||
};
|
frontUrl.searchParams.append("amount", payment.amount.toString());
|
||||||
|
} else if (verifyData.code === 101) {
|
||||||
|
await queryRunner.commitTransaction();
|
||||||
|
frontUrl.searchParams.append("status", PaymentStatus.PENDING);
|
||||||
|
frontUrl.searchParams.append("id", payment.id);
|
||||||
|
frontUrl.searchParams.append("date", payment.createdAt.toISOString());
|
||||||
|
frontUrl.searchParams.append("amount", payment.amount.toString());
|
||||||
|
} else {
|
||||||
|
await this.handleFailedPayment(payment.id, queryRunner);
|
||||||
|
await queryRunner.commitTransaction();
|
||||||
|
frontUrl.searchParams.append("status", PaymentStatus.FAILED);
|
||||||
|
frontUrl.searchParams.append("id", payment.id);
|
||||||
|
frontUrl.searchParams.append("date", payment.createdAt.toISOString());
|
||||||
|
frontUrl.searchParams.append("amount", payment.amount.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verifyData.code === 101) {
|
console.log(frontUrl.toString());
|
||||||
await queryRunner.commitTransaction();
|
return rep.status(HttpStatus.FOUND).redirect(frontUrl.toString());
|
||||||
|
|
||||||
return {
|
|
||||||
message: "not valid payment",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.handleFailedPayment(payment.id, queryRunner);
|
|
||||||
await queryRunner.commitTransaction();
|
|
||||||
|
|
||||||
return { message: "nok" };
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await queryRunner.rollbackTransaction();
|
await queryRunner.rollbackTransaction();
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { createHmac } from "node:crypto";
|
import { createHmac } from "node:crypto";
|
||||||
|
|
||||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
import { BadRequestException, HttpStatus, Injectable, Logger } from "@nestjs/common";
|
||||||
import { ConfigService } from "@nestjs/config";
|
import { ConfigService } from "@nestjs/config";
|
||||||
import { FastifyReply } from "fastify";
|
import { FastifyReply } from "fastify";
|
||||||
import slugify from "slugify";
|
import slugify from "slugify";
|
||||||
@@ -159,6 +159,7 @@ export class UsersService {
|
|||||||
/************************************************************ */
|
/************************************************************ */
|
||||||
//TODO:fix this later
|
//TODO:fix this later
|
||||||
async verifyEmail(queryDto: EmailVerifyQueryDto, rep: FastifyReply) {
|
async verifyEmail(queryDto: EmailVerifyQueryDto, rep: FastifyReply) {
|
||||||
|
const frontUrl = this.configService.getOrThrow<string>("FRONT_URL");
|
||||||
const { token, userId, ts } = queryDto;
|
const { token, userId, ts } = queryDto;
|
||||||
|
|
||||||
const user = await this.userRepository.findOneBy({ id: userId });
|
const user = await this.userRepository.findOneBy({ id: userId });
|
||||||
@@ -181,7 +182,7 @@ export class UsersService {
|
|||||||
await this.userRepository.save(user);
|
await this.userRepository.save(user);
|
||||||
|
|
||||||
// return { message: UserMessage.EMAIL_VERIFIED };
|
// return { message: UserMessage.EMAIL_VERIFIED };
|
||||||
return rep.redirect("https://google.com");
|
return rep.status(HttpStatus.FOUND).redirect(frontUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************************************ */
|
/************************************************************ */
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ export class UsersController {
|
|||||||
/************************************************************ */
|
/************************************************************ */
|
||||||
|
|
||||||
@ApiOperation({ summary: "Verify email" })
|
@ApiOperation({ summary: "Verify email" })
|
||||||
@HttpCode(HttpStatus.PERMANENT_REDIRECT)
|
|
||||||
@Get("verify-email")
|
@Get("verify-email")
|
||||||
verifyEmail(@Query() queryDto: EmailVerifyQueryDto, @Res({ passthrough: true }) rep: FastifyReply) {
|
verifyEmail(@Query() queryDto: EmailVerifyQueryDto, @Res({ passthrough: true }) rep: FastifyReply) {
|
||||||
return this.usersService.verifyEmail(queryDto, rep);
|
return this.usersService.verifyEmail(queryDto, rep);
|
||||||
|
|||||||
Reference in New Issue
Block a user