|   Register
Monday, December 01, 2008   
You are here :     Blog
Commerce Starter Kit 2.0 - Payment Provider UserControl Installation
Location: BlogsKing Wilder's Blog    
Posted by: kwdnnsuper 4/4/2007

Installation Instructions

There are several easy steps to enable this UserControl and make it work with CSK.

These instructions are based on a default installation of CSK 2.0.  If you have already modified your application to use a custom payment provider you can still use this control.

Here are a few steps you need to make to alter your application to use this control.

1) Add your new web.config PaymentService section for your provider

 

<paymentservice defaultprovider="LinkPointPaymentProvider" acceptcreditcards="true">   	
	<providers>    	
	<clear></clear>    	
	<add type="Commerce.Providers.PayPalPaymentProvider" 		
		name="PayPalPaymentProvider" 		
		currencycode="USD" 		
		merchantid="business@csk.com" 		
		sslcertpassword="commerce" 		
		sslcertfile="csk.pfx" 		
		servicepassword="commerce" 		
		serviceusername="business_api1.csk.com"></add>    	
	<add type="Commerce.Providers.LinkPointPaymentProvider" 		
		name="LinkPointPaymentProvider" 		
		currencycode="USD" 		
		port="1129" 		
		host="staging.linkpt.net" 		
		keyfile="App_Data\[your config id].pem" 		
		configfile="[your config id]"></add>    	
	<add type="Commerce.Providers.AuthorizeNetPaymentProvider, Commerce.PaymentProvider" 		
		name="AuthorizeNetPaymentProvider" 		
		serverurl="https://test.authorize.net/gateway/transact.dll" 		
		currencycode="USD" 		
		transactionkey="COAd7pinhMc5Jgs9" 		
		servicepassword="authnet101" 		
		serviceusername="cnpdev1799"></add>   	
	</providers>  
</paymentservice>




2) Add a new custom payment provider, I built a LinkPoint provider and saved it here: /App_Code/Services/PaymentProvider/LinkPointPaymentProvider.cs

 

using System; 
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.IO;

///
/// Summary description for LinkPointPaymentProvider
///
///
namespace Commerce.Providers {
public class LinkPointPaymentProvider:PaymentProvider
{
// Members and Methods go here
}
}




3) Add the new UserControl, CreditCardSettings.ascx, to the public /Modules/Admin directory

CreditCardSettings UserControl


4) Open the PaymentConfiguration.aspx.cs code-behind file and comment out the btnSetCC_Click event handler

    //protected void btnSetCC_Click(object sender, EventArgs e)
    //{
       
    //    // Get the current configuration file.
    //    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
    //    Commerce.Providers.PaymentServiceSection section = (Commerce.Providers.PaymentServiceSection)config.GetSection("PaymentService");

    //    //load up the ConfigSection
    //    ProviderSettings settings = new ProviderSettings();
    //    try
    //    {
    //        string userName = txtCCUserName.Text;
    //        string password = txtCCPassword.Text;
    //        string key = txtCCKey.Text;
    //        string certPath = "";
    //        string certPassword = "";
    //        string providerName = ddlCCProvider.SelectedValue;

    //        section.AcceptCreditCards = chkAcceptCC.Checked;
    //        //section.CurrencyCode = ddlCurrencyType.SelectedValue;
    //        section.Providers.Clear();
    //        //apply the settings based on the provider
    //        if (providerName == "PayPalPaymentProvider")
    //        {
    //            section.DefaultProvider = "PayPalPaymentProvider";
    //            settings.Name = "PayPalPaymentProvider";
    //            settings.Parameters.Add("type", "Commerce.Providers.PayPalPaymentProvider");
    //            settings.Parameters.Add("serviceUserName", txtPPAPIAccount.Text);
    //            settings.Parameters.Add("servicePassword", txtPPAPIPassword.Text);
    //            settings.Parameters.Add("sslCertFile", txtPPCertName.Text);
    //            settings.Parameters.Add("sslCertPassword", txtPPCertPassword.Text);
    //            settings.Parameters.Add("merchantID", txtBusinessEmail.Text);
    //            settings.Parameters.Add("currencyCode", ddlCurrencyType.SelectedValue);
    //            section.Providers.Add(settings);

    //        }
    //        else
    //        {
    //            section.DefaultProvider = providerName;
    //            settings.Name = providerName;
    //            settings.Parameters.Add("type", "Commerce.Providers." + providerName);
    //            settings.Parameters.Add("serviceUserName", txtPPAPIAccount.Text);
    //            settings.Parameters.Add("servicePassword", txtPPAPIPassword.Text);
    //            settings.Parameters.Add("transactionKey", txtCCKey.Text);
    //            settings.Parameters.Add("serverURL", txtCCUrl.Text);
    //            settings.Parameters.Add("currencyCode", ddlCurrencyType.SelectedValue);
    //            section.Providers.Add(settings);

    //        }
    //     config.Save();
    //     ResultMessage5.ShowSuccess("Credit Card Settings Saved");
    //   }
    //    catch (Exception x)
    //    {
    //        ResultMessage5.ShowFail("Cannot write to the Web.Config file;
