payment
This commit is contained in:
@@ -26,6 +26,15 @@ export class PaymentsController {
|
|||||||
return this.paymentMethodService.findByRestaurant(restId);
|
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)
|
// @UseGuards(AdminAuthGuard)
|
||||||
// @ApiBearerAuth()
|
// @ApiBearerAuth()
|
||||||
// @Get('admin/payments')
|
// @Get('admin/payments')
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export class CreatePaymentDto {
|
|||||||
|
|
||||||
@ApiProperty({ description: 'Payment method' })
|
@ApiProperty({ description: 'Payment method' })
|
||||||
@IsEnum(PaymentMethodEnum)
|
@IsEnum(PaymentMethodEnum)
|
||||||
paymentMethod: PaymentMethodEnum;
|
method: PaymentMethodEnum;
|
||||||
|
|
||||||
@ApiProperty({ description: 'Payment authority' })
|
@ApiProperty({ description: 'Payment authority' })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ export class PaymentGatewayService {
|
|||||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||||
throw new BadRequestException(`Failed to connect to payment gateway: ${errorMessage}`);
|
throw new BadRequestException(`Failed to connect to payment gateway: ${errorMessage}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If gateway is not supported, throw an error
|
// If gateway is not supported, throw an error
|
||||||
throw new BadRequestException(`Unsupported payment gateway: ${String(gateway)}`);
|
throw new BadRequestException(`Unsupported payment gateway: ${String(gateway)}`);
|
||||||
|
|||||||
@@ -14,24 +14,81 @@ export class PaymentsService {
|
|||||||
private readonly paymentGatewayService: PaymentGatewayService,
|
private readonly paymentGatewayService: PaymentGatewayService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async initializePayment(
|
// async initializePayment(
|
||||||
paymentMethodId: string,
|
// paymentMethodId: string,
|
||||||
amount: number,
|
// amount: number,
|
||||||
orderId: string,
|
// orderId: string,
|
||||||
): Promise<{ paymentUrl: string | null }> {
|
// ): 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
|
// Validate amount
|
||||||
if (amount <= 0) {
|
if (amount <= 0) {
|
||||||
throw new BadRequestException('Amount must be greater than zero');
|
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
|
// Load restaurant payment method with payment method relationship
|
||||||
const paymentMethod = await this.em.findOne(PaymentMethod, { id: paymentMethodId }, { populate: ['restaurant'] });
|
|
||||||
if (!paymentMethod) {
|
if (!paymentMethod) {
|
||||||
throw new NotFoundException('Payment method not found');
|
throw new NotFoundException('Payment method not found');
|
||||||
}
|
}
|
||||||
@@ -39,21 +96,12 @@ export class PaymentsService {
|
|||||||
if (paymentMethod.method === PaymentMethodEnum.Online && !paymentMethod.merchantId) {
|
if (paymentMethod.method === PaymentMethodEnum.Online && !paymentMethod.merchantId) {
|
||||||
throw new BadRequestException('Merchant ID is required for online payments');
|
throw new BadRequestException('Merchant ID is required for online payments');
|
||||||
}
|
}
|
||||||
|
const merchantId = paymentMethod.merchantId;
|
||||||
// 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;
|
const gateway = paymentMethod.gateway;
|
||||||
const { authority } = await this.createPayment(restaurantDomain, {
|
const method = paymentMethod.method;
|
||||||
amount,
|
return { amount, method, restaurantDomain, gateway, merchantId };
|
||||||
orderId,
|
|
||||||
paymentMethod: paymentMethod.method,
|
|
||||||
merchantId: paymentMethod.merchantId ?? null,
|
|
||||||
gateway,
|
|
||||||
});
|
|
||||||
|
|
||||||
const paymentUrl = this.paymentGatewayService.zarinpalPaymentUrl(gateway, authority);
|
|
||||||
|
|
||||||
return { paymentUrl };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createPayment(domain: string, dto: CreatePaymentDto) {
|
async createPayment(domain: string, dto: CreatePaymentDto) {
|
||||||
|
|||||||
Reference in New Issue
Block a user