コード例 #1
0
 static async Task Main(string[] args)
 {
     var exchangeRate = await CurrencyConvert.GetExchangeRate(new List <string> {
         "EUR", "GBP", "ILS"
     });
 }
コード例 #2
0
        public async Task <IActionResult> Create()
        {
            try
            {
                Customer customer = await GetCurrentUserAsync();

                List <Cart> itemsInCart = await GetItemsInCart(customer);

                bool inactiveItemsInCart = itemsInCart.Any(c => !c.Item.Active);

                if (inactiveItemsInCart)
                {
                    _flashMessage.Danger("Some items in cart are no longer available");
                    return(RedirectToAction("Index", "Carts"));
                }

                const int defaultPaymentCost = 10;
                double    itemsCost          = itemsInCart.Aggregate <Cart, double>(0, (current, cart) => current + cart.Item.Price * cart.Quantity);

                if (itemsCost == 0)
                {
                    _flashMessage.Danger("Your cart does no longer has items, please add items to cart before proceed to checkout");
                    return(RedirectToAction("Index", "Carts"));
                }

                var totalCost = itemsCost + defaultPaymentCost;
                customer.Address ??= new Address();

                var currencies = await CurrencyConvert.GetExchangeRate(new List <string>
                {
                    "EUR", "GBP", "ILS"
                });

                foreach (CurrencyInfo currency in currencies)
                {
                    currency.Total = totalCost * currency.Value;
                }

                var itemsIdsInCartList = new List <int>();

                foreach (Cart itemInCart in itemsInCart)
                {
                    for (var i = 1; i <= itemInCart.Quantity; i++)
                    {
                        itemsIdsInCartList.Add(itemInCart.ItemId);
                    }
                }

                var viewModel = new CreateOrderViewModel
                {
                    Cart            = itemsInCart,
                    Customer        = customer,
                    ShippingAddress = customer.Address,
                    Payment         = new Payment
                    {
                        ShippingCost = defaultPaymentCost,
                        ItemsCost    = itemsCost,
                        Total        = totalCost
                    },
                    ItemsInCart        = await CountItemsInCart(),
                    Currencies         = currencies,
                    ItemsIdsInCartList = itemsIdsInCartList
                };

                _flashMessage.Warning("Please verify your items before placing the order");

                return(View(viewModel));
            }
            catch (Exception e)
            {
                _flashMessage.Danger("Your order could not be placed. Please contact support for help");
                _logger.LogError($"an order could not be created, e: {e}");

                return(View(new CreateOrderViewModel()
                {
                    ItemsInCart = await CountItemsInCart()
                }));
            }
        }