Exemplo n.º 1
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 _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);

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

            return(basket);
        }
Exemplo n.º 2
0
        public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            StripeConfiguration.ApiKey = _config["StripeSettings:SecreatKey"];
            var basket = await _basketRepository.GetBasketAsync(basketId);

            var shippingPrice = 0m; // 259 "m" for money

            if (basket == null)
            {
                return(null);                // 270
            }
            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);

                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); //  what we get back from stripe

                    basket.PaymentIntentId = intent.Id;
                    basket.ClientSecret    = intent.ClientSecret;
                }
                else
                { // posibility client get changers after going thrugh the check out after already created payment intent
                    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); // 259 update if there any changed
            }
            return(basket);
        }
Exemplo n.º 3
0
        public async Task <CustomerCart> CreateOrUpdatePaymentIntent(string cartId)
        {
            StripeConfiguration.ApiKey = _configuration["StripeSettings:SecretKey"];

            var cart = await _cartRepository.GetCartAsync(cartId);

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

            // m for money
            var shippingPrice = 0m;

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

                shippingPrice = deliveryMethode.Price;
            }

            foreach (var item in cart.CartItems)
            {
                var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.CartItemId);

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

            // PaymentIntentService coming from stripe
            var service = new PaymentIntentService();

            PaymentIntent intent;

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

                intent = await service.CreateAsync(options);

                cart.PaymentIntentId = intent.Id;
                cart.ClientSecret    = intent.ClientSecret;
            }
            else
            {
                var options = new PaymentIntentUpdateOptions()
                {
                    Amount = (long)cart.CartItems.Sum(i => i.Quantity * (i.Price * 100)) + (long)shippingPrice * 100
                };

                await service.UpdateAsync(cart.PaymentIntentId, options);
            }

            await _cartRepository.UpdateCartAsync(cart);

            return(cart);
        }
Exemplo n.º 4
0
        public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            StripeConfiguration.ApiKey = _configuration["StripeSettings:Secretkey"];

            var basket = await _basketRepo.GetBasketAsync(basketId);

            var shippingPrice = 0m;

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

            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 <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 intentOptions = new PaymentIntentCreateOptions
                {
                    //stripe does not accept decimal so converting into long and also mutiplying by 100
                    Amount             = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long)shippingPrice * 100,
                    Currency           = "inr",
                    PaymentMethodTypes = new List <string> {
                        "card"
                    },
                    Shipping = new ChargeShippingOptions
                    {
                        Name    = "Jenny Rosen",
                        Address = new AddressOptions
                        {
                            Line1      = "510 Townsend St",
                            PostalCode = "98140",
                            City       = "San Francisco",
                            State      = "CA",
                            Country    = "US",
                        },
                    },
                    Description = "Software development services"
                };
                intent = await service.CreateAsync(intentOptions);

                basket.PaymentIntentId = intent.Id;
                basket.ClientSecret    = intent.ClientSecret;
            }
            else
            {
                var options = new PaymentIntentUpdateOptions
                {
                    //stripe does not accept decimal so converting into long and also mutiplying by 100
                    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);
        }
Exemplo n.º 5
0
        public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            // Needed for Payment intent
            StripeConfiguration.ApiKey = _configuration["StripeSettings:SecretKey"];

            var basket = await _basketRepository.GetBasketAsync(basketId);

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

            //Set shipping price to 0
            var shippingPrice = 0m;

            // Dont trust the value in the basket and always get the price from the database
            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);

                // When there is a mismatch in the price in the basket vs price in the DB
                // Set it to the price in the DB
                if (item.Price != productItem.Price)
                {
                    item.Price = productItem.Price;
                }

                // Stripe class for payment intent
                var           service = new PaymentIntentService();
                PaymentIntent intent;

                // Check if no payment has been set already. If it is an update to the basket
                // then intent may already be available and the client makes a change after checkout
                if (string.IsNullOrEmpty(basket.PaymentIntentId))
                {
                    _logger.LogDebug("SERVICE ENTRY: Creating new Payment intent Id");
                    var options = new PaymentIntentCreateOptions
                    {
                        // Converting from decimal to long needs multiplication by 100
                        Amount = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100))
                                 + (long)shippingPrice * 100,
                        Currency           = "inr",
                        PaymentMethodTypes = new List <string> {
                            "card"
                        }
                    };
                    // Create the intent on Stripe
                    intent = await service.CreateAsync(options);

                    // Update basket with the payment intent Id and client secret
                    basket.PaymentIntentId = intent.Id;
                    basket.ClientSecret    = intent.ClientSecret;
                }
                // Update the payment intent
                else
                {
                    _logger.LogDebug("SERVICE ENTRY: Updating existing Payment intent Id {PaymentIntentId}", basket.PaymentIntentId);
                    var options = new PaymentIntentUpdateOptions
                    {
                        Amount = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100))
                                 + (long)shippingPrice * 100
                    };

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

            //Update the basket with the information from Stripe
            await _basketRepository.CreateOrUpdateBasketAsync(basket);

            return(basket);
        }
Exemplo n.º 6
0
        public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
        {
            // get stripe secret key
            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 _unitOfWork.Repository <DeleveryMethod>()
                                     .GetByIdAsync((int)basket.DeliveryMethodId);

                shippingPrice = deliveryMethod.Price;
            }

            foreach (var item in basket.Items)
            {
                var productItem = await _unitOfWork.Repository <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
            {
                // this else block will be used and takes care of the possibility that the client's away the change the basket after going
                // through the checkout and then backing out after we have already created a payment intent.
                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);

            return(basket);
        }