credircard payment

This commit is contained in:
2026-06-15 11:59:52 +03:30
parent 4eedcf2576
commit 278f7accb6
7 changed files with 96 additions and 11 deletions
+34 -2
View File
@@ -64,6 +64,7 @@ export class OrdersService {
async checkout(userId: string, restaurantId: string) {
const cart = await this.cartService.findOneOrFail(userId, restaurantId);
const validated = await this.validateCartForOrder(userId, restaurantId, cart);
console.log(cart);
const order = await this.em.transactional(async em => {
const order = em.create(Order, {
@@ -115,8 +116,7 @@ export class OrdersService {
status: PaymentStatusEnum.Pending,
method: order.paymentMethod.method,
gateway: order.paymentMethod.gateway ?? null,
...(cart.attachments?.length ? { attachments: cart.attachments } : {}),
...(cart.paymentDesc ? { description: cart.paymentDesc } : {}),
...this.buildCreditCardPaymentFields(validated.paymentMethod, cart.paymentDesc, cart.attachments),
});
em.persist(payment);
@@ -164,6 +164,7 @@ export class OrdersService {
const paymentMethod = await this.getPaymentMethodOrFail(dto.paymentMethodId, restaurantId);
this.assertPaymentMethodEnabled(paymentMethod);
this.assertCreditCardPaymentDetails(paymentMethod, dto.paymentDesc, dto.paymentAttachments);
this.assertAdminDeliveryRequirements(dto, delivery);
@@ -215,6 +216,7 @@ export class OrdersService {
status: PaymentStatusEnum.Pending,
method: order.paymentMethod.method,
gateway: order.paymentMethod.gateway ?? null,
...this.buildCreditCardPaymentFields(paymentMethod, dto.paymentDesc, dto.paymentAttachments),
});
em.persist(payment);
@@ -255,6 +257,7 @@ export class OrdersService {
const paymentMethod = await this.getPaymentMethodOrFail(cart.paymentMethodId!, restaurantId);
this.assertPaymentMethodEnabled(paymentMethod);
this.assertCreditCardPaymentDetails(paymentMethod, cart.paymentDesc, cart.attachments);
const orderItemsData = await this.buildOrderItemsData(cart, restaurantId);
@@ -479,6 +482,35 @@ export class OrdersService {
}
}
private buildCreditCardPaymentFields(
paymentMethod: PaymentMethod,
paymentDesc?: string | null,
attachments?: string[] | null,
): Pick<Payment, 'description' | 'attachments'> {
return {
...(attachments?.length ? { attachments } : {}),
...(paymentDesc?.trim() ? { description: paymentDesc.trim() } : {}),
};
}
private assertCreditCardPaymentDetails(
paymentMethod: PaymentMethod,
paymentDesc?: string | null,
attachments?: string[] | null,
): void {
if (paymentMethod.method !== PaymentMethodEnum.CreditCard) {
return;
}
const hasDescription = Boolean(paymentDesc?.trim());
const hasAttachments = Boolean(attachments?.length);
if (!hasDescription && !hasAttachments) {
throw new BadRequestException(OrderMessage.CREDIT_CARD_PAYMENT_DETAILS_REQUIRED);
}
}
private assertDeliveryMethodRequirements(cart: Cart, delivery: Delivery) {
if (delivery.method === DeliveryMethodEnum.DineIn) {
if (!cart.tableNumber || cart.tableNumber.trim() === '') {