2. Construct
__construct()
public function __construct() {
// Define gateway unique slug
$this->unique_id = 'sample';
// Add gateway options to Admin > Jobster Settings > Payment Gateways
add_action( 'wpj_after_admin_paypal_settings_fields', function() {
if ( class_exists( 'Redux' ) ) {
Redux::setSection( 'jobster_settings', array(
'id' => 'sample-settings', // settings option name
'title' => __( 'Sample', 'wpjobster-sample' ), // gateway settings page title
'desc' => __( 'Sample Settings', 'wpjobster-sample' ), // gateway settings page description
'subsection' => true, // subsection of Payment Gateways section
'fields' => wpj_get_gateway_default_fields(
array(
'gateway_id' => $this->unique_id, // gateway id
'gateway_name' => 'Sample', // gateway name
'gateway_version' => WPJ_SAMPLE_VERSION, // gateway version
'gateway_instructions' => array( // gateway instructions
'Do you have any special instructions for your gateway?',
'You can put them here.',
),
'license' => true, // include license field
'enable' => true, // include enable field
'enable_sandbox' => true, // include enable sandbox field
'exclude_payment_type' => array( 'withdraw' ), // exclude payment types from options; accepted values: job_purchase, 'topup', 'featured', 'withdraw', 'tips', 'subscription', 'custom_extra'
'button_name' => true, // include button name field
'public_key' => false, // include public key field
'secret_key' => false, // include secret key field
'succes_page_url' => true, // include transaction success page field
'fail_page_url' => true, // include transaction failure page field
'new_fields' => array( // extra fields (optional fields)
array(
'unique_id' => 'sample-settings-section',
'type' => 'section',
'title' => esc_html__( 'Keys', 'wpjobster-sample' ),
'indent' => true,
),
array(
'unique_id' => 'wpjobster_sample_id',
'type' => 'text',
'title' => __( 'Sample ID', 'wpjobster-sample' ),
),
array(
'unique_id' => 'wpjobster_sample_key',
'type' => 'text',
'title' => __( 'Sample Key', 'wpjobster-sample' )
)
)
)
)
) );
}
});
// Add gateway (button) to payment methods list - add button to checkout page
add_filter( 'wpj_payment_gateways_filter', function ( $payment_gateways_list ) {
$payment_gateways_list[$this->unique_id] = __( 'Sample', 'wpjobster-sample' );
return $payment_gateways_list;
}, 10, 1 );
// Add gateway to payment process flow - send and receive the payment info
add_action( 'wpjobster_taketo_' . $this->unique_id . '_gateway', array( $this, 'initializePayment' ), 10, 2 );
add_action( 'wpjobster_processafter_' . $this->unique_id . '_gateway', array( $this, 'processPayment' ), 10, 2 );
// Use this filter if your gateway works with a specific currency only
add_filter( 'wpjobster_take_allowed_currency_' . $this->unique_id, array( $this, 'setGatewayCurrency' ) );
// Set plugin compatibility - prevents the plugin from activating on other themes
add_action( 'admin_notices', array( $this, 'checkCompatibility' ) );
// Set plugin action link - show 'Settings' link to plugins page
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) {
return function_exists( 'wpj_generate_settings_link' ) ? array_merge( array( wpj_generate_settings_link( $this->unique_id ) ), $links ) : $links;
});
// Set plugin textdomain
add_action( 'plugins_loaded', function () {
load_plugin_textdomain( 'wpjobster-sample', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) );
}, 0 );
// Gateway name translatable - allows the Gateway name to be translated into other languages
add_filter( 'wpjobster_database_strings_filter', array( $this, 'translateGatewayName' ), 10, 1 );
// Add gateway to all plugins list
add_filter( 'wpj_gateways_plugins_folder_list_filter', array( $this, 'addToGatwaysList' ), 10, 1 );
}
$this->unique_id - This is the lowercase name of your gateway and it really needs to be unique.
setGatewayCurrency()
public function setGatewayCurrencies( $currency ) {
$currency = 'EUR'; return $currency;
}
This function is filtering the currency for processing the payment.
If your gateway only accepts a certain currency this is the place where you can define it.
Feel free to play around with the conditions. For example, if your gateway accepts a limited range of currencies you can check if the selected currency is in the range, otherwise return a specific currency.
checkCompatibility()
public function checkCompatibility() {
if ( ! function_exists( 'wpj_get_wpjobster_plugins_list' ) && ! defined( 'wpjobster_VERSION' ) )
$error = sprintf( __( 'The current theme is not compatible with the %s gateway. Activate the WPJobster theme before installing this plugin.', 'wpjobster' ), $this->unique_id );
if ( ! empty( $error ) && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
deactivate_plugins( plugin_basename( __FILE__ ), true ); ?>
<div data-dismissible="compatibility-theme-notice" class="notice notice-error is-dismissible"><p><?php echo $error; ?></p></div>
<?php if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
}
do_action( 'wpj_after_plugin_compatibility_errors', WPJ_SAMPLE_VERSION, 'Sample', plugin_basename( __FILE__ ), WPJ_SAMPLE_REQUIRED_THEME_VERSION );
}<br>
This function checks if the gateway is compatible with the theme activated on the site, and if not, it will display a message and will not allow the plugin to be activated until the WPJobster theme is activated.
translateGatewayName()
public function translateGatewayName( $strings ) {
$strings['sample'] = _x( 'Sample', 'Sample gateway', 'wpjobster-sample' );
return $strings;
}
This function will allow the name of the plugin to be translated when it is saved as a string in the database, when an order is paid.
addToGatwaysList()
public function addToGatwaysList( $plugins ) {
$plugins[] = 'wpjobster-sample-gateway';
return $plugins;
}
This function will add the gateway to a WPJobster array containing all the gateways and extensions we have.