admin

python.ds.com@gmail.com

CCVenue Payment Gateway integration in PHP CodeIgniter

Posted on February 15, 2021



#Step1: Create Library for encryption/decription


<?php

defined('BASEPATH') OR exit('No direct script access allowed');

error_reporting(0);

class Ccvenuecrypto {

function encrypt($plainText,$key)

{

$key = $this->hextobin(md5($key));

$initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);

$openMode = openssl_encrypt($plainText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector);

$encryptedText = bin2hex($openMode);

return $encryptedText;

}

function decrypt($encryptedText,$key)

{

$key = $this->hextobin(md5($key));

$initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);

$encryptedText = $this->hextobin($encryptedText);

$decryptedText = openssl_decrypt($encryptedText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector);

return $decryptedText;

}

//*********** Padding Function *********************

public function pkcs5_pad($plainText, $blockSize)

{

$pad = $blockSize - (strlen($plainText) % $blockSize);

return $plainText . str_repeat(chr($pad), $pad);

}

//********** Hexadecimal to Binary function for php 4.0 version ********

public function hextobin($hexString)

{

$length = strlen($hexString);

$binString="";

$count=0;

while($count<$length)

{

$subString =substr($hexString,$count,2);

$packedString = pack("H*",$subString);

if ($count==0)

{

$binString=$packedString;

}

else

{

$binString.=$packedString;

}

$count+=2;

}

return $binString;

}

}


#Step2: Create Controller for logic

class Order_Api extends CI_Controller

{

public function __construct()

{

parent::__construct();

$this->load->library('ccvenuecrypto');

date_default_timezone_set('Asia/Kolkata');

}


