Example #1
0
        public void TaxRatesServiceJSON(string zip, double exprate)
        {
            var expectedrate = (decimal)exprate;

            // Test Calculator JSON
            var taxcalc = new TaxCalculator.Taxjar.CalculatorAPI() as ITaxService;
            var result  = taxcalc.GetTaxRate(zip);

            result.Wait();
            var salestaxcalc = result.Result;
            var diff         = salestaxcalc - expectedrate;

            Assert.IsTrue(Math.Abs(diff) < 0.01m);

            // Test Calculator raw JSON
            taxcalc = new TaxCalculator.Taxjar.CalculatorJSON() as ITaxService;
            result  = taxcalc.GetTaxRate(zip);
            result.Wait();
            var salestaxraw = result.Result;

            diff = salestaxraw - expectedrate;
            Assert.IsTrue(Math.Abs(diff) < 0.01m);

            // pass raw json caculator and test as TaxService
            var taxserv = new Services.TaxService(taxcalc);

            result = taxserv.GetTaxRate(zip);
            result.Wait();
            var salestaxserv = result.Result;

            diff = salestaxserv - expectedrate;
            Assert.IsTrue(Math.Abs(diff) < 0.01m);
        }
Example #2
0
        public HttpResponseMessage GetTaxRate([FromBody] GetTaxRateRequest request)
        {
            //Prepare the Response
            var    response = Request.CreateResponse();
            string result   = "";

            Services.TaxService taxService;

            try
            {
                //Define the TaxService class;
                //TODO: At some other time, we should define what consumer is calling this function to decide on
                //What to do with deciding on a Calculator to use
                taxService = new Services.TaxService(new TaxJar_Calculator());

                //Filter Inputs on the top; Assuming that any calculator would need this information.
                if (request.ZipCode.IsEmpty())
                {
                    throw new ArgumentException("Zip Code is Required", "ZipCode");
                }
                if (request.Country.IsEmpty() || request.Country.Length < 2)
                {
                    throw new ArgumentException("Country Code requires two characters", "Country");
                }

                //Call TaxService to get the Calculator's TaxRate Function.
                var objResult = taxService.GetTaxRate(request);
                //Serialize the result to be sent over HTTP and return a 200
                result = JsonConvert.SerializeObject(objResult);
                response.StatusCode = HttpStatusCode.OK;
            }
            catch (ArgumentException ex)
            {
                //Return 400 level errors here if any are caught
                if (ex.ParamName == "Country")
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "Country code requires two characters"));
                }
                if (ex.ParamName == "ZipCode")
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "Zip Code is Required"));
                }
                if (ex.ParamName == "US ZipCode")
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "US Zip Code Must Be at least 3 characters long"));
                }
                if (ex.ParamName == "CA ZipCode")
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "CA Zip Code Must Be 6 characters long"));
                }
            }

            //Make sure the response is formatted properly to be sent over HTTP correctly.
            response.Content = new StringContent(result, Encoding.UTF8, "application/json");
            return(response);
        }