II. Construct

__construct()

protected function __construct() {
    $this->priority = 1111;           // 100, 200, 300 [...] are reserved
    $this->unique_slug = 'sample';    // this needs to be unique
 
    add_action( 'admin_init',       array( $this, 'check_environment' ) );
    add_action( 'admin_notices',    array( $this, 'admin_notices' ), 15 );
    add_action( 'plugins_loaded',   array( $this, 'init_gateways' ), 0 );
    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ),
                                    array( $this, 'plugin_action_links' ) );
 
    add_action( 'wpjobster_taketo_' . $this->unique_slug . '_gateway',
                                    array( $this, 'taketogateway_function' ), 10 );
    add_action( 'wpjobster_processafter_' . $this->unique_slug . '_gateway',
                                    array( $this, 'processgateway_function' ), 10 );
 
    // use this filter if your gateway works with a specific currency only
    add_filter( 'wpjobster_take_allowed_currency_' . $this->unique_slug,
                                    array( $this,'get_gateway_currency' ) );
 
    if ( isset( $_POST[ 'wpjobster_save_' . $this->unique_slug ] ) ) {
        add_action( 'wpjobster_payment_methods_action', array( $this, 'save_gateway' ), 11 );
    }
}

The class constructor explained:

  1. “priority”:
    The payment gateways are ordered based on this in the admin area and in the sales page. The default gateways are using multiples of 100 as priorities, so a priority between 100 and 200 would place your gateway between the first and the second default gateways.
    It’s better to try something unique, to avoid strange behaviors.
  2. “unique_slug”:
    This is the lowercase name of your gateway and it really needs to be unique.

init_gateways()

public function init_gateways() {
    load_plugin_textdomain( 'wpjobster-sample', false, 
                    trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) );
    add_filter( 'wpjobster_payment_gateways', array( $this, 'add_gateways' ) );
}

This function will merge the gateway that we are building with the other gateways.

add_gateways( $methods )

public function add_gateways( $methods ) {
    $methods[$this->priority] =
        array(
            'label'           => __( 'Sample', 'wpjobster-sample' ),
            'unique_id'       => $this->unique_slug,
            'action'          => 'wpjobster_taketo_' . $this->unique_slug . '_gateway', 
            'response_action' => 'wpjobster_processafter_' . $this->unique_slug . '_gateway', 
        );
    add_action( 'wpjobster_show_paymentgateway_forms', array( $this, 'show_gateways' ), $this->priority, 3 );
 
    return $methods;
}

This function links the settings, checkout and processing functions to the actions from the constructor.

‘action’ is called when user request to send payment to gateway
‘response_action’ is called when any response comes from gateway after payment

save_gateway()

public function save_gateway() {
    if ( isset( $_POST['wpjobster_save_' . $this->unique_slug] ) ) {
 
        // _enable and _button_caption are mandatory
        update_option( 'wpjobster_' . $this->unique_slug . '_enable',
                        trim( $_POST['wpjobster_' . $this->unique_slug . '_enable'] ) );
        update_option( 'wpjobster_' . $this->unique_slug . '_button_caption',
                        trim( $_POST['wpjobster_' . $this->unique_slug . '_button_caption'] ) );
        update_option( 'wpjobster_' . $this->unique_slug . '_enable_topup',
                        trim( $_POST['wpjobster_' . $this->unique_slug . '_enable_topup'] ) );
        update_option( 'wpjobster_' . $this->unique_slug . '_enable_featured',
                        trim( $_POST['wpjobster_' . $this->unique_slug . '_enable_featured'] ) );
 
        // you can add here any other information that you need from the user
        update_option( 'wpjobster_sample_enablesandbox', trim( $_POST['wpjobster_sample_enablesandbox'] ) );
        update_option( 'wpjobster_sample_id',            trim( $_POST['wpjobster_sample_id'] ) );
        update_option( 'wpjobster_sample_key',           trim( $_POST['wpjobster_sample_key'] ) );
 
        update_option( 'wpjobster_sample_success_page',  trim( $_POST['wpjobster_sample_success_page'] ) );
        update_option( 'wpjobster_sample_failure_page',  trim( $_POST['wpjobster_sample_failure_page'] ) );
 
        echo '
<div class="updated fade">
 
' . __( 'Settings saved!', 'wpjobster-sample' ) . '
</div>
 
';
    }
}

Save the information filled by the admin, with show_gateways(). You need to populate this with all the input names that you have added on the show_gateways() function.

show_gateways()

This function contains a HTML table with a form where you will add your inputs for the credentials and settings that you need from the admin.

There are two required fields: Enable and Button Caption.

‘wpjobster_’ . $this->unique_slug . ‘_enable’ // used for enabling or disabling the inclusion of your plugin in the theme.

‘wpjobster_’ . $this->unique_slug . ‘_button_caption’ // the text showing on the button which takes the user to the gateway