WCFM Marketplace – Custom Payment Gateway (Developers Guide)

This is a technical document for adding your custom payment gateway for WCFM – Marketplace vendor payment.

Well, this is mainly for developers but a non-developer may also use this with vary basic PHP knowledge.

Before going to start I want mention one important thing, I have used a variable here in almost every function, named “$gateway_slug“, now mainly you have to change this with your gateway slug. Well, “slug” is not a particular ting related to gateway, but it should be a unique key in this case to identify your custom gateway.

So, lt’s start –

First we are adding new payment gateway option under WCFM Marketplace withdrawal payment gateway options –

add_filter( 'wcfm_marketplace_withdrwal_payment_methods', function( $payment_methods ) {
$payment_methods['brain_tree'] = 'Brain Tree';
return $payment_methods;
});

Here I am adding a custom gateway “Brain Tree”, look I am using “brain_tree” as gateway slug here.

Now I am adding payment gateway API keys fields –

add_filter( 'wcfm_marketplace_settings_fields_withdrawal_payment_keys', function( $payment_keys, $wcfm_withdrawal_options ) {
$gateway_slug = 'brain_tree';
$withdrawal_brain_tree_client_id = isset( $wcfm_withdrawal_options[$gateway_slug.'_client_id'] ) ? $wcfm_withdrawal_options[$gateway_slug.'_client_id'] : '';
$withdrawal_brain_tree_secret_key = isset( $wcfm_withdrawal_options[$gateway_slug.'_secret_key'] ) ? $wcfm_withdrawal_options[$gateway_slug.'_secret_key'] : '';
$payment_brain_tree_keys = array(
"withdrawal_".$gateway_slug."_client_id" => array('label' => __('Brain Tree Client ID', 'wc-multivendor-marketplace'), 'name' => 'wcfm_withdrawal_options['.$gateway_slug.'_client_id]', 'type' => 'text', 'class' => 'wcfm-text wcfm_ele withdrawal_mode withdrawal_mode_live withdrawal_mode_'.$gateway_slug, 'label_class' => 'wcfm_title withdrawal_mode withdrawal_mode_live withdrawal_mode_'.$gateway_slug, 'value' => $withdrawal_brain_tree_client_id ),
"withdrawal_".$gateway_slug."_secret_key" => array('label' => __('Brain Tree Secret Key', 'wc-multivendor-marketplace'), 'name' => 'wcfm_withdrawal_options['.$gateway_slug.'_secret_key]', 'type' => 'text', 'class' => 'wcfm-text wcfm_ele withdrawal_mode withdrawal_mode_live withdrawal_mode_'.$gateway_slug, 'label_class' => 'wcfm_title withdrawal_mode withdrawal_mode_live withdrawal_mode_'.$gateway_slug, 'value' => $withdrawal_brain_tree_secret_key )
);
$payment_keys = array_merge( $payment_keys, $payment_brain_tree_keys );
return $payment_keys;
}, 50, 2);

I have added here two fields – Client ID and Secret Key, off course you may change this to add your own, even you may add more fields.

Well, you may add test key fields as well –

add_filter( 'wcfm_marketplace_settings_fields_withdrawal_payment_test_keys', function( $payment_test_keys, $wcfm_withdrawal_options ) {
$gateway_slug = 'brain_tree';
$withdrawal_brain_tree_test_client_id = isset( $wcfm_withdrawal_options[$gateway_slug.'_test_client_id'] ) ? $wcfm_withdrawal_options[$gateway_slug.'_test_client_id'] : '';
$withdrawal_brain_tree_test_secret_key = isset( $wcfm_withdrawal_options[$gateway_slug.'_test_secret_key'] ) ? $wcfm_withdrawal_options[$gateway_slug.'_test_secret_key'] : '';
$payment_brain_tree_test_keys = array(
"withdrawal_".$gateway_slug."_test_client_id" => array('label' => __('Brain Tree Client ID', 'wc-multivendor-marketplace'), 'name' => 'wcfm_withdrawal_options['.$gateway_slug.'_test_client_id]', 'type' => 'text', 'class' => 'wcfm-text wcfm_ele withdrawal_mode withdrawal_mode_test withdrawal_mode_'.$gateway_slug, 'label_class' => 'wcfm_title withdrawal_mode withdrawal_mode_test withdrawal_mode_'.$gateway_slug, 'value' => $withdrawal_brain_tree_test_client_id ),
"withdrawal_".$gateway_slug."_test_secret_key" => array('label' => __('Brain Tree Secret Key', 'wc-multivendor-marketplace'), 'name' => 'wcfm_withdrawal_options['.$gateway_slug.'_test_secret_key]', 'type' => 'text', 'class' => 'wcfm-text wcfm_ele withdrawal_mode withdrawal_mode_test withdrawal_mode_'.$gateway_slug, 'label_class' => 'wcfm_title withdrawal_mode withdrawal_mode_test withdrawal_mode_'.$gateway_slug, 'value' => $withdrawal_brain_tree_test_secret_key )
);
$payment_test_keys = array_merge( $payment_test_keys, $payment_brain_tree_test_keys );
return $payment_test_keys;
}, 50, 2);

You may add withdrawal charge option for your custom gateway as well –

add_filter( 'wcfm_marketplace_settings_fields_withdrawal_charges', function( $withdrawal_charges, $wcfm_withdrawal_options, $withdrawal_charge ) {
$gateway_slug = 'brain_tree';
$withdrawal_charge_brain_tree = isset( $withdrawal_charge[$gateway_slug] ) ? $withdrawal_charge[$gateway_slug] : array();
$payment_withdrawal_charges = array( "withdrawal_charge_".$gateway_slug => array( 'label' => __('Brain Tree Charge', 'wc-multivendor-marketplace'), 'type' => 'multiinput', 'name' => 'wcfm_withdrawal_options[withdrawal_charge]['.$gateway_slug.']', 'class' => 'withdraw_charge_block withdraw_charge_'.$gateway_slug, 'label_class' => 'wcfm_title wcfm_ele wcfm_fill_ele withdraw_charge_block withdraw_charge_'.$gateway_slug, 'value' => $withdrawal_charge_brain_tree, 'custom_attributes' => array( 'limit' => 1 ), 'options' => array(
"percent" => array('label' => __('Percent Charge(%)', 'wc-multivendor-marketplace'), 'type' => 'number', 'class' => 'wcfm-text wcfm_ele withdraw_charge_field withdraw_charge_percent withdraw_charge_percent_fixed', 'label_class' => 'wcfm_title wcfm_ele withdraw_charge_field withdraw_charge_percent withdraw_charge_percent_fixed', 'attributes' => array( 'min' => '0.1', 'step' => '0.1') ),
"fixed" => array('label' => __('Fixed Charge', 'wc-multivendor-marketplace'), 'type' => 'number', 'class' => 'wcfm-text wcfm_ele withdraw_charge_field withdraw_charge_fixed withdraw_charge_percent_fixed', 'label_class' => 'wcfm_title wcfm_ele withdraw_charge_field withdraw_charge_fixed withdraw_charge_percent_fixed', 'attributes' => array( 'min' => '0.1', 'step' => '0.1') ),
"tax" => array('label' => __('Charge Tax', 'wc-multivendor-marketplace'), 'type' => 'number', 'class' => 'wcfm-text wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'attributes' => array( 'min' => '0.1', 'step' => '0.1'), 'hints' => __( 'Tax for withdrawal charge, calculate in percent.', 'wc-multivendor-marketplace' ) ),
) ) );
$withdrawal_charges = array_merge( $withdrawal_charges, $payment_withdrawal_charges );
return $withdrawal_charges;
}, 50, 3);

Now we are going to add new payment gateway setting field under vendor’s setting panel –

add_filter( 'wcfm_marketplace_settings_fields_billing', function( $vendor_billing_fileds, $vendor_id ) {
$gateway_slug = 'brain_tree';
$vendor_data = get_user_meta( $vendor_id, 'wcfmmp_profile_settings', true );
if( !$vendor_data ) $vendor_data = array();
$brain_tree = isset( $vendor_data['payment'][$gateway_slug]['email'] ) ? esc_attr( $vendor_data['payment'][$gateway_slug]['email'] ) : '' ;
$vendor_brain_tree_billing_fileds = array(
$gateway_slug => array('label' => __('Brain Tree Email', 'wc-frontend-manager'), 'name' => 'payment['.$gateway_slug.'][email]', 'type' => 'text', 'class' => 'wcfm-text wcfm_ele paymode_field paymode_'.$gateway_slug, 'label_class' => 'wcfm_title wcfm_ele paymode_field paymode_'.$gateway_slug, 'value' => $brain_tree ),
);
$vendor_billing_fileds = array_merge( $vendor_billing_fileds, $vendor_brain_tree_billing_fileds );
return $vendor_billing_fileds;
}, 50, 2);

I have added only email field here, you have add more fields as per your requirement.

Now your vendors are able to place withdrawal request using your new Payment Gateway!

Now it’s time for most important thing, payment processing.

For the purpose you have to add such a gateway class, be careful at class name – it should start with “WCFMmp_Gateway_” and then “gateway_slug”

class WCFMmp_Gateway_Brain_Tree {
public $id;
public $message = array();
public $gateway_title;
public $payment_gateway;
public $withdrawal_id;
public $vendor_id;
public $withdraw_amount = 0;
public $currency;
public $transaction_mode;
private $reciver_email;
public $test_mode = false;
public $client_id;
public $client_secret;
public function __construct() {
$this->id = 'brain_tree';
$this->gateway_title = __('Brain Tree', 'wc-multivendor-marketplace');
$this->payment_gateway = $this->id;
}
public function gateway_logo() { global $WCFMmp; return $WCFMmp->plugin_url . 'assets/images/'.$this->id.'.png'; }
public function process_payment( $withdrawal_id, $vendor_id, $withdraw_amount, $withdraw_charges, $transaction_mode = 'auto' ) {
global $WCFM, $WCFMmp;
$this->withdrawal_id = $withdrawal_id;
$this->vendor_id = $vendor_id;
$this->withdraw_amount = $withdraw_amount;
$this->currency = get_woocommerce_currency();
$this->transaction_mode = $transaction_mode;
$this->reciver_email = $WCFMmp->wcfmmp_vendor->get_vendor_payment_account( $this->vendor_id, $this->id );
$withdrawal_test_mode = isset( $WCFMmp->wcfmmp_withdrawal_options['test_mode'] ) ? 'yes' : 'no';
$this->client_id = isset( $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_client_id'] ) ? $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_client_id'] : '';
$this->client_secret = isset( $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_secret_key'] ) ? $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_secret_key'] : '';
if ( $withdrawal_test_mode == 'yes') {
$this->test_mode = true;
$this->client_id = isset( $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_test_client_id'] ) ? $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_test_client_id'] : '';
$this->client_secret = isset( $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_test_secret_key'] ) ? $WCFMmp->wcfmmp_withdrawal_options[$this->id.'_test_secret_key'] : '';
}
if ( $this->validate_request() ) {
// Updating withdrawal meta
$WCFMmp->wcfmmp_withdraw->wcfmmp_update_withdrawal_meta( $this->withdrawal_id, 'withdraw_amount', $this->withdraw_amount );
$WCFMmp->wcfmmp_withdraw->wcfmmp_update_withdrawal_meta( $this->withdrawal_id, 'currency', $this->currency );
$WCFMmp->wcfmmp_withdraw->wcfmmp_update_withdrawal_meta( $this->withdrawal_id, 'reciver_email', $this->reciver_email );
return array( 'status' => true, 'message' => __('New transaction has been initiated', 'wc-multivendor-marketplace') );
} else {
return $this->message;
}
}
public function validate_request() {
global $WCFMmp;
return true;
}
}

Well, I have written this class for manual payment processing, you may modify this for automated payment process. For reference download this (WCFM Marketplace PayPal payment process class).

Transaction using custom added payment gateway. cool right!

Well, you may add all this code by creating a new plugin or add under your child theme’s functions.php

Feel free to reach us for any further assistance on this!

Just to mention, this only works for WCFM – Marketplace.