CCVenue Payment Transactional API integration in PHP CodeIgniter

Production API URL:-https://api.ccavenue.com/apis/servlet/DoWebTrans

Staging API URL:- https://apitest.ccavenue.com/apis/servlet/DoWebTran


public function ccaVenueStatusSuccess()

{

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

$txnid=$orders['txnid'];

$time=3*60; // 3 minute

if(isset($_REQUEST['time']) && $_REQUEST['time']>0)

$time=$_REQUEST['time'];

sleep($time);

$working_key='XXXX';//Shared by CCAVENUES

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

$merchant_json_data = array('order_no' => $order_id);

// $merchant_json_data = array('txnid' => $txnid);

$merchant_data = json_encode($merchant_json_data);

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

$final_data = 'enc_request='.$encrypted_data.'&access_code='.$access_code.'&command=orderStatusTracker&request_type=JSON&response_type=JSON';

// https://apitest.ccavenue.com/apis/servlet/DoWebTrans

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.ccavenue.com/apis/servlet/DoWebTrans");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER,'Content-Type: application/json') ;

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $final_data);

// Get server response ...

$result = curl_exec($ch);

curl_close($ch);

$status = '';

$information = explode('&', $result);

print_r($information);

$dataSize = sizeof($information);

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

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

if ($info_value[0] == 'enc_response') {

$status = $this->ccvenuecrypto->decrypt(trim($info_value[1]), $working_key);

}

}

echo 'Status revert is: ' . $status.'<pre>';

$obj = json_decode($status);

print_r($obj);

if($obj->Order_Status_Result->status==0 && $obj->Order_Status_Result->order_status=='Shipped'){

$bank_reponse=$obj->Order_Status_Result->order_bank_response;

$payment_status=2;

if($bank_reponse=='Y' && $order_r_id==$order_id)

{

 // Logic Here

}

}

}

View More...

CCVenue Payment Gateway integration in PHP CodeIgniter

#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);

}

}


View More...

PayU Payment Gateway integration in PHP CodeIgniter

class Order_Api extends CI_Controller

{

     public function __construct()

    {

        parent::__construct();

       date_default_timezone_set('Asia/Kolkata');

     }

     public function payU()

    {

         $MERCHANT_KEY = "";

        $SALT = "";

       // Merchant Key and Salt as provided by Payu.

      $PAYU_BASE_URL = "https://sandboxsecure.payu.in"; // For Sandbox Mode

      $PAYU_BASE_URL = "https://secure.payu.in"; // For Production Mode

     $action = '';

      $posted = array();

     $hash = '';

    if(empty($posted['txnid'])) {

    // Generate random transaction id

    $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);

    } else {

      $txnid = $posted['txnid'];

   }

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

$extra = $discount_id.'||'.$delivery_id.'||'.$delivery_amount.'||'.$discount_amount;

$amount = 10;

$name = "Vishavjeet Singh";

$email = "xyz@gmail.com";

$phone = "88888xxxxx";

$productinfo = array();

$surl = SUCCESSURL;

$furl = "https://www.python-ds.com/failde";

$curl = "https://www.python-ds.com/canceled";

$udf1 = $user_id;

$udf2 = $token;

$udf3 = $order_id;

$udf4 = $discount_id;

$udf5 = "";

$posted['key']=$MERCHANT_KEY;

$posted['hash']=$hash;

$posted['txnid']=$txnid;

$posted['amount']=$amount;

$posted['firstname']=$name;

$posted['email']=$email;

$posted['phone']=$phone;

$posted['productinfo']=$productinfo;

$posted['surl']=$surl;

$posted['furl']=$furl;

$posted['curl']=$curl;

$posted['udf1']=$udf1;

$posted['udf2']=$udf2;

$posted['udf3']=$udf3;

$posted['udf4']=$udf4;

$posted['udf5']=$udf5;

$posted['service_provider']='payu_paisa';

$hash = '';

$hashVarsSeq = explode('|', $hashSequence);

$hash_string = '';

foreach($hashVarsSeq as $hash_var) {

$hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : '';

$hash_string .= '|';

}

$hash_string .= $SALT;

$hash = strtolower(hash('sha512', $hash_string));

$action = $PAYU_BASE_URL . '/_payment';

$form = '

<form action="'.$action.'" method="post" name="payU_submit" id="payU_submit">

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

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

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

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

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

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

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

<textarea hidden name="productinfo">'.$productinfo.'</textarea>

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

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

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

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

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

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

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

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

<input type="hidden" name="service_provider" value="payu_paisa" />

</form>

<script>

document.getElementById("payU_submit").submit();

</script>';

';

}

