/// <summary>
 /// Gets the Billing Address from the model as an <see cref="IAddress"/>.
 /// </summary>
 /// <param name="form">
 /// The form.
 /// </param>
 /// <returns>
 /// The <see cref="IAddress"/>.
 /// </returns>
 public static IAddress GetShippingAddress(this CheckoutAddressForm form)
 {
     return(new Address()
     {
         AddressType = AddressType.Shipping,
         Name = form.ShippingName,
         Address1 = form.ShippingAddress1,
         Address2 = form.ShippingAddress2,
         Locality = form.ShippingLocality,
         Region = form.ShippingRegion,
         CountryCode = form.ShippingCountryCode,
         PostalCode = form.ShippingPostalCode,
         Email = form.ShippingEmail,
         Phone = form.ShippingPhone
     });
 }
        public ActionResult SaveAddresses(CheckoutAddressForm model)
        {
            // we have to do custom validation here since there are so many 
            // different variations of the submitted model
            var isValid = true;
            if (model.BillingAddressKey.Equals(Guid.Empty))
            {
                isValid = ModelState.IsValidField("BillingName") && ModelState.IsValidField("BillingEmail")
                          && ModelState.IsValidField("BillingAddress1") && ModelState.IsValidField("BillingLocality")
                          && ModelState.IsValidField("BillingPostalCode");
            }
            if (!isValid) return this.CurrentUmbracoPage();

            if (model.ShippingAddressKey.Equals(Guid.Empty))
            {
                isValid = ModelState.IsValidField("ShippingName") && ModelState.IsValidField("ShippingEmail")
                          && ModelState.IsValidField("ShippingAddress1") && ModelState.IsValidField("ShippingLocality")
                          && ModelState.IsValidField("ShippingPostalCode");
            }

            if (!isValid) return this.CurrentUmbracoPage();

            var preparation = Basket.SalePreparation();
            preparation.RaiseCustomerEvents = false;

            var saveBilling = false;
            var saveShipping = false;

            IAddress billingAddress;
            if (!model.BillingAddressKey.Equals(Guid.Empty))
            {
                var billing = MerchelloServices.CustomerService.GetAddressByKey(model.BillingAddressKey);
                billingAddress = billing.AsAddress(billing.FullName);
            }
            else
            {
                billingAddress = model.GetBillingAddress();
                saveBilling = true;
            }

            IAddress shippingAddress;
            if (!model.ShippingAddressKey.Equals(Guid.Empty))
            {
                var shipping = MerchelloServices.CustomerService.GetAddressByKey(model.ShippingAddressKey);
                shippingAddress = shipping.AsAddress(shipping.FullName);
            }
            else
            {
                shippingAddress = model.GetShippingAddress();
                saveShipping = true;
            }

            if (model.SaveCustomerAddress)
            {
                var redirect = (saveBilling && !this.ModelState.IsValidField("BillingAddressLabel")) || 
                    (saveShipping && (!this.ModelState.IsValidField("ShippingAddressLabel") && !model.BillingIsShipping));
                if (redirect) return this.CurrentUmbracoPage();
                
                //// at this point we know the customer is an ICustomer 
                var customer = (ICustomer)CurrentCustomer;
                
                if (saveBilling)
                {
                    customer.CreateCustomerAddress(billingAddress, model.BillingAddressLabel, AddressType.Billing);
                }

                if (saveShipping)
                {
                    if (model.BillingIsShipping) model.ShippingAddressLabel = model.BillingAddressLabel;
                    customer.CreateCustomerAddress(shippingAddress, model.ShippingAddressLabel, AddressType.Shipping);
                }
            }

            preparation.SaveBillToAddress(billingAddress);
            preparation.SaveShipToAddress(shippingAddress);
            
            return RedirectToUmbracoPage(model.ConfirmSalePageId);
        }