Пример #1
0
        public GeoTaxResult EstimateTax(decimal latitude, decimal longitude, decimal saleAmount)
        {
            // Call the service
            Uri            address = new Uri(svcURL + "tax/" + latitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "," + longitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "/get.xml?saleamount=" + saleAmount);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            GeoTaxResult result = new GeoTaxResult();

            try
            {
                WebResponse   response = request.GetResponse();
                XmlSerializer r        = new XmlSerializer(result.GetType());
                result = (GeoTaxResult)r.Deserialize(response.GetResponseStream());
            }
            catch (WebException ex)
            {
                Stream       responseStream = ((HttpWebResponse)ex.Response).GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                string       responseString = reader.ReadToEnd();

                // The service returns some error messages in JSON for authentication/unhandled errors.
                if (responseString.StartsWith("{") || responseString.StartsWith("["))
                {
                    result            = new GeoTaxResult();
                    result.ResultCode = SeverityLevel.Error;
                    Message msg = new Message();
                    msg.Severity = result.ResultCode;
                    msg.Summary  = "The request was unable to be successfully serviced, please try again or contact Customer Service.";
                    msg.Source   = "Avalara.Web.REST";
                    if (!((HttpWebResponse)ex.Response).StatusCode.Equals(HttpStatusCode.InternalServerError))
                    {
                        msg.Summary = "The user or account could not be authenticated.";
                        msg.Source  = "Avalara.Web.Authorization";
                    }

                    result.Messages = new Message[1] {
                        msg
                    };
                }
                else
                {
                    XmlSerializer r      = new XmlSerializer(result.GetType());
                    byte[]        temp   = Encoding.ASCII.GetBytes(responseString);
                    MemoryStream  stream = new MemoryStream(temp);
                    result = (GeoTaxResult)r.Deserialize(stream); // Inelegant, but the deserializer only takes streams, and we already read ours out.
                }
            }

            return(result);
        }
Пример #2
0
        public GeoTaxResult EstimateTax(decimal latitude, decimal longitude, decimal saleAmount)
        {
            // Call the service
            var address = new Uri(svcURL + "tax/" + latitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "," + longitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "/get?saleamount=" + saleAmount);
            var request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            var result = new GeoTaxResult();

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    // Get the stream containing content returned by the server.
                    var responseStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    using (var reader = new StreamReader(responseStream))
                    {
                        result = JsonConvert.DeserializeObject <GeoTaxResult>(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    result.ResultCode = SeverityLevel.Error;
                    result.Messages   = new[] { new Message {
                                                    Severity = SeverityLevel.Error, Summary = ex.Message
                                                } };
                    return(result);
                }

                using (var response = ex.Response)
                {
                    using (var data = response.GetResponseStream())
                    {
                        // Open the stream using a StreamReader for easy access.
                        using (var reader = new StreamReader(data))
                        {
                            result = JsonConvert.DeserializeObject <GeoTaxResult>(reader.ReadToEnd());
                        }
                    }
                }
            }

            return(result);
        }
Пример #3
0
        public GeoTaxResult EstimateTax(decimal latitude, decimal longitude, decimal saleAmount)
        {
            // Call the service
            Uri address = new Uri(svcURL + "tax/" + latitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "," + longitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "/get.xml?saleamount=" + saleAmount);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            GeoTaxResult result = new GeoTaxResult();
            try
            {
                WebResponse response = request.GetResponse();
                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (GeoTaxResult)r.Deserialize(response.GetResponseStream());
            }
            catch (WebException ex)
            {
                Stream responseStream = ((HttpWebResponse)ex.Response).GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                string responseString = reader.ReadToEnd();

                // The service returns some error messages in JSON for authentication/unhandled errors.
                if (responseString.StartsWith("{") || responseString.StartsWith("[")) 
                {
                    result = new GeoTaxResult();
                    result.ResultCode = SeverityLevel.Error;
                    Message msg = new Message();
                    msg.Severity = result.ResultCode;
                    msg.Summary = "The request was unable to be successfully serviced, please try again or contact Customer Service.";
                    msg.Source = "Avalara.Web.REST";
                    if (!((HttpWebResponse)ex.Response).StatusCode.Equals(HttpStatusCode.InternalServerError))
                    {
                        msg.Summary = "The user or account could not be authenticated.";
                        msg.Source = "Avalara.Web.Authorization";
                    }

                    result.Messages = new Message[1] { msg };
                }
                else
                {
                    XmlSerializer r = new XmlSerializer(result.GetType());
                    byte[] temp = Encoding.ASCII.GetBytes(responseString);
                    MemoryStream stream = new MemoryStream(temp);
                    result = (GeoTaxResult)r.Deserialize(stream); // Inelegant, but the deserializer only takes streams, and we already read ours out.
                }
            }

            return result;        
        }
