Пример #1
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int delieveryMethodId, 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);
            }

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

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

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

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

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

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



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

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


            // return order
            return(order);
        }
Пример #2
0
        public async Task <Core.Models.OrderAggregate.Order> UpdateOrderPaymentFailed(string paymentIntentId)
        {
            var spec  = new OrderByPaymentIntentWithItemsSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

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

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

            await _unitOfWork.Complete();

            return(order);
        }
Пример #3
0
        public async Task <Order> UpdateOrderPaymentSucceeded(string paymentIntentId)
        {
            var spec  = new OrderByPaymentIntentWithItemsSpecification(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.SaveChanges();

            return(order);
        }