Integration Payment Gateway PHP 

CCAvenue payment integration using PHP

CCavenue payment gateway is simple to integrate and the following are things you need before integrating.

  • We need a merchant id and working for implementing payment gateway
  • These two details will be given by CCavenue
  • They use a redirect model for payment
  • ie the user will be rediected to there site for making the payment
  • Before for sending the details to CCavenue we need to save the details so that we can verify the details after the payment
  • we need to generate a signature(checksum) that is it contain some details like amount, orderid, working key, merchantid and rediecturl
  • On the buy page on submitting we need a serverside validation of the details the user have entered and after that the user details need to be stored in our DB after the payment is successfully completed
  • After entering all the details we need to redirect to another page it contain a form and the CCavenue form variables which are in hidden and we need to auto submit after loading the page.

This is the First page before payment.

<?php
 $Merchant_Id = "your_merchantid";//
 $Amount = "amount";
 $Order_Id ="orderid";//unique Id that should be passed to payment gateway
 $WorkingKey = "working_key";//Given to merchant by ccavenue
 $Redirect_Url ="sucessurl";
 $Checksum = getCheckSum($Merchant_Id,$Amount,$Order_Id ,$Redirect_Url,$WorkingKey);                 // Validate All value
 
 //creating a signature using the given details for security reasons
 function getchecksum($MerchantId,$Amount,$OrderId ,$URL,$WorkingKey)
 {
 $str ="$MerchantId|$OrderId|$Amount|$URL|$WorkingKey";
 $adler = 1;
 $adler = adler32($adler,$str);
 return $adler;
 }
 //functions
 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;
 }
 //leftshift function
 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) ;
 }
 //cdec function
 function cdec($num)
 {
 for ($n = 0 ; $n < strlen($num) ; $n++)
 {
 $temp = $num[$n] ;
 $dec = $dec + $temp*pow(2 , strlen($num) - $n - 1);
 }
 return $dec;
 }
 ?>
 <form id="ccavenue" method="post" action="https://www.ccavenue.com/shopzone/cc_details.jsp">
 <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="success 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 payment to get the Payment Gateway Response. After the successful payment,CCavenue will redirect to our success url with the some details as post url/get url

Also Read:  How to create server side session using javascript

There are mainly 3 auth_status are there they are

  • Y successfully authorised by gateway.
  • N Unsuccessful transaction.
  • B not authenticated at this pont of time.

We need to verify the payment gateway details send by CCavenue is tampered or not for that we have to use the Verify checksum function.

//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" ;
 }
 
 //functions
 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;
 }
 //leftshift function
 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) ;
 }
 //cdec function
 function cdec($num)
 {
 for ($n = 0 ; $n < strlen($num) ; $n++)
 {
 $temp = $num[$n] ;
 $dec = $dec + $temp*pow(2 , strlen($num) - $n - 1);
 }
 return $dec;
 }
  • we need to pass the details into it, if it returns true than the details are ok and payment was successful redirect to success page
  • if it returns false then the details are tampered while sending to our application and something wrong has occurred and redirect to failed /unsuccessful page

Check out the below link for more details in ccaveneu site.

    click her to see user manual

    Arjun is is a web developer/designer, who is fond of the web. He lives in Manipal, Karnataka, India.

    Related posts