/// <summary> /// This function is used purely to check the inputs for each given function, to ensure that the request bodies are built properly. /// </summary> /// <param name="rateRequest"></param> /// <param name="calculateRequest"></param> /// <returns></returns> private string CheckForValidInput(GetTaxRateRequest rateRequest = null, CalculateTaxRequest calculateRequest = null) { if (rateRequest != null) { switch (rateRequest.Country) { case "US": if (rateRequest.ZipCode.Length < 3) { return("US ZipCode Length"); } break; case "CA": if (rateRequest.ZipCode.Length < 5) { return("CA ZipCode Length"); } break; case "": case null: return("No Country Code"); } } if (calculateRequest != null) { //If Country is not correct, throw an error; no need to check the switch for that if (calculateRequest.from_country.Length < 2 || calculateRequest.to_country.Length < 2) { return("Short Country Code"); } if (calculateRequest.from_state.Length < 2 || calculateRequest.to_state.Length < 2) { return("Short State Code"); } if (calculateRequest.from_country == "US" && calculateRequest.from_zip.Length < 3 || calculateRequest.to_zip.Length < 3) { return("US ZipCode Length"); } if (calculateRequest.from_country == "CA" && calculateRequest.from_zip.Length < 5 || calculateRequest.from_zip.Length < 5) { return("CA ZipCode Length"); } if (calculateRequest.line_items.Count < 1) { return("No Line Items"); } } return("OK"); }
/// <summary> /// POSTS a request to the TaxJar API to return all applicable taxable information on a given order. /// This will return a double value with the total amount of tax to collect /// </summary> /// <param name="request"></param> /// <returns></returns> public double CalculateTaxForOrder(CalculateTaxRequest request) { //Filter Inputs string errorText = CheckForValidInput(null, request); string responseText; double amountToCollect = 0.0; if (errorText == "OK") { try { //Finish Url TaxJarUrl.Append("taxes"); //Create HTTP Web Request var httpWebRequest = WebRequest.CreateHttp(TaxJarUrl.ToString()); httpWebRequest.Method = "POST"; httpWebRequest.Timeout = 300000; //Build Custom Auth header httpWebRequest.Headers["Authorization"] = $"Token token=\"{apiToken}\""; httpWebRequest.ContentType = "application/json"; //pass JSON body for POST using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { //Serialize the request with the Serializer settings that Ignore null / default values streamWriter.Write(JsonConvert.SerializeObject(request, settings)); } //Get Response from POST var response = (HttpWebResponse)httpWebRequest.GetResponse(); var stream = response.GetResponseStream(); if (stream != null) { using (var streamReader = new StreamReader(stream)) { responseText = streamReader.ReadToEnd(); } //Parse object for easy access var contentObject = JObject.Parse(responseText); //Grab Amount to collect value from parsed object amountToCollect = Convert.ToDouble(contentObject["tax"]["amount_to_collect"]); } } catch (WebException wex) { //Throw web exceptions to the Controller throw wex; } } else { switch (errorText) { case "Short Country Code": throw new ArgumentException("Country Code must be two characters", "Country"); case "Short State Code": throw new ArgumentException("State Code must be two characters", "State"); case "US ZipCode Length": throw new ArgumentException("US Zipcodes must be at least 3 characters in length", "US ZipCode"); case "CA ZipCode Length": throw new ArgumentException("CA Zipcodes must be at least 5 character in length", "CA ZipCode"); case "No Line Items": throw new ArgumentException("Order Requests must have at least one line item", "Line_Items"); } } //return Amount_to_Collect return(amountToCollect); }