initiate-payment api modified for dynamic amount from UI

This commit is contained in:
Kallesh B S 2025-08-22 15:16:02 +05:30
parent 0e59a652b0
commit 45d57cee8d
4 changed files with 54 additions and 11 deletions

View File

@ -46,6 +46,27 @@ export class ORDERID_DTO {
P_ORDERID: string; P_ORDERID: string;
} }
export class PRICE_DTO {
@ApiProperty({ required: true })
// @Max(999999999, {
// message: 'Property P_PRICE must not exceed 999999999',
// })
@Min(0.1, { message: 'Property P_PRICE must be greater than zero' })
// @IsInt({ message: 'Property P_PRICE allows only whole numbers' })
@IsNumber({}, { message: 'Property P_PRICE must be a number' })
@Transform(({ value }) => Number(value))
@IsDefined({ message: 'Property P_PRICE is required' })
P_PRICE: number;
}
export class DESCRIPTION_DTO {
@ApiProperty({ required: true })
@IsString()
@IsDefined({ message: 'Property P_DESCRIPTION is required' })
P_DESCRIPTION: string;
}
export class HEADERID_DTO { export class HEADERID_DTO {
@ApiProperty({ required: true }) @ApiProperty({ required: true })
@Max(999999999, { @Max(999999999, {

View File

@ -3,8 +3,10 @@ import { IntersectionType, PartialType } from "@nestjs/swagger";
import { import {
APPLICATIONNAME_DTO, AUTHREP_DTO, AUTO_FLAG_DTO, CARNETNO_DTO, COMMERCIAL_SAMPLE_FLAG_DTO, APPLICATIONNAME_DTO, AUTHREP_DTO, AUTO_FLAG_DTO, CARNETNO_DTO, COMMERCIAL_SAMPLE_FLAG_DTO,
COUNTRYTABLE_DTO, CUSTCOURIERNO_DTO, DELIVERYMETHOD_DTO, DELIVERYTYPE_DTO, COUNTRYTABLE_DTO, CUSTCOURIERNO_DTO, DELIVERYMETHOD_DTO, DELIVERYTYPE_DTO,
DESCRIPTION_DTO,
EXIBITIONS_FAIR_FLAG_DTO, EXTENSION_PERIOD_DTO, FORMOFSECURITY_DTO, GLTABLE_DTO, GLTABLE_ITEMNO_OPTIONAL_DTO, GOODS_COUNTRY_DTO, GOODS_PORT_DTO, HEADERID_DTO, EXIBITIONS_FAIR_FLAG_DTO, EXTENSION_PERIOD_DTO, FORMOFSECURITY_DTO, GLTABLE_DTO, GLTABLE_ITEMNO_OPTIONAL_DTO, GOODS_COUNTRY_DTO, GOODS_PORT_DTO, HEADERID_DTO,
HORSE_FLAG_DTO, INSPROTECTION_DTO, ITEMNO_DTO, LDIPROTECTION_DTO, ORDERID_DTO, ORDERTYPE_DTO, PAYMENTMETHOD_DTO, HORSE_FLAG_DTO, INSPROTECTION_DTO, ITEMNO_DTO, LDIPROTECTION_DTO, ORDERID_DTO, ORDERTYPE_DTO, PAYMENTMETHOD_DTO,
PRICE_DTO,
PRINTGL_DTO, PRINTGL_DTO,
PROF_EQUIPMENT_FLAG_DTO, REASON_CODE_DTO, REFNO_DTO, SHIPADDRID_DTO, SHIPCONTACTID_DTO, SHIPNAME_DTO, SHIPTOTYPE_DTO, USSETS_DTO PROF_EQUIPMENT_FLAG_DTO, REASON_CODE_DTO, REFNO_DTO, SHIPADDRID_DTO, SHIPCONTACTID_DTO, SHIPNAME_DTO, SHIPTOTYPE_DTO, USSETS_DTO
} from "./carnet-application-property.dto"; } from "./carnet-application-property.dto";
@ -69,6 +71,7 @@ export class SaveExtensionApplicationDTO extends (IntersectionType(
export class PrintCarnetDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO, PRINTGL_DTO)) { } export class PrintCarnetDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO, PRINTGL_DTO)) { }
export class PrintGLDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO)) { } export class PrintGLDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO)) { }
export class CapturePaymentDTO extends (IntersectionType(ORDERID_DTO)) { } export class CapturePaymentDTO extends (IntersectionType(ORDERID_DTO)) { }
export class InitiatePaymentDTO extends (IntersectionType(PRICE_DTO, DESCRIPTION_DTO)){}
export class CreateApplicationDTO extends IntersectionType( export class CreateApplicationDTO extends IntersectionType(
SPID_DTO, CLIENTID_DTO, LOCATIONID_DTO, USERID_DTO, APPLICATIONNAME_DTO, SPID_DTO, CLIENTID_DTO, LOCATIONID_DTO, USERID_DTO, APPLICATIONNAME_DTO,

View File

@ -8,7 +8,7 @@ import {
AddGenerallistItemsDTO, AddGenerallistItemsDTO,
CA_UpdateHolderDTO, CA_UpdateHolderDTO,
CapturePaymentDTO, CapturePaymentDTO,
CarnetProcessingCenterDTO, CarnetProcessingCenterDTO2, CopyCarnetDTO, CreateApplicationDTO, DeleteGenerallistItemsDTO, EditGenerallistItemsDTO, GetCarnetControlCenterDTO, GetExtendedSectionDTO, PrintCarnetDTO, PrintGLDTO, SaveCarnetApplicationDTO, SaveExtensionApplicationDTO, TransmitApplicationtoProcessDTO, CarnetProcessingCenterDTO, CarnetProcessingCenterDTO2, CopyCarnetDTO, CreateApplicationDTO, DeleteGenerallistItemsDTO, EditGenerallistItemsDTO, GetCarnetControlCenterDTO, GetExtendedSectionDTO, InitiatePaymentDTO, PrintCarnetDTO, PrintGLDTO, SaveCarnetApplicationDTO, SaveExtensionApplicationDTO, TransmitApplicationtoProcessDTO,
UpdateExpGoodsAuthRepDTO, UpdateExpGoodsAuthRepDTO,
UpdateShippingDetailsDTO UpdateShippingDetailsDTO
} from 'src/dto/property.dto'; } from 'src/dto/property.dto';
@ -276,8 +276,8 @@ export class CarnetApplicationController {
@Post('InitiatePayment') @Post('InitiatePayment')
@UseGuards() @UseGuards()
async InitiatePayment() { async InitiatePayment(@Body() body: InitiatePaymentDTO) {
return this.carnetApplicationService.InitiatePayment(); return this.carnetApplicationService.InitiatePayment(body);
} }
@Post('CompletePayment') @Post('CompletePayment')

View File

@ -21,7 +21,8 @@ import {
GetExtendedSectionDTO, GetExtendedSectionDTO,
SaveExtensionApplicationDTO, SaveExtensionApplicationDTO,
CarnetProcessingCenterDTO2, CarnetProcessingCenterDTO2,
CapturePaymentDTO CapturePaymentDTO,
InitiatePaymentDTO
} from 'src/dto/property.dto'; } from 'src/dto/property.dto';
import { OracleService } from '../oracle.service'; import { OracleService } from '../oracle.service';
import { BadRequestException } from 'src/exceptions/badRequest.exception'; import { BadRequestException } from 'src/exceptions/badRequest.exception';
@ -1589,8 +1590,26 @@ export class CarnetApplicationService {
// [payment] // [payment]
async formatAmount(amount) {
if (typeof amount !== 'number' || isNaN(amount)) {
throw new Error('Amount must be a valid number');
}
if (amount < 0) {
throw new BadRequestException('Amount must be non-negative');
}
return amount.toFixed(2); // Always returns a string with 2 decimal places
}
async InitiatePayment(body: InitiatePaymentDTO) {
const quantity = 1;
const formattedAmount = await this.formatAmount(body.P_PRICE * quantity);
async InitiatePayment() {
try { try {
const accessToken = await this.payPalService.generateAccessToken(); const accessToken = await this.payPalService.generateAccessToken();
@ -1609,22 +1628,22 @@ export class CarnetApplicationService {
{ {
items: [ items: [
{ {
name: 'carnet', name: 'Carnet',
description: 'carnet', description: body.P_DESCRIPTION,
quantity: 1, quantity: quantity,
unit_amount: { unit_amount: {
currency_code: 'USD', currency_code: 'USD',
value: '100.00' value: formattedAmount
} }
} }
], ],
amount: { amount: {
currency_code: 'USD', currency_code: 'USD',
value: '100.00', value: formattedAmount,
breakdown: { breakdown: {
item_total: { item_total: {
currency_code: 'USD', currency_code: 'USD',
value: '100.00' value: formattedAmount
} }
} }
} }