Пример #4
0
        public GeoTaxResult EstimateTax(decimal latitude, decimal longitude, decimal saleAmount)
        {
            // Call the service
            var address = new Uri(svcURL + "tax/" + latitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "," + longitude.ToString(CultureInfo.InvariantCulture).Replace(',', '.') + "/get?saleamount=" + saleAmount);
            var request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            var result = new GeoTaxResult();
            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    // Get the stream containing content returned by the server.
                    var responseStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    using (var reader = new StreamReader(responseStream))
                    {
                        result = JsonConvert.DeserializeObject<GeoTaxResult>(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    result.ResultCode = SeverityLevel.Error;
                    result.Messages = new[] { new Message { Severity = SeverityLevel.Error, Summary = ex.Message } };
                    return result;
                }

                using (var response = ex.Response)
                {
                    using (var data = response.GetResponseStream())
                    {
                        // Open the stream using a StreamReader for easy access.
                        using (var reader = new StreamReader(data))
                        {
                            result = JsonConvert.DeserializeObject<GeoTaxResult>(reader.ReadToEnd());
                        }
                    }
                }
            }

            return result;        
        }
Пример #5
0
        public static void Test()
        {
            // Header Level Elements
            // Required Header Level Elements
            string accountNumber = ConfigurationManager.AppSettings["AvaTax:AccountNumber"];
            string licenseKey    = ConfigurationManager.AppSettings["AvaTax:LicenseKey"];
            string serviceURL    = ConfigurationManager.AppSettings["AvaTax:ServiceUrl"];

            TaxSvc taxSvc = new TaxSvc(accountNumber, licenseKey, serviceURL);

            GeoTaxResult geoTaxResult = taxSvc.Ping();

            Console.WriteLine("PingTest Result: " + geoTaxResult.ResultCode.ToString());
            if (!geoTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in geoTaxResult.Messages)
                {
                    Console.WriteLine(message.Summary);
                }
            }
        }
        public static void Test()
        {
            // Header Level Elements
            // Required Header Level Elements
            string accountNumber = ConfigurationManager.AppSettings["AvaTax:AccountNumber"];
            string licenseKey    = ConfigurationManager.AppSettings["AvaTax:LicenseKey"];
            string serviceURL    = ConfigurationManager.AppSettings["AvaTax:ServiceUrl"];

            TaxSvc taxSvc = new TaxSvc(accountNumber, licenseKey, serviceURL);

            // Required Request Parameters
            decimal latitude   = (decimal)47.627935;
            decimal longitude  = (decimal) - 122.51702;
            decimal saleAmount = (decimal)10;

            GeoTaxResult geoTaxResult = taxSvc.EstimateTax(latitude, longitude, saleAmount);

            // Print results
            Console.WriteLine("EstimateTaxTest Result: " + geoTaxResult.ResultCode.ToString());
            if (!geoTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in geoTaxResult.Messages)
                {
                    Console.WriteLine(message.Summary);
                }
            }
            else
            {
                Console.WriteLine("Total Rate: " + geoTaxResult.Rate + " Total Tax: " + geoTaxResult.Tax);

                foreach (TaxDetail taxDetail in geoTaxResult.TaxDetails ?? Enumerable.Empty <TaxDetail>())
                {
                    Console.WriteLine("    " + "Jurisdiction: " + taxDetail.JurisName + " Tax: " + taxDetail.Tax.ToString());
                }
            }
        }