Пример #1
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);

            CancelTaxRequest cancelTaxRequest = new CancelTaxRequest();

            // Required Request Parameters
            cancelTaxRequest.CompanyCode = "APITrialCompany";
            cancelTaxRequest.DocType     = DocType.SalesInvoice;
            cancelTaxRequest.DocCode     = "INV001";
            cancelTaxRequest.CancelCode  = CancelCode.DocVoided;

            CancelTaxResult cancelTaxResult = taxSvc.CancelTax(cancelTaxRequest);

            // Print results
            Console.WriteLine("CancelTaxTest Result: " + cancelTaxResult.ResultCode.ToString());
            if (!cancelTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in cancelTaxResult.Messages)
                {
                    Console.WriteLine(message.Summary);
                }
            }
        }
        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();
        }
Пример #3
0
        // This calls CancelTax to void a transaction specified in taxreq
        public CancelTaxResult CancelTax(CancelTaxRequest cancelTaxRequest)
        {
            var jsonRequest = JsonConvert.SerializeObject(cancelTaxRequest);

            // Call the service
            var address = new Uri(svcURL + "tax/cancel");
            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 cancelResponse = new CancelTaxResult();

            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))
                    {
                        cancelResponse = JsonConvert.DeserializeObject <CancelTaxResult>(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    cancelResponse.ResultCode = SeverityLevel.Error;
                    cancelResponse.Messages   = new[] { new Message {
                                                            Severity = SeverityLevel.Error, Summary = ex.Message
                                                        } };
                    return(cancelResponse);
                }

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

            return(cancelResponse);
        }
Пример #4
0
        // This calls CancelTax to void a transaction specified in taxreq
        public CancelTaxResult CancelTax(CancelTaxRequest cancelTaxRequest)
        {
            var jsonRequest = JsonConvert.SerializeObject(cancelTaxRequest);

            // Call the service
            var address = new Uri(svcURL + "tax/cancel");
            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 cancelResponse = new CancelTaxResult();
            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))
                    {
                        cancelResponse = JsonConvert.DeserializeObject<CancelTaxResult>(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    using (var data = response.GetResponseStream())
                    {
                        // Open the stream using a StreamReader for easy access.
                        using (var reader = new StreamReader(data))
                        {
                            cancelResponse = JsonConvert.DeserializeObject<CancelTaxResult>(reader.ReadToEnd());
                        }
                    }
                }
            }

            return cancelResponse;
        }