static void Main()
        {
            GetTaxRequest calcReq = DocumentLoader.Load(); //Loads document from file to generate request

            //Run address validation test (address/validate)
            try
            {
                ValidateResult addressResult = ValidateAddress.Validate(calcReq.Addresses[0], ACCTNUM, KEY, WEBADDR); //Validates a given address.
                Console.Write("ValidateAddress test result: " + addressResult.ResultCode.ToString() + " >> ");
                if (addressResult.ResultCode.Equals(SeverityLevel.Success))
                {
                    Console.WriteLine("Address=" + addressResult.Address.Line1 + " " + addressResult.Address.City + " " + addressResult.Address.Region + " " + addressResult.Address.PostalCode);//At this point, you would display the validated result to the user for approval, and write it to the customer record.
                }
                else
                {
                    Console.WriteLine(addressResult.Messages[0].Summary);
                }
            }
            catch (Exception ex)
            { Console.WriteLine("ValidateAddress Exception: " + ex.Message); }

            //Run tax calculation test (tax/get POST)
            try
            {
                GetTaxResult calcresult = GetTax.Get(calcReq, ACCTNUM, KEY, COMPANYCODE, WEBADDR); //Calculates tax on document
                Console.Write("GetTax test result: " + calcresult.ResultCode.ToString() + " >> ");
                if (calcresult.ResultCode.Equals(SeverityLevel.Success))
                {
                    Console.WriteLine("TotalTax=" + calcresult.TotalTax.ToString()); //At this point, you would write the tax calculated to your database and display to the user.
                }
                else
                {
                    Console.WriteLine(calcresult.Messages[0].Summary);
                }
            }
            catch (Exception ex)
            { Console.WriteLine("GetTax Exception: " + ex.Message); }

            //Run cancel tax test (tax/cancel)
            try
            {
                CancelTaxResult cancelresult = CancelTax.Cancel(calcReq, ACCTNUM, KEY, COMPANYCODE, WEBADDR); //Let's void this document to demonstrate tax/cancel
                //You would normally initiate a tax/cancel call upon voiding or deleting the document in your system.
                Console.Write("CancelTax test result: " + cancelresult.ResultCode.ToString() + " >> ");
                //Let's display the result of the cancellation. At this point, you would allow your system to complete the delete/void.
                if (cancelresult.ResultCode.Equals(SeverityLevel.Success))
                {
                    Console.WriteLine("Document Cancelled");
                }
                else
                {
                    Console.WriteLine(cancelresult.Messages[0].Summary);
                }
            }
            catch (Exception ex)
            { Console.WriteLine("CancelTax Exception: " + ex.Message); }

            Console.WriteLine("Done");
            Console.ReadLine();
        }
Пример #2
0
        // This actually calls the service to perform the tax calculation, and returns the calculation result.
        public GetTaxResult GetTax(GetTaxRequest req)
        {
            var jsonRequest = JsonConvert.SerializeObject(req);

            // Call the service
            var address = new Uri(svcURL + "tax/get");
            var request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method        = "POST";
            request.ContentType   = "text/json";
            request.ContentLength = jsonRequest.Length;
            var newStream = request.GetRequestStream();

            newStream.Write(ASCIIEncoding.ASCII.GetBytes(jsonRequest), 0, jsonRequest.Length);

            var result = new GetTaxResult();

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    // Get the stream containing content returned by the server.
                    newStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    using (var reader = new StreamReader(newStream))
                    {
                        result = JsonConvert.DeserializeObject <GetTaxResult>(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 <GetTaxResult>(reader.ReadToEnd());
                        }
                    }
                }
            }
            return(result);
        }
Пример #3
0
        // This actually calls the service to perform the tax calculation, and returns the calculation result.
        public GetTaxResult GetTax(GetTaxRequest req)
        {
            var jsonRequest = JsonConvert.SerializeObject(req);
            
            // Call the service
            var address = new Uri(svcURL + "tax/get");
            var request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "POST";
            request.ContentType = "text/json";
            request.ContentLength = jsonRequest.Length;
            var newStream = request.GetRequestStream();
            newStream.Write(ASCIIEncoding.ASCII.GetBytes(jsonRequest), 0, jsonRequest.Length);
            
            var result = new GetTaxResult();
            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    // Get the stream containing content returned by the server.
                    newStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    using (var reader = new StreamReader(newStream))
                    {
                        result = JsonConvert.DeserializeObject<GetTaxResult>(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<GetTaxResult>(reader.ReadToEnd());
                        }
                    }
                }
            }
            return result;
        }
