Ejemplo n.º 1
0
        public ValidateResult Validate(Address address)
        {
            // Convert input address data to query string
            string querystring = string.Empty;
            querystring += (address.Line1 == null) ? string.Empty : "Line1=" + address.Line1.Replace(" ", "+");
            querystring += (address.Line2 == null) ? string.Empty : "&Line2=" + address.Line2.Replace(" ", "+");
            querystring += (address.Line3 == null) ? string.Empty : "&Line3=" + address.Line3.Replace(" ", "+");
            querystring += (address.City == null) ? string.Empty : "&City=" + address.City.Replace(" ", "+");
            querystring += (address.Region == null) ? string.Empty : "&Region=" + address.Region.Replace(" ", "+");
            querystring += (address.PostalCode == null) ? string.Empty : "&PostalCode=" + address.PostalCode.Replace(" ", "+");
            querystring += (address.Country == null) ? string.Empty : "&Country=" + address.Country.Replace(" ", "+");

            // Call the service
            Uri webAddress = new Uri(svcURL + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(webAddress) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            ValidateResult result = new ValidateResult();
            try
            {
                WebResponse response = request.GetResponse();
                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                address = result.Address; // If the address was validated, take the validated address.
            }
            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 ValidateResult();
                    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 = (ValidateResult)r.Deserialize(stream); // Inelegant, but the deserializer only takes streams, and we already read ours out.
                }
            }

            return result;
        }
Ejemplo n.º 2
0
        public ValidateResult Validate(Address address)
        {
            // Convert input address data to query string
            string querystring = string.Empty;
            querystring += (address.Line1 == null) ? string.Empty : "Line1=" + address.Line1.Replace(" ", "+");
            querystring += (address.Line2 == null) ? string.Empty : "&Line2=" + address.Line2.Replace(" ", "+");
            querystring += (address.Line3 == null) ? string.Empty : "&Line3=" + address.Line3.Replace(" ", "+");
            querystring += (address.City == null) ? string.Empty : "&City=" + address.City.Replace(" ", "+");
            querystring += (address.Region == null) ? string.Empty : "&Region=" + address.Region.Replace(" ", "+");
            querystring += (address.PostalCode == null) ? string.Empty : "&PostalCode=" + address.PostalCode.Replace(" ", "+");
            querystring += (address.Country == null) ? string.Empty : "&Country=" + address.Country.Replace(" ", "+");

            // Call the service
            var webAddress = new Uri(svcURL + "address/validate?" + querystring);
            var request = WebRequest.Create(webAddress) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            var result = new ValidateResult();

            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<ValidateResult>(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                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<ValidateResult>(reader.ReadToEnd());
                        }
                    }
                }
            }
            
            return result;
        }
Ejemplo n.º 3
0
        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, COMPANYCODE, WEBADDR);                                                 //Validates a given address.
                Console.WriteLine("ValidateAddress test result: " + addressResult.ResultCode.ToString() + ", \nAddress="
                                  + 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.
            }
            catch (Exception ex)
            { Console.WriteLine("ValidateAddress test: 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.WriteLine("GetTax test result: " + calcresult.ResultCode.ToString() + ", " +
                                  "TotalTax=" + calcresult.TotalTax.ToString());                   //At this point, you would write the tax calculated to your database and display to the user.
            }
            catch (Exception ex)
            { Console.WriteLine("GetTax test: 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.WriteLine("CancelTax test result: " + cancelresult.ResultCode.ToString() + ", Document Voided");
                //Let's display the result of the cancellation. At this point, you would allow your system to complete the delete/void.
            }
            catch (Exception ex)
            { Console.WriteLine("CancelTax test: Exception " + ex.Message); }

            Console.WriteLine("Done");
        }
        public static ValidateResult Validate(Address addr, string AcctNum, string LicKey, string CompanyCode, string webaddr)
        {
            //Convert input address data to query string
            string querystring = "";
            if (addr.Line1 != null) { querystring = querystring + "Line1=" + addr.Line1.Replace(" ", "+"); }
            if (addr.Line2 != null) { querystring = querystring + "&Line2=" + addr.Line2.Replace(" ", "+"); }
            if (addr.Line3 != null) { querystring = querystring + "&Line3=" + addr.Line3.Replace(" ", "+"); }
            if (addr.City != null) { querystring = querystring + "&City=" + addr.City.Replace(" ", "+"); }
            if (addr.Region != null) { querystring = querystring + "&Region=" + addr.Region.Replace(" ", "+"); }
            if (addr.PostalCode != null) { querystring = querystring + "&PostalCode=" + addr.PostalCode.Replace(" ", "+"); }
            if (addr.Country != null) { querystring = querystring + "&Country=" + addr.Country.Replace(" ", "+"); }

            //Call the service
            Uri address = new Uri(webaddr + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(AcctNum + ":" + LicKey)));
            request.Method = "GET";

            ValidateResult result = new ValidateResult();
            try
            {
                WebResponse response = request.GetResponse();
                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                addr = result.Address; //If the address was validated, take the validated address.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " on address object");
            }
            Console.WriteLine(result.ResultCode.ToString());
            return result;
        }
        public static ValidateResult Validate(Address addr, string AcctNum, string LicKey, string webaddr)
        {
            //Convert input address data to query string
            string querystring = "";

            if (addr.Line1 != null)
            {
                querystring = querystring + "Line1=" + addr.Line1.Replace(" ", "+");
            }
            if (addr.Line2 != null)
            {
                querystring = querystring + "&Line2=" + addr.Line2.Replace(" ", "+");
            }
            if (addr.Line3 != null)
            {
                querystring = querystring + "&Line3=" + addr.Line3.Replace(" ", "+");
            }
            if (addr.City != null)
            {
                querystring = querystring + "&City=" + addr.City.Replace(" ", "+");
            }
            if (addr.Region != null)
            {
                querystring = querystring + "&Region=" + addr.Region.Replace(" ", "+");
            }
            if (addr.PostalCode != null)
            {
                querystring = querystring + "&PostalCode=" + addr.PostalCode.Replace(" ", "+");
            }
            if (addr.Country != null)
            {
                querystring = querystring + "&Country=" + addr.Country.Replace(" ", "+");
            }


            //Call the service
            Uri            address = new Uri(webaddr + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

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

            ValidateResult result = new ValidateResult();

            try
            {
                WebResponse   response = request.GetResponse();
                XmlSerializer r        = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                addr   = result.Address; //If the address was validated, take the validated address.
            }
            catch (WebException ex)
            {
                Stream       responseStream = ((HttpWebResponse)ex.Response).GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                String       responseString = reader.ReadToEnd();
                if (responseString.StartsWith("{") || responseString.StartsWith("["))  //The service returns some error messages in JSON for authentication/unhandled errors.
                {
                    result            = new ValidateResult();
                    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 = (ValidateResult)r.Deserialize(stream); //Inelegant, but the deserializer only takes streams, and we already read ours out.
                }
            }
            return(result);
        }