예제 #1
0
        /// <summary>
        /// Mapper method to map the GetTaxRates Partner Uri
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string MapGetTaxRatesUri(GetTaxRateRequest request)
        {
            var getTaxRatesUri = $"v2/rates/{request.Zip}";
            var uriSb          = new StringBuilder();

            if (!string.IsNullOrWhiteSpace(request.Street))
            {
                uriSb.Append($"&street{request.Street}");
            }

            if (!string.IsNullOrWhiteSpace(request.City))
            {
                uriSb.Append($"&city{request.City}");
            }

            if (!string.IsNullOrWhiteSpace(request.State))
            {
                uriSb.Append($"&state{request.State}");
            }

            if (!string.IsNullOrWhiteSpace(request.Country))
            {
                uriSb.Append($"&country{request.Country}");
            }

            return(uriSb.Length > 0 ? $"{getTaxRatesUri}?{uriSb.ToString()}" : getTaxRatesUri);
        }
예제 #2
0
 private static void ValidateZipCode5(GetTaxRateRequest request)
 {
     if (string.IsNullOrWhiteSpace(request.ZipCode5))
     {
         throw new Exception($"{nameof(request.ZipCode5)} expected");
     }
 }
예제 #3
0
        public void CallTaxService_GenerateTaxJarCalculator_GetTaxRate_ShortCAZipCode_ReturnsArgumentException()
        {
            //Arrange
            var rateRequest   = new GetTaxRateRequest("V5K", "CA", "Vancouver", null);
            var taxjarService = new TaxService.Services.TaxService(new TaxJar_Calculator("https://api.taxjar.com/v2/"));

            Assert.ThrowsException <System.ArgumentException>(() => taxjarService.GetTaxRate(rateRequest));
        }
        public void GetTaxRate_TaxJar_ShortUSZipCode_ReturnsArgumentException()
        {
            //Arrange
            var rateRequest   = new GetTaxRateRequest("32", "US", "Orlando", null);
            var taxjarService = new TaxJar_Calculator("https://api.taxjar.com/v2/");

            Assert.ThrowsException <System.ArgumentException>(() => taxjarService.GetTaxRate(rateRequest));
        }
예제 #5
0
        public void CallTaxService_GenerateTaxJarCalculator_GetTaxRate_MissingCountryCode_ReturnsArgumentException()
        {
            //Arrange
            var rateRequest   = new GetTaxRateRequest("32821", "", "Orlando", null);
            var taxjarService = new TaxService.Services.TaxService(new TaxJar_Calculator("https://api.taxjar.com/v2/"));

            Assert.ThrowsException <System.ArgumentException>(() => taxjarService.GetTaxRate(rateRequest));
        }
예제 #6
0
        public HttpResponseMessage GetRate([FromUri] GetTaxRateRequest request)
        {
            if (request == null || !request.IsValid)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, worker.GetRate(request.City, request.Date.Value)));
        }
예제 #7
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);
        }
        public void GetTaxRate_TaxJar_EU_ProperBody_ReturnsCombinedRateAsDouble()
        {
            //Arrange
            var    rateRequest          = new GetTaxRateRequest("00150", "FI", "Helsinki", null);
            var    taxjarService        = new TaxJar_Calculator("https://api.taxjar.com/v2/");
            double expectedCombinedRate = 0.24;
            var    taxjarResponse       = taxjarService.GetTaxRate(rateRequest);

            Assert.AreEqual(expectedCombinedRate, taxjarResponse, 0.001, "Tax Rate is different, not an error");
        }
        public void GetTaxRate_TaxJar_WithProperRequestBody_ReturnsCombinedRateAsDouble()
        {
            //Arrange
            var    rateRequest          = new GetTaxRateRequest("32821", "US", "Orlando", null);
            var    taxjarService        = new TaxJar_Calculator("https://api.taxjar.com/v2/");
            double expectedCombinedRate = 0.065;

            var taxjarResponse = taxjarService.GetTaxRate(rateRequest);

            Assert.AreEqual(expectedCombinedRate, taxjarResponse, 0.001, "Tax Rate is different, not an error");
        }
