Custom Tax Connector

Narendra Dol asked on January 11, 2017 09:50

i need to implement customize tax connector for tax calculation in kentico so i want to know how to write own class for tax module and call external tax service through my tax class

Correct Answer

Roman Hutnyk answered on January 11, 2017 11:29

You'll need to implement your custom tax provider following documentation.

New provider should inherit TaxClassInfoProvider class, but you'll need to override some methods.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 12, 2017 05:07 (last edited on January 12, 2017 05:08)

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.

1 votesVote for this answer Mark as a Correct answer

Narendra Dol answered on January 12, 2017 07:07

Thanks Brenden And Roman for your needful response

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.