Пример #4
0
        //This actually calls the service to perform the tax calculation, and returns the calculation result.
        public static GetTaxResult Get(GetTaxRequest req, string acctNum, string licKey, string companyCode, string webAddr)
        {
            //Company Code is ususally maintiained with the account credentials, so it's passed in to this function even though it's included in the body of the GetTaxRequest.
            req.CompanyCode = companyCode;


            //Convert the request to XML
            XmlSerializerNamespaces namesp = new XmlSerializerNamespaces();

            namesp.Add("", "");
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.OmitXmlDeclaration = true;
            XmlSerializer x  = new XmlSerializer(req.GetType());
            StringBuilder sb = new StringBuilder();

            x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp);
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(sb.ToString());
            //doc.Save(@"get_tax_request.xml");

            //Call the service
            Uri            address = new Uri(webAddr + "tax/get");
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(acctNum + ":" + licKey)));
            request.Method        = "POST";
            request.ContentType   = "text/xml";
            request.ContentLength = sb.Length;
            Stream newStream = request.GetRequestStream();

            newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
            GetTaxResult result = new GetTaxResult();

            try
            {
                WebResponse   response = request.GetResponse();
                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);
        }
Пример #5
0
        // This actually calls the service to perform the tax calculation, and returns the calculation result.
        public GetTaxResult GetTax(GetTaxRequest req)
        {
            // Convert the request to XML
            XmlSerializerNamespaces namesp = new XmlSerializerNamespaces();

            namesp.Add(string.Empty, string.Empty);
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.OmitXmlDeclaration = true;
            XmlSerializer x  = new XmlSerializer(req.GetType());
            StringBuilder sb = new StringBuilder();

            x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp);
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(sb.ToString());

            // Call the service
            Uri            address = new Uri(svcURL + "tax/get");
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method        = "POST";
            request.ContentType   = "text/xml";
            request.ContentLength = sb.Length;
            Stream newStream = request.GetRequestStream();

            newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
            GetTaxResult result = new GetTaxResult();

            try
            {
                WebResponse   response = request.GetResponse();
                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((ex.Response).GetResponseStream());
            }

            return(result);
        }
Пример #6
0
        // This actually calls the service to perform the tax calculation, and returns the calculation result.
        public GetTaxResult GetTax(GetTaxRequest req)
        {
            // Convert the request to XML
            XmlSerializerNamespaces namesp = new XmlSerializerNamespaces();
            namesp.Add(string.Empty, string.Empty);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            XmlSerializer x = new XmlSerializer(req.GetType());
            StringBuilder sb = new StringBuilder();
            x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sb.ToString());

            // Call the service
            Uri address = new Uri(svcURL + "tax/get");
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "POST";
            request.ContentType = "text/xml";
            request.ContentLength = sb.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
            GetTaxResult result = new GetTaxResult();
            try
            {
                WebResponse response = request.GetResponse();
                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((ex.Response).GetResponseStream());
            }

            return result;
        }
