Get Virtualizor

Integrate payment gateway

Overview

We have introduced the Cloud Billing module earlier this year in Virtualizor. And now we have made payment gateway integration possible for all. You can integrate your own custom payment gateway using following guide.

Currently available gateway

  1. PayPal
  2. PayuMonney India
  3. CCAvenue (NOTE: Hook is needed. For details click here)
  4. Duitku
  5. Stripe
  6. 2Checkout

How to integrate?

To integrate custom gateway you will need two files. Frirst is YOUR_GATEWAY.php and YOUR_GATEWAY.js
Here we have explained with the example of PayuMoney India payment gateway integration.

PHP Implementation

  • You need to create a file with the name of your payment gateway inside '/usr/local/virtualizor/enduser/gateways/YOUR_GATEWAY.php' directory.
  • Here is how the sample gateway looks like,
<?php

function payumoney_config(){

	$configarray = array(
		'name'         => 'PayuMoney India',
		'description'  => 'This is a sample config function for an addon module',
		'version'      => '1.0',
		'author'       => 'Virtualizor',
		'fields'       => array(
			'payumoney_sandbox' => array(
				'type'          => 'checkbox',
				'friendly_name' => 'PayuMoney Sandbox',
				'description'   => ''
			),
			'payumoney_merchant_key' => array(
				'type'          => 'text',
				'friendly_name' => 'PayuMoney Merchant Key',
				'description'   => ''
			),
			'payumoney_merchant_salt' => array(
				'type'          => 'text',
				'friendly_name' => 'PayuMoney Merchant Salt',
				'description'   => ''
			)
		)
	);
	
	return $configarray;
}

function payumoney_execute_payment($data){
	global $error, $l;

	$data['firstname'] = optPOST('firstname');
	$data['phone'] = optPOST('phone');

	if($data['payumoney_sandbox'] == 1){
		$data['payment_url'] = 'https://sandboxsecure.payu.in/_payment';
	}else{
		$data['payment_url'] = 'https://secure.payu.in/_payment';
	}

	$params = array(
		'key'         => $data['payumoney_merchant_key'],
		'txnid'       => $data['token'],
		'amount'      => $data['amount'],
		'productinfo' => 'Cloud Billing',
		'firstname'   => $data['firstname'],
		'email'       => $data['email'],
		'phone'       => $data['phone'],
		'surl'        => $data['success_url'],
		'furl'        => $data['fail_url'],
		'payment_url' => $data['payment_url']
	);

	$hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";

	$hashVarsSeq = explode('|', $hashSequence);
	$hash_string = '';
	$postFields = '';
	foreach($hashVarsSeq as $hash_var) {
      $hash_string .= isset($params[$hash_var]) ? $params[$hash_var] : '';
	  $hash_string .= '|';
    }

	$hash_string .= $data['payumoney_merchant_salt'];

	$hash = strtolower(hash('sha512', $hash_string));
	$params['hash'] = $hash;

	$fields = optREQ('api');
	if(!empty($fields)){
		echo json_encode($params);
		die();
	}
}

function payumoney_process_payment($data){
	global $user_details, $error, $l;

	$status = $_POST["status"];
	$firstname = $_POST["firstname"];
	$amount = $_POST["amount"];
	$txnid = $_POST["txnid"];
	$posted_hash = $_POST["hash"];
	$key = $_POST["key"];
	$productinfo = $_POST["productinfo"];
	$email = $_POST["email"];
	$salt = $data['payumoney_merchant_salt'];

	// Salt should be same Post Request 
	$retHashSeq = $salt.'|'.$status.'|||||||||||'.$email.'|'.$firstname.'|'.$productinfo.'|'.$amount.'|'.$txnid.'|'.$key;

	$hash = hash("sha512", $retHashSeq);

	if ($hash != $posted_hash || $status != 'success') {
		$error['pay_data_missing'] = $l['pay_data_missing'];
		return false;
	}else{
		$user_details = array(
			'fees' => 0,
			'status' => 1,
			'data' => array(),
			'order_id' => $_POST["txnid"]
		);
	}
}

Javascript Implementation

  • To show your custom gateway form fields to your users you will need to make changes in YOUR_PAYMENT_GATEWAY_requirement() function.
  • Languages must be added to /usr/local/virtualizor/languages/YOUR_PREFERRED_LANGUAGE/enduser_lang.php
  • After defining language strings you can use those strings as {{LANGUAGE_KEY}}, which will be parsed with your defined language value.
  • In the following example code we have shown an example code for Payumoney India payment gateway :
