Exemple #1
0
        public async Task <OrderTax> GetSalesTax(Order order)
        {
            TaxJarOrder taxJarOrder = TaxJarOrder.MapFromOrder(order);

            TarJarOrderValidator validator = new();

            validator.ValidateAndThrow(taxJarOrder);

            var response = await _httpClient.PostAsJsonAsync <TaxJarOrder>("taxes", taxJarOrder);

            response.EnsureSuccessStatusCode();

            TaxJarTaxResponse tjTaxResponse = await response.Content.ReadFromJsonAsync <TaxJarTaxResponse>();

            OrderTax orderTax = tjTaxResponse.Tax.MapToOrderTax();

            orderTax.OrderId    = order.Id;
            orderTax.CustomerId = order.CustomerId;
            return(orderTax);
        }
Exemple #2
0
 public void Setup()
 {
     validOrder = new TaxJarOrder
     {
         from_country    = "US",
         from_state      = "Florida",
         from_city       = "Miami",
         from_zip        = "33193",
         to_country      = "US",
         to_state        = "CA",
         to_city         = "San Francisco",
         to_zip          = "94111",
         amount          = 100m,
         shipping        = 25m,
         customer_id     = "customer",
         exemption_type  = "non_exempt",
         nexus_addresses = new List <TaxJarNexusAddress> {
             new TaxJarNexusAddress
             {
                 id      = "nexus1",
                 country = "US",
                 state   = "FL",
                 city    = "Miami",
                 zip     = "33155"
             }
         },
         line_items = new List <TaxJarLineItem>()
         {
             new TaxJarLineItem
             {
                 id               = "line1",
                 discount         = 0m,
                 product_tax_code = "P1TC",
                 unit_price       = 22m,
                 quantity         = 3
             }
         }
     };
 }
        public OrderTaxInfo CalculateTaxesForOrder(OrderInfo orderInfo)
        {
            var taxJarOrder      = new TaxJarOrder(orderInfo);
            var validationErrors = taxJarOrder.ValidationErrors();

            if (validationErrors.Any())
            {
                var aggregatedMessage = string.Join(Environment.NewLine, validationErrors);
                throw new ArgumentException("There is something wrong with the order data", nameof(orderInfo), new Exception(aggregatedMessage));
            }

            using (var client = clientFactory.CreateClient("TaxJar"))
            {
                var content = new StringContent(JsonConvert.SerializeObject(taxJarOrder), Encoding.UTF8, "application/json");
                try
                {
                    var response = client.PostAsync("taxes", content)
                                   .ConfigureAwait(false)
                                   .GetAwaiter()
                                   .GetResult();
                    if (response.IsSuccessStatusCode)
                    {
                        var result = ParseJsonResponseIntoAnonymousType(response, new { tax = new TaxJarOrderTax() });
                        return(result.tax.ToOrderTaxInfo());
                    }
                    // log the response error
                    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);
                }
            }
        }