//       in order to have the application update the configuration,
//       you have to allow write access to the ASNET account
//       (or NETWORK SERVICE) and make sure the file is not marked as Read Only");
    //    }
    //}

 

Also in this file, you'll need to comment out some lines in the LoadSettings method.

void LoadSettings()
{
//general settings
ddlLogin.SelectedValue = SiteConfig.RequireLogin;
ddlCurrencyType.SelectedValue =
Enum.GetName(typeof(Commerce.Common.CurrencyCode),SiteConfig.CurrencyCode);

//standard
chkUsePPStandard.Checked = SiteConfig.UsePayPalPaymentsStandard;
chkUsePPStandardSandbox.Checked = SiteConfig.UsePPStandardSandbox;
txtBusinessEmail.Text = SiteConfig.BusinessEmail;
txtPDTID.Text = SiteConfig.PayPalPDTID;

//pro settings
chkUsePPPro.Checked = SiteConfig.UsePayPalExpressCheckout;
chkUsePPSandbox.Checked = SiteConfig.UsePPProSandbox;
txtPPAPIAccount.Text = SiteConfig.PayPalAPIAccountName;
txtPPAPIPassword.Text = SiteConfig.PayPalAPIAccountPassword;
txtPPCertName.Text = SiteConfig.PayPalAPICertificate;
txtPPCertPassword.Text = SiteConfig.PayPalAPICertificationPassword;


//CC Bits
//chkAcceptCC.Checked = SiteConfig.AcceptCreditCards;

//get the provider, if there is one, and set the boxes
//System.Configuration.Configuration config =
//WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//Commerce.Providers.PaymentServiceSection section =
//(Commerce.Providers.PaymentServiceSection)config.GetSection("PaymentService");

//if (section.Providers.Count > 0)
//{
// if (section.Providers[0].Name == "PayPalPaymentProvider")
// {

// }
// else
// {
// txtCCKey.Text =
//section.Providers[0].Parameters["transactionKey"].ToString();
// txtCCPassword.Text =
//section.Providers[0].Parameters["servicePassword"].ToString();
// txtCCUrl.Text =
//section.Providers[0].Parameters["serverUrl"].ToString();
// txtCCUserName.Text =
//section.Providers[0].Parameters["serviceUserName"].ToString();

// }
//}

}


5) In the PaymentConfiguration.aspx HTML source, comment out (or delete) the HTML table that contains the Credit Card settings

 

 

<%--    <table>
        <tr>
            <td >
                Accept Credit Cards</td>
            <td class="adminitem">
                <asp:CheckBox ID="chkAcceptCC" runat="server" /></td>
        </tr>
        <tr>
            <td >
                Gateway Provider</td>
            <td class="adminitem">
                <asp:DropDownList ID="ddlCCProvider" runat="server">
                    <asp:ListItem Value="AuthorizeNetPaymentProvider">Authorize.NET</asp:ListItem>
                    <asp:ListItem Selected="True" Value="PayPalPaymentProvider">PayPal DirectPay</asp:ListItem>
                    <asp:ListItem>LinkPoint</asp:ListItem>
                </asp:DropDownList><br />
                Note: If you select PayPal DirectPay, you don't need to add the information below;
                you will however need to set the PaymentsPro settings above as DirectPay is part
                of PaymentsPro</td>
        </tr>
        <tr>
            <td >
                Username</td>
            <td class="adminitem">
                <asp:TextBox ID="txtCCUserName" runat="server"></asp:TextBox><br />
                The username to access the gateway</td>
        </tr>
        <tr>
            <td >
                Password</td>
            <td class="adminitem">
                <asp:TextBox ID="txtCCPassword" runat="server"></asp:TextBox><br />
                The password to access the gateway</td>
        </tr>
        <tr>
            <td >
                Key</td>
            <td class="adminitem">
                <asp:TextBox ID="txtCCKey" runat="server"></asp:TextBox><br />
                Sometimes you are given an access key for a given gateway</td>
        </tr>
        <tr>
            <td >
                Server URL</td>
            <td class="adminitem">
                <asp:TextBox ID="txtCCUrl" runat="server" Width="411px"></asp:TextBox><br />
                The access URL</td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnSetCC" runat="server" Text="Set" OnClick="btnSetCC_Click" CssClass="frmbutton" /></td>
            <td>
                <uc1:ResultMessage ID="ResultMessage5" runat="server" />
            </td>
        </tr>
    </table>--%>

 

6) Modify the Transactions class to add necessary properties.  You'll need to modify the \App_Code\DataAccess\Store\Transaction.cs file that will contain a set of LinkPoint specific properties needed to capture responses from the object.  Just add the following code to the class.

#region LinkPoint Specific bits
private string _ResponseMessage;

public string ResponseMessage
{
get
{
return _ResponseMessage;
}
set
{
_ResponseMessage = value;
}
}
private bool _IsApproved;

public bool IsApproved
{
get
{
return _IsApproved;
}
set
{
_IsApproved = value;
}
}
private string _Approved;

public string Approved
{
get
{
return _Approved;
}
set
{
_Approved = value;
}
}
private string _Code;

