public IEnumerable <TaxRate> GetRates(Sitecore.Ecommerce.DomainModel.Addresses.AddressInfo address)
        {
            /**
             * See README, this sort of rate provider is usually a bad idea -- you should utilize a service that can look up
             * tax rates for the specific street address!
             */

            //read the state code from the provided address
            var state = address.State != null?address.State.ToUpper() : null;

            if (state == null)
            {
                return(EmptyRates);
            }

            //look for a configured rate for the given state
            var rateStr = Sitecore.Configuration.Settings.GetSetting(string.Format(RateSetting, state), null);

            if (string.IsNullOrEmpty(rateStr))
            {
                return(EmptyRates);
            }

            //attempt to parse into decimal
            decimal rate    = 0m;
            var     success = decimal.TryParse(rateStr, out rate);

            if (!success)
            {
                return(EmptyRates);
            }

            //look for a configured name for the state, defaulting to "State"
            var name = Sitecore.Configuration.Settings.GetSetting(string.Format(NameSetting, state), ActiveCommerce.Taxes.TaxJurisdiction.Types.STATE);

            //return a list of rates -- in a more complex example, could include additional jurisdictions (city, county rates)
            return(new TaxRate[]
            {
                new TaxRate
                {
                    Compounds = false,                                      //usually only applies to Canadian GST/PST
                    Name = name,                                            //name of the jurisdiction
                    Rate = rate,                                            //the rate as a decimal (e.g. 0.05), not a percentage (5%)
                    Type = ActiveCommerce.Taxes.TaxJurisdiction.Types.STATE //use provided jurisdiction constants here if possible
                }
            });
        }
Exemple #2
0
        public Sitecore.Ecommerce.DomainModel.Addresses.AddressInfo Validate(Sitecore.Ecommerce.DomainModel.Addresses.AddressInfo address)
        {
            ValidateRequired(address.Address, "Address Line 1");
            ValidateRequired(address.City, "City");
            ValidateRequired(address.Country.Code, "Country");

            /**
             * After doing basic validations, you could call an address validation service or API here.
             * Instead, we're going to do some custom validation. The builtin default address validator is
             * definitely more thorough -- this is for example purposes only.
             */

            if (address.Country.Code.Equals("US", StringComparison.InvariantCultureIgnoreCase))
            {
                ValidateRequired(address.State, "State");
                ValidateRequired(address.Zip, "Postal Code");

                //zip or zip plus 4
                if (address.Zip.Length != 5 && address.Zip.Length != 10)
                {
                    throw new AddressValidationException("Not a valid U.S. zip code");
                }
            }

            /**
             * To emphasize that this is for example only :)
             *
             */
            if (address.Address.Contains("hubert"))
            {
                throw new AddressValidationException("You taste like soot and...");
            }

            /**
             * We can make simple corrections to the address as well. Interactive address
             * corrections (e.g. with user acceptance of corrected address) would require
             * customization of the checkout UX.
             */
            address.Address  = address.Address.ToUpper();
            address.Address2 = address.Address2 != null?address.Address2.ToUpper() : null;

            address.City  = address.City.ToUpper();
            address.State = address.State.ToUpper();
            address.Zip   = address.Zip.ToUpper();

            return(address);
        }