Create Custom Payment Gateway

Creating new online payment gateway will require more advanced php knowledge and also knowledge of the payment gateway and the requirements.

Because HDS CRM uses Codeigniter framework to integrate the payment gateway you will need to create 1 gateway library (gateway config and process method) and 1 gateway controller (for HTTP requests, show form etc..), you can also take a look at the other gateways files in order to get the idea.

In the invoice HTML area when a customer clicks on the button PAY NOW, we call 1 method from the gateway library which will process everything additional that is required for this gateway eq redirect to gateway website and pass parameters or redirect to controller and show form etc…

We have simplified a little the process for creating new gateways e.q. the gateway will be auto showed in Setup->Settings->Payment Gateways, encrypting fields, 1 unique function from each gateway library to call etc… but this will still require effort to get started.

The main folders who you will use when creating a new gateway are:

  1. application/libraries/gateways – This file holds the main logic for the gateway eq settings, adding the gateway to the system, the main process_payment function which is called each time a customer click on PAY NOW  button.
  2. application/gateways/controllers – This folder is used in case you need to create valid URL/s for your gateway eq. webhook or to take the HTTP response from the gateway request. In this case, you should create a Codeigniter controller there.

The PHP Class

Let’s assume for this example your gateway name is Example

In application/libraries/gateways/ create file Example_gateway.php with the following content:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Example_gateway extends App_gateway
{
    public function __construct()
    {
        /**
        * Call App_gateway __construct function
        */
        parent::__construct();

        /**
         * Gateway unique id - REQUIRED
	 * 
         * * The ID must be alphanumeric
         * * The filename (Example_gateway.php) and the class name must contain the id as ID_gateway
         * * In this case our id is "example"
         * * Filename will be Example_gateway.php (first letter is uppercase)
         * * Class name will be Example_gateway (first letter is uppercase)
         */
        $this->setId('example');

        /**
         * REQUIRED
         * Gateway name
         */
        $this->setName('Example');

        /**
         * Add gateway settings
         * You can add other settings here to fit for your gateway requirements
         *
         * Currently only 2 field types are accepted for gateway
         *
         * 'type'=>'yes_no'
         * 'type'=>'input'
         */
        $this->setSettings(array(
            array(
                'name' => 'api_secret_key',
                'encrypted' => true,
                'label' => 'API KEY',
                'type'=>'input',
            ),
            array(
                'name' => 'api_publishable_key',
                'label' => 'SECRET KEY',
                'type'=>'input'
            ),
            array(
                'name' => 'currencies',
                'label' => 'settings_paymentmethod_currencies',
                'default_value' => 'USD,CAD'
            ),
        ));

        /**
         * REQUIRED
         * Hook gateway with other online payment modes
         */
        hooks()->add_filter('app_payment_gateways', [ $this, 'initMode' ]);
    }

    /**
     * Each time a customer click PAY NOW button on the invoice HTML area, the script will process the payment via this function.
     * You can show forms here, redirect to gateway website, redirect to Codeigniter controller etc..
     * @param  array $data - Contains the total amount to pay and the invoice information
     * @return mixed
     */
    public function process_payment($data)
    {
        var_dump($data);
        die;
    }
}

There are comments on the functions and variables, you should spend some time to read them because they are important if you want to help you get started.

Additionally, you can take a look at the other gateway example in application/libraries/gateways or application/controllers/gateways.

Now after you create and save the file navigate to Setup->Settings->Payment Gateways you will be able to see Example gateway listed, what’s up to you now you need to implement the gateway logic how you are going to process the payments, this all depends by the requirements of the payment gateway.

Avez-vous trouvé cet article utile ?