예제 #1
0
        private TaxCalculatorRequestDto CreateTaxEstimationRequest(double totalItemsPrice, double shippingCosts, BillingAddress addressFrom, DeliveryAddress addressTo)
        {
            var taxRequest = new TaxCalculatorRequestDto()
            {
                TotalBasePrice = totalItemsPrice,
                ShipCost       = shippingCosts
            };

            var stateTo = kenticoLocalization.GetStates().FirstOrDefault(s => s.Id == (addressTo?.State?.Id ?? 0));

            if (addressFrom != null)
            {
                taxRequest.ShipFromCity  = addressFrom.City ?? string.Empty;
                taxRequest.ShipFromState = addressFrom.State ?? string.Empty;
                taxRequest.ShipFromZip   = addressFrom.Zip ?? string.Empty;
            }

            if (addressTo != null)
            {
                taxRequest.ShipToCity  = addressTo.City ?? string.Empty;
                taxRequest.ShipToState = stateTo?.StateCode ?? string.Empty;
                taxRequest.ShipToZip   = addressTo.Zip ?? string.Empty;
            }

            return(taxRequest);
        }
예제 #2
0
        private async Task <decimal> EstimateTotalTax(string serviceEndpoint, TaxCalculatorRequestDto taxRequest)
        {
            if (taxRequest.TotalBasePrice == 0.0d && taxRequest.ShipCost == 0.0d)
            {
                // no need for tax estimation
                return(0.0m);
            }

            var cacheKey     = $"DeliveryPriceEstimationClient|{serviceEndpoint}|{Newtonsoft.Json.JsonConvert.SerializeObject(taxRequest)}";
            var cachedResult = cache.Get(cacheKey);

            if (cachedResult != null)
            {
                return((decimal)cachedResult);
            }

            var response = await taxCalculator.CalculateTax(taxRequest);

            if (response.Success)
            {
                var result = response.Payload;
                cache.Insert(cacheKey, result);
                return(result);
            }
            else
            {
                kenticoLog.LogError("TaxCalculatorClient", $"Call for tax estimation to service URL '{serviceEndpoint}' resulted with error {response.Error?.Message ?? string.Empty}");
                return(0.0m);
            }
        }
예제 #3
0
        public async Task <BaseResponseDto <decimal> > CalculateTax(TaxCalculatorRequestDto request)
        {
            var url = _properties.GetServiceUrl(_serviceUrlSettingKey);

            url = $"{url}/api/taxcalculator";
            return(await Post <decimal>(url, request).ConfigureAwait(false));
        }