function YOUR_PAYMENT_GATEWAY_requirement(){

        var html = '';
	
	$('.payment_gateway_fields').html(html);
	
	html += '<div class="form-group row payu_gateway_field" style="display:none">';
	html += '<label class="col-sm-4 control-label"><strong>{{pr_firstname}} :</strong><br /></label>';
	html += '<div class="col-sm-6 col-md-5">';
	html += '<input type="text" name="firstname" class="form-control" size="10" />';
	html += '</div>';
	html += '</div>';
	html += '<div class="form-group row payu_gateway_field" style="display:none">';
	html += '<label class="col-sm-4 control-label"><strong>{{pr_phone}} :</strong><br /></label>';
	html += '<div class="col-sm-6 col-md-5">';
	html += '<input type="text" name="phone" class="form-control" size="10" />';
	html += '</div>';
	html += '</div>';
	html += '<input type="hidden" name="key" id="payu_key" value="">';
	html += '<input type="hidden" name="txnid" id="payu_txnid" value="">';
	html += '<input type="hidden" name="amount" id="payu_amount" value="">';
	html += '<input type="hidden" name="productinfo" id="payu_productinfo" value="">';
	html += '<input type="hidden" name="email" id="payu_email" value="">';
	html += '<input type="hidden" name="surl" id="payu_surl" value="">';
	html += '<input type="hidden" name="furl" id="payu_furl" value="">';
	html += '<input type="hidden" name="hash" id="payu_hash" value="">';
	html += '<input type="hidden" name="service_provide" value="payu_paisa">';
	
	$('.payment_gateway_fields').html(html);
};

function YOUR_PAYMENT_GATEWAY_process(){
        var data = $('#make_payment_form').serializeArray();
	var uri = $('#make_payment_form').attr('action') + '&api=json';
	$.ajax({
		url: uri,
		data: data,
		method: 'POST',
		success: function(res){
			var finalData = JSON.parse(res);
			$('#payu_key').val(finalData.key);
			$('#payu_txnid').val(finalData.txnid);
			$('#payu_amount').val(finalData.amount);
			$('#payu_productinfo').val(finalData.productinfo);
			$('#payu_surl').val(finalData.surl);
			$('#payu_furl').val(finalData.furl);
			$('#payu_hash').val(finalData.hash);
			$('#payu_email').val(finalData.email);
			$('#make_payment_form').attr('action',finalData.payment_url);
			$('#make_payment_form').submit();
		}
	});
};
  • finalData will be coming from the function payumoney_execute_payment() in php file.
  • After writing the gateway you can see it inside Admin Panel >> Billing >> Billing Settings >> Navigate to "Payment Gateways" Section.
  • You need to enable it and save the settings, after enabling the gateway it will be displayed to the Cloud Users.
  • Once you save the setting you can see your fields below "Payment Gateways" Section.
  • Fill your fields with valid credentials and save settings.
  • Now your user can add funds using your created custom payment gateway.

Error Handling

  • You can see from above example that we have declared $error and $l variable as global.
  • Whenever validation fails at that time you need to fill the error in $error array, for example,
if ($hash != $posted_hash || $status != 'success') {
      $error[] = "Invalid Transaction";
 }else{
      $user_details = array(
                'fees' => 0,
                'status' => 1, 
                'data' => array()
 );
} 

Things to Note

  • After successful payment you need to fill the $user_details variable with the mentions fields in gateway example.
  • Function names must be starting with your gateway name.
  • Functions given in the example gateway are necessary and required else it will throw an error.
  • Make sure you don't have any syntax error in your files.
  • Test your gateway twice before publishing it.

Hooks required

For CCAvenue : You will need to create the pre_process_payment hook and add the following code in that hook. For more details about how to create hook files please click here.

function __pre_process_payment(){
	$ccavenue_resp = optREQ('encResp');
	if(!empty($ccavenue_resp)){
		$_REQUEST['process_payment'] = 'ccavenue';
		$_REQUEST['id'] = optREQ('orderNo');
	}
	
}
    Was this page helpful?
    Newsletter Subscription
    Subscribing you to the mailing list