public TaxCloud() { ValidateAppConfigs(); LoadConfiguration(); tc = new net.taxcloud.api.TaxCloud(); refOrigin = ConvertAddress(GetOriginAddress()); }
public decimal GetTaxRate(Customer customer, CartItemCollection cartItems, IEnumerable <OrderOption> orderOptions) { if (!customer.HasAtLeastOneAddress() || !cartItems.Any()) { return(0m); } // Return the cached value if it's present and still valid string lastCartHash = HttpContext.Current.Items["TaxCloud.CartHash"] as string; decimal?lastTaxAmount = HttpContext.Current.Items["TaxCloud.TaxAmount"] as decimal?; string currentCartHash = GetCartHash(cartItems, customer, orderOptions); if (lastTaxAmount != null && currentCartHash == lastCartHash) { return(lastTaxAmount.Value); } // Create line items for all cart items and shipping selections decimal taxAmount = 0M; List <CouponObject> CouponList = cartItems.CouponList; List <QDObject> QuantityDiscountList = cartItems.QuantityDiscountList; if (Shipping.GetDistinctShippingAddressIDs(cartItems).Count == 1) { IEnumerable <net.taxcloud.api.CartItem> refCartitems = ConvertCartItems(cartItems, customer); net.taxcloud.api.Address destAddress = ConvertAddress(customer.PrimaryShippingAddress); refCartitems = refCartitems.Concat(CreateCartShippingLineItem(customer, cartItems, orderOptions)).Concat(CreateOrderOptionLineItems(orderOptions)); taxAmount = lookupTaxRate(customer.CustomerID.ToString(), refCartitems.ToArray(), refOrigin, destAddress); } else { List <int> shipAddresses = Shipping.GetDistinctShippingAddressIDs(cartItems); foreach (int _addressID in shipAddresses) { net.taxcloud.api.Address destAddress = ConvertAddress(_addressID); IEnumerable <CartItem> tmpcic = cartItems.Where(r => r.ShippingAddressID == _addressID); IEnumerable <net.taxcloud.api.CartItem> refCartitems = ConvertCartItems(tmpcic, customer, CouponList, QuantityDiscountList); refCartitems = refCartitems.Concat(CreateCartShippingLineItem(customer, tmpcic, orderOptions)); if (_addressID == customer.PrimaryShippingAddressID) { refCartitems = refCartitems.Concat(CreateOrderOptionLineItems(orderOptions)); } taxAmount += lookupTaxRate(customer.CustomerID.ToString(), refCartitems.ToArray(), refOrigin, destAddress); } } //Cache the tax amount HttpContext.Current.Items["TaxCloud.CartHash"] = currentCartHash; HttpContext.Current.Items["TaxCloud.TaxAmount"] = taxAmount; return(taxAmount); }
public void OrderPlaced(Order order) { //string cartId = GetCartID(order.CustomerID.ToString(), refDest.Zip5, refItems.First().Index); string _cartID = string.Empty; List <int> shipAddresses = Shipping.GetDistinctShippingAddressIDs(order.CartItems); if (shipAddresses.Count == 1) { _cartID = GetCartID(order.CustomerID.ToString(), order.ShippingAddress.m_Zip, order.CartItems.First().ShoppingCartRecordID); AuthorizedRsp response = tc.Authorized(ApiLoginID, ApiKey, order.CustomerID.ToString(), _cartID, order.OrderNumber.ToString(), DateTime.Now); if (response.ResponseType != MessageType.OK) { string errormsg = String.Empty; foreach (ResponseMessage message in response.Messages) { errormsg += string.Format("Purchase could not be Authorized({0}-{1})", message.ResponseType.ToString(), message.Message); } throw new Exception(errormsg); } } else { Customer customer = new Customer(order.CustomerID); foreach (int _addressID in shipAddresses) { net.taxcloud.api.Address destAddress = ConvertAddress(_addressID); IEnumerable <CartItem> tmpcic = order.CartItems.Where(r => r.ShippingAddressID == _addressID); Address _address = new Address(); _address.LoadFromDB(_addressID); _cartID = GetCartID(order.CustomerID.ToString(), _address.Zip, tmpcic.First().ShoppingCartRecordID); AuthorizedRsp response = tc.Authorized(ApiLoginID, ApiKey, order.CustomerID.ToString(), _cartID, GetMultipleShippingAddressOrderNumber(order.OrderNumber, _addressID), DateTime.Now); if (response.ResponseType != MessageType.OK) { string errormsg = String.Empty; foreach (ResponseMessage message in response.Messages) { errormsg += string.Format("Purchase could not be Authorized({0}-{1})", message.ResponseType.ToString(), message.Message); } throw new Exception(errormsg); } } } }
private decimal lookupTaxRate(string custID, net.taxcloud.api.CartItem[] refItems, net.taxcloud.api.Address refOrigin, net.taxcloud.api.Address refDest) { double taxRate = 0.0f; ExemptionCertificate exemptCert = null; string _cartID = GetCartID(custID, refDest.Zip5, refItems.First().Index); using (SqlConnection conn = DB.dbConn()) { conn.Open(); using (IDataReader rs = DB.GetRS("select Top 1 certificateID from ShoppingCart where CustomerID=" + custID, conn)) { if (rs.Read() && DB.RSField(rs, "certificateID") != "") { exemptCert = new ExemptionCertificate(); exemptCert.CertificateID = DB.RSField(rs, "certificateID"); } } } LookupRsp response = tc.Lookup(ApiLoginID, ApiKey, custID, _cartID, refItems, refOrigin, refDest, DeliveredBySeller, exemptCert); if (response.ResponseType == MessageType.Error) { string errormsg = String.Empty; foreach (ResponseMessage message in response.Messages) { errormsg += string.Format("TaxCloudError:{0}-{1}", message.ResponseType.ToString(), message.Message); } throw new Exception(errormsg); } else { foreach (CartItemResponse cir in response.CartItemsResponse) { taxRate += Math.Round(cir.TaxAmount, 2); } } return((decimal)taxRate); }