PHP

Integrate CCavenue Payment Gateway In PHP With Simple Step — Lelocode

23 April, 2021 10 minute read

Integrate CCavenue Payment Gateway In PHP With Simple Step — Lelocode

 

 

Summary: CCavenue is a popular Payment Gateway that allows online merchants to process their all online transactions under one roof that ranges from credit card transactions to direct debit from online bank accounts. Here is the process to integrate using PHP.

Getting Started

CCAvenue payment integration kit allows merchants to instantly collect payments from their users using various payment modes like credit cards, debit cards, cash cards, net banking etc.

The CCAvenue payment integration supports a seamless payment experience on your platform, while protecting your application from payment frauds and complexity related to various regulations.

Steps to Integrate Payment Gateway :

At first create a merchant account on

https://dashboard.ccavenue.com/web/registration.do?command=Preview

 

2. After successful registration CCavenue team will verify your provided phone no. and email Id.

 

3. After account activation your website will be verified and the team will provided your the merchant Id and working key which will be needed while integrating the payment gateway.

4. Login to your account on

https://dashboard.ccavenue.com/jsp/merchant/merchantLogin.jsp

5. Go to dashboard and click on setting tab then select API KEYS.

6. Copy all three keys Merchant ID, Access Code and Working Key.

 

Testing and Production Environment

 

CCAvenue test and production environments are separate.

Merchants need an active CCAvenue account to use the test environment and production environment. Merchants will have to log in to their CCAvenue M.A.R.S account and get the API credentials for using these environments.

All transactions initiated by the merchant on our test environment are not processed. Test environment is strictly for testing the request and response functions.

After successfully testing the integration, merchant can move to the production environment by changing the URL.

CCAvenue test URL is: https://test.ccavenue.com

CCAvenue production URL is: https://secure.ccavenue.com

To test the integration login to your CCAvenue M.A.R.S account, under Settings tab -> API Keys page; copy the following credentials:

1. Merchant ID

2. Access Code

3. Encryption Key

Note : Transactions to this test URL will not be processed.

After making sure you can move to production url

I.e https://secure.ccavenue.com which is mentioned in RequestHandler.php

 

Now follow the following code steps to integrate Ccavenue :

 

1.Create a file containing the form e.g. index.php and paste the below code:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <form method="post" class="form-inline" action="dataform.php">
    <div class="form-group">
           <input type=“name” class="form-control" id=“name” placeholder="Enter name” name=“name”>
    </div>
    <div class="form-group">
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
    <div class="form-group">
            <input type=“phone” class="form-control" id=“phone” placeholder="Enter phone” name=“phone”>
    </div>
    <div class="form-group">
            <input type="amount" class="form-control" id=“amount” placeholder="Enter amount” name=“amount”>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>
</body>
</html>

2. Create a new file dataform.php with the following code:

<html>
<head>
<script>
window.onload = function() {
var d = new Date().getTime();
document.getElementById("tid").value = d;
};
</script>
</head>
<body>
<?php
// product_id is used to find product details like amount etc.
$product_id = isset($_POST['product_id']) ? $_POST['product_id'] : 0;
$name = isset($_POST[‘name’]) ? $_POST[‘name’] : '' ;
$email = isset($_POST['email']) ? $_POST['email'] : '';
$phone = isset($_POST['phone']) ? $_POST['phone'] : '';
?><form method="post" name="customerData" action="ccavRequestHandler.php">
<table width="40%" height="100" border='1' align="center"><caption><font size="4" color="blue"><b>CCAVENUE PAYMENT GATEWAY</b></font></caption></table>
<table width="40%" height="100" border='1' align="center">
<tr>
<td>Parameter Name:</td><td>Parameter Value:</td>
</tr>
<tr>
<td colspan="2"> Compulsory information</td>
</tr>
<tr>
<td>Transaction ID :</td><td><input type="text" name="tid" id="tid" readonly /></td>
</tr>
<tr>
<td>Currency :</td><td><input type="text"  name="currency" value="INR"/></td>
</tr>
<tr>
<td>Language :</td><td><input type="text" name="language" value="EN"/></td>
 </tr>
<input type="hidden" name="billing_name" value="<?php echo $name;?>"/>
<input type="hidden" name="product_id" value="<?php echo $product_id;?>"/></td>
<input type="hidden" name="billing_address" value=“website_name”/></td>
<input type="hidden" name="billing_city" value=“city”/></td>
<input type="hidden" name="billing_state" value=“state”/></td>
<input type="hidden" name="billing_zip" value=“12345”/></td>
<input type="hidden" name="billing_country" value="India"/></td>
<input type="hidden" name="billing_tel" value="<?php echo $phone;?>"/></td>
<input type="hidden" name="billing_email" value="<?php echo $email;?>"/></td>
<input type="hidden" name="delivery_name" value=“abcd”/></td>
<input type="hidden" name="delivery_address" value=“abcd”/></td>
<input type="hidden" name="delivery_city" value=“abcd”/></td>
<input type="hidden" name="delivery_state" value=“abcd”/></td>
<input type="hidden" name="delivery_zip" value=“1234”/></td>
<input type="hidden" name="delivery_country" value="India"/></td>
<input type="hidden" name="delivery_tel" value="<?php echo $phone;?>"/></td>
<input type="hidden" name="merchant_param1" value="additional Info."/></td>
<input type="hidden" name="merchant_param2" value="additional Info."/></td>
<input type="hidden" name="merchant_param3" value="additional Info."/></td>
<input type="hidden" name="merchant_param4" value="additional Info."/></td>
<input type="hidden" name="merchant_param5" value="additional Info."/></td>
<input type="hidden" name="promo_code" value=""/></td>
<input type="hidden" name="customer_identifier" value=""/></td>
<tr>
<td></td><td><input type="submit" value="CheckOut"></td>
</tr>
<input type="hidden"  name="integration_type" value="iframe_normal"/>
<input type="hidden" name="redirect_url" value="http://youdomainname.com/ccavResponseHandler.php"/>
<input type="hidden" name="cancel_url" value="http://youdomainname.com/ccavResponseHandler.php"/>
</table>
</form>
</body>
</html>

