From 8298e2fb3efd960ac8bcd31793fc1b77cab5b1e6 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 2 Dec 2025 23:04:09 +0330 Subject: [PATCH] update --- .../payments/services/payments.service.ts | 79 ++++++++++++------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/src/modules/payments/services/payments.service.ts b/src/modules/payments/services/payments.service.ts index 7b41c24..eebace3 100644 --- a/src/modules/payments/services/payments.service.ts +++ b/src/modules/payments/services/payments.service.ts @@ -39,21 +39,22 @@ export class PaymentsService { throw new NotFoundException('Payment method not found'); } - if (paymentMethod.method !== PaymentMethodEnum.Online && !paymentMethod.merchantId) { - throw new BadRequestException('Merchant ID is not provided'); + 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 as PaymentGatewayEnum | null; const { authority } = await this.createPayment(restaurantDomain, { amount, orderId, paymentMethod: paymentMethod.method, merchantId: paymentMethod.merchantId ?? null, - gateway: paymentMethod.gateway as PaymentGatewayEnum | null, + gateway, }); - const paymentUrl = this.zarinpalPaymentUrl(paymentMethod.gateway, authority); + const paymentUrl = this.zarinpalPaymentUrl(gateway, authority); return { paymentUrl }; } @@ -83,34 +84,56 @@ export class PaymentsService { domain: string, gateway: PaymentGatewayEnum | null | undefined, ): Promise<{ authority: string | null }> { - if (paymentMethod === PaymentMethodEnum.Online && merchantId) { - const callbackUrl = `${domain}/payments/callback`; - if (gateway === PaymentGatewayEnum.ZarinPal) { - try { - const gatewayResponse = await this.requestToZarinPalGateway({ - amount, - merchantId, - description: `Payment for order #${orderId}`, - callbackUrl, - metadata: { - orderId, - }, - }); - // Check gateway response code (typically 100 means success for Iranian gateways) - if (gatewayResponse.code !== 100 && gatewayResponse.code !== 0) { - throw new BadRequestException(`Payment gateway error: ${gatewayResponse.message || 'Unknown error'}`); - } + // For non-online payment methods, no gateway request is needed + if (paymentMethod !== PaymentMethodEnum.Online) { + return { authority: null }; + } - if (!gatewayResponse.authority) { - throw new BadRequestException('Payment gateway did not return an authority token'); - } - } catch (error) { - const errorMessage = error instanceof Error ? error.message : 'Unknown error'; - throw new BadRequestException(`Failed to connect to payment gateway: ${errorMessage}`); + // Online payments require merchantId and gateway + if (!merchantId) { + throw new BadRequestException('Merchant ID is required for online payments'); + } + + if (!gateway) { + throw new BadRequestException('Payment gateway is required for online payments'); + } + + const callbackUrl = `${domain}/payments/callback`; + + // Handle ZarinPal gateway + if (gateway === PaymentGatewayEnum.ZarinPal) { + try { + const gatewayResponse = await this.requestToZarinPalGateway({ + amount, + merchantId, + description: `Payment for order #${orderId}`, + callbackUrl, + metadata: { + orderId, + }, + }); + + // Check gateway response code (typically 100 means success for Iranian gateways) + if (gatewayResponse.code !== 100 && gatewayResponse.code !== 0) { + throw new BadRequestException(`Payment gateway error: ${gatewayResponse.message || 'Unknown error'}`); } + + if (!gatewayResponse.authority) { + throw new BadRequestException('Payment gateway did not return an authority token'); + } + + return { authority: gatewayResponse.authority }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; + throw new BadRequestException(`Failed to connect to payment gateway: ${errorMessage}`); } } - return { authority: null }; + + // If gateway is not supported, throw an error + throw new BadRequestException(`Unsupported payment gateway: ${String(gateway)}`); } private async requestToZarinPalGateway(requestPayment: IPaymentRequest): Promise {