Exemplo n.º 1
0
        public TaxRatesInfo TaxRatesForLocation(TaxLocationInfo locationInfo)
        {
            if (string.IsNullOrWhiteSpace(locationInfo.ZipCode))
            {
                throw new ArgumentException("The Location's zipcode is required", nameof(locationInfo.ZipCode));
            }

            using (var client = clientFactory.CreateClient("TaxJar"))
            {
                try
                {
                    var response = client.GetAsync($"rates/{locationInfo.ZipCode}?{locationInfo.ToQueryString()}")
                                   .ConfigureAwait(false)
                                   .GetAwaiter()
                                   .GetResult();
                    if (response.IsSuccessStatusCode)
                    {
                        var result = ParseJsonResponseIntoAnonymousType(response, new { rate = new TaxJarRates() });
                        return(result.rate.ToTaxRatesInfo());
                    }
                    // ToDo: log the error/warning
                    return(null);
                }
                catch (Exception)
                {
                    // logging should be added here
                    // I decided to swallow the exeption instead of bubling it up
                    // this might change depending on how we want to wire the service
                    // to deal with it
                    return(null);
                }
            }
        }
Exemplo n.º 2
0
        public static string ToQueryString(this TaxLocationInfo locationInfo)
        {
            var dictionary = new Dictionary <string, object>
            {
                ["country"] = locationInfo.Country,
                ["state"]   = locationInfo.State,
                ["city"]    = locationInfo.City,
                ["street"]  = locationInfo.Street
            };

            return(dictionary.ToQueryString());
        }
 public void SetUp()
 {
     clientFactory = new Mock <IHttpClientFactory>();
     validOrder    = new OrderInfo
     {
         Amount        = 25m,
         ExemptionType = ExemptionType.Marketplace,
         CustomerId    = "customer1",
         Shipping      = 32m,
         From          = new Address
         {
             Country = "US",
             State   = "NJ",
             ZipCode = "07001"
         },
         To = new Address
         {
             Country = "US",
             State   = "Florida",
             ZipCode = "33155"
         },
         NexusAddresses = new NexusAddress[]
         {
             new NexusAddress
             {
                 Id      = "Nexus1",
                 Country = "US",
                 State   = "FL",
                 ZipCode = "32801"
             }
         },
         LineItems = new LineItem[]
         {
             new LineItem
             {
                 Id             = "Line1",
                 UnitPrice      = 15m,
                 Quantity       = 2,
                 Discount       = 5m,
                 ProductTaxCode = "Code1"
             }
         }
     };
     validLocation = new TaxLocationInfo
     {
         City    = "Miami",
         Country = "US",
         State   = "FL",
         ZipCode = "33193"
     };
 }
Exemplo n.º 4
0
        public void TaxRatesCallsTaxCalculator()
        {
            var mockMethodCalled = false;
            var mock             = new Mock <ITaxCalculator>();

            mock.Setup(calculator => calculator.TaxRatesForLocation(It.IsAny <TaxLocationInfo>())).Callback(() => mockMethodCalled = true);

            var service  = new TaxService(mock.Object);
            var location = new TaxLocationInfo();

            service.TaxRatesForLocation(location);

            Assert.IsTrue(mockMethodCalled);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            BuildConfiguration();
            ConfigureServices();

            var calculator   = ServiceProvider.GetService <ITaxCalculator>();
            var locationInfo = new TaxLocationInfo
            {
                Country = "US",
                City    = "Santa Monica",
                ZipCode = "90404"
            };
            var orderInfo = new OrderInfo
            {
                From = new Address
                {
                    Country = "US",
                    State   = "NJ",
                    ZipCode = "07001"
                },
                To = new Address
                {
                    Country = "US",
                    State   = "NJ",
                    ZipCode = "07446"
                },
                Amount    = 16.5m,
                Shipping  = 1.5m,
                LineItems = new[]
                {
                    new LineItem
                    {
                        Quantity       = 1,
                        UnitPrice      = 15m,
                        ProductTaxCode = "31000"
                    }
                }
            };

            var rates = calculator.TaxRatesForLocation(locationInfo);

            Console.WriteLine(rates.StateRate);

            var taxes = calculator.CalculateTaxesForOrder(orderInfo);

            Console.WriteLine(taxes.AmountToCollect);

            System.Console.ReadLine();
        }
Exemplo n.º 6
0
 public TaxRatesInfo TaxRatesForLocation(TaxLocationInfo locationInfo)
 {
     return(taxCalculator.TaxRatesForLocation(locationInfo));
 }