[Drupal] How to create custom offsite payment gateway module in Drupal 7

| | 2 min read

A Payment gateway tends to offer multiple types of payment services, that provides onsite and offsite options. Onsite payment is processed by a third party API using web services with the information gathered from our site through a checkout form (For example : Credit card/Debit card credentials). Offsite payment method, on the other hand, processes data entered in the payment gateway page. It redirects the users to the processing page of the payment gateway using the checkout form. After the payment is completed, the user will be redirected to the payment page, from where the user can move forward or backward in the checkout process. Here, let's take a look at the creation of custom offsite payment gateway module.

Creating Payment method

For creating a payment method, we should define a method using the hook_commerce_payment_method_info().

function hook_commerce_payment_method_info() {
  $payment_methods = array();
  
  $logo_url = drupal_get_path('module', 'custom') . '/images/techprocess_logo.png';
  $display_title = t('!logo  payment gateway',
                    array('!logo' => '<img width="172" height="61" src=' . "/$logo_url" . '>'));

  $payment_methods['payment_method_name'] = array(
    'base' => 'base',
    'title' => t('title'),
    'short_title' => t('short tile'),
    'display_title' => $display_title,
    'description' => t('payment details'),
    'terminal' => FALSE,
    'offsite' => TRUE,
    'offsite_autoredirect' => TRUE
  );
  return $payment_methods;
}

Here the parameters offsite is set to true; this means that the payment method is meant for offsite payment. You can find all the supporting parameters from the link Payment info hooks.

Set the module dependencies

Add the dependencies for modules: Commerce, Commerce UI, Commerce Payment, Commerce Order. Add the below code to your info file.

dependencies[] = commerce
dependencies[] = commerce_ui
dependencies[] = commerce_payment
dependencies[] = commerce_order

Enable the payment method

When you declare a payment method, you should enable the payment method. Go to 'admin/commerce/config/payment-methods' and enable the method.

set up configuration form

For offsite payment method, you should have to use the key sets provided by the payment service provider with which the payment call is done. Create an admin/config form to save this values as system variables.

Redirect form function(base_redirect_form()).

This is the hook function that will call when we click the make payment function. We can put the custom function for making payment inside this hook.

Redirect form validate function(base_redirect_form_validate()).

This is the hook function that will call when the payment is completed and redirected to our site. Here, we can check the status of the transactions. Most of the offsite payment services provides the second verification for a completed transaction. On success, it will return false, which will go to the success page.

Currently, we are contributing the 'Tech-process payment module' to Drupal. This is one of the simplest offsite payment drupal modules.

If you need any further assistance, please feel free to get in touch with us.