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' ) );<br>	add_action( 'admin_notices',    array( $this, 'admin_notices' ), 15 );
    <br>	add_action( 'plugins_loaded',   array( $this, 'init_gateways' ), 0 );
    <br>	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 on 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 behaviours.
  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'] ) );
        <br>	// array() param is used to exclude payment types from options
	// array accept the following values: 'job_purchase', 'topup', 'featured', 'withdraw', 'tips', 'subscription', 'custom_extra'
	// e.g. the following code will show the options to activate / deactivate each type of payment of the site, except the withdrawal
	foreach ( wpj_get_payment_types( array( 'withdraw' ) ) as $payment_type_enable_key => $payment_type_enable ) {
		if ( isset( $_POST['wpjobster_' . $this->unique_slug . '_enable_' . $payment_type_enable_key] ) ) {
			update_option( 'wpjobster_' . $this->unique_slug . '_enable_' . $payment_type_enable_key,
				trim( $_POST['wpjobster_' . $this->unique_slug . '_enable_' . $payment_type_enable_key] ) );

		}
	}

        // 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

public function show_gateways( $wpjobster_payment_gateways, $arr, $arr_pages ) {
	$tab_id = get_tab_id( $wpjobster_payment_gateways ); ?>


	<div id="tabs<?php echo $tab_id?>">
		<form method="post" action="<?php bloginfo( 'url' ); ?>/wp-admin/admin.php?page=payment-methods&active_tab=tabs<?php echo $tab_id; ?>">
			<table width="100%" class="wpj-admin-table">
				<tr>
					<td valign=top width="22"><?php wpjobster_theme_bullet(); ?></td>
					<td valign="top"><?php _e( 'Sample Gateway Note:', 'wpjobster-sample' ); ?></td>
					<td>
						<p><?php _e( 'Do you have any special instructions for your gateway?', 'wpjobster-sample' ); ?></p>
						<p><?php _e( 'You can put them here.', 'wpjobster-sample' ); ?></p>
					</td>
				</tr>
				<tr>
					<?php // _enable and _button_caption are mandatory ?>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Enable/Disable Sample payment gateway', 'wpjobster-sample' ) ); ?></td>
					<td width="200"><?php _e( 'Enable:', 'wpjobster-sample' ); ?></td>
					<td><?php echo wpjobster_get_option_drop_down( $arr, 'wpjobster_' . $this->unique_slug . '_enable', 'no' ); ?></td>
				</tr>
				<tr>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Enable/Disable Sample test mode.', 'wpjobster-sample' ) ); ?></td>
					<td width="200"><?php _e( 'Enable Test Mode:', 'wpjobster-sample' ); ?></td>
					<td><?php echo wpjobster_get_option_drop_down( $arr, 'wpjobster_' . $this->unique_slug . '_enablesandbox', 'no' ); ?></td>
				</tr>

				<!-- array() param is used to exclude payment types from options -->
				<!-- array accept the following values: job_purchase, 'topup', 'featured', 'withdraw', 'tips', 'subscription', 'custom_extra' -->
				<!-- e.g. the following code will show the options to activate / deactivate each type of payment of the site, except the withdrawal -->
				<?php foreach ( wpj_get_payment_types( array( 'withdraw' ) ) as $payment_type_enable_key => $payment_type_enable ) { ?>

					<tr>
						<td valign=top width="22"><?php wpjobster_theme_bullet( $payment_type_enable['hint_label'] ); ?></td>
						<td width="200"><?php echo $payment_type_enable['enable_label']; ?></td>
						<td><?php echo wpjobster_get_option_drop_down( $arr, 'wpjobster_' . $this->unique_slug . '_enable_' . $payment_type_enable_key ); ?></td>
					</tr>

				<?php } ?>

				<tr>
					<?php // _enable and _button_caption are mandatory ?>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Put the Sample button caption you want user to see on purchase page', 'wpjobster-sample' ) ); ?></td>
					<td><?php _e( 'Sample Button Caption:', 'wpjobster-sample' ); ?></td>
					<td><input type="text" size="45" name="wpjobster_<?php echo $this->unique_slug; ?>_button_caption" value="<?php echo get_option( 'wpjobster_' . $this->unique_slug . '_button_caption' ); ?>" /></td>
				</tr>

				<tr>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Your Sample Merchant ID', 'wpjobster-sample' ) ); ?></td>
					<td ><?php _e( 'Sample Merchant ID:', 'wpjobster-sample' ); ?></td>
					<td><input type="text" size="45" name="wpjobster_sample_id" value="<?php echo get_option( 'wpjobster_sample_id' ); ?>" /></td>
				</tr>
				<tr>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Your Sample Key', 'wpjobster-sample' ) ); ?></td>
					<td ><?php _e( 'Sample Merchant KEY:', 'wpjobster-sample' ); ?></td>
					<td><input type="text" size="45" name="wpjobster_sample_key" value="<?php echo get_option( 'wpjobster_sample_key' ); ?>" /></td>
				</tr>

				<tr>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Please select a page to show when Sample payment successful. If empty, it redirects to the transaction page', 'wpjobster-sample' ) ); ?></td>
					<td><?php _e( 'Transaction Success Redirect:', 'wpjobster-sample' ); ?></td>
					<td><?php echo wpjobster_get_option_drop_down( $arr_pages, 'wpjobster_' . $this->unique_slug . '_success_page', '', ' class="select2" ' ); ?></td>
				</tr>
				<tr>
					<td valign=top width="22"><?php wpjobster_theme_bullet( __( 'Please select a page to show when Sample payment failed. If empty, it redirects to the transaction page', 'wpjobster-sample' ) ); ?></td>
					<td><?php _e( 'Transaction Failure Redirect:', 'wpjobster-sample' ); ?></td>
					<td><?php echo wpjobster_get_option_drop_down( $arr_pages, 'wpjobster_' . $this->unique_slug . '_failure_page', '', ' class="select2" ' ); ?></td>
				</tr>

				<tr>
					<td></td>
					<td></td>
					<td><input type="submit" name="wpjobster_save_<?php echo $this->unique_slug; ?>" value="<?php _e( 'Save Options', 'wpjobster-sample' ); ?>" /></td>
				</tr>
			</table>
		</form>
	</div>

<?php }