update
This commit is contained in:
@@ -39,21 +39,22 @@ export class PaymentsService {
|
|||||||
throw new NotFoundException('Payment method not found');
|
throw new NotFoundException('Payment method not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paymentMethod.method !== PaymentMethodEnum.Online && !paymentMethod.merchantId) {
|
if (paymentMethod.method === PaymentMethodEnum.Online && !paymentMethod.merchantId) {
|
||||||
throw new BadRequestException('Merchant ID is not provided');
|
throw new BadRequestException('Merchant ID is required for online payments');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create payment record and save authority
|
// Create payment record and save authority
|
||||||
const restaurantDomain = paymentMethod.restaurant.domain;
|
const restaurantDomain = paymentMethod.restaurant.domain;
|
||||||
|
const gateway = paymentMethod.gateway as PaymentGatewayEnum | null;
|
||||||
const { authority } = await this.createPayment(restaurantDomain, {
|
const { authority } = await this.createPayment(restaurantDomain, {
|
||||||
amount,
|
amount,
|
||||||
orderId,
|
orderId,
|
||||||
paymentMethod: paymentMethod.method,
|
paymentMethod: paymentMethod.method,
|
||||||
merchantId: paymentMethod.merchantId ?? null,
|
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 };
|
return { paymentUrl };
|
||||||
}
|
}
|
||||||
@@ -83,34 +84,56 @@ export class PaymentsService {
|
|||||||
domain: string,
|
domain: string,
|
||||||
gateway: PaymentGatewayEnum | null | undefined,
|
gateway: PaymentGatewayEnum | null | undefined,
|
||||||
): Promise<{ authority: string | null }> {
|
): Promise<{ authority: string | null }> {
|
||||||
if (paymentMethod === PaymentMethodEnum.Online && merchantId) {
|
// For non-online payment methods, no gateway request is needed
|
||||||
const callbackUrl = `${domain}/payments/callback`;
|
if (paymentMethod !== PaymentMethodEnum.Online) {
|
||||||
if (gateway === PaymentGatewayEnum.ZarinPal) {
|
return { authority: null };
|
||||||
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) {
|
// Online payments require merchantId and gateway
|
||||||
throw new BadRequestException('Payment gateway did not return an authority token');
|
if (!merchantId) {
|
||||||
}
|
throw new BadRequestException('Merchant ID is required for online payments');
|
||||||
} catch (error) {
|
}
|
||||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
||||||
throw new BadRequestException(`Failed to connect to payment gateway: ${errorMessage}`);
|
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<IPaymentResponse> {
|
private async requestToZarinPalGateway(requestPayment: IPaymentRequest): Promise<IPaymentResponse> {
|
||||||
|
|||||||
Reference in New Issue
Block a user