public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            //get basket from repo
            var basket = await _baseketRepo.GetBasketAsync(basketId);

            //get item 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);
            }

            //get delivery method from repo
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            //calculate subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            //check to see if order exists
            var spec          = new OrderByPaymentIntentWithItemSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }

            //create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal,
                                  basket.PaymentIntentId);

            _unitOfWork.Repository <Order>().Add(order);

            //save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            //return order
            return(order);
        }
        public async Task <Core.Entities.OrderAggregate.Order> UpdateOrderPaymentFailed(string paymentIntentId)
        {
            var spec  = new  OrderByPaymentIntentWithItemSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (order == null)
            {
                return(null);
            }

            order.Status = OrderStatus.PaymentFailed;

            await _unitOfWork.Complete();

            return(order);
        }
        public async Task <Order> UpdateOrderPaymentSucceeded(string paymentIntentId)
        {
            var spec  = new  OrderByPaymentIntentWithItemSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (order == null)
            {
                return(null);
            }

            order.Status = OrderStatus.PaymentReceived;
            _unitOfWork.Repository <Order>().Update(order);

            await _unitOfWork.Complete();

            return(order);
        }