Exemplo n.º 1
0
        private async Task AddOrder(List <string> productIds, string userId)
        {
            var user = await userManager.FindByIdAsync(userId);

            var products = new List <Product>();

            foreach (var id in productIds)
            {
                var product = await productService.Get(int.Parse(id));

                product.Status = ProductStatus.Sold;
                await productService.Update(product);

                products.Add(product);
            }

            Order order = new Order
            {
                Products  = products,
                Total     = taxService.GetCostDetails(products).Total,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now,
                User      = user,
                UserId    = user.Id
            };

            await orderService.Add(order);

            SendEmail(user, order);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetCostDetails([FromQuery] string productIds)
        {
            List <Product> products = new List <Product>();

            string[] ids = productIds.Split(',');

            foreach (var id in ids)
            {
                var product = await productService.Get(int.Parse(id));

                products.Add(product);
            }

            var costDetailsDto = costDetailsService.GetCostDetails(products);

            return(Ok(costDetailsDto));
        }
Exemplo n.º 3
0
        public Session CreateSession(ApplicationUser user, IEnumerable <ProductDto> products)
        {
            var lineItems = new List <SessionLineItemOptions>();

            foreach (var product in products)
            {
                lineItems.Add(new SessionLineItemOptions
                {
                    PriceData = new Stripe.SessionLineItemPriceDataOptions
                    {
                        Currency    = "usd",
                        ProductData = new Stripe.SessionLineItemPriceDataProductDataOptions
                        {
                            Name        = product.Name,
                            Description = product.Description,
                            Images      = new List <string>
                            {
                                product.Image != null && product.Image != string.Empty ? product.Image : "https://via.placeholder.com/150"
                            },
                            Metadata = new Dictionary <string, string>
                            {
                                { "AppId", product.Id.ToString() },
                                { "UserId", user.Id }
                            }
                        },
                        // Must include product tax to send to Stripe
                        UnitAmount = Convert.ToInt64((product.Price * 100) + (costDetailsService.GetCostDetails(new List <Product> {
                            ToProduct(product)
                        }).Tax * 100))
                    },
                    Quantity = 1
                });
            }

            var list = new List <Product>();

            foreach (var productDto in products)
            {
                list.Add(ToProduct(productDto));
            }

            var shipping = costDetailsService.GetCostDetails(list).Shipping;

            // Required to add shipping to line items
            if (shipping > 0)
            {
                lineItems.Add(new SessionLineItemOptions
                {
                    PriceData = new Stripe.SessionLineItemPriceDataOptions
                    {
                        Currency    = "usd",
                        ProductData = new Stripe.SessionLineItemPriceDataProductDataOptions
                        {
                            Name        = "Shipping",
                            Description = "Shipping",
                            Images      = new List <string>
                            {
                                "https://via.placeholder.com/150"
                            },
                            Metadata = new Dictionary <string, string>
                            {
                                { "AppId", "-1" },
                                { "UserId", user.Id }
                            }
                        },
                        UnitAmount = Convert.ToInt64(decimal.Parse(configuration["Business:ShippingRate"]) * 100)
                    },
                    Quantity = 1
                });
            }

            var options = new SessionCreateOptions
            {
                PaymentMethodTypes = new List <string>
                {
                    "card"
                },
                CustomerEmail = user.Email,
                LineItems     = lineItems,
                Mode          = "payment",
                SuccessUrl    = "https://localhost:44301/success?session_id={CHECKOUT_SESSION_ID}",
                CancelUrl     = "https://localhost:44301/cart"
            };

            var service = new SessionService();

            return(service.Create(options));
        }