public string Code
{
get
{
return _Code;
}
set
{
_Code = value;
}
}
private string _OrderID;

public string OrderNum
{
get
{
return _OrderID;
}
set
{
_OrderID = value;
}
}
private string _TransactionDate;

public string LPTransactionDate
{
get
{
return _TransactionDate;
}
set
{
_TransactionDate = value;
}
}
private string _PPResponseError;

public string PPResponseError
{
get
{
return _PPResponseError;
}
set
{
_PPResponseError = value;
}
}
private string _AVSCode;

public string AVSCode
{
get
{
return _AVSCode;
}
set
{
_AVSCode = value;
}
}
private string _RefNumber;

public string RefNumber
{
get
{
return _RefNumber;
}
set
{
_RefNumber = value;
}
}
#endregion

7) Drag the UserControl onto the /Admin/PaymentConfiguration.aspx page

Drop the control onto the ASPX page

8) One last thing is to drop your LinkPoint certificate (pem file) into the App_Data directory.  You can actually put the file anywhere, you'll just need to change the location in the LinkPoint provider setting of the PaymentService section in the web.config file.

That's basically it!

IMPORTANT!!!

Now here are a few things to check on and modify so that PayPal, or the first provider, isn't the only one used.  These changes only need to be made once! 

CSK was built initially to incorporate the PayPal Website Payments Pro and other payment providers, but was also built to be flexible enough for other payment processors.  The problem is, that in some places in the code, the PayPal provider (or more accurately, the first provider) is hard coded.  And until this is changed, my UserControl won't work.  So we need to make a few other changes that will make it totally flexible.


-- Open /App_Code/Services/PaymentProvider/PaymentServiceSection.cs and look for the defaultProvider property.  In the ConfigurationProperty attribute, remove the DefaultValue="PayPalPaymentProvider" setting.

Change this:

 

[StringValidator(MinLength = 1)]
[ConfigurationProperty("defaultProvider", DefaultValue="PayPalPaymentProvider")]
public string DefaultProvider
{
  get { return (string)base["defaultProvider"]; }
  set { base["defaultProvider"] = value; }
}



To this:

//[StringValidator(MinLength = 1)]
[ConfigurationProperty("defaultProvider")]
public string DefaultProvider
{
  get { return (string)base["defaultProvider"]; }
  set { base["defaultProvider"] = value; }
}



-- Open the /App_Code/Services/PaymentProvider/PaymentService.cs class and go down to line 56 (if you haven't modified it). 

It should look like this:

_provider = (PaymentProvider)ProvidersHelper.      
    InstantiateProvider(section.Providers[0], typeof(PaymentProvider));




The index of zero makes the first provider listed become the instantiated provider, regardless of what is listed as the defaultProvider.

Change it to look like this:

_provider = (PaymentProvider)ProvidersHelper.      
    InstantiateProvider(section.Providers[section.DefaultProvider], typeof(PaymentProvider));




Save all your changes and that should be it!


Test it!

Now we can test it. 

-- Open the CSK application in a browser and log on as an admin.
-- Click the Payment configuration link under the Configuration category
-- Scroll to the bottom of the page and you should see the Credit Card Settings section. 
-- Click the drop down list control and see if you custom provider is there.  If it is, select it.
-- Your key/value pairs should show up after a few moments
-- Edit a value and click the Set button.  You should see a message saying the settings were saved.
-- That's it!

You can either open your web.config file to make sure the settings were saved, or simply click on another page, then click back to the Payment Configuration page and see if your settings are displayed.  If they are, then it works.

NOTE: when you select a new provider using the drop down control, the web.config file is automatically updated with the defaultProvider attribute value.  You do not need to click Set in order to save the new provider as the default provider. 

The Set button is simply to modify the settings of a particular provider.

 

The User Control should look like this.

User control rendered

What now?

Well if you are a developer who will be using CSK 2.0 for many different clients that most likely have different payment processors, this is the best way to go to easily accommodate those changes quickly.

The nice thing about this UserControl is that, as you start building providers for different payment processors, if you have a new client that uses an existing processor, then you don't even need to go back into code to set their payment provider.

I hope this control helps you and I would appreciate any feedback.

 

Permalink |  Trackback

Comments (2)   Add Comment
Re: Commerce Starter Kit 2.0 - Payment Processor UserControl Installation    By DeepBlue on 5/3/2007
Thank you very much for this... Looks to save my butt... I have tried implementing but had a couple of snags... Left a post at CSK Forums... If you have time I would greatly appreciate a bit of help...

Thank you...

Re: Commerce Starter Kit 2.0 - Payment Processor UserControl Installation    By King on 5/4/2007
Sorry about thiat, I left out some code. My bad. Step 6 above is the newly added code that was missing. It handles capturing LinkPoint responses in the Transaction object and passes them back to the Order object. I also updated the download file with this new code, so it should be complete now.


Your name:
Title:
Comment:
Add Comment   Cancel 
King's Blog
King's Blog
King's Blog

Copyright 2007 by King Wilder   |  Privacy Statement  |  Terms Of Use