From 3042b4fa387ee5ca4e6681d8ef73f76e15fad742 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 4 Jan 2026 17:31:43 +0330 Subject: [PATCH] fix : sep gateway --- src/core/middlewares/validator.middleware.ts | 143 ------------------ src/modules/payment/payment.controller.ts | 5 +- .../payment/providers/payment.service.ts | 5 +- 3 files changed, 3 insertions(+), 150 deletions(-) delete mode 100644 src/core/middlewares/validator.middleware.ts diff --git a/src/core/middlewares/validator.middleware.ts b/src/core/middlewares/validator.middleware.ts deleted file mode 100644 index 29d27f4..0000000 --- a/src/core/middlewares/validator.middleware.ts +++ /dev/null @@ -1,143 +0,0 @@ -import { ClassConstructor, plainToInstance } from "class-transformer"; -import { ValidationError, validate } from "class-validator"; -import { NextFunction, Request, RequestHandler, Response } from "express"; -import { injectable } from "inversify"; - -import { BadRequestError } from "../app/app.errors"; - -@injectable() -export class ValidationMiddleware { - static validateInputWithLogging(DTO: ClassConstructor, logPrefix: string = "Request Body"): RequestHandler { - return async (req: Request, _res: Response, next: NextFunction): Promise => { - try { - // Log the raw request body before validation - console.log(`${logPrefix}:`, JSON.stringify(req.body, null, 2)); - - // Convert the request body to an instance of the DTO class - const validationClass = plainToInstance(DTO, req.body, { - excludeExtraneousValues: true, - }); - - // Validate the DTO instance, including nested objects - const errors: ValidationError[] = await validate(validationClass, { - whitelist: true, - // forbidNonWhitelisted: true, - // transform: true, - }); - if (errors.length > 0) { - const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat(); - - next(new BadRequestError(errorMsg)); - // throw new BadRequestError("Bad Request", errorMsg); - } else { - req.body = validationClass; - next(); - } - } catch (error) { - next(error); - } - }; - } - - static validateInput(DTO: ClassConstructor): RequestHandler { - return async (req: Request, _res: Response, next: NextFunction): Promise => { - try { - // Convert the request body to an instance of the DTO class - const validationClass = plainToInstance(DTO, req.body, { - excludeExtraneousValues: true, - }); - - // Validate the DTO instance, including nested objects - const errors: ValidationError[] = await validate(validationClass, { - whitelist: true, - // forbidNonWhitelisted: true, - // transform: true, - }); - if (errors.length > 0) { - const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat(); - - next(new BadRequestError(errorMsg)); - // throw new BadRequestError("Bad Request", errorMsg); - } else { - req.body = validationClass; - next(); - } - } catch (error) { - next(error); - } - }; - } - - static validateParameter(DTO: ClassConstructor): RequestHandler { - return async (req: Request, _res: Response, next: NextFunction): Promise => { - try { - // Convert the request body to an instance of the DTO class - const validationClass = plainToInstance(DTO, req.params, { - excludeExtraneousValues: true, - enableImplicitConversion: true, - }); - - // Validate the DTO instance, including nested objects - const errors: ValidationError[] = await validate(validationClass, { - whitelist: true, - // forbidNonWhitelisted: true, - // transform: true, - }); - if (errors.length > 0) { - const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat(); - - next(new BadRequestError(errorMsg)); - // throw new BadRequestError("Bad Request", errorMsg); - } else { - req.query = validationClass; - next(); - } - } catch (error) { - next(error); - } - }; - } - - static validateQuery(DTO: ClassConstructor): RequestHandler { - return async (req: Request, _res: Response, next: NextFunction): Promise => { - try { - // Convert the request body to an instance of the DTO class - const validationClass = plainToInstance(DTO, req.query, { - excludeExtraneousValues: true, - enableImplicitConversion: true, - }); - - // Validate the DTO instance, including nested objects - const errors: ValidationError[] = await validate(validationClass, { - whitelist: true, - // forbidNonWhitelisted: true, - // transform: true, - }); - if (errors.length > 0) { - const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat(); - - next(new BadRequestError(errorMsg)); - // throw new BadRequestError("Bad Request", errorMsg); - } else { - req.query = validationClass; - next(); - } - } catch (error) { - next(error); - } - }; - } - - // Helper function to recursively flatten nested validation errors - private static flattenValidationErrors(error: ValidationError): string[] { - if (error.constraints) { - return Object.values(error.constraints); - } - - if (error.children && error.children.length > 0) { - return error.children.map(ValidationMiddleware.flattenValidationErrors).flat(); - } - - return []; - } -} diff --git a/src/modules/payment/payment.controller.ts b/src/modules/payment/payment.controller.ts index c18570b..d4cf415 100644 --- a/src/modules/payment/payment.controller.ts +++ b/src/modules/payment/payment.controller.ts @@ -12,7 +12,6 @@ import { BaseController } from "../../common/base/controller"; import { ApiAuth, ApiModel, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from "../../common/decorator/swggerDocs"; import { GatewayProvider } from "../../common/enums/payment.enum"; import { Guard } from "../../core/middlewares/guard.middleware"; -import { ValidationMiddleware } from "../../core/middlewares/validator.middleware"; import { IOCTYPES } from "../../IOC/ioc.types"; import { verifyPaymentTemplate } from "../../template/payment"; import { verifyPRPaymentTemplate } from "../../template/PRPayment"; @@ -96,7 +95,6 @@ class PaymentController extends BaseController { @queryParam("Status") paymentStatus: string, @response() res: Response, ) { - console.log("get verify cart checkout", authority, paymentStatus); const verifyCallbackData = { status: paymentStatus, authority: authority, @@ -111,7 +109,7 @@ class PaymentController extends BaseController { @ApiOperation("verify a payment for current session user for cartCheckout (method post for SEP)") @ApiResponse("successful", HttpStatus.Ok) @ApiParam("gateway", "the name of api gateway", true) - @httpPost("/verify/:gateway/cart", ValidationMiddleware.validateInputWithLogging(VerifyOnlinePaymentDTO, "Verify Cart Checkout")) + @httpPost("/verify/:gateway/cart") public async VerifyCartCheckoutPost( @requestParam("gateway") gateway: string, @response() res: Response, @@ -125,7 +123,6 @@ class PaymentController extends BaseController { const data = await this.paymentService.verifyCartPayment(gateway as GatewayProvider, verifyCallbackData); const html = verifyPaymentTemplate(data); return res.send(html); - // return this.response(data); } @ApiOperation("verify a payment for current session seller for product request checkout") diff --git a/src/modules/payment/providers/payment.service.ts b/src/modules/payment/providers/payment.service.ts index 02dd4ac..a142c8e 100644 --- a/src/modules/payment/providers/payment.service.ts +++ b/src/modules/payment/providers/payment.service.ts @@ -185,7 +185,7 @@ class PaymentService { // Handle unsuccessful payment status if (status !== "OK") { - return this.buildPaymentResponse(status, PaymentMessage.PaymentNotSuccessful, paymentData._id.toString(), order._id); + return this.buildPaymentResponse(status ?? 'Nok', PaymentMessage.PaymentNotSuccessful, paymentData._id.toString(), order._id); } // Verify payment through gateway @@ -194,8 +194,7 @@ class PaymentService { ? { refNum, amount: paymentData.totalPrice } : { authority, amount: paymentData.totalPrice }; const data = await this.paymentGateway.verifyPayment(gateway, verifyData); - - console.log("verify payment data**", data); + if (data.code === 100 || data.code === 101) { await this.handleSellerNotify(order._id, user, session);