public async Task <Order> UpdateOrderPaymentFailed(string paymentIntentId) { var spec = new OrderbyPaymentIntentIdSpecification(paymentIntentId); var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); if (order == null) { return(null); } order.Status = OrderStatus.Failed; await _unitOfWork.Complete(); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // get basket from repo var basket = await _basketRepo.GetBasketAsync(basketId); //get items from the product repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); var subTotal = items.Sum(item => item.Price * item.Quantity); var spec = new OrderbyPaymentIntentIdSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } var order = new Order(buyerEmail, shippingAddress, deliveryMethod, items, subTotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } return(order); }