Пример #7
0
        //This actually calls the service to perform the tax calculation, and returns the calculation result.
        public static GetTaxResult Get(GetTaxRequest req, string acctNum, string licKey, string companyCode, string webAddr)
        {
            //Company Code is ususally maintiained with the account credentials, so it's passed in to this function even though it's included in the body of the GetTaxRequest.
            req.CompanyCode = companyCode;

            //Convert the request to XML
            XmlSerializerNamespaces namesp = new XmlSerializerNamespaces();
            namesp.Add("", "");
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            XmlSerializer x = new XmlSerializer(req.GetType());
            StringBuilder sb = new StringBuilder();
            x.Serialize(XmlTextWriter.Create(sb, settings), req, namesp);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sb.ToString());
            //doc.Save(@"get_tax_request.xml");

            //Call the service
            Uri address = new Uri(webAddr + "tax/get");
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(acctNum + ":" + licKey)));
            request.Method = "POST";
            request.ContentType = "text/xml";
            request.ContentLength = sb.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(ASCIIEncoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
            GetTaxResult result = new GetTaxResult();
            try
            {
                WebResponse response = request.GetResponse();
                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;
        }
Пример #8
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);

            GetTaxRequest getTaxRequest = new GetTaxRequest();

            // Document Level Elements
            // Required Request Parameters
            getTaxRequest.CustomerCode = "ABC4335";
            getTaxRequest.DocDate      = "2014-01-01";

            // Best Practice Request Parameters
            getTaxRequest.CompanyCode = "APITrialCompany";
            getTaxRequest.Client      = "AvaTaxSample";
            getTaxRequest.DocCode     = "INV001";
            getTaxRequest.DetailLevel = DetailLevel.Tax;
            getTaxRequest.Commit      = false;
            getTaxRequest.DocType     = DocType.SalesInvoice;

            // 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
            getTaxRequest.PurchaseOrderNo = "PO123456";
            getTaxRequest.ReferenceCode   = "ref123456";
            getTaxRequest.PosLaneCode     = "09";
            getTaxRequest.CurrencyCode    = "USD";

            // Address Data
            Address address1 = new Address();

            address1.AddressCode = "01";
            address1.Line1       = "45 Fremont Street";
            address1.City        = "San Francisco";
            address1.Region      = "CA";

            Address address2 = new Address();

            address2.AddressCode = "02";
            address2.Line1       = "118 N Clark St";
            address2.Line2       = "Suite 100";
            address2.Line3       = "ATTN Accounts Payable";
            address2.City        = "Chicago";
            address2.Region      = "IL";
            address2.Country     = "US";
            address2.PostalCode  = "60602";

            Address address3 = new Address();

            address3.AddressCode = "03";
            address3.Latitude    = (decimal)47.627935;
            address3.Longitude   = (decimal) - 122.51702;
            Address[] addresses = { address1, address2, address3 };
            getTaxRequest.Addresses = addresses;

            // Line Data
            // Required Parameters
            Line line1 = new Line();

            line1.LineNo          = "01";
            line1.ItemCode        = "N543";
            line1.Qty             = 1;
            line1.Amount          = 10;
            line1.OriginCode      = "01";
            line1.DestinationCode = "02";

            // Best Practice Request Parameters
            line1.Description = "Red Size 7 Widget";
            line1.TaxCode     = "NT";

            // 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
            line1.Ref1 = "ref123";
            line1.Ref2 = "ref456";

            Line line2 = new Line();

            line2.LineNo          = "02";
            line2.ItemCode        = "T345";
            line2.Qty             = 3;
            line2.Amount          = 150;
            line2.OriginCode      = "01";
            line2.DestinationCode = "03";
            line2.Description     = "Size 10 Green Running Shoe";
            line2.TaxCode         = "PC030147";

            Line line3 = new Line();

            line3.LineNo          = "02-FR";
            line3.ItemCode        = "FREIGHT";
            line3.Qty             = 1;
            line3.Amount          = 15;
            line3.OriginCode      = "01";
            line3.DestinationCode = "03";
            line3.Description     = "Shipping Charge";
            line3.TaxCode         = "FR";
            Line[] lines = { line1, line2, line3 };
            getTaxRequest.Lines = lines;

            GetTaxResult getTaxResult = taxSvc.GetTax(getTaxRequest);

            // Print results
            Console.WriteLine("GetTaxTest Result: " + getTaxResult.ResultCode.ToString());
            if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in getTaxResult.Messages)
                {
                    Console.WriteLine(message.Summary);
                }
            }
            else
            {
                Console.WriteLine("Document Code: " + getTaxResult.DocCode + " Total Tax: " + getTaxResult.TotalTax);
                foreach (TaxLine taxLine in getTaxResult.TaxLines ?? Enumerable.Empty <TaxLine>())
                {
                    Console.WriteLine("    " + "Line Number: " + taxLine.LineNo + " Line Tax: " + taxLine.Tax.ToString());
                    foreach (TaxDetail taxDetail in taxLine.TaxDetails ?? Enumerable.Empty <TaxDetail>())
                    {
                        Console.WriteLine("        " + "Jurisdiction: " + taxDetail.JurisName + "Tax: " + taxDetail.Tax.ToString());
                    }
                }
            }
        }