Example #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);
        }
Example #2
0
        public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            StripeConfiguration.ApiKey = _config["StripeSettings:SecretKey"];

            var basket = await _basketRepository.GetBasketAsync(basketId);

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

            var shippingPrice = 0m;

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

                shippingPrice = deliveryMethod.Price;
            }

            foreach (var item in basket.Items)
            {
                var productItem = await _storeRepo.Repository <Product>().GetByIdAsync(item.Id);

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

            var paymentIntentService = new PaymentIntentService();

            long amountAsLong = (long)basket.Items.Sum(x => (x.Price * 100) * x.Quantity) + (long)(shippingPrice * 100);

            if (string.IsNullOrWhiteSpace(basket.PaymentIntentId))
            {
                var options = new PaymentIntentCreateOptions
                {
                    Amount             = amountAsLong,
                    Currency           = "usd",
                    PaymentMethodTypes = new List <string> {
                        "card"
                    }
                };

                PaymentIntent intent = await paymentIntentService.CreateAsync(options);

                basket.PaymentIntentId = intent.Id;
                basket.ClientSecret    = intent.ClientSecret;
            }
            else
            {
                var options = new PaymentIntentUpdateOptions
                {
                    Amount = amountAsLong
                };

                await paymentIntentService.UpdateAsync(basket.PaymentIntentId, options);
            }

            await _basketRepository.UpdateBasketAsync(basket);

            return(basket);
        }