This commit is contained in:
2025-12-06 08:45:59 +03:30
parent 63c62c4bab
commit 683406a396
4 changed files with 83 additions and 26 deletions
@@ -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')
@@ -13,7 +13,7 @@ export class CreatePaymentDto {
@ApiProperty({ description: 'Payment method' })
@IsEnum(PaymentMethodEnum)
paymentMethod: PaymentMethodEnum;
method: PaymentMethodEnum;
@ApiProperty({ description: 'Payment authority' })
@IsOptional()
@@ -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) {