From 683406a396014d5c5dbb2a5c2c296a53f5cb8106 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 6 Dec 2025 08:45:59 +0330 Subject: [PATCH] payment --- .../controllers/payments.controller.ts | 9 ++ .../payments/dto/create-payment.dto.ts | 2 +- .../services/payment-gateway.service.ts | 2 +- .../payments/services/payments.service.ts | 96 ++++++++++++++----- 4 files changed, 83 insertions(+), 26 deletions(-) diff --git a/src/modules/payments/controllers/payments.controller.ts b/src/modules/payments/controllers/payments.controller.ts index e0b55ae..36f8c93 100644 --- a/src/modules/payments/controllers/payments.controller.ts +++ b/src/modules/payments/controllers/payments.controller.ts @@ -26,6 +26,15 @@ export class PaymentsController { return this.paymentMethodService.findByRestaurant(restId); } + @UseGuards(AuthGuard) + @ApiBearerAuth() + @Get('public/payments/methods/restaurant') + @ApiOperation({ summary: 'Get the restaurant payment methods' }) + @ApiNotFoundResponse({ description: 'Restaurant payment methods not found' }) + payOrder(@RestId() restId: string) { + return this.paymentMethodService.findByRestaurant(restId); + } + // @UseGuards(AdminAuthGuard) // @ApiBearerAuth() // @Get('admin/payments') diff --git a/src/modules/payments/dto/create-payment.dto.ts b/src/modules/payments/dto/create-payment.dto.ts index a4870d5..85a63a3 100644 --- a/src/modules/payments/dto/create-payment.dto.ts +++ b/src/modules/payments/dto/create-payment.dto.ts @@ -13,7 +13,7 @@ export class CreatePaymentDto { @ApiProperty({ description: 'Payment method' }) @IsEnum(PaymentMethodEnum) - paymentMethod: PaymentMethodEnum; + method: PaymentMethodEnum; @ApiProperty({ description: 'Payment authority' }) @IsOptional() diff --git a/src/modules/payments/services/payment-gateway.service.ts b/src/modules/payments/services/payment-gateway.service.ts index 1ed717c..18485f9 100644 --- a/src/modules/payments/services/payment-gateway.service.ts +++ b/src/modules/payments/services/payment-gateway.service.ts @@ -85,7 +85,7 @@ export class PaymentGatewayService { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new BadRequestException(`Failed to connect to payment gateway: ${errorMessage}`); } - } + } // If gateway is not supported, throw an error throw new BadRequestException(`Unsupported payment gateway: ${String(gateway)}`); diff --git a/src/modules/payments/services/payments.service.ts b/src/modules/payments/services/payments.service.ts index 815ba76..ea56e4a 100644 --- a/src/modules/payments/services/payments.service.ts +++ b/src/modules/payments/services/payments.service.ts @@ -14,24 +14,81 @@ export class PaymentsService { private readonly paymentGatewayService: PaymentGatewayService, ) {} - async initializePayment( - paymentMethodId: string, - amount: number, - orderId: string, - ): Promise<{ paymentUrl: string | null }> { + // async initializePayment( + // paymentMethodId: string, + // amount: number, + // orderId: string, + // ): Promise<{ paymentUrl: string | null }> { + // // Validate amount + // if (amount <= 0) { + // throw new BadRequestException('Amount must be greater than zero'); + // } + + // // Validate order exists + // const order = await this.em.findOne(Order, { id: orderId }); + // if (!order) { + // throw new NotFoundException('Order not found'); + // } + + // // Load restaurant payment method with payment method relationship + // const paymentMethod = await this.em.findOne(PaymentMethod, { id: paymentMethodId }, { populate: ['restaurant'] }); + // if (!paymentMethod) { + // throw new NotFoundException('Payment method not found'); + // } + + // if (paymentMethod.method === PaymentMethodEnum.Online && !paymentMethod.merchantId) { + // throw new BadRequestException('Merchant ID is required for online payments'); + // } + + // // Create payment record and save authority + // const restaurantDomain = paymentMethod.restaurant.domain; + // const gateway = paymentMethod.gateway; + // const { authority } = await this.createPayment(restaurantDomain, { + // amount, + // orderId, + // paymentMethod: paymentMethod.method, + // merchantId: paymentMethod.merchantId ?? null, + // gateway, + // }); + + // const paymentUrl = this.paymentGatewayService.zarinpalPaymentUrl(gateway, authority); + + // return { paymentUrl }; + // } + asynv paymentGatewayAuthorize(amount: number, orderId: string, merchantId: string, gateway: PaymentGatewayEnum){ + + } + + async startPayment(orderId: string): Promise<{ paymentUrl: string | null }> { + const { amount, method, restaurantDomain, gateway, merchantId } = await this.validateOrder(orderId); + + const { authority } = await this.createPayment(restaurantDomain, { + amount, + orderId, + method, + merchantId, + gateway, + }); + + const paymentUrl = this.paymentGatewayService.zarinpalPaymentUrl(gateway, authority); + + return { paymentUrl }; + } + + private async validateOrder(orderId: string) { + const order = await this.em.findOne(Order, { id: orderId }); + if (!order) { + throw new NotFoundException('Order not found'); + } + const paymentMethod = order.paymentMethod; + const amount = order.total; + // Validate amount if (amount <= 0) { throw new BadRequestException('Amount must be greater than zero'); } - // Validate order exists - const order = await this.em.findOne(Order, { id: orderId }); - if (!order) { - throw new NotFoundException('Order not found'); - } - // Load restaurant payment method with payment method relationship - const paymentMethod = await this.em.findOne(PaymentMethod, { id: paymentMethodId }, { populate: ['restaurant'] }); if (!paymentMethod) { throw new NotFoundException('Payment method not found'); } @@ -39,21 +96,12 @@ export class PaymentsService { if (paymentMethod.method === PaymentMethodEnum.Online && !paymentMethod.merchantId) { throw new BadRequestException('Merchant ID is required for online payments'); } - + const merchantId = paymentMethod.merchantId; // Create payment record and save authority const restaurantDomain = paymentMethod.restaurant.domain; const gateway = paymentMethod.gateway; - const { authority } = await this.createPayment(restaurantDomain, { - amount, - orderId, - paymentMethod: paymentMethod.method, - merchantId: paymentMethod.merchantId ?? null, - gateway, - }); - - const paymentUrl = this.paymentGatewayService.zarinpalPaymentUrl(gateway, authority); - - return { paymentUrl }; + const method = paymentMethod.method; + return { amount, method, restaurantDomain, gateway, merchantId }; } async createPayment(domain: string, dto: CreatePaymentDto) {