public async void Get_ThrowsOrderTaxNotFoundException_WhenToCountryNotProvided() { //Arrange OrderTax orderTax = null; var serviceMock = new Mock <ITaxCalculatorService>(); serviceMock.Setup(o => o.GetSalesTaxForOrder(It.IsAny <Order>())).ReturnsAsync(orderTax); var appSettingsMock = new Mock <IOptions <AppSettings> >(); var controller = new OrderTaxController(_mockLogger.Object, _mapper, serviceMock.Object, appSettingsMock.Object); // Act + Assert await Assert.ThrowsAsync <OrderTaxNotFoundException>(() => controller.GetOrderTax(new OrderTaxRequestDto())); }
public async Task <ActionResult <OrderTax> > GetTaxes([FromBody] Order order) { try { // TODO: Integrate with ASPNET Core OrderValidator validator = new(); validator.ValidateAndThrow(order); var taxCalculator = _taxCalculatorProvider.GetTaxCalculator(order.TaxCalculatorId); OrderTax tax = await taxCalculator.GetSalesTax(order); tax.TaxCalculatorId = order.TaxCalculatorId; return(Ok(tax)); } catch (Exception vex) { return(BadRequest(vex.Message)); } }
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); }
public void Get_ReturnsOkResult_WhenToCountryProvidedAndValid() { //Arrange var orderTax = new OrderTax(); var serviceMock = new Mock <ITaxCalculatorService>(); serviceMock.Setup(o => o.GetSalesTaxForOrder(It.IsAny <Order>())).ReturnsAsync(orderTax); var appSettingsMock = new Mock <IOptions <AppSettings> >(); // Act var controller = new OrderTaxController(_mockLogger.Object, _mapper, serviceMock.Object, appSettingsMock.Object); var result = controller.GetOrderTax(new OrderTaxRequestDto { ToCountry = "US" }).Result; // Assert Assert.IsType <OkObjectResult>(result); }
public async Task <OrderTax> GetSalesTaxForOrder(Order order) { OrderTax orderTax = null; var taxJarOrderTaxRequestDto = _mapper.Map <TaxJarOrderTaxRequestDto>(order); var buffer = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(taxJarOrderTaxRequestDto)); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var requestUri = BuildUriForApiCall("https://api.taxjar.com/v2", "taxes"); var response = await _httpClient.PostAsync(requestUri, byteContent); if (response.IsSuccessStatusCode) { var responseJson = await response.Content.ReadAsStringAsync(); var taxJarOrderTaxResponseDto = JsonConvert.DeserializeObject <TaxJarOrderTaxResponseDto>(responseJson); orderTax = _mapper.Map <OrderTax>(taxJarOrderTaxResponseDto); } return(orderTax); }
public async Task <IActionResult> SyncOrderTaxes() { var errorList = new List <string>(); try { if (_db.Connection.State == System.Data.ConnectionState.Closed) { await _db.Connection.OpenAsync(); } var query = new OrderQueries(_db); var orders = await query.GetAllOrders(); foreach (var order in orders) { decimal taxGst = 0; if (!string.IsNullOrEmpty(order._pos_sale_tax_GST)) { decimal.TryParse(order._pos_sale_tax_GST, out taxGst); } decimal taxPst = 0; if (!string.IsNullOrEmpty(order._pos_sale_tax_PST)) { decimal.TryParse(order._pos_sale_tax_PST, out taxPst); } var found = await _context.OrderTax.FindAsync(int.Parse(order.id.ToString())); if (found == null) { if (taxPst > 0) { var orderTaxGst = new OrderTax { OrderId = int.Parse(order.id.ToString()), TaxAmount = taxGst, TaxId = 1 }; await _context.OrderTax.AddAsync(orderTaxGst); } if (taxPst > 0) { var orderTaxPst = new OrderTax { OrderId = int.Parse(order.id.ToString()), TaxAmount = taxPst, TaxId = 2 }; await _context.OrderTax.AddAsync(orderTaxPst); } await _context.SaveChangesAsync(); } else { // await _context.SaveChangesAsync(); } } _db.Connection.Close(); } catch (Exception ex) { errorList.Add("order taxes:" + ex.ToString()); } return(Ok(errorList)); }
async Task Controller_returns_the_right_SalesTaxForOrder() { // Arrange var taxCalculatorMock = new Mock <ITaxCalculator>(); var loggerMock = new Mock <ILogger <TaxCalculatorController> >(); OrderTax orderTax = new() { OrderId = "1", CustomerId = "1", TaxCalculatorId = "TAXJAR", AmountToCollect = 1.50m, FreightTaxable = false, OrderTotalAmount = 16.5m, Shipping = 1.5m, TaxableAmount = 15.0m }; taxCalculatorMock.Setup(tx => tx.GetSalesTax(It.IsAny <Order>())).ReturnsAsync(orderTax); var taxCalculatorProviderMock = new Mock <ITaxCalculatorProvider>(); taxCalculatorProviderMock.Setup(tp => tp.GetTaxCalculator(It.IsAny <string>())).Returns(taxCalculatorMock.Object); var controller = new TaxCalculatorController(taxCalculatorProviderMock.Object, loggerMock.Object); // Minimal Order To Pass Validation var order = new Order { Id = "1", CustomerId = "1", TaxCalculatorId = "TAXJAR", Shipping = 1.5m, AddressFrom = new() { CountryCode = "US", StateCode = "FL", Zip = "33029" }, AddressTo = new() { CountryCode = "US", StateCode = "FL", Zip = "33174" }, LineItems = new() { new() { Id = "abc", ProductTaxCode = "1111", Quantity = 10, UnitPrice = 1.5m, Discount = 0 } } }; // Act ActionResult <OrderTax> response = await controller.GetTaxes(order); // Assert OkObjectResult result = response.Result as OkObjectResult; Assert.NotNull(result); OrderTax tax = Assert.IsAssignableFrom <OrderTax>(result.Value); Assert.Equal(orderTax.OrderId, tax.OrderId); Assert.Equal(orderTax.CustomerId, tax.CustomerId); Assert.Equal(orderTax.AmountToCollect, tax.AmountToCollect); Assert.Equal(orderTax.FreightTaxable, tax.FreightTaxable); Assert.Equal(orderTax.OrderTotalAmount, tax.OrderTotalAmount); Assert.Equal(orderTax.Shipping, tax.Shipping); Assert.Equal(orderTax.TaxableAmount, tax.TaxableAmount); }
public async Task Returned_OrderID_equals_originating_Order_Id() { var response = new { tax = new { order_total_amount = 16.5m, shipping = 1.5m, taxable_amount = 15m, amount_to_collect = 1.35m, rate = 0.09m, has_nexus = true, freight_taxable = false, jurisdictions = new { country = "US", state = "CA", county = "LOS ANGELES", city = "LOS ANGELES" } } }; var json = JsonSerializer.Serialize <dynamic>(response); string baseUrl = "https://test.com"; var handler = new Mock <HttpMessageHandler>(); handler .SetupRequest(HttpMethod.Post, $"{baseUrl}/taxes") .ReturnsResponse(json, "application/json"); var client = handler.CreateClient(); client.BaseAddress = new Uri(baseUrl); TaxJarTaxCalculator calculator = new(client); Order order = new Order() { AddressTo = new Location { City = "Los Angeles", CountryCode = "US", StateCode = "CA", Zip = "90002", Street = "1335 E 103rd St" }, AddressFrom = new Location { City = "La Jolla", CountryCode = "US", StateCode = "CA", Zip = "92093", Street = "9500 Gilman Drive" }, CustomerId = "1", Id = "1", Shipping = 1.5m, LineItems = new List <LineItem> { new LineItem { Id = "1", Quantity = 1, UnitPrice = 15m, Discount = 0, ProductTaxCode = "20010" } } }; OrderTax tax = await calculator.GetSalesTax(order); Assert.Equal(order.Id, tax.OrderId); Assert.Equal(order.CustomerId, tax.CustomerId); Assert.Equal(response.tax.amount_to_collect, tax.AmountToCollect); Assert.Equal(response.tax.order_total_amount, tax.OrderTotalAmount); Assert.Equal(response.tax.rate, tax.Rate); Assert.Equal(response.tax.freight_taxable, tax.FreightTaxable); }