Beispiel #1
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from the repo
            // var basket = await _basketRepo.GetBasketAsync(basketId);
            var apiPath = _config.GetSection("APIPaths:BasketService").Value + "?id=" + basketId;
            var basket  = await InvokeBasketService.GetCustomerBasketAsync(apiPath);

            // get items from the product repo
            var items          = new List <OrderItem>();
            var apiPathProduct = _config.GetSection("APIPaths:ProductService").Value;  // + "/" + item.Id;

            foreach (var item in basket.Items)
            {
                //var productItem = await _unitOfWork.Repository<Product>().GetByIdAsync(item.Id);
                apiPathProduct = apiPathProduct + "/" + item.Id;
                var productItem = await InvokeProductService.GetProductAsync(apiPathProduct);

                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);

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

            // check to see if order exists
            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);
            }

            // 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 <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            StripeConfiguration.ApiKey = _config["StripePrivateKey"];

            // var basket = await _basketRepository.GetBasketAsync(basketId);
            var apiPathBasket = _config.GetSection("APIPaths:BasketService").Value + "?id=" + basketId;
            var basket        = await InvokeBasketService.GetCustomerBasketAsync(apiPathBasket);

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

            var shippingPrice = 0m;

            if (basket.DeliveryMethodId.HasValue)
            {
                var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>()
                                     .GetByIdAsync((int)basket.DeliveryMethodId);

                shippingPrice = deliveryMethod.Price;
            }

            foreach (var item in basket.Items)
            {
                // var productItem = await _unitOfWork.Repository<Product>().GetByIdAsync(item.Id);
                var apiPathProduct = _config.GetSection("APIPaths:ProductService").Value + "/" + item.Id;
                var productItem    = await InvokeProductService.GetProductAsync(apiPathProduct);

                if (item.Price != productItem.Price)
                {
                    item.Price = productItem.Price;
                }
            }

            var service = new PaymentIntentService();

            PaymentIntent intent;

            if (string.IsNullOrEmpty(basket.PaymentIntentId))
            {
                var options = new PaymentIntentCreateOptions
                {
                    Amount             = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long)shippingPrice * 100,
                    Currency           = "usd",
                    PaymentMethodTypes = new List <string> {
                        "card"
                    }
                };
                intent = await service.CreateAsync(options);

                basket.PaymentIntentId = intent.Id;
                basket.ClientSecret    = intent.ClientSecret;
            }
            else
            {
                var options = new PaymentIntentUpdateOptions
                {
                    Amount = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long)shippingPrice * 100
                };
                await service.UpdateAsync(basket.PaymentIntentId, options);
            }

            // await _basketRepository.UpdateBasketAsync(basket);
            await InvokeBasketService.UpdateCustomerBasketAsync(_config.GetSection("APIPaths:BasketService").Value, basket);

            return(basket);
        }