// This actually calls the service to perform the tax calculation, and returns the calculation result. public async Task<GetTaxResult> GetTax(GetTaxRequest req) { // Convert the request to XML XmlSerializerNamespaces namesp = new XmlSerializerNamespaces(); namesp.Add(string.Empty, string.Empty); XmlWriterSettings settings = new XmlWriterSettings {OmitXmlDeclaration = true}; XmlSerializer x = new XmlSerializer(req.GetType()); StringBuilder sb = new StringBuilder(); x.Serialize(XmlWriter.Create(sb, settings), req, namesp); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); // Call the service Uri address = new Uri(_svcUrl + "tax/get"); var request = HttpHelper.CreateRequest(address, _accountNumber, _license); request.Method = "POST"; request.ContentType = "text/xml"; //request.ContentLength = sb.Length; Stream newStream = await request.GetRequestStreamAsync(); newStream.Write(Encoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length); GetTaxResult result = new GetTaxResult(); try { WebResponse response = await request.GetResponseAsync(); XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult) r.Deserialize(response.GetResponseStream()); } catch (WebException ex) { XmlSerializer r = new XmlSerializer(result.GetType()); result = (GetTaxResult) r.Deserialize(((HttpWebResponse) ex.Response).GetResponseStream()); } return result; }
public async Task GeoTaxTest() { // 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); GetTaxRequest getTaxRequest = new GetTaxRequest { CustomerCode = "ABC4335", DocDate = "2014-01-01", CompanyCode = "APITrialCompany", Client = "AvaTaxSample", DocCode = "INV001", DetailLevel = DetailLevel.Tax, Commit = false, DocType = DocType.SalesInvoice, PurchaseOrderNo = "PO123456", ReferenceCode = "ref123456", PosLaneCode = "09", CurrencyCode = "USD" }; // Document Level Elements // Required Request Parameters // Best Practice Request Parameters // Situational Request Parameters // getTaxRequest.CustomerUsageType = "G"; // getTaxRequest.ExemptionNo = "12345"; // getTaxRequest.BusinessIdentificationNo = "234243"; // getTaxRequest.Discount = 50; // getTaxRequest.TaxOverride = new TaxOverrideDef(); // getTaxRequest.TaxOverride.TaxOverrideType = "TaxDate"; // getTaxRequest.TaxOverride.Reason = "Adjustment for return"; // getTaxRequest.TaxOverride.TaxDate = "2013-07-01"; // getTaxRequest.TaxOverride.TaxAmount = "0"; // Optional Request Parameters // Address Data Address address1 = new Address { AddressCode = "01", Line1 = "45 Fremont Street", City = "San Francisco", Region = "CA" }; Address address2 = new Address { AddressCode = "02", Line1 = "118 N Clark St", Line2 = "Suite 100", Line3 = "ATTN Accounts Payable", City = "Chicago", Region = "IL", Country = "US", PostalCode = "60602" }; Address address3 = new Address { AddressCode = "03", Latitude = (decimal) 47.627935, Longitude = (decimal) -122.51702 }; Address[] addresses = {address1, address2, address3}; getTaxRequest.Addresses = addresses; // Line Data // Required Parameters Line line1 = new Line { LineNo = "01", ItemCode = "N543", Qty = 1, Amount = 10, OriginCode = "01", DestinationCode = "02", Description = "Red Size 7 Widget", TaxCode = "NT", Ref1 = "ref123", Ref2 = "ref456" }; // Best Practice Request Parameters // Situational Request Parameters // line1.CustomerUsageType = "L"; // line1.Discounted = true; // line1.TaxIncluded = true; // line1.BusinessIdentificationNo = "234243"; // line1.TaxOverride = new TaxOverrideDef(); // line1.TaxOverride.TaxOverrideType = "TaxDate"; // line1.TaxOverride.Reason = "Adjustment for return"; // line1.TaxOverride.TaxDate = "2013-07-01"; // line1.TaxOverride.TaxAmount = "0"; // Optional Request Parameters Line line2 = new Line { LineNo = "02", ItemCode = "T345", Qty = 3, Amount = 150, OriginCode = "01", DestinationCode = "03", Description = "Size 10 Green Running Shoe", TaxCode = "PC030147" }; Line line3 = new Line { LineNo = "02-FR", ItemCode = "FREIGHT", Qty = 1, Amount = 15, OriginCode = "01", DestinationCode = "03", Description = "Shipping Charge", TaxCode = "FR" }; Line[] lines = {line1, line2, line3}; getTaxRequest.Lines = lines; GetTaxResult getTaxResult = await taxSvc.GetTax(getTaxRequest); // Print results Console.WriteLine("GetTaxTest Result: {0}", getTaxResult.ResultCode); if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success)) { foreach (Message message in getTaxResult.Messages) { Console.WriteLine(message.Summary); } } else { Console.WriteLine("Document Code: {0} Total Tax: {1}", getTaxResult.DocCode, getTaxResult.TotalTax); foreach (TaxLine taxLine in getTaxResult.TaxLines ?? Enumerable.Empty<TaxLine>()) { Console.WriteLine(" Line Number: {0} Line Tax: {1}", taxLine.LineNo, taxLine.Tax.ToString(CultureInfo.CurrentCulture)); foreach (TaxDetail taxDetail in taxLine.TaxDetails ?? Enumerable.Empty<TaxDetail>()) { Console.WriteLine(" Jurisdiction: {0}Tax: {1}", taxDetail.JurisName, taxDetail.Tax.ToString(CultureInfo.CurrentCulture)); } } } }