public virtual async Task <CheckoutBillingAddressModel> PrepareBillingAddress(
            IList <ShoppingCartItem> cart, string selectedCountryId = null,
            bool prePopulateNewAddressWithCustomerFields            = false, string overrideAttributesXml = "")
        {
            var model = new CheckoutBillingAddressModel();

            model.ShipToSameAddressAllowed = _shippingSettings.ShipToSameAddress && cart.RequiresShipping();
            model.ShipToSameAddress        = true;

            //existing addresses
            var addresses = new List <Address>();

            foreach (var item in _workContext.CurrentCustomer.Addresses)
            {
                if (string.IsNullOrEmpty(item.CountryId))
                {
                    addresses.Add(item);
                    continue;
                }
                var country = await _countryService.GetCountryById(item.CountryId);

                if (country == null || (country.AllowsBilling && _storeMappingService.Authorize(country)))
                {
                    addresses.Add(item);
                    continue;
                }
            }

            foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                await _addressViewModelService.PrepareModel(model : addressModel, address : address, excludeProperties : false);

                model.ExistingAddresses.Add(addressModel);
            }

            //new address
            model.NewAddress.CountryId = selectedCountryId;
            var countries = await _countryService.GetAllCountriesForBilling(_workContext.WorkingLanguage.Id);

            await _addressViewModelService.PrepareModel(model : model.NewAddress, address : null, excludeProperties : false,
                                                        loadCountries : () => countries,
                                                        prePopulateWithCustomerFields : prePopulateNewAddressWithCustomerFields,
                                                        customer : _workContext.CurrentCustomer,
                                                        overrideAttributesXml : overrideAttributesXml
                                                        );

            return(model);
        }
        /// <summary>
        /// Prepare billing address model
        /// </summary>
        /// <param name="cart">Cart</param>
        /// <param name="selectedCountryId">Selected country identifier</param>
        /// <param name="prePopulateNewAddressWithCustomerFields">Pre populate new address with customer fields</param>
        /// <param name="overrideAttributesXml">Override attributes xml</param>
        /// <returns>Billing address model</returns>
        public virtual CheckoutBillingAddressModel PrepareBillingAddressModel(IList <ShoppingCartItem> cart,
                                                                              int?selectedCountryId = null,
                                                                              bool prePopulateNewAddressWithCustomerFields = false,
                                                                              string overrideAttributesXml = "")
        {
            var model = new CheckoutBillingAddressModel
            {
                ShipToSameAddressAllowed = _shippingSettings.ShipToSameAddress && cart.RequiresShipping(_productService, _productAttributeParser),
                //allow customers to enter (choose) a shipping address if "Disable Billing address step" setting is enabled
                ShipToSameAddress = !_orderSettings.DisableBillingAddressCheckoutStep
            };

            //existing addresses
            var addresses = _workContext.CurrentCustomer.Addresses
                            .Where(a => a.Country == null ||
                                   (//published
                                       a.Country.Published &&
                                       //allow billing
                                       a.Country.AllowsBilling &&
                                       //enabled for the current store
                                       _storeMappingService.Authorize(a.Country)))
                            .ToList();

            foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                _addressModelFactory.PrepareAddressModel(addressModel,
                                                         address: address,
                                                         excludeProperties: false,
                                                         addressSettings: _addressSettings);
                model.ExistingAddresses.Add(addressModel);
            }

            //new address
            model.BillingNewAddress.CountryId = selectedCountryId;
            _addressModelFactory.PrepareAddressModel(model.BillingNewAddress,
                                                     address: null,
                                                     excludeProperties: false,
                                                     addressSettings: _addressSettings,
                                                     loadCountries: () => _countryService.GetAllCountriesForBilling(_workContext.WorkingLanguage.Id),
                                                     prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields,
                                                     customer: _workContext.CurrentCustomer,
                                                     overrideAttributesXml: overrideAttributesXml);
            return(model);
        }