    public function ccvpayementform()

{

$tid=time();

$shippingData = $this->Front_model->getDataRow('billing_shippings', 'billing_shipping_id', $billing_shipping_id);

$name = "Vishavjeet Singh";

$email = "xyz@gmail.com";

$phone = "8888XXXXX";

$orderData = array('txnid' => $tid);

$en_order_id=$order_id+12844;

$merchant_data="$en_order_id=$token";

$encrypted_order_id=$this->ccvenuecrypto->encrypt($merchant_data,$working_key);

// Method for encrypting the data.

$action= CCVENUEREQUESTHANDLE;

$form = '<form method="POST" name="customerData" id="ccVenue_submit" action="'.$action.'">

<input type="hidden" name="tid" id="tid" readonly value="'.$tid.'" />

<input type="hidden" name="merchant_id" value="XXXXX" />

<input type="hidden" name="order_id" value="'.$order_id.'" />

<input type="hidden" name="amount" value="'.$order_total.'" />

<input type="hidden" name="currency" value="INR"/>

<input type="hidden" name="redirect_url" value="'.CCVENUEREDIRECTURL.'" />

<input type="hidden" name="cancel_url" value="'.CCVENUECANCELURL.'" />

<input type="hidden" name="language" value="EN"/>

<input type="hidden" name="billing_name" value="'.$name.'" />

<input type="hidden" name="billing_email" value="'.$email.'" />

<input type="hidden" name="billing_tel" value="'.$phone.'" />

<input type="hidden" name="merchant_param1" value="'.$user_id.'" />

<input type="hidden" name="merchant_param2" value="'.$token.'" />

<input type="hidden" name="merchant_param3" value="'.$order_id.'" />

<input type="hidden" name="merchant_param4" value="'.$discount_id.'" />

<input type="hidden" name="merchant_param5" value="'.$encrypted_order_id.'" />

<input type="hidden" name="customer_identifier" value=""/>

<INPUT TYPE="submit" value="CheckOut">

<p>Payment Redirect to CCAVENUES, Please wait...</p>

</form>

<script>document.getElementById("ccVenue_submit").submit();</script>';

}

public function ccavRequestHandler()

{

$this->load->library('ccvenuecrypto');

error_reporting(0);

$merchant_data='XXXX';

$working_key='XYZ';

//Shared by CCAVENUES

$access_code='XXXXX';//Shared by CCAVENUES

$rcvdString=$this->ccvenuecrypto->decrypt($_POST['merchant_param5'],$workingKey);

$decryptValues=explode('=', $rcvdString);

$oid=$decryptValues[0]-12844; // Custom decription as per your need

$token=$decryptValues[1];

$user_id=$_POST['merchant_param1'];

if($oid!=$_POST['order_id'])

{

$this->failureCCVenuePayment($oid, $user_id, $token, $_POST['tid']);

}

$orders = $this->Front_model->getDataRow('orders', 'order_id', $oid);

if(empty($orders))

{

$this->failureCCVenuePayment($oid, $user_id, $token, $_POST['tid']);

}

if($_POST['currency']!='INR')

{

$this->failureCCVenuePayment($oid, $user_id, $token, $_POST['tid']);

}

if($orders['order_total']!=$_POST['amount'])

{

$this->failureCCVenuePayment($oid, $user_id, $token, $_POST['tid']);

}

if($token!=$_POST['merchant_param2'])

{

$this->failureCCVenuePayment($oid, $user_id, $token, $_POST['tid']);

}

if($orders['txnid']!=$_POST['tid'])

{

$this->failureCCVenuePayment($oid, $user_id, $token, $_POST['tid']);

}

foreach ($_POST as $key => $value){

$merchant_data.=$key.'='.urlencode($value).'&';

}

$encrypted_data=$this->ccvenuecrypto->encrypt($merchant_data,$working_key);

// Method for encrypting the data.

// https://test.ccavenue.com/transaction/transaction.do?command=initiateTransaction

?>

<form method="post" name="redirect" action="https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction">

<?php

echo "<input type=hidden name=encRequest value=$encrypted_data>";

echo "<input type=hidden name=access_code value=$access_code>";

?>

</form>

</center>

<script language='javascript'>document.redirect.submit();</script>

<?php

}

public function ccavResponseHandler()

{

$this->load->library('ccvenuecrypto');

error_reporting(E_ALL);

ini_set('display_errors', 1);

$workingKey='XYZ';//Shared by CCAVENUES

$encResponse=$_POST["encResp"];

//This is the response sent by the CCAvenue Server

$rcvdString=$this->ccvenuecrypto->decrypt($encResponse,$workingKey);

//Crypto Decryption used as per the specified working key.

$order_status="";

$decryptValues=explode('&', $rcvdString);

$dataSize=sizeof($decryptValues);

$user_id=$token=$order_id=$discount_id=$tracking_id=$bank_ref_no=$tracking_id='';

for($i = 0; $i < $dataSize; $i++)

{

$information=explode('=',$decryptValues[$i]);

if($i==0) $order_id=$information[1];

else if($i==1) $tracking_id=$information[1];

else if($i==2) $bank_ref_no=$information[1];

else if($i==3) $order_status=$information[1];

else if($i==26) $user_id=$information[1];

else if($i==27) $token=$information[1];

else if($i==29) $discount_id=$information[1];

}

if($order_status==="Success")

$this->successCCVenuePayment($user_id, $token, $order_id, $discount_id, $tracking_id, $bank_ref_no, $tracking_id );

else if($order_status==="Aborted")

$this->cancelCCVenuePayment($order_id, $user_id, $token, $tracking_id);

else if($order_status==="Failure")

$this->failureCCVenuePayment($order_id, $user_id, $token, $tracking_id);

else

$this->failureCCVenuePayment($order_id, $user_id, $token, $tracking_id);

}

public function ccvenueCancel()

{

$this->load->library('ccvenuecrypto');

error_reporting(E_ALL);

ini_set('display_errors', 1);

$workingKey='XXXXX';//Shared by CCAVENUES

$encResponse=$_POST["encResp"];

//This is the response sent by the CCAvenue Server

$rcvdString=$this->ccvenuecrypto->decrypt($encResponse,$workingKey);

//Crypto Decryption used as per the specified working key.

$order_status="";

$decryptValues=explode('&', $rcvdString);

$dataSize=sizeof($decryptValues);

$user_id=$token=$order_id=$discount_id=$tracking_id=$bank_ref_no=$tracking_id='';

for($i = 0; $i < $dataSize; $i++)

{

$information=explode('=',$decryptValues[$i]);

if($i==0) $order_id=$information[1];

else if($i==1) $tracking_id=$information[1];

else if($i==2) $bank_ref_no=$information[1];

else if($i==3) $order_status=$information[1];

else if($i==26) $user_id=$information[1];

else if($i==27) $token=$information[1];

else if($i==29) $discount_id=$information[1];

}

$this->cancelCCVenuePayment($order_id, $user_id, $token, $tracking_id);

}

}



Sign In for comment and like the post.