1. Home
  2. Docs
  3. Payment Gateways API
  4. _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( 'prefive_taketo_' . $this->unique_slug . '_gateway', array( $this, 'taketogateway_function' ), 10 );
	add_action( 'prefive_processafter_' . $this->unique_slug . '_gateway', array( $this, 'processgateway_function' ), 10 );
 
	// use this filter if your gateway works with a specific currency only
	add_filter( 'prefive_take_allowed_currency_' . $this->unique_slug, array( $this,'get_gateway_currency' ) );
 
	if ( isset( $_POST[ 'prefive_save_' . $this->unique_slug ] ) ) {
		add_action( 'prefive_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( 'prefive-sample', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) );
	add_filter( 'prefive_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', 'prefive-sample' ),
            'unique_id'       => $this->unique_slug,
            'action'          => 'prefive_taketo_' . $this->unique_slug . '_gateway', 
            'response_action' => 'prefive_processafter_' . $this->unique_slug . '_gateway', 
        );
    add_action( 'prefive_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['prefive_save_' . $this->unique_slug] ) ) {
 
        // _enable and _button_caption are mandatory
        update_option( 'prefive_' . $this->unique_slug . '_enable',
                        trim( $_POST['prefive_' . $this->unique_slug . '_enable'] ) );
        update_option( 'prefive_' . $this->unique_slug . '_button_caption',
                        trim( $_POST['prefive_' . $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['prefive_' . $this->unique_slug . '_enable_' . $payment_type_enable_key] ) ) {
			update_option( 'prefive_' . $this->unique_slug . '_enable_' . $payment_type_enable_key,
				trim( $_POST['prefive_' . $this->unique_slug . '_enable_' . $payment_type_enable_key] ) );

		}
	}

        // you can add here any other information that you need from the user
        update_option( 'prefive_sample_enablesandbox', trim( $_POST['prefive_sample_enablesandbox'] ) );
        update_option( 'prefive_sample_id',            trim( $_POST['prefive_sample_id'] ) );
        update_option( 'prefive_sample_key',           trim( $_POST['prefive_sample_key'] ) );
 
        update_option( 'prefive_sample_success_page',  trim( $_POST['prefive_sample_success_page'] ) );
        update_option( 'prefive_sample_failure_page',  trim( $_POST['prefive_sample_failure_page'] ) );
 
        echo '<div class="updated fade">' . __( 'Settings saved!', 'prefive-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.

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

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

public function show_gateways( $prefive_payment_gateways, $arr, $arr_pages ) {
	$tab_id = get_tab_id( $prefive_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 prefive_theme_bullet(); ?></td>
					<td valign="top"><?php _e( 'Sample Gateway Note:', 'prefive-sample' ); ?></td>
					<td>
						<p><?php _e( 'Do you have any special instructions for your gateway?', 'prefive-sample' ); ?></p>
						<p><?php _e( 'You can put them here.', 'prefive-sample' ); ?></p>
					</td>
				</tr>
				<tr>
					<?php // _enable and _button_caption are mandatory ?>
					<td valign=top width="22"><?php prefive_theme_bullet( __( 'Enable/Disable Sample payment gateway', 'prefive-sample' ) ); ?></td>
					<td width="200"><?php _e( 'Enable:', 'prefive-sample' ); ?></td>
					<td><?php echo prefive_get_option_drop_down( $arr, 'prefive_' . $this->unique_slug . '_enable', 'no' ); ?></td>
				</tr>
				<tr>
					<td valign=top width="22"><?php prefive_theme_bullet( __( 'Enable/Disable Sample test mode.', 'prefive-sample' ) ); ?></td>
					<td width="200"><?php _e( 'Enable Test Mode:', 'prefive-sample' ); ?></td>
					<td><?php echo prefive_get_option_drop_down( $arr, 'prefive_' . $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 prefive_theme_bullet( $payment_type_enable['hint_label'] ); ?></td>
						<td width="200"><?php echo $payment_type_enable['enable_label']; ?></td>
						<td><?php echo prefive_get_option_drop_down( $arr, 'prefive_' . $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 prefive_theme_bullet( __( 'Put the Sample button caption you want user to see on purchase page', 'prefive-sample' ) ); ?></td>
					<td><?php _e( 'Sample Button Caption:', 'prefive-sample' ); ?></td>
					<td><input type="text" size="45" name="prefive_<?php echo $this->unique_slug; ?>_button_caption" value="<?php echo get_option( 'prefive_' . $this->unique_slug . '_button_caption' ); ?>" /></td>
				</tr>

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

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

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

<?php }
Was this article helpful to you? Yes No

How can we help?