Exemplo n.º 1
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, IAddress shippingAddress)
        {
            // Get basket from repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // Get the items from the product repo
            var orderItems = new List <OrderItem>();

            foreach (var basketItem in basket.Items)
            {
                var product = await _repoUnitOfWork.Repository <Product>().GetByIdAsync(basketItem.Id);

                if (product != null)
                {
                    var productItemOrdered = new ProductItemOrdered(product);
                    var orderItem          = new OrderItem(productItemOrdered, product.Price, basketItem.Quantity);
                    orderItems.Add(orderItem);
                }
            }

            // Get the delivery method
            var deliveryMethod = await _repoUnitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // Calc subtotal
            var subTotal = orderItems.Sum(x => x.Price * x.Quantity);

            // Check to see if order exists
            var spec = new OrderForPaymentIntentIdSpec(basket.PaymentIntentId);

            var existingOrder = await _repoUnitOfWork.Repository <Order>().GetEntityAsync(spec);

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

            // Create order
            var order = new Order
                        (
                orderItems,
                buyerEmail,
                shippingAddress,
                deliveryMethod,
                subTotal,
                basket.PaymentIntentId
                        );

            // Save to db
            _repoUnitOfWork.Repository <Order>().Add(order);

            var result = await _repoUnitOfWork.SaveChanges();

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

            return(order);
        }
Exemplo n.º 2
0
        private async Task <Order> UpdateOrderPaymentStatus(string paymentIntentId, OrderStatus orderStatus)
        {
            var spec = new OrderForPaymentIntentIdSpec(paymentIntentId);

            var order = await _storeRepo.Repository <Order>().GetEntityAsync(spec);

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

            order.Status = orderStatus;

            _storeRepo.Repository <Order>().Update(order);

            await _storeRepo.SaveChanges();

            return(order);
        }