internal static ITaxMethod ToTaxMethod(this TaxMethodDisplay taxMethodDisplay, ITaxMethod destination)
        {
            if (taxMethodDisplay.Key != Guid.Empty)
            {
                destination.Key = taxMethodDisplay.Key;
            }


            destination.Name = taxMethodDisplay.Name;
            destination.PercentageTaxRate = taxMethodDisplay.PercentageTaxRate;

            // this may occur when creating a new tax method since the UI does not
            // query for provinces
            // TODO fix
            if (destination.HasProvinces && !taxMethodDisplay.Provinces.Any())
            {
                taxMethodDisplay.Provinces = destination.Provinces.Select(x => x.ToTaxProvinceDisplay()).ToArray();
            }

            foreach (var province in taxMethodDisplay.Provinces)
            {
                var p = destination.Provinces.FirstOrDefault(x => x.Code == province.Code);
                if (p != null)
                {
                    p.PercentAdjustment = province.PercentAdjustment;
                }
            }

            return(destination);
        }
        public HttpResponseMessage AddTaxMethod(TaxMethodDisplay method)
        {
            var response = Request.CreateResponse(HttpStatusCode.OK);

            try
            {
                var provider = _taxationContext.ResolveByKey(method.ProviderKey);

                var taxationGatewayMethod = provider.CreateTaxMethod(method.CountryCode, method.PercentageTaxRate);

                method.ToTaxMethod(taxationGatewayMethod.TaxMethod);

                provider.SaveTaxMethod(taxationGatewayMethod);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, String.Format("{0}", ex.Message));
            }

            return response;
        }
        public HttpResponseMessage PutTaxMethod(TaxMethodDisplay method)
        {
            var response = Request.CreateResponse(HttpStatusCode.OK);

            try
            {
                var provider = _taxationContext.GetProviderByKey(method.ProviderKey);

                var taxMethod = provider.TaxMethods.FirstOrDefault(x => x.Key == method.Key);

                if (taxMethod == null)
                {
                    var deleteMethod = _taxationContext.GetTaxMethodForCountryCode(method.CountryCode);
                    this.DeleteTaxMethod(deleteMethod.Key);
                    return this.AddTaxMethod(method);
                }

                taxMethod = method.ToTaxMethod(taxMethod);

                provider.GatewayProviderService.Save(taxMethod);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, string.Format("{0}", ex.Message));
            }

            return response;
        }
        public HttpResponseMessage PutTaxMethod(TaxMethodDisplay method)
        {
            var response = Request.CreateResponse(HttpStatusCode.OK);

            try
            {
                var provider = _taxationContext.ResolveByKey(method.ProviderKey);

                var taxMethod = provider.TaxMethods.FirstOrDefault(x => x.Key == method.Key);

                taxMethod = method.ToTaxMethod(taxMethod);

                provider.GatewayProviderService.Save(taxMethod);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, String.Format("{0}", ex.Message));
            }

            return response;
        }