Here is an example of one I wrote Narendra.
using CMS;
using CMS.Ecommerce;
using CMS.Helpers;
using System;
using System.Data;
using MyCustomNameSpace.Utilities.Vertex.Model;
using MyCustomNameSpace.Utilities.Vertex.Database;
[assembly: RegisterCustomProvider(typeof(CustomTaxInfoProvider))]
/// <summary>
/// Summary description for CustomTaxClassInfoProider
/// </summary>
public class CustomTaxInfoProvider : TaxClassInfoProvider
{
protected override DataSet GetTaxesInternal(ShoppingCartInfo cart)
{
// Create an empty taxes table
DataTable table = GetNewTaxesTable();
DataSet ds = new DataSet();
if (cart.ShoppingCartBillingAddress != null)
{
MyCustomNameSpace.Utilities.Vertex.Model.TaxQuote tq = new MyCustomNameSpace.Utilities.Vertex.Model.TaxQuote();
tq.DocumentDate = DateTime.Now;
Customer c = new Customer();
Seller s = new Seller();
Location l = new Location
{
City = cart.ShoppingCartBillingAddress.AddressCity,
State = cart.ShoppingCartBillingAddress.GetStateCode(),
StreetAddress1 = cart.ShoppingCartBillingAddress.AddressLine1,
StreetAddress2 = cart.ShoppingCartBillingAddress.AddressLine2,
ZipCode = cart.ShoppingCartBillingAddress.AddressZip
};
s.PhysicalOrigin = l;
c.Destination = l;
tq.Seller = s;
tq.Customer = c;
foreach (ShoppingCartItemInfo item in cart.CartItems)
{
LineItem[] lineItems = new LineItem[1];
LineItem lineItem = new LineItem();
Product p = new Product
{
ProductClass = item.SKU.SKUNumber,
};
lineItem.Product = p;
lineItem.UnitPrice = ValidationHelper.GetDecimal(item.UnitPrice, 0);
lineItem.UnitPriceSpecified = true;
lineItem.Quantity = new Measure
{
Value = item.CartItemUnits
};
int skuId = item.SKUID;
lineItems[0] = lineItem;
tq.LineItem = lineItems;
var response = MyCustomNameSpace.Utilities.Vertex.Database.TaxQuote.GetTaxQuoteResponse(tq);
AddTaxRow(table, item.SKUID, String.Format("Ship to {0}", cart.ShoppingCartBillingAddress.GetStateCode()), ValidationHelper.GetDouble(response.TotalTax, 0.0));
}
}
ds.Tables.Add(table);
return ds;
}
#region "Private methods"
/// <summary>
/// Creates an empty taxes table.
/// </summary>
private DataTable GetNewTaxesTable()
{
DataTable table = new DataTable();
// Add required columns
table.Columns.Add("SKUID", typeof(int));
table.Columns.Add("TaxClassDisplayName", typeof(string));
table.Columns.Add("TaxValue", typeof(double));
table.Columns.Add("TaxIsFlat", typeof(bool));
return table;
}
/// <summary>
/// Creates tax row which holds the data of the tax which should be applied to the given SKU.
/// </summary>
/// <param name="taxTable">Tax table the row should be added to.</param>
/// <param name="skuId">SKU ID</param>
/// <param name="taxName">Tax name</param>
/// <param name="taxValue">Tax value</param>
private void AddTaxRow(DataTable taxTable, int skuId, string taxName, double taxValue)
{
DataRow row = taxTable.NewRow();
// Set required columns
row["SKUID"] = skuId;
row["TaxClassDisplayName"] = taxName;
row["TaxValue"] = taxValue;
row["TaxIsFlat"] = true;
taxTable.Rows.Add(row);
}
#endregion
}
Sorry for all the code but it gives an example of what you need for the dynamic tax provider. This was written for v8.2 I believe and it might work in v9. Essentially you need to override GetTaxesInternal()
, create a tax table, add rows to it and return that dataset. This will populate the tax information on your shopping cart. This line here:
var response = MyCustomNameSpace.Utilities.Vertex.Database.TaxQuote.GetTaxQuoteResponse(tq);
is actually going out and doing a web service call to a service to get the real-time tax info based on the info sent in.