Difference between revisions of "Integrating A Tax Provider"

From AbleCommerce Wiki
Jump to: navigation, search
m (Deployment)
(remove definitions for itaxprovider and taxproviderbase, replace with a skeleton code class suitable for customer extension)
Line 1: Line 1:
==ITaxProvider==
+
== Sample Provider ==
To integrate a tax provider with Ablecommerce 7 you need to implement '''CommerceBuilder.Taxes.Providers.ITaxProvider''' interface. This interface defines how tax providers interact with Ablecommerce.
+
To integrate a tax provider with Ablecommerce 7 you must implement '''CommerceBuilder.Taxes.Providers.ITaxProvider''' interface. This interface defines how tax providers interact with Ablecommerce.  This should be done by extending the TaxProviderBase class.  Below is a sample code to get you started.
  
In version 7.0 final, '''ITaxProvider''' interface is defined as follows
+
<code lang="csharp">
<code>
+
using System;
<pre>
+
using System.Collections.Generic;
    /// <summary>
+
using System.Text;
    /// Interface that must be implemented by tax providers
+
using CommerceBuilder.Common;
    /// </summary>
+
using CommerceBuilder.Orders;
     public interface ITaxProvider
+
using CommerceBuilder.Taxes.Providers;
 +
 
 +
namespace SampleTaxProvider
 +
{
 +
     public class TaxProvider : TaxProviderBase
 
     {
 
     {
         /// <summary>
+
         #region Properties
        /// The gateway Id of the tax provider. This is passed at the time of initialization.
+
        /// </summary>
+
        int TaxGatewayId { get; }
+
 
+
 
         /// <summary>
 
         /// <summary>
 
         /// The name of the tax provider implementation
 
         /// The name of the tax provider implementation
 
         /// </summary>
 
         /// </summary>
         string Name { get; }
+
         public override string Name
 +
        {
 +
            get { throw new Exception("The method or operation is not implemented."); }
 +
        }
  
 
         /// <summary>
 
         /// <summary>
 
         /// The version of the tax provider implementation
 
         /// The version of the tax provider implementation
 
         /// </summary>
 
         /// </summary>
         string Version { get; }
+
         public override string Version
 +
        {
 +
            get { throw new Exception("The method or operation is not implemented."); }
 +
        }
  
 
         /// <summary>
 
         /// <summary>
 
         /// Is the tax provider activated or not
 
         /// Is the tax provider activated or not
 
         /// </summary>
 
         /// </summary>
         bool Activated { get; }
+
         public override bool Activated
 
+
        /// <summary>
+
        /// Initialize the tax provider with the given configuration data. Called by AC at the time of initialization.
+
        /// </summary>
+
        /// <param name="taxGatewayId">The tax gateway id</param>
+
        /// <param name="configurationData">Configuration data in the form of name value paris</param>
+
        void Initialize(int taxGatewayId, Dictionary<String, String> configurationData);
+
 
+
        /// <summary>
+
        /// Get the configuration data in the form on name value pairs
+
        /// </summary>
+
        /// <returns>The configuration data as name value pairs</returns>
+
        Dictionary<string, string> GetConfigData();
+
 
+
        /// <summary>
+
        /// Cancel the taxes by this provider for the given basket
+
        /// </summary>
+
        /// <param name="basket">The basket for which to cancel the taxes</param>
+
        void Cancel(Basket basket);
+
 
+
        /// <summary>
+
        /// Commit the taxes by this provider for the given order
+
        /// </summary>
+
        /// <param name="order">The order for which to commit the taxes</param>
+
        void Commit(Order order);
+
 
+
        /// <summary>
+
        /// Calculate the taxes using this provider for the given basket
+
        /// </summary>
+
        /// <param name="basket">The basket to calculate tax for</param>
+
        /// <returns>Total tax calculated</returns>
+
        LSDecimal Calculate(Basket basket);
+
    }
+
 
+
</pre>
+
</code>
+
 
+
 
+
== TaxProviderBase ==
+
To implement a tax provider you can either implement the '''ITaxProvider''' interface directly or you can extend the '''CommerceBuilder.Taxes.Providers.TaxProviderBase''' class. For the sake of easy upgrades in future it is highly recommended that you extend '''TaxProviderBase''' class instead of directly implementing the '''ITaxProvider''' interface.
+
 
+
The '''TaxProviderBase''' class is defined as follows
+
<code>
+
<pre>
+
    /// <summary>
+
    /// Base class for tax provider implementations
+
    /// </summary>
+
    public abstract class TaxProviderBase : ITaxProvider
+
    {
+
 
+
        private int _TaxGatewayId;
+
 
+
        /// <summary>
+
        /// The gateway Id of the tax provider. This is passed at the time of initialization.
+
        /// </summary>
+
        public int TaxGatewayId
+
 
         {
 
         {
             get { return _TaxGatewayId; }
+
             get { throw new Exception("The method or operation is not implemented."); }
            set { _TaxGatewayId = value; }
+
 
         }
 
         }
 
+
         #endregion
         /// <summary>
+
        /// The name of the tax provider implementation
+
        /// </summary>
+
        public abstract string Name { get; }
+
       
+
        /// <summary>
+
        /// The version of the tax provider implementation
+
        /// </summary>
+
        public abstract string Version { get; }
+
 
+
        /// <summary>
+
        /// Is the tax provider activated or not
+
        /// </summary>
+
        public abstract bool Activated { get;}
+
  
 
         /// <summary>
 
         /// <summary>
Line 110: Line 45:
 
         /// <param name="taxGatewayId">The tax gateway id</param>
 
         /// <param name="taxGatewayId">The tax gateway id</param>
 
         /// <param name="configurationData">Configuration data in the form of name value paris</param>
 
         /// <param name="configurationData">Configuration data in the form of name value paris</param>
         public virtual void Initialize(int taxGatewayId, Dictionary<string, string> configurationData)
+
         public override void Initialize(int taxGatewayId, Dictionary<string, string> configurationData)
 
         {
 
         {
             this._TaxGatewayId = taxGatewayId;
+
             base.Initialize(taxGatewayId, configurationData);
 
         }
 
         }
  
 
         /// <summary>
 
         /// <summary>
         /// Get the configuration data in the form on name value pairs
+
         /// Calculate the taxes using this provider for the given basket
 
         /// </summary>
 
         /// </summary>
         /// <returns>The configuration data as name value pairs</returns>
+
         /// <param name="basket">The basket to calculate tax for</param>
         public virtual Dictionary<string, string> GetConfigData()
+
         /// <returns>Total tax calculated</returns>
 +
        public override LSDecimal Calculate(Basket basket)
 
         {
 
         {
             Dictionary<string, string> configData = new Dictionary<string, string>();
+
             throw new Exception("The method or operation is not implemented.");
            return configData;
+
 
         }
 
         }
  
Line 129: Line 64:
 
         /// </summary>
 
         /// </summary>
 
         /// <param name="basket">The basket for which to cancel the taxes</param>
 
         /// <param name="basket">The basket for which to cancel the taxes</param>
         public abstract void Cancel(Basket basket);
+
         public override void Cancel(Basket basket)
 +
        {
 +
            throw new Exception("The method or operation is not implemented.");
 +
        }
  
 
         /// <summary>
 
         /// <summary>
Line 135: Line 73:
 
         /// </summary>
 
         /// </summary>
 
         /// <param name="order">The order for which to commit the taxes</param>
 
         /// <param name="order">The order for which to commit the taxes</param>
         public abstract void Commit(Order order);
+
         public override void Commit(Order order)
 
+
         {
         /// <summary>
+
            throw new Exception("The method or operation is not implemented.");
        /// Calculate the taxes using this provider for the given basket
+
        }
        /// </summary>
+
        /// <param name="basket">The basket to calculate tax for</param>
+
        /// <returns>Total tax calculated</returns>
+
        public abstract LSDecimal Calculate(Basket basket);
+
 
+
 
     }
 
     }
</pre>
+
}
 