public function successPayment()

{

$user_id = $_REQUEST['udf1'];

$token = $_REQUEST['udf2'];

$order_id = $_REQUEST['udf3'];

$discount_id = $_REQUEST['udf4'];

$payuMoneyId = $_REQUEST['payuMoneyId'];

$mihpayid = $_REQUEST['mihpayid'];

$txnid = $_REQUEST['txnid'];

}

public function failurePayment()

{

$user_id = $_REQUEST['udf1'];

$token = $_REQUEST['udf2'];

$order_id = $_REQUEST['udf3'];

$discount_id = $_REQUEST['udf4'];

$payuMoneyId = $_REQUEST['payuMoneyId'];

$mihpayid = $_REQUEST['mihpayid'];

$txnid = $_REQUEST['txnid'];

}

public function cancelPayment()

{

           $user_id = $_REQUEST['udf1'];

            $token = $_REQUEST['udf2'];

             $order_id = $_REQUEST['udf3'];

               $discount_id = $_REQUEST['udf4'];

          $payuMoneyId = $_REQUEST['payuMoneyId'];

          $mihpayid = $_REQUEST['mihpayid'];

      $txnid = $_REQUEST['txnid'];

   } 

}

View More...

How can create cache in Codeigniter?

public function index(){

 $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

if($this->cache->apc->is_supported()) echo 'driver is available';

else echo 'driver is not available';

if(!$this->cache->get('homePage'))

{

$myArray = array('one'=>1, 'two'=>2);

$this->cache->save('homePage', $myArray, 300);

echo 'cache not available';

}

$showCache =null;

$showCache = $this->cache->get('homePage');

var_dump($showCache);die();


}

View More...

How to upload file on AWS S3 Bucket CDN using PHP CodeIgniter

#Step1: Create Aws3 class which file path application/libraries/Aws3.php

Source Code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

include("./vendor/autoload.php");

use Aws\S3\S3Client;

use Aws\Exception\AwsException;

class Aws3{

private $S3;

public function __construct(){

$this->S3 = S3Client::factory([

'key' => 'XXXXXXX',

'secret' => 'XXXXXXX',

'region' => 'ap-south-1'

]);

}

    public function addBucket($bucketName){

    $result = $this->S3->createBucket(array(

    'Bucket'=>$bucketName,

    'LocationConstraint'=> 'ap-south-1'));

    return $result;

   }

  public function sendFile($bucketName, $file_path, $filename){

  $bucketName='vishavjeet-s3-repo';

   $result = $this->S3->putObject(array(

   'Bucket' => $bucketName,

   'Key' => $file_path,

  'SourceFile' => $filename['tmp_name'],

  'ContentType' => 'image/png',

  'StorageClass' => 'STANDARD',

   'ACL' => 'public-read'

  ));

return $result['ObjectURL']."\n";

}

  public function uploadfile_s3($keyName,$file)

  {

      $bucketName = 'vishavjeet-s3-repo';

     $IAM_KEY = 'XXXXX';

    $IAM_SECRET = 'XXXXXX';

    try {

       $s3 = S3Client::factory(

    array(

        'credentials' => array(

        'key' => $IAM_KEY,

       'secret' => $IAM_SECRET

      ),

   'version' => 'latest',

   'region' => 'ap-south-1'

    )

 );

} catch (Exception $e) { 

    die("Error: " . $e->getMessage()); 

  }

  try {

     $s3->putObject(

     array(

         'Bucket'=>$bucketName,

         'Key' => $keyName,

         'ACL' => 'public-read',

        'CacheControl' => 'max-age=172800',

        "Expires"=> gmdate("D, d M Y H:i:s T", strtotime("+7 days")),

        'SourceFile' =>$file,

        /*'StorageClass' => 'REDUCED_REDUNDANCY'*/

         )

     );

   } catch (S3Exception $e) {

        die('Error:' . $e->getMessage());

     } catch (Exception $e) {

           die('Error:' . $e->getMessage());

         }

      }

}


#Step2: load library in Controller

class Product_Api extends CI_Controller

{

      public function __construct()

     {

        parent::__construct();

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

        date_default_timezone_set('Asia/Kolkata');

    }

  public function addImage()

  {

         $keyName = 'uploads/product/'.$product_id.'/'.$_FILES[$filename]['name'];

         $file = $_FILES[$filename]['tmp_name'];

      $response=$this->uploadfile_s3($keyName,$file)

   }

}

View More...