Esempio n. 1
0
        public ViewResult Checkout(Cart cart)
        {
            var user = _userRepository.GetUserById(User.Identity.GetUserId());

            cart.UserId = user.Id;
            cart.User   = user;

            var viewModel = new CheckoutViewModel
            {
                Cart                     = cart,
                ShippingMethods          = ShippingMethodsProvider.CreateSelectList(_shippingMethodRepository.GetShippingMethods().ToList()),
                OrderValue               = cart.GetValue(),
                SelectedShippingMethodId = -1
            };

            ViewBag.CountryList = _countriesProvider.FillCountryList();

            return(View(viewModel));
        }
Esempio n. 2
0
        public ViewResult Checkout(CheckoutViewModel viewModel, Cart cart)
        {
            if (cart.CartItems.Count == 0)
            {
                ModelState.AddModelError("", @"Your cart is empty. Please add products to order.");
            }

            //Server side verification
            if (ModelState.IsValid)
            {
                Order order = new Order(
                    cart,
                    viewModel.UseUserAddress,
                    viewModel.SelectedShippingMethodId,
                    !viewModel.UseUserAddress ? viewModel.ShippingAddress : null);

                order.ShippingMethod = _shippingMethodRepository.GetShippingMethodById(order.ShippingMethodId);

                if (_orderRepository.AddOrder(order))
                {
                    var crmGuid = _userRepository.GetUserById(cart.UserId).CrmGuid;

                    if (crmGuid.HasValue && crmGuid.Value != Guid.Empty)
                    {
                        _crmClient.AddPointsForCustomer(crmGuid.Value, (int)order.GetTotalValue());
                    }

                    _emailSender.ProcessOrder(order);
                    return(View("Completed"));
                }
            }

            viewModel.Cart            = cart;
            viewModel.ShippingMethods =
                ShippingMethodsProvider.CreateSelectList(_shippingMethodRepository.GetShippingMethods().ToList());
            viewModel.OrderValue = cart.GetValue();
            viewModel.SelectedShippingMethodId = -1;

            ViewBag.CountryList = _countriesProvider.FillCountryList();

            return(View(viewModel));
        }