</code>
 
</code>
 
  
 
== Implementing Required Methods ==
 
== Implementing Required Methods ==
Line 173: Line 105:
  
 
== Resources ==
 
== Resources ==
* Complete Source of implementation of CCH Tax Gateway ... TODO
 
 
* [http://forums.ablecommerce.com/viewtopic.php?f=42&t=5935 Community Forums Discussion]
 
* [http://forums.ablecommerce.com/viewtopic.php?f=42&t=5935 Community Forums Discussion]

Revision as of 15:34, 2 September 2009

Sample Provider

To integrate a tax provider with Ablecommerce 7 you must implement CommerceBuilder.Taxes.Providers.ITaxProvider interface. This interface defines how tax providers interact with Ablecommerce. This should be done by extending the TaxProviderBase class. Below is a sample code to get you started.

using System; using System.Collections.Generic; using System.Text; using CommerceBuilder.Common; using CommerceBuilder.Orders; using CommerceBuilder.Taxes.Providers;

namespace SampleTaxProvider {

   public class TaxProvider : TaxProviderBase
   {
       #region Properties
       /// <summary>
       /// The name of the tax provider implementation
       /// </summary>
       public override string Name
       {
           get { throw new Exception("The method or operation is not implemented."); }
       }
       /// <summary>
       /// The version of the tax provider implementation
       /// </summary>
       public override string Version
       {
           get { throw new Exception("The method or operation is not implemented."); }
       }
       /// <summary>
       /// Is the tax provider activated or not
       /// </summary>
       public override bool Activated
       {
           get { throw new Exception("The method or operation is not implemented."); }
       }
       #endregion
       /// <summary>
       /// Initialize the tax provider with the given configuration data. Called by AC at the time of initialization.
       /// </summary>
       /// <param name="taxGatewayId">The tax gateway id</param>
       /// <param name="configurationData">Configuration data in the form of name value paris</param>
       public override void Initialize(int taxGatewayId, Dictionary<string, string> configurationData)
       {
           base.Initialize(taxGatewayId, configurationData);
       }
       /// <summary>
       /// Calculate the taxes using this provider for the given basket
       /// </summary>
       /// <param name="basket">The basket to calculate tax for</param>
       /// <returns>Total tax calculated</returns>
       public override LSDecimal Calculate(Basket basket)
       {
           throw new Exception("The method or operation is not implemented.");
       }
       /// <summary>
       /// Cancel the taxes by this provider for the given basket
       /// </summary>
       /// <param name="basket">The basket for which to cancel the taxes</param>
       public override void Cancel(Basket basket)
       {
           throw new Exception("The method or operation is not implemented.");
       }
       /// <summary>
       /// Commit the taxes by this provider for the given order
       /// </summary>
       /// <param name="order">The order for which to commit the taxes</param>
       public override void Commit(Order order)
       {
           throw new Exception("The method or operation is not implemented.");
       }
   }

}

Implementing Required Methods

When you extend TaxProviderBase class you need to implement the abstract methods and properties defined in this class. Some of the properties and methods are for information purposes only and are easy to implement. For example Name and Version.

The important methods to implement are Calculate, Commit and Cancel.

Calculate

This method takes a basket object as input. From there, you can get the billing and shipping addresses as well as line items. What you need to do is determine the tax to charge - then add line items to the basket (BasketItem class) for each line item. The method returns the total tax that was added to the basket.

Commit

This method takes an order object as input and commits any taxes that were charged. This may not be required by your integration. Some providers may need to know what taxes were actually charged in order to produce accurate reports on tax liability.

Cancel

This method takes a basket object as input. When called, it cancels any tax charges currently in progress. Your integration may or may not require this. Some providers may use this to reset the calculations for a particular order in progress.


Deployment

Once your class is implemented and compiled, you need to register it with the store. Since we did not include our tax providers, there is no web based interface to do this yet. The simplest choice, if available, is to add a record to the database in the ac_TaxGateways table:
TaxGatewayId = auto number ID
StoreId = ID of your store, probably 1
Name = Name of your gateway
ClassId = .NET Class ID used to create an instance of your gateway. For examples, you can look in ac_PaymentGateways (assuming you have a gateway configured).
ConfigData = If you need to provide configuration data it can be stored here in a url encoded string. For example, key1=value&key2=value. The values in the field will be accessible in your integration by using the GetConfigData() method. If you are producing an integration only for yourselves, you don't need to worry about this as any configuration should be hard coded instead.

Resources