示例#1
0
        /// <summary>
        /// Gets tax total
        /// </summary>
        /// <param name="taxTotalRequest">Tax total request</param>
        /// <returns>Tax total</returns>
        public TaxTotalResult GetTaxTotal(TaxTotalRequest taxTotalRequest)
        {
            //cache tax total within the request
            var key = $"Smi.TaxTotal-{taxTotalRequest.UsePaymentMethodAdditionalFee}";

            if (!(_actionContextAccessor.ActionContext.HttpContext.Items.TryGetValue(key, out var result) &&
                  result is TaxTotalResult taxTotalResult))
            {
                //create a transaction
                var transaction = _avalaraTaxManager.CreateTaxTotalTransaction(taxTotalRequest);

                if (transaction?.totalTax == null)
                {
                    return new TaxTotalResult {
                               Errors = new List <string> {
                                   "No response from the service"
                               }
                    }
                }
                ;

                //and get tax details
                taxTotalResult = new TaxTotalResult {
                    TaxTotal = transaction.totalTax.Value
                };
                transaction.summary?
                .Where(summary => summary.rate.HasValue && summary.tax.HasValue)
                .Select(summary => new { Rate = summary.rate.Value * 100, Value = summary.tax.Value })
                .ToList().ForEach(taxRate =>
                {
                    if (taxTotalResult.TaxRates.ContainsKey(taxRate.Rate))
                    {
                        taxTotalResult.TaxRates[taxRate.Rate] += taxRate.Value;
                    }
                    else
                    {
                        taxTotalResult.TaxRates.Add(taxRate.Rate, taxRate.Value);
                    }
                });

                _actionContextAccessor.ActionContext.HttpContext.Items.TryAdd(key, taxTotalResult);
            }

            return(taxTotalResult);
        }