Note: If you get Null response while transaction from CCavenue server then try with “https:/…..” In place of “http” in redirect_url and cancel_url.

3. Create a new file named ccavRequestHandler.php and paste the below code:

<html>
<head>
<title> Iframe</title>
</head>
<body>
<center>
<?php include('Crypto.php')?>
<?php 
   error_reporting(0);
   $working_key='';//Shared by CCAVENUES
   $access_code='';//Shared by CCAVENUES
   $merchant_data='';   foreach ($_POST as $key => $value){
      $merchant_data.=$key.'='.$value.'&';
   }
    $order_id='234';
     // use product id and find amount and other details of product.
     $product_id=isset($_POST['product_id']) ? $_POST['product_id'] : '' ;
     $sql_statement = 'put your sql query to find product amount and merchant_id';
     // fetch product amount from sql query
     $amount_value = $sql_statement->amount;
     // fetch merchant id from sql query
     $merchant_id = $sql_statement->merchant_id;
     $merchant_data.='merchant_id'.'='.$merchant_id.'&'.'amount'.'='.$amount_value.'&'.'order_id'.'='.$order_id.'&';     $encrypted_data=encrypt($merchant_data,$working_key); // Method for encrypting the data.   $production_url='https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction&encRequest='.$encrypted_data.'&access_code='.$access_code;
?>
<iframe src="<?php echo $production_url?>" id="paymentFrame" width="482" height="450" frameborder="0" scrolling="No" ></iframe><script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
       $(document).ready(function(){
           window.addEventListener('message', function(e) {
              $("#paymentFrame").css("height",e.data['newHeight']+'px');     
          }, false);      });
</script>
</center>
</body>
</html>

 

Note: Make sure that you have changes working_key and access_code after login from merchant account.

 

4. Now create ccavResponseHandler.php and paste the below code:

<?php include('Crypto.php')?>
<?php   error_reporting(0);   $workingKey='';       //Working Key should be provided here.
   $encResponse=$_POST["encResp"];          //This is the response sent by the CCAvenue Server
   $rcvdString=decrypt($encResponse,$workingKey);    //Crypto Decryption used as per the specified working key.
   $order_status="";
   $decryptValues=explode('&', $rcvdString);
   $dataSize=sizeof($decryptValues);
   echo "<center>";   for($i = 0; $i < $dataSize; $i++) 
   {
      $information=explode('=',$decryptValues[$i]);
      if($i==3)  $order_status=$information[1];
   }   if($order_status==="Success")
   {
      echo "<br>Thank you for shopping with us. Your credit card has been charged and your transaction is successful. We will be shipping your order to you soon.";   }
   else if($order_status==="Aborted")
   {
      echo "<br>Thank you for shopping with us.We will keep you posted regarding the status of your order through e-mail";   }
   else if($order_status==="Failure")
   {
      echo "<br>Thank you for shopping with us.However,the transaction has been declined.";
   }
   else
   {
      echo "<br>Security Error. Illegal access detected";   }   echo "<br><br>";   echo "<table cellspacing=4 cellpadding=4>";
   for($i = 0; $i < $dataSize; $i++) 
   {
      $information=explode('=',$decryptValues[$i]);
          echo '<tr><td>'.$information[0].'</td><td>'.$information[1].'</td></tr>';
   }   echo "</table><br>";
   echo "</center>";
?>

5. Now the last step, create a new file Crypto.php and paste below code:

Note: Here the encryption and decryption methods of text and key and mentioned.

<?php   error_reporting(0);   function encrypt($plainText,$key)
   {
      $secretKey = hextobin(md5($key));
      $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
      $openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
      $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
      $plainPad = pkcs5_pad($plainText, $blockSize);
      if (mcrypt_generic_init($openMode, $secretKey, $initVector) != -1) 
      {
            $encryptedText = mcrypt_generic($openMode, $plainPad);
                mcrypt_generic_deinit($openMode);      } 
      return bin2hex($encryptedText);
   }   function decrypt($encryptedText,$key)
   {
      $secretKey = hextobin(md5($key));
      $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
      $encryptedText=hextobin($encryptedText);
      $openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
      mcrypt_generic_init($openMode, $secretKey, $initVector);
      $decryptedText = mdecrypt_generic($openMode, $encryptedText);
      $decryptedText = rtrim($decryptedText, "\0");
      mcrypt_generic_deinit($openMode);
      return $decryptedText;   }
   //*********** Padding Function *********************    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 ********   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; 
         } 
?>

Super you done good job now your payment gateway ready to accept payment ….Awesome….Boom!!

Shobhit Singh

Shobhit Singh

Lead Developer at Signifier Technology Pvt Ltd

Leave a Reply

More from Rannkly

Our best blog on PHP

PHP

PHP Latitude Longitude To Address

Vishnu Sharma

Vishnu Sharma

1 minute read

Read more

Laravel Latest Version 5.7 With Material...

Stuti varshney

Stuti varshney

1 minute read

Read more
PHP

Sending Mobile Verification OTP SMS Usin...

Shobhit Singh

Shobhit Singh

2 minute read

Read more