예제 #10
0
        public async Task <Rate> GetTaxRate(GetTaxRateRequest request)
        {
            if (request == null)
            {
                throw new Exception($"{nameof(GetTaxRateRequest)} expected");
            }

            var     url = BuildUrl($"{GetBaseAddress()}{endPoint}/{GetZipFullCode(request)}", request);
            JObject f   = await GetAsync <JObject>(url);

            return(f.ToRate());
        }
예제 #11
0
        public void ValidateGetTaxRateRequest(GetTaxRateRequest getTaxRateRequest)
        {
            if (getTaxRateRequest == null)
            {
                throw new MissingFieldException("MissingParameter.Request");
            }

            if (getTaxRateRequest.Zip == null)
            {
                throw new MissingFieldException("MissingParameter.Zip");
            }
        }
예제 #12
0
        public void CallTaxService_GenerateTaxJarCalculator_GetTaxRate_ReturnsDouble()
        {
            //Arrange
            var rateRequest = new GetTaxRateRequest("32821", "US", "Orlando", null);

            var taxjarService = new TaxService.Services.TaxService(new TaxJar_Calculator("https://api.taxjar.com/v2/"));

            double expectedCombinedRate = 0.065;

            var taxjarResponse = taxjarService.GetTaxRate(rateRequest);

            Assert.AreEqual(expectedCombinedRate, taxjarResponse, 0.0);
        }
예제 #13
0
        /// <summary>
        ///  This method is used to get the sales tax rates for a given location.
        ///  Calls TaxRepository.GetTaxRateResponse, which in turn calls TaxJar API to fetch the required details
        /// </summary>
        /// <param name="getTaxRateRequest"></param>
        /// <returns>GetTaxRateResponse</returns>
        public async Task <GetTaxRateResponse> GetTaxRates(GetTaxRateRequest getTaxRateRequest)
        {
            // Validate Request Data
            _taxProviderValidator.ValidateGetTaxRateRequest(getTaxRateRequest);

            // Map Downstream Api request
            var getTaxRatesPartnerUri = _taxProviderMapper.MapGetTaxRatesUri(getTaxRateRequest);

            // Process Request
            var response = await _taxRepository.GetTaxRateResponse(getTaxRatesPartnerUri);

            // Return Response
            return(response);
        }
예제 #14
0
        public async Task <IActionResult> GetAsync([FromRoute] GetTaxRateRequest request)
        {
            try
            {
                var response = await _taxRateService.GetTaxRate(request);

                return(ProcessResponse(response));
            }
            catch  //(Exception ex)
            {
                //Add loggin functionality here to log the stack trace for support purposes  ex

                return(StatusCode(StatusCodes.Status500InternalServerError, "Server Error"));
            }
        }
        public async Task TaxJarCalculatorService_GetTaxRate_Should_Response_Unssucess_When_GetTaxRateRequest_Is_Not_Null()
        {
            //Arrange
            bool expectedSuccess      = true;
            GetTaxRateRequest request = new GetTaxRateRequest();

            _taxCalculatorProxy.Setup(x => x.GetTaxRate(It.IsAny <GetTaxRateRequest>())).Returns(async() =>
            {
                var rate = _fixture.Create <Rate>();
                return(await Task.FromResult(rate));
            }
                                                                                                 );
            //Act
            var actualResult = await _taxJarCalculatorService.GetTaxRate(request);

            //Assert
            Assert.AreEqual(expectedSuccess, actualResult.Success);
        }
예제 #16
0
 public double GetTaxRate(GetTaxRateRequest request)
 {
     return(calculator.GetTaxRate(request));
 }
예제 #17
0
        public async Task <IActionResult> GetTaxRates([FromQuery] GetTaxRateRequest request)
        {
            var getTaxRateResponse = await _taxProvider.GetTaxRates(request);

            return(Ok(getTaxRateResponse));
        }
예제 #18
0
 internal string BuildUrl(string url, GetTaxRateRequest request)
 {
     url = AddCountryParameter(url, request.Country);
     return(AddCityParameter(url, request.City));
 }
예제 #19
0
        internal string GetZipFullCode(GetTaxRateRequest request)
        {
            ValidateZipCode5(request);

            return(!string.IsNullOrWhiteSpace(request.ZipCode4) ? $"{request.ZipCode5}-{request.ZipCode4}" : request.ZipCode5);
        }