Integrate CCAvenue Payment Gateway using PHP
CCAvenue is a popular Payment Gateway that allows e-transaction. It is designed to integrate quickly and hassle free. This is authorized by Indian financial institutions and one of the most secure payment gateway to transact money for an online e-commerce/shopping. I will explain you step by step process to Integrate CCAvenue Payment Gateway using PHP for accept online payment.This is a very simple example, you can just copy paste and change according to your requirement.
Steps 1: Create Account and integrate CCAvenue Payment Gateway
- Create Merchant Account.
- Login to Merchant Dashboard and download CCAvenue payment integration kit.
- CCAvenue verify your website and provide all required details like Marchant ID, Working Key etc.
- Configure CCAvenue security keys and (test/live) endpoint in your PHP application.
- Create Payment form and deploy request / response handlers and with the guidance of the integration kit.
- Create return pages for handling payment notification on success or cancel.
The following screenshot shows the CCAvenue merchant sign-up form used to create a new account as the first step of this payment integration.
Before started to implement the Integrate CCAvenue Payment Gateway using PHP, look files structure:
- integrate-ccavenue-payment-gateway-using-php
- css
- style.css
- images
- templates
- header.php
- footer.php
- config.php
- index.php
- ccAvenueRequestHandler.php
- Crypto.php
- ccAvenueResponseHandler.php
- css
Step 2: Create a file named index (index.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php require_once "config.php"; ?> <?php include('templates/header.php');?> <div id="ccav-payment-form"> <form name="frmPayment" action="ccAvenueRequestHandler.php" method="POST"> <input type="hidden" name="merchant_id" value="<?php echo CCA_MERCHANT_ID; ?>"> <input type="hidden" name="language" value="EN"> <input type="hidden" name="currency" value="INR"> <input type="hidden" name="redirect_url" value="http://localhost/integrate-ccavenue-payment-gateway-using-php/ccavResponseHandler.php"> <input type="hidden" name="cancel_url" value="http://localhost/integrate-ccavenue-payment-gateway-using-php/ccavResponseHandler.php"> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Integrate CCAvenue Payment Gateway using PHP</h2> </div> <div class="row align-items-center"> <div class="form-group col-md-12"> <label for="inputEmail4">Amount</label> <input type="text" class="form-control" id="amount" name="amount" placeholder="amount" value="1.00" readonly="readonly"> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-12"> <label for="inputEmail4">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder="Name" value=""> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-12"> <label for="inputEmail4">Address</label> <input type="email" class="form-control" id="address" name="address" placeholder="Address" value=""> </div> </div> <div class="row justify-content-start mt-4"> <div class="col"> <button type="submit" class="btn btn-primary mt-4 float-right btn-payment">Pay Now</button> </div> </div> </div> </section> </form> </div> <?php include('templates/footer.php');?> |
Step 3 : Create a file named ccAvenueRequestHandler (ccAvenueRequestHandler.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<html> <head> <title>Integrate CCAvenue Payment Gateway using PHP</title> </head> <body> <center> <?php include('config.php'); include('Crypto.php'); // config file error_reporting(0); $merchant_data=''; $working_key = CCAVENUE_WORKING_KEY; $access_code = CCAVENUE_ACCESS_CODE; foreach ($_POST as $key => $value){ $merchant_data.=$key.'='.$value.'&'; } $merchant_data .= "order_id=".$orderId; $encrypted_data=encrypt($merchant_data,$working_key); ?> <form method="post" name="redirect" action="https://test.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> </body> </html> |
Step 4: Create a file named Crypto (Crypto.php)
This file contains the functions to encrypt or decrypt the payment information posted via the HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?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; } ?> |
Step 5: Create a file named ccAvenueResponseHandler (ccAvenueResponseHandler.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php include('Crypto.php'); // config file require_once('config.php'); error_reporting(0); $msg = ''; $workingKey=CCAVENUE_WORKING_KEY; //Working Key. $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); for($i = 0; $i < $dataSize; $i++) { $information=explode('=',$decryptValues[$i]); if($i==3) $order_status=$information[1]; } if($order_status==="Success") { $msg = '<div class="alert alert-success" role="alert">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.</div>'; } else if($order_status==="Aborted") { $msg = '<div class="alert alert-danger" role="alert">Thank you for shopping with us.We will keep you posted regarding the status of your order through e-mail</div>'; } else if($order_status==="Failure") { $msg = '<div class="alert alert-danger" role="alert">Thank you for shopping with us.However,the transaction has been declined.</div>'; } else { $msg = '<div class="alert alert-danger" role="alert">Security Error. Illegal access detected</div>'; } ?> <?php include('templates/header.php');?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Integrate CCAvenue Payment Gateway using PHP</h2> </div> <div class="row"> <div class="col-md-12"><?php echo $msg; ?></div> </div> <?php if(!empty($dataSize)) { for($i = 0; $i < $dataSize; $i++) { $information=explode('=',$decryptValues[$i]); echo '<div class="row"><div class="col-md-12"><?php print $information[0]; ?>: <?php echo $information; ?></div></div>'; } } ?> </div> </section> <?php include('templates/footer.php');?> |
Step 6: Create files named (header and footer)
Create a view files named header.php and footer.php inside “templates” folder.
This view contains the headerand footer section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header and footer section.
header.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>Integrate CCAvenue Payment Gateway using PHP | Tech Arise</title> <link rel="icon" type="image/ico" href="<?php print HTTP_IMAGE_PATH; ?>favicon.ico"> <!-- Bootstrap core CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" /> <!-- Custom fonts for this template --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" /> <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="css/style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;"> <div class="container"> <a class="navbar-brand font-weight-bold" href="https://techarise.com"><h1>Tech Arise</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="https://techarise.com">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://techarise.com/php-free-script-demos/">Live Demo</a> </li> </ul> </div> </div> </nav> |
footer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<!-- Footer --> <footer class="footer bg-light footer-bg-dark"> <div class="container"> <div class="row"> <div class="col-lg-6 h-100 text-center text-lg-left my-auto"> <ul class="list-inline mb-2"> <li class="list-inline-item"> <a href="#">About</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Contact</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> </ul> <p class="text-muted small mb-4 mb-lg-0">Copyright © 2011 - <?php print date('Y', time());?> <a href="https://techarise.com/">TECHARISE.COM</a> All rights reserved.</p> </div> <div class="col-lg-6 h-100 text-center text-lg-right my-auto"> <ul class="list-inline mb-0"> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-facebook fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-twitter-square fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item"> <a href="#"> <i class="fab fa-instagram fa-2x fa-fw"></i> </a> </li> </ul> </div> </div> </div> </footer> <script> var baseurl = "<?php print site_url();?>"; </script> <!-- Bootstrap core JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> </body> </html> |