CCAvenue Payment Gateway Integration in PHP

CCAvenue is a popular payment gateway that offers e-merchants a one stop solution for processing e-transaction ranging from credit card transaction to direct debit from their online bank accounts. It is designed to integrate quickly without any hassle.

To integrate CCAvenue payment gateway in your website, you should have CCAvenue account and they will give you a merchant id and a unique key for your website to perform transaction. So here in this tutorial, I have explained easy steps to integrate CCAvenue Payment Gateway with PHP.

Also, read:

As I have explained earlier that it is very easy to integrate, you just need to pass required input filed to CCAvenue to complete transaction. The CCAvenue system expects certain input data with specific names to be sent to the CCAvenue website. For this reason, it is essential that the names of the input fields on the merchant website and the CCAvenue website should match exactly. Also there are some input filed values are mandatory like Merchant_Id, Amount and Order_Id. The Merchant_Id and working key will be assigned to you by CCAvenue. Below is form with action URL to CCAvenue and input field with required values.

<?php
// Merchant id provided by CCAvenue
$Merchant_Id = "1234566";
// Item amount for which transaction perform
$Amount = "100";
// Unique OrderId that should be passed to payment gateway
$Order_Id = "006789";
// Unique Key provided by CCAvenue
$WorkingKey= "";
// Success page URL
$Redirect_Url="success.php";
$Checksum = getCheckSum($Merchant_Id,$Amount,$Order_Id ,$Redirect_Url,$WorkingKey);
?>
<form id="ccavenue" method="post" 
action="https://world.ccavenue.com/servlet/ccw.CCAvenueController">
<input type=hidden name="Merchant_Id" value="Merchant_Id">
<input type="hidden" name="Amount" value="Amount">
<input type="hidden" name="Order_Id" value="Order_Id">
<input type="hidden" name="Redirect_Url" value="<?php echo $Redirect_Url; ?>">
<input type="hidden" name="TxnType" value="A">
<input type="hidden" name="ActionID" value="TXN">
<input type="hidden" name="Checksum" value="<?php echo $Checksum; ?>">
<input type="hidden" name="billing_cust_name" value="name of user">
<input type="hidden" name="billing_cust_address" value="address of user">
<input type="hidden" name="billing_cust_country" value="user country">
<input type="hidden" name="billing_cust_state" value="state of user">
<input type="hidden" name="billing_cust_city" value="city">
<input type="hidden" name="billing_zip" value="zip/pin code">
<input type="hidden" name="billing_cust_tel" value="telphone no">
<input type="hidden" name="billing_cust_email" value="emailid">
<input type="hidden" name="delivery_cust_name" value="user name">
<input type="hidden" name="delivery_cust_address" value="delivering address">
<input type="hidden" name="delivery_cust_country" value="delivering country">
<input type="hidden" name="delivery_cust_state" value="delivering state">
<input type="hidden" name="delivery_cust_tel" value="telphone no">
<input type="hidden" name="delivery_cust_notes" value="this is a test">
<input type="hidden" name="Merchant_Param" value="">
<input type="hidden" name="billing_zip_code" value="zip/pin">
<input type="hidden" name="delivery_cust_city" value="city">
<input type="submit" value="Buy Now" />
</form>

After the successful payment, the CCavenue will redirect to your provided success URL with the some transaction details with $_GET variable having auth_status. If it is “Y” then it is successfully authorized by gateway. If it is “N” then it is unsuccessful transaction. If it is “B” then it is not authenticated at this point of time.


We also need to verify the payment gateway details send by CCavenue is tampered or not for that we have to use the verify checksum function verifychecksum($MerchantId,$OrderId,$Amount,$AuthDesc,$CheckSum,$WorkingKey). We need to pass the transaction details to this function. If it returns true than the details are ok and payment page was successfully redirect to success page. If it returns false then the details are tampered while sending to our application and something wrong has occurred and need to redirect to unsuccessful page .

Use below PHP functions to get check sum using function  getchecksum() and verify returned checksum using function verifychecksum().

<?php
// Get the checksum
function getchecksum($MerchantId,$Amount,$OrderId ,$URL,$WorkingKey)
{
$str ="$MerchantId|$OrderId|$Amount|$URL|$WorkingKey";
$adler = 1;
$adler = adler32($adler,$str);
return $adler;
}
?>
<?php
//Verify the the checksum
function verifychecksum($MerchantId,$OrderId,$Amount,
$AuthDesc,$CheckSum,$WorkingKey)
{
$str = "$MerchantId|$OrderId|$Amount|$AuthDesc|$WorkingKey";
$adler = 1;
$adler = adler32($adler,$str);
if($adler == $CheckSum)
return "true";
else
return "false" ;
}
?>
<?php
function leftshift($str , $num) {
$str = DecBin($str);
for( $i = 0 ; $i < (64 – strlen($str)) ; $i++)
$str = "0".$str ;
for($i = 0 ; $i < $num ; $i++) {
$str = $str."0";
$str = substr($str , 1 ) ;
}
return cdec($str) ;
}
?>
<?php
function cdec($num) {
for ($n = 0 ; $n < strlen($num) ; $n++) {
$temp = $num[$n] ;
$dec = $dec + $temp*pow(2 , strlen($num) – $n – 1);
}
return $dec;
}
?>
<?php
function adler32($adler , $str) {
$BASE = 65521 ;
$s1 = $adler & 0xffff ;
$s2 = ($adler >> 16) & 0xffff;
for($i = 0 ; $i < strlen($str) ; $i++) {
$s1 = ($s1 + Ord($str[$i])) % $BASE ;
$s2 = ($s2 + $s1) % $BASE ;
}
return leftshift($s2 , 16) + $s1;
}
?>

You may also like:

4 thoughts on “CCAvenue Payment Gateway Integration in PHP

  1. Add a semicolon after merchant id variable. Also this code is not working. Invalid action id detected by ccavenue

Leave a Reply

Your email address will not be published. Required fields are marked *