示例#1
0
        public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            StripeConfiguration.ApiKey = _config["StripeSettings:SecretKey"];
            var basket = await _basketRepo.GetBasketAsync(basketId);

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

            var shippingPrice = 0m;

            if (basket.DeliveryMethodId != null)
            {
                shippingPrice = (await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync((int)basket.DeliveryMethodId)).Price;
            }

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

                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 _basketRepo.UpdateBasketAsync(basket);

            return(basket);
        }
示例#2
0
        public async Task <IActionResult> UpdateBasket(CustomerBasketDto basketDto)
        {
            var customerBasket = _mapper.Map <CustomerBasketDto, CustomerBasket>(basketDto);

            var updated = await _basketRepo.UpdateBasketAsync(customerBasket);

            return(Ok(customerBasket));
        }