fix : sep gateway
This commit is contained in:
@@ -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<any>, logPrefix: string = "Request Body"): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
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<any>): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
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<any>): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
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<any>): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
@@ -195,7 +195,6 @@ class PaymentService {
|
||||
: { 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);
|
||||
|
||||
Reference in New Issue
Block a user