예제 #3
0
        public ActionResult NewBillingAddress(CheckoutBillingAddressModel model)
        {
            //validation
            var cart = _workContext.CurrentCustomer.GetCartItems(ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

            if (cart.Count == 0)
            {
                return(RedirectToRoute("ShoppingCart"));
            }

            if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
            {
                return(new HttpUnauthorizedResult());
            }

            if (ModelState.IsValid)
            {
                var address = model.NewAddress.ToEntity();
                address.CreatedOnUtc = DateTime.UtcNow;
                //some validation
                if (address.CountryId == 0)
                {
                    address.CountryId = null;
                }
                if (address.StateProvinceId == 0)
                {
                    address.StateProvinceId = null;
                }
                _workContext.CurrentCustomer.Addresses.Add(address);
                _workContext.CurrentCustomer.BillingAddress = address;
                _customerService.UpdateCustomer(_workContext.CurrentCustomer);

                return(RedirectToAction("ShippingAddress"));
            }


            //If we got this far, something failed, redisplay form
            model = PrepareBillingAddressModel(model.NewAddress.CountryId);
            return(View(model));
        }
예제 #4
0
        protected virtual CheckoutBillingAddressModel PrepareBillingAddressModel(
            IList <ShoppingCartItem> cart, string selectedCountryId = null,
            bool prePopulateNewAddressWithCustomerFields            = false, string overrideAttributesXml = "")
        {
            var model = new CheckoutBillingAddressModel();

            model.ShipToSameAddressAllowed = _shippingSettings.ShipToSameAddress && cart.RequiresShipping();
            model.ShipToSameAddress        = true;

            //existing addresses

            var addresses = _workContext.CurrentCustomer.Addresses
                            .Where(a => a.CountryId == "" ||
                                   (_countryService.GetCountryById(a.CountryId) != null ? _countryService.GetCountryById(a.CountryId).AllowsBilling : false)
                                   )
                            .Where(a => a.CountryId == "" ||
                                   _storeMappingService.Authorize((_countryService.GetCountryById(a.CountryId))
                                                                  ))
                            .ToList();

            foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                _addressWebService.PrepareModel(model: addressModel, address: address, excludeProperties: false);
                model.ExistingAddresses.Add(addressModel);
            }

            //new address
            model.NewAddress.CountryId = selectedCountryId;
            _addressWebService.PrepareModel(model: model.NewAddress, address: null, excludeProperties: false,
                                            loadCountries: () => _countryService.GetAllCountriesForBilling(_workContext.WorkingLanguage.Id),
                                            prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields,
                                            customer: _workContext.CurrentCustomer,
                                            overrideAttributesXml: overrideAttributesXml
                                            );
            return(model);
        }
예제 #5
0
        protected CheckoutBillingAddressModel PrepareBillingAddressModel(int? selectedCountryId = null)
        {
            var model = new CheckoutBillingAddressModel();
            //existing addresses
            var addresses = _workContext.CurrentCustomer.Addresses.Where(a => a.Country == null || a.Country.AllowsBilling).ToList();
            foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                addressModel.PrepareModel(address,
                    false,
                    _addressSettings);
                model.ExistingAddresses.Add(addressModel);
            }

            //new address
            model.NewAddress.CountryId = selectedCountryId;
            model.NewAddress.PrepareModel(null,
                false,
                _addressSettings,
                _localizationService,
                _stateProvinceService,
                () => _countryService.GetAllCountriesForBilling());
            return model;
        }
