示例#1
0
        public async Task <GeoTaxResult> EstimateTax(decimal latitude, decimal longitude, decimal saleAmount)
        {
            // Call the service
            Uri address = new Uri($"{_svcUrl}tax/{latitude},{longitude}/get.xml?saleamount={saleAmount}");
            var request = HttpHelper.CreateRequest(address, _accountNumber, _license);

            request.Method = "GET";

            GeoTaxResult result = new GeoTaxResult();

            try
            {
                WebResponse response = await request.GetResponseAsync();

                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 {
                        ResultCode = SeverityLevel.Error
                    };
                    Message msg = new Message
                    {
                        Severity = result.ResultCode,
                        Summary  =
                            "The request was unable to be successfully serviced, please try again or contact Customer Service.",
                        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[] { 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);
        }
        public async Task PingTest()
        {
            // Header Level Elements
            // Required Header Level Elements
            var    configSection = ConfigurationHelper.GetConfiguration();
            string accountNumber = configSection["accountNumber"];
            string licenseKey    = configSection["licenseKey"];
            string serviceUrl    = configSection["serviceUrl"];

            ITaxService taxSvc = new TaxService(accountNumber, licenseKey, serviceUrl);

            GeoTaxResult geoTaxResult = await taxSvc.Ping();

            Console.WriteLine("PingTest Result: {0}", geoTaxResult.ResultCode);
            if (!geoTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in geoTaxResult.Messages)
                {
                    Console.WriteLine(message.Summary);
                }
            }
        }
        public async Task EstimateTaxTest()
        {
            // Header Level Elements
            // Required Header Level Elements
            var    configSection = ConfigurationHelper.GetConfiguration();
            string accountNumber = configSection["accountNumber"];
            string licenseKey    = configSection["licenseKey"];
            string serviceUrl    = configSection["serviceUrl"];

            ITaxService taxSvc = new TaxService(accountNumber, licenseKey, serviceUrl);

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

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

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

                foreach (TaxDetail taxDetail in geoTaxResult.TaxDetails ?? Enumerable.Empty <TaxDetail>())
                {
                    Console.WriteLine("    Jurisdiction: {0} Tax: {1}", taxDetail.JurisName,
                                      taxDetail.Tax.ToString(CultureInfo.CurrentCulture));
                }
            }
        }
示例#4
0
        public async Task <bool> PingTax()
        {
            GeoTaxResult geoTaxResult = await _taxSvc.Ping();

            return(geoTaxResult != null ? true : false);
        }