public WebStoreCart CalculateCartTax(WebStoreCart cart) { // TODO: Make this real. Mocking for now foreach (var item in cart.CartItems) { cart.TaxAmount += Math.Round(item.ExtendedPrice * 0.07m, 2); // just assuming 7% tax for everything for now } // update the cart total with the tax amount cart.Total += Math.Round(cart.TaxAmount, 2); return(cart); }
public void TaxCalculationEngine_CalculateCartTax() { var address = new Address() { First = "Marvin", Last = "Waller", Addr1 = "4928 Commerce Boulevard", State = "Nebraska", City = "Lincoln", Postal = "68508", }; WebStoreCart wsCart = new WebStoreCart() { Id = new Guid("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), SubTotal = 15.00m, Total = 15.00m, BillingAddress = address, ShippingAddress = address, CartItems = new WebStoreCartItem[] { new WebStoreCartItem() { ProductId = 1, ProductName = "Test Product 1", UnitPrice = 5.99m, Quantity = 2, ExtendedPrice = 10.00m }, new WebStoreCartItem() { ProductId = 2, ProductName = "Test Product 2", UnitPrice = 5.99m, Quantity = 1, ExtendedPrice = 5.99m }, } }; var taxCalculationEngine = new EngineFactory(new AmbientContext(), null, null).CreateEngine <ITaxCalculationEngine>(); var result = taxCalculationEngine.CalculateCartTax(wsCart); Assert.AreEqual(15.00m, result.SubTotal); Assert.AreEqual(1.12m, result.TaxAmount); Assert.AreEqual(16.12m, result.Total); }
public WebStoreCart GenerateCartPricing(Cart cart) { WebStoreCart result = new WebStoreCart(); // map the cart input to a webstore cart DTOMapper.Map(cart, result); // loop through all cart items, get the current price and apply to the cart item ICatalogAccessor catAccessor = AccessorFactory.CreateAccessor <ICatalogAccessor>(); foreach (WebStoreCartItem item in result.CartItems) { if (item.Quantity > 0) { // get the current unit price and update the cart item var product = catAccessor.FindProduct(item.ProductId); if (product != null) { decimal unitPrice = product.Price; decimal extendedPrice = Math.Round(unitPrice * item.Quantity, 2); // update the web store cart item.UnitPrice = unitPrice; item.ExtendedPrice = extendedPrice; // add the amount to the subtotal result.SubTotal += Math.Round(extendedPrice, 2); } else { Logger.Error("Invalid Product Id"); throw new ArgumentException("Invalid Product Id"); } } else { Logger.Error("Invalid item quantity"); throw new ArgumentException("Invalid item quantity"); } } // set the cart total to the subtotal result.Total = result.SubTotal; return(result); }
public WebStoreCart CalculateCartTax(WebStoreCart cart) { if (cart != null && cart.BillingAddress != null && !string.IsNullOrWhiteSpace(cart.BillingAddress.Postal)) { var taxRate = AccessorFactory.CreateAccessor<ITaxRateAccessor>() .Rate(cart.BillingAddress); foreach (var item in cart.CartItems) { cart.TaxAmount += Math.Round(item.ExtendedPrice * taxRate, 2); } // update the cart total with the tax amount cart.Total += Math.Round(cart.TaxAmount, 2); } return cart; }