예제 #6
0
        public ActionResult NewOrderBillingAddress(CheckoutBillingAddressModel model, FormCollection form)
        {
            //validation
            var cart = _workContext.CurrentCustomer.ShoppingCartItems
                       .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                       .LimitPerStore(_storeContext.CurrentStore.Id)
                       .ToList();

            if (cart.Count == 0)
            {
                return(RedirectToRoute("ShoppingCart"));
            }

            if (_orderSettings.OnePageCheckoutEnabled)
            {
                return(RedirectToRoute("CheckoutOnePage"));
            }

            if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
            {
                return(new HttpUnauthorizedResult());
            }

            //custom address attributes
            var customAttributes        = form.ParseCustomAddressAttributes(_addressAttributeParser, _addressAttributeService);
            var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes);

            foreach (var error in customAttributeWarnings)
            {
                ModelState.AddModelError("", error);
            }

            if (ModelState.IsValid)
            {
                //try to find an address with the same values (don't duplicate records)
                var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress(
                    model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber,
                    model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company,
                    model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City,
                    model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode,
                    model.NewAddress.CountryId, customAttributes,
                    model.NewAddress.RecipientTitle, model.NewAddress.RecipientName);
                if (address == null)
                {
                    //address is not found. let's create a new one
                    address = model.NewAddress.ToEntity();
                    address.CustomAttributes = customAttributes;
                    address.CreatedOnUtc     = DateTime.UtcNow;
                    //some validation
                    if (address.CountryId == 0)
                    {
                        address.CountryId = null;
                    }
                    if (address.StateProvinceId == 0)
                    {
                        address.StateProvinceId = null;
                    }
                    _workContext.CurrentCustomer.Addresses.Add(address);
                }
                _workContext.CurrentCustomer.BillingAddress = address;
                _customerService.UpdateCustomer(_workContext.CurrentCustomer);

                #region save payment info
                var paymentmethod     = form["paymentmethod"];
                var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(paymentmethod);
                if (paymentMethodInst == null ||
                    !paymentMethodInst.IsPaymentMethodActive(_paymentSettings) ||
                    !_pluginFinder.AuthenticateStore(paymentMethodInst.PluginDescriptor, _storeContext.CurrentStore.Id))
                {
                    return(PaymentMethod());
                }

                //save
                _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer,
                                                       SystemCustomerAttributeNames.SelectedPaymentMethod, paymentmethod, _storeContext.CurrentStore.Id);
                #endregion

                //redirect to EnterPaymentInfo
                return(EnterPaymentInfo(form));
            }


            //If we got this far, something failed, redisplay form
            model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId);
            return(View(model));
        }
예제 #7
0
        public ActionResult NewBillingAddress(CheckoutBillingAddressModel model)
        {
            //validation
            var cart = _workContext.CurrentCustomer.GetCartItems();

            if (!cart.Any())
            {
                return(RedirectToRoute("ShoppingCart"));
            }

            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                return(new HttpUnauthorizedResult());
            }

            if (ModelState.IsValid)
            {
                var address = model.NewAddress.ToEntity();

                _workContext.CurrentCustomer.Addresses.Add(address);
                _workContext.CurrentCustomer.BillingAddress = address;

                _customerService.Update(_workContext.CurrentCustomer);
            }

            //var addresses = _workContext.CurrentCustomer.Addresses;

            //if (ModelState.IsValid)
            //{
            //    if (addresses.Count == 0 || !addresses.Any())
            //    {
            //        var address = model.NewAddress.ToEntity();

            //        //some validation
            //        if (address.CountryId == 0)
            //            address.CountryId = null;

            //        if (address.StateProvinceId == 0)
            //            address.StateProvinceId = null;

            //        _workContext.CurrentCustomer.Addresses.Add(address);
            //        _workContext.CurrentCustomer.BillingAddress = address;

            //        _customerService.Update(_workContext.CurrentCustomer);

            //        //return RedirectToAction("PaymentMethod");
            //    }
            //    else
            //    {
            //        //var objAddress = addresses.FirstOrDefault();

            //        var objAddress = model.NewAddress.ToEntity();
            //        objAddress.FirstName = model.NewAddress.FirstName;
            //        objAddress.Email = model.NewAddress.Email;
            //        objAddress.PhoneNumber = model.NewAddress.PhoneNumber;
            //        objAddress.Address1 = model.NewAddress.Address1;
            //        _customerService.Update(_workContext.CurrentCustomer);
            //        _addressService.Create(objAddress);

            //        //return RedirectToAction("PaymentMethod");
            //    }
            //}

            //If we got this far, something failed, redisplay form
            model = PrepareBillingAddressModel();

            return(View(model));
        }
예제 #8
0
 public CheckoutAddressModel()
 {
     CheckoutShippingAddressModel = new CheckoutShippingAddressModel();
     CheckoutBillingAddressModel  = new CheckoutBillingAddressModel();
 }