To integrate your custom payment gateway:
Create a payment gateway form with your custom controls to allow customers to enter their payment data. For example credit card numbers, credit cart codes, etc.
Create a custom payment gateway class and override methods required for processing the payment.
If you create the custom payment gateway class in your project's App_Code folder (or Old_App_Code on web applications), you need to register the class using:
[assembly: RegisterCustomClass("CustomGateway", typeof(CustomGateway))]Open the Store configuration application.
Switch to the Payment methods tab.
Create a new payment method and register your custom payment gateway.
If you want to offer your customers the custom payment gateway during the checkout process, you now need to add the respective payment method to selected shipping options.
Create a new web user control (*.ascx) and place it into your website folder, which is located in the root of your web project.
Because the control is located in the website folder, it is included in the export package of your site. See Export folder structure for more details.
Set the control class to inherit from the abstract CMS.EcommerceProvider.CMSPaymentGatewayForm class.
Override the following methods to reach the required functionality.
Besides, there are several properties to get or set information related to the purchase:
Payment data, such as credit card numbers, credit card codes, etc., is not saved into the database due to security reasons.
Create the CustomGateway.cs class under the App_Code directory (Old_App_Code for web applications).
Set the class to inherit from the CMS.EcommerceProvider.CMSPaymentGatewayProvider abstract class*.*
(Optional) Override the following methods to reach the required functionality.
Besides, there are several properties to get or set information related to the purchase:
Compile the project.
No compilation is needed if you installed your Kentico project as a web project.
To add the payment method (custom payment gateway) to selected shipping options:
Your customers can now use for selected shipping options the custom payment method.
The examples demonstrate an implementation of a custom payment processor. The processor allows customers to pay for their orders using an external payment gateway similar to PayPal.
The system saves the order before asking the customer to pay. Specifically, this happens immediately after placing the order, i.e. after clicking Order now in the sample checkout process.
It is not secure to send credit card information as a part of the payment gateway URL. Therefore, customers are usually asked for their credit card details after being redirected to the payment gateway.
If this is not the case, we recommend that you use some other way of sending your customers' credit card information.
Here you can see a simple form with one input field where the customers enter their credit card numbers.
If you installed your Kentico project as a web application, for the code example to work, you need to rename the CodeFile property on the first line to Codebehind.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomGatewayForm.ascx.cs" Inherits="EcommerceSite_CustomGatewayForm" %>
<asp:Label ID="lblTitle" runat="server" EnableViewState="false" CssClass="BlockTitle" />
<asp:Label ID="lblError" runat="server" EnableViewState="false" CssClass="ErrorLabel" Visible="false" />
<asp:Label ID="lblCardNumber" EnableViewState="false" runat="server" />
<asp:TextBox ID="txtCardNumber" runat="server" />using System;
using CMS.EcommerceProvider;
using CMS.Helpers;
public partial class EcommerceSite_CustomGatewayForm : CMSPaymentGatewayForm
{
protected void Page_Load(object sender, EventArgs e)
{
// Initialize label
lblTitle.Text = "Your credit card details";
lblCardNumber.Text = "Credit card number:";
}
/// <summary>
/// Initializes form controls with customer payment data.
/// </summary>
public override void LoadData()
{
// Display customer credit card number
txtCardNumber.Text = ValidationHelper.GetString(PaymentGatewayCustomData["CustomGatewayCardNumber"], "");
}
/// <summary>
/// Validates customer payment data.
/// </summary>
/// <returns></returns>
public override string ValidateData()
{
if (txtCardNumber.Text.Trim() == "")
{
lblError.Visible = true;
lblError.Text = "Please enter your credit card number";
return lblError.Text;
}
return "";
}
/// <summary>
/// Process customer payment data.
/// </summary>
/// <returns></returns>
public override string ProcessData()
{
// Save credit card number
PaymentGatewayCustomData["CustomGatewayCardNumber"] = txtCardNumber.Text.Trim();
return "";
}
}The following example creates and registers the gateway class in the project's App_Code folder (or Old_App_Code on web applications).
using System;
using System.Collections;
using CMS;
using CMS.Base;
using CMS.EcommerceProvider;
using CMS.Helpers;
[assembly: RegisterCustomClass("CustomGateway", typeof(CustomGateway))]
public class CustomGateway : CMSPaymentGatewayProvider
{
/// <summary>
/// Returns path to payment gateway form with custom controls.
/// </summary>
public override string GetPaymentDataFormPath()
{
return "~/EcommerceSite/CustomGatewayForm.ascx";
}
/// <summary>
/// Process payment.
/// </summary>
public override void ProcessPayment()
{
// Get payment gateway url
string url = GetPaymentGatewayUrl();
if (url != "")
{
// Initialize payment parameters
Hashtable parameters = InitializePaymentParameters();
// Add required payment data to the url
url = GetFullPaymentGatewayUrl(url, parameters);
// Redirect to payment gateway to finish payment
URLHelper.Redirect(url);
}
else
{
// Show error message - payment gateway url not found
ErrorMessage = "Unable to finish payment: Payment gateway url not found.";
// Update payment result
PaymentResult.PaymentDescription = ErrorMessage;
PaymentResult.PaymentIsCompleted = false;
// Update order payment result in database
UpdateOrderPaymentResult();
}
}
/// <summary>
/// Returns table with initialized payment parameters.
/// </summary>
/// <returns></returns>
private Hashtable InitializePaymentParameters()
{
Hashtable parameters = new Hashtable();
parameters["orderid"] = ShoppingCartInfoObj.OrderId;
parameters["price"] = ShoppingCartInfoObj.TotalPrice;
parameters["currency"] = ShoppingCartInfoObj.Currency.CurrencyCode;
parameters["cardnumber"] = ValidationHelper.GetString(PaymentDataForm.PaymentGatewayCustomData["CustomGatewayCardNumber"], "");
return parameters;
}
/// <summary>
/// Returns payment gateway url with payment data in query string.
/// </summary>
/// <param name="url">Payment gateway url.</param>
/// <param name="parameters">Initialized payment parameters.</param>
/// <returns></returns>
private string GetFullPaymentGatewayUrl(string url, Hashtable parameters)
{
foreach (DictionaryEntry parameter in parameters)
{
// Add payment data to the url
url = URLHelper.AddParameterToUrl(url, Convert.ToString(parameter.Key), URLHelper.EncodeQueryString(parameter.Value.ToString()));
}
return url;
}
}For further details on how to create your custom payment gateway, see the Developing Custom Payment Gateway (opens in a new tab) webinar.