public ActionResult PreviewAndPay(PreviewAndPayViewModel model)
        {
            // Gets the current shopping cart
            ShoppingCartInfo cart = shoppingService.GetCurrentShoppingCart();

            // Validates the shopping cart
            var validationErrors = ShoppingCartInfoProvider.ValidateShoppingCart(cart);

            // Gets the selected payment method, assigns it to the shopping cart, and evaluates the cart
            shoppingService.SetPaymentOption(model.PaymentMethod.PaymentMethodID);

            // If the validation was not successful, displays the preview step again
            if (validationErrors.Any() || !IsPaymentMethodValid(model.PaymentMethod.PaymentMethodID))
            {
                // Prepares a view model from the order review step
                PreviewAndPayViewModel viewModel = PreparePreviewViewModel();

                // Displays the order review step again
                return(View("PreviewAndPay", viewModel));
            }

            // Creates an order from the current shopping cart
            // If the order was created successfully, empties the cart
            OrderInfo order = shoppingService.CreateOrder();

            // Redirects to the payment gateway
            return(RedirectToAction("Index", "Payment", new { orderID = order.OrderID }));
        }
        public ActionResult PreviewAndPay(PreviewAndPayViewModel model)
        {
            // Gets the current user's shopping cart
            ShoppingCart cart = shoppingService.GetCurrentShoppingCart();

            // Validates the shopping cart
            ShoppingCartCheckResult checkResult = cart.ValidateContent();

            // Gets the selected payment method and assigns it to the shopping cart
            cart.PaymentMethod = paymentRepository.GetById(model.PaymentMethod.PaymentMethodID);

            // Evaluates the shopping cart
            cart.Evaluate();

            // If the validation was not successful, displays the preview step again
            if (checkResult.CheckFailed || !IsPaymentMethodValid(model.PaymentMethod.PaymentMethodID))
            {
                // Prepares a model from the preview step
                PreviewAndPayViewModel viewModel = PreparePreviewViewModel();

                // Displays the preview step again
                return(View("PreviewAndPay", viewModel));
            }

            // Creates an order from the shopping cart
            Order order = shoppingService.CreateOrder(cart);

            // Deletes the shopping cart from the database
            shoppingService.DeleteShoppingCart(cart);

            // Redirects to the payment gateway
            return(RedirectToAction("Index", "Payment", new { orderID = order.OrderID }));
        }
        //EndDocSection:PreparePayment


        //DocSection:PreparePreview
        /// <summary>
        /// Display the preview checkout process step.
        /// </summary>
        public ActionResult PreviewAndPay()
        {
            // If the current shopping cart is empty, returns to the shopping cart action
            if (shoppingService.GetCurrentShoppingCart().IsEmpty)
            {
                return(RedirectToAction("ShoppingCart"));
            }

            // Prepares a model from the preview step
            PreviewAndPayViewModel model = PreparePreviewViewModel();

            // Displays the preview step
            return(View(model));
        }
        /// <summary>
        /// Prepares a view model of the preview checkout process step including the shopping cart,
        /// the customer details, and the payment method.
        /// </summary>
        /// <returns>View model with information about the future order.</returns>
        private PreviewAndPayViewModel PreparePreviewViewModel()
        {
            // Gets the current user's shopping cart
            ShoppingCartInfo cart = shoppingService.GetCurrentShoppingCart();

            // Prepares the customer details
            DeliveryDetailsViewModel deliveryDetailsModel = new DeliveryDetailsViewModel
            {
                Customer       = new CustomerViewModel(shoppingService.GetCurrentCustomer()),
                BillingAddress = new BillingAddressViewModel(shoppingService.GetBillingAddress(), null, null),
                ShippingOption = new ShippingOptionViewModel()
                {
                    ShippingOptionID          = cart.ShippingOption.ShippingOptionID,
                    ShippingOptionDisplayName = ShippingOptionInfoProvider.GetShippingOptionInfo(cart.ShippingOption.ShippingOptionID).ShippingOptionDisplayName
                }
            };

            // Prepares the payment method
            PaymentMethodViewModel paymentViewModel = new PaymentMethodViewModel
            {
                PaymentMethods = new SelectList(GetApplicablePaymentMethods(cart), "PaymentOptionID", "PaymentOptionDisplayName")
            };

            // Gets the selected payment method
            PaymentOptionInfo paymentMethod = cart.PaymentOption;

            if (paymentMethod != null)
            {
                paymentViewModel.PaymentMethodID = paymentMethod.PaymentOptionID;
            }

            // Prepares a model from the preview step
            PreviewAndPayViewModel model = new PreviewAndPayViewModel
            {
                DeliveryDetails = deliveryDetailsModel,
                Cart            = new ShoppingCartViewModel(cart),
                PaymentMethod   = paymentViewModel
            };

            return(model);
        }
        //EndDocSection:PreparePayment

        //DocSection:PreparePreview
        /// <summary>
        /// Prepares a view model of the preview checkout process step including the shopping cart,
        /// the customer details, and the payment method.
        /// </summary>
        /// <returns>View model with information about the future order.</returns>
        private PreviewAndPayViewModel PreparePreviewViewModel()
        {
            // Gets the current user's shopping cart
            ShoppingCart cart = shoppingService.GetCurrentShoppingCart();

            // Prepares the customer details
            DeliveryDetailsViewModel deliveryDetailsModel = new DeliveryDetailsViewModel
            {
                Customer       = new CustomerModel(cart.Customer),
                BillingAddress = new BillingAddressModel(cart.BillingAddress, null, null)
            };

            // Prepares the payment method
            PaymentMethodViewModel paymentViewModel = new PaymentMethodViewModel
            {
                PaymentMethods = new SelectList(GetApplicablePaymentMethods(cart), "PaymentOptionID", "PaymentOptionDisplayName")
            };

            // Gets the selected payment method if any
            PaymentOptionInfo paymentMethod = cart.PaymentMethod;

            if (paymentMethod != null)
            {
                paymentViewModel.PaymentMethodID = paymentMethod.PaymentOptionID;
            }

            // Prepares a model from the preview step
            PreviewAndPayViewModel model = new PreviewAndPayViewModel
            {
                DeliveryDetails = deliveryDetailsModel,
                Cart            = cart,
                PaymentMethod   = paymentViewModel
            };

            return(model);
        }