Create A Payment Gateway For Tajer

Creating a payment gateway for Tajer is a very simple process. The code of the payment methods that bundled by default with Tajer very self-explanation if you want to take a look at it directly and skip this tutorial you can check the following files:
“wp-content/plugins/tajer/includes/gateways/paypal-standard.php” and
“wp-content/plugins/tajer/js/frontend/paypal-standard.js” files for PayPal, and

“wp-content/plugins/tajer/includes/gateways/test.php” file for Test Payment payment method

We’ll explain here howPayPal payment method works.

The first thing you should do when you create a payment gateway for Tajer is to register you gateway like the following for the PayPal payment gateway:

function tajer_register_paypal_gateway( $gateways ) {
function tajer_register_paypal_gateway( $gateways ) {
    $gateways['paypal'] = array(
        'admin_label'    => __( 'PayPal', 'tajer' ),
        'checkout_label' => __( 'PayPal', 'tajer' )

    return $gateways;

add_filter( 'tajer_payment_gateways', 'tajer_register_paypal_gateway' );

You can hook into tajer_payment_gateways  filter and add your payment gateway, the index in the $gateways array is the gateway id in our case ‘paypal'(we will use this id later). The ‘admin_label’ is the label that will appear on the admin “Payment Gateways” page, and the ‘checkout_label’ is the label that will appear on the frontend cart page.

The second step is to register the payment gateway settings, in our example the PayPal settings registered like the following:

function tajer_paypal_payment_gateway_settings_fields( $fields ) {
    $paypal_fields = array(
            'label' => __( 'PayPal Settings', 'tajer' ),
            'type'  => 'header'
            'label' => __( 'Test Mode', 'tajer' ),
            'name'  => 'tajer_payment_settings[paypal_test_mode]',
            'type'  => 'checkbox',
            'help'  => __( 'Enable or disable PayPal demo mode.', 'tajer' )
            'label' => __( 'PayPal Email', 'tajer' ),
            'name'  => 'tajer_payment_settings[paypal_email]',
            'type'  => 'email',
            'help'  => __( 'Enter your PayPal account\'s email', 'tajer' )
            'label' => __( 'PayPal Page Style', 'tajer' ),
            'name'  => 'tajer_payment_settings[paypal_page_style]',
            'type'  => 'text',
            'help'  => __( 'Enter the name of the page style to use, or leave blank for default', 'tajer' )
            'label' => __( 'Disable PayPal IPN Verification', 'tajer' ),
            'name'  => 'tajer_payment_settings[disable_paypal_verification]',
            'type'  => 'checkbox',
            'help'  => __( 'If payments are not getting marked as complete, then check this box. This forces the site to use a slightly less secure method of verifying purchases.', 'tajer' )

    return array_merge( $fields, $paypal_fields );

add_filter( 'tajer_payment_settings_fields', 'tajer_paypal_payment_gateway_settings_fields' );

We used ‘tajer_payment_settings_fields’ Tajer filter to hook our settings with the default Tajer payment settings fields(the allowed fields types are: password, text, textarea, email, file, button, select, multi_select, radio, radios, checkbox, multi_check, header, custom, and tajer_settings_’ . $field[‘type’] . ‘_field_renderer hook, to find all of them please see “wp-content/plugins/tajer/classes/admin/class-tajer-settings.php” file.)

Now it is time to implement our payment form on the frontend cart page. This step is optional because you can use the default Tajer credit card form.
Let’s take a look at how we implement PayPal form:

    function tajer_paypal_form() {
	$secondary_color = tajer_get_option( 'secondary_color', 'tajer_general_settings', 'green' );
	ob_start(); ?>

	<input type="hidden" name="quantity" value="">

	<div class="field">
        <div class="two fields">
            <div class="field">
                <input type="submit" class='fluid ui <?php echo $secondary_color; ?> button' id="tajer_paypal_submit_button" name="tajer-purchase"
            <div class="field">
                <a href="<?php echo tajer_get_option( 'continue_shopping', 'tajer_general_settings', '#' ); ?>"
                   class="fluid ui button tajer-continue-shopping"><?php _e( 'Continue Shopping?', 'tajer' ); ?></a>

	$form_fields = ob_get_clean();

	//AJAX json response
	$response = array(
		'form_fields' => $form_fields,
		'form_action' => esc_url( add_query_arg( array( 'payment-mode' => 'paypal' ), get_permalink( (int) tajer_get_option( 'cart', 'tajer_general_settings', '' ) ) ) )
	tajer_response( $response );

//this action is consist of tajer_{gateway ID}_purchase_form used to overwrite the default checkout form
add_action( 'tajer_paypal_purchase_form', 'tajer_paypal_form' );

When the user selects a payment method Tajer will check to see if there is any form registered with this payment gateway using tajer_{gateway ID}_purchase_form hook(replace the {gateway ID} with your gateway id that you registered your gateway with when we stated this tutorial), if Tajer couldn’t find any form it will use its default checkout form.

Because we don’t want to echo the form fields and we want to return them via Ajax we used the output buffering here(ob_start() and ob_get_clean() functions). All the form fields saved as strings in the $form_fields variable. We respond to Tajer when it asks us about our gateway form using tajer_response() function. Our respond now is just an array of tow elements the first one is ‘form_fields’ which is the form fields string, and the second one is ‘form_action’ Tajer will put this in the ‘action’ attribute of the checkout form. Now let’s explain what will happen when the user click on the “Checkout” button.

    function tajer_submit_paypal_purchase_form() {

    $customer_details = tajer_customer_details();

    $result = tajer_insert_order( array( 'gateway' => 'paypal' ) );

    tajer_update_order_meta( $result['id'], 'currency', tajer_get_option( 'currency', 'tajer_general_settings', 'USD' ) );

    // Get the PayPal redirect uri
    $paypal_redirect = trailingslashit( tajer_get_paypal_redirect() ) . '?';

    // Get the success url
    $return_url = add_query_arg( array(
        'tajer_gateway'     => 'paypal',
        'tajer_order_id'    => $result['id'],
        'tajer_secret_code' => $result['secret_code']
    ), get_permalink( (int) tajer_get_option( 'thank_you_page', 'tajer_general_settings', '' ) ) );

    // Setup PayPal arguments
    $paypal_args = array(
        'business'      => tajer_get_option( 'paypal_email', 'tajer_payment_settings' ),
        'email'         => $customer_details->email,
        'invoice'       => $result['id'],
        'no_shipping'   => '1',
        'shipping'      => '0',
        'no_note'       => '1',
        'currency_code' => tajer_get_option( 'currency', 'tajer_general_settings', 'USD' ),
        'charset'       => get_bloginfo( 'charset' ),
        'custom'        => $result['secret_code'],
        'rm'            => '2',
        'return'        => $return_url,
//        'cancel_return' => edd_get_failed_transaction_uri( '?payment-id=' . $payment ),
        'notify_url'    => add_query_arg( 'tajer-listener', 'IPN', home_url( 'index.php' ) ),
        'page_style'    => tajer_get_option( 'paypal_page_style', 'tajer_payment_settings', 'paypal' ),
        'cbt'           => get_bloginfo( 'name' ),
        'bn'            => 'Tajer_IQ',
        'cmd'           => '_cart',
        'quantity_1'    => 1,
        'amount_1'      => tajer_sanitize_amount( $result['total'] ),
        'item_name_1'   => stripslashes_deep( html_entity_decode( apply_filters( 'tajer_paypal_item_name', get_bloginfo( 'name' ) . '-' . __( 'Product(s)', 'tajer' ) ), ENT_COMPAT, 'UTF-8' ) ),
        'upload'        => '1'

    // Build query
    $paypal_redirect .= http_build_query( $paypal_args );

    // Fix for some sites that encode the entities
    $paypal_redirect = str_replace( '&', '&', $paypal_redirect );

    // Redirect to PayPal
    wp_redirect( $paypal_redirect );

add_action( 'tajer_submit_paypal_purchase_form', 'tajer_submit_paypal_purchase_form' );

When the user submit the checkout form Tajer will fire:

do_action( 'tajer_submit_' . sanitize_text_field( $_GET['payment-mode'] ) . '_purchase_form' );

We simply hook into that action and get customer details using tajer_customer_details() function and create an order using tajer_insert_order() function and passing an array that contains one element which is the gateway id, we also save the currency code at this point because we will use it later to validate PayPal respond, then we simply redirect the user to PayPal.

Note: In case for example your payment gateway allows you to handle the credit card on your website, using the default Tajer form or another form also if you used the get or post requests you can simply do this in a function like tajer_submit_paypal_purchase_form() function, it will insert the order then make the post or get request then finalize the order using tajer_order_completed() function(see bellow how we used this function) then redirect the user to the “Thank You” page for example.

After users’ pay the user will be redirected to “Thank You” page Tajer will fire tajer_{gateway ID}_parameters_parser hook because PayPal returned to us the value of the tajer_gateway form field which is the PayPal payment gateway id.

We simply will wait and listen to PayPal IPN.

add_action( 'init', 'tajer_listen_for_paypal_ipn' );

After validating PayPal response we simply can run tajer_order_completed() function and that’s all!

Note: some payment gateways specially the ones that use AJAX to handle the checkout process Tajer requires it to check tajer_purchase_form_errors() function before use tajer_insert_order() function.

You can take a look at “wp-content/plugins/tajer/includes/gateways/test.php” file which is work in that way.

For any questions don’t be hesitated to contact us also if you want to gain profits from your payment gateway just contact us.