set payment method

This commit is contained in:
2025-11-23 10:03:44 +03:30
parent cc6614fe97
commit 4ea7c8d068
5 changed files with 95 additions and 2 deletions
+65 -2
View File
@@ -8,10 +8,11 @@ import { AddItemToCartDto } from '../dto/add-item.dto';
import { UpdateItemQuantityDto } from '../dto/update-item.dto';
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
import { SetAddressDto } from '../dto/set-address.dto';
import { SetPaymentMethodDto } from '../dto/set-payment-method.dto';
import { UserAddress } from '../../users/entities/user-address.entity';
import { RestaurantPaymentMethod } from '../../payments/entities/restaurant-payment-method.entity';
import { Cart, CartItem, CartCoupon } from '../interfaces/cart.interface';
@Injectable()
export class CartService {
private readonly CART_TTL = 1200; // 20 minutes in seconds
@@ -44,6 +45,7 @@ export class CartService {
restaurantId,
restaurantName: restaurant.name,
items: [],
paymentMethodFee: 0,
totalPrice: 0,
totalDiscount: 0,
couponDiscount: 0,
@@ -279,6 +281,52 @@ export class CartService {
return cart;
}
/**
* Set payment method for cart
*/
async setPaymentMethod(
userId: string,
restaurantId: string,
setPaymentMethodDto: SetPaymentMethodDto,
): Promise<Cart> {
const cart = await this.findOne(userId, restaurantId);
// Find and validate payment method belongs to restaurant
const restaurantPaymentMethod = await this.em.findOne(
RestaurantPaymentMethod,
{
restaurant: { id: restaurantId },
paymentMethod: { id: setPaymentMethodDto.paymentMethodId },
},
{ populate: ['restaurant', 'paymentMethod'] },
);
if (!restaurantPaymentMethod) {
throw new NotFoundException(
`Payment method with ID ${setPaymentMethodDto.paymentMethodId} not found for restaurant ${restaurantId}`,
);
}
// Verify payment method is active
if (!restaurantPaymentMethod.isActive) {
throw new BadRequestException('Payment method is not active for this restaurant');
}
// Verify the payment method itself is active
if (!restaurantPaymentMethod.paymentMethod.isActive) {
throw new BadRequestException('Payment method is not active');
}
cart.paymentMethodId = setPaymentMethodDto.paymentMethodId;
cart.paymentMethodFee = Number(restaurantPaymentMethod.price) || 0;
// Recalculate cart totals to include payment method fee
await this.recalculateCartTotals(cart);
await this.saveCart(cart);
return cart;
}
/**
* Recalculate cart totals (including coupon discount and VAT)
*/
@@ -323,7 +371,22 @@ export class CartService {
}
cart.vatAmount = vatAmount;
cart.finalPrice = priceAfterDiscounts + vatAmount;
// Get payment method fee if payment method is set
let paymentMethodFee = 0;
if (cart.paymentMethodId) {
const restaurantPaymentMethod = await this.em.findOne(RestaurantPaymentMethod, {
restaurant: { id: cart.restaurantId },
paymentMethod: { id: cart.paymentMethodId },
});
if (restaurantPaymentMethod) {
paymentMethodFee = Number(restaurantPaymentMethod.price) || 0;
}
}
cart.paymentMethodFee = paymentMethodFee;
// Final price includes items, discounts, VAT, and payment method fee
cart.finalPrice = priceAfterDiscounts + vatAmount + paymentMethodFee;
cart.totalItems = totalItems;
cart.updatedAt = new Date().toISOString();
}