public ActionResult DeliveryDetails(DeliveryDetailsViewModel model)
        {
            // Check the selected shipping option
            if (!mCheckoutService.IsShippingOptionValid(model.ShippingOption.ShippingOptionID))
            {
                ModelState.AddModelError("ShippingOption.ShippingOptionID", ResHelper.GetString("DancingGoatMvc.Shipping.ShippingOptionRequired"));
            }

            // Check if the billing address's country and state are valid
            var countryStateViewModel = model.BillingAddress.BillingAddressCountryStateSelector;

            if (!mCheckoutService.IsCountryValid(countryStateViewModel.CountryID))
            {
                countryStateViewModel.CountryID = 0;
                ModelState.AddModelError("BillingAddress.BillingAddressCountryStateSelector.CountryID", ResHelper.GetString("DancingGoatMvc.Address.CountryIsRequired"));
            }
            else if (!mCheckoutService.IsStateValid(countryStateViewModel.CountryID, countryStateViewModel.StateID))
            {
                countryStateViewModel.StateID = 0;
                ModelState.AddModelError("BillingAddress.BillingAddressCountryStateSelector.StateID", ResHelper.GetString("DancingGoatMvc.Address.StateIsRequired"));
            }

            if (!ModelState.IsValid)
            {
                var viewModel = mCheckoutService.PrepareDeliveryDetailsViewModel(model.Customer, model.BillingAddress, model.ShippingOption);
                return(View(viewModel));
            }

            var cart     = mShoppingService.GetCurrentShoppingCart();
            var customer = cart.Customer ?? new Customer();

            bool emailCanBeChanged = !User.Identity.IsAuthenticated || string.IsNullOrWhiteSpace(customer.Email);

            model.Customer.ApplyToCustomer(customer, emailCanBeChanged);
            cart.Customer = customer;

            var modelAddressID = model.BillingAddress.BillingAddressSelector?.AddressID ?? 0;
            var billingAddress = mCheckoutService.GetAddress(modelAddressID) ?? new CustomerAddress();

            model.BillingAddress.ApplyTo(billingAddress);
            billingAddress.PersonalName = $"{customer.FirstName} {customer.LastName}";

            cart.BillingAddress = billingAddress;
            cart.ShippingOption = mCheckoutService.GetShippingOption(model.ShippingOption.ShippingOptionID);

            cart.Evaluate();
            cart.Save();

            return(RedirectToAction("PreviewAndPay"));
        }
Esempio n. 2
0
        public ActionResult DeliveryDetails(DeliveryDetailsViewModel model, [FromServices] IStringLocalizer <SharedResources> localizer)
        {
            // Check the selected shipping option
            bool isShippingNeeded = shoppingService.GetCurrentShoppingCart().IsShippingNeeded;

            if (isShippingNeeded && !checkoutService.IsShippingOptionValid(model.ShippingOption.ShippingOptionID))
            {
                ModelState.AddModelError("ShippingOption.ShippingOptionID", localizer["Please select a delivery method"]);
            }

            // Check if the billing address's country and state are valid
            var countryStateViewModel = model.BillingAddress.BillingAddressCountryStateSelector;

            if (!checkoutService.IsCountryValid(countryStateViewModel.CountryID))
            {
                countryStateViewModel.CountryID = 0;
                ModelState.AddModelError("BillingAddress.BillingAddressCountryStateSelector.CountryID", localizer["The Country field is required"]);
            }
            else if (!checkoutService.IsStateValid(countryStateViewModel.CountryID, countryStateViewModel.StateID))
            {
                countryStateViewModel.StateID = 0;
                ModelState.AddModelError("BillingAddress.BillingAddressCountryStateSelector.StateID", localizer["The State field is required"]);
            }

            if (!ModelState.IsValid)
            {
                var viewModel = checkoutService.PrepareDeliveryDetailsViewModel(model.Customer, model.BillingAddress, model.ShippingOption);

                return(View(viewModel));
            }

            var  customer          = GetCustomerOrCreateFromAuthenticatedUser() ?? new CustomerInfo();
            bool emailCanBeChanged = !User.Identity.IsAuthenticated || string.IsNullOrWhiteSpace(customer.CustomerEmail);

            model.Customer.ApplyToCustomer(customer, emailCanBeChanged);
            shoppingService.SetCustomer(customer);

            var modelAddressID = model.BillingAddress.BillingAddressSelector?.AddressID ?? 0;
            var billingAddress = checkoutService.GetAddress(modelAddressID) ?? new AddressInfo();

            model.BillingAddress.ApplyTo(billingAddress);
            billingAddress.AddressPersonalName = $"{customer.CustomerFirstName} {customer.CustomerLastName}";

            shoppingService.SetBillingAddress(billingAddress);
            shoppingService.SetShippingOption(model.ShippingOption?.ShippingOptionID ?? 0);

            return(RedirectToAction("PreviewAndPay"));
        }