CCAvenue is the most popular payment gateway in India. As the eCommerce is picking momentum and growing at exponential rate, it is high time you think about setting up a shopping cart. If you are based out of India then CCAvenue is one among the best choice.
composer require kishanio/ccavenue
OR
# Login to ccavanue account and goto resources / web integrated kit option. # Click download integration kit. # Extract zip file and go to CUSTOM_CHECKOUT_FORM_KIT folder and open dataform.htm file in code editor. # Rename dataform.htm into checkout.php. # https://dashboard.ccavenue.com/settings/apiKeys.do go for cred.
checkout.php
<?php
require_once "config.php";
?>
<h1>CCAvenue Payment Gateway Intgration</h1>
<div id="ccav-payment-form">
<form name="frmPayment" action="ccavRequestHandler.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="amount" value="1">
<input type="hidden" name="currency" value="INR">
<input type="hidden" name="redirect_url" value="http://youdomain.com/payment-response.php">
<input type="hidden" name="cancel_url" value="http://youdomain.com/payment-cancel.php">
<div>
<input type="text" name="billing_name" value="" class="form-field" Placeholder="Billing Name">
<input type="text" name="billing_address" value="" class="form-field" Placeholder="Billing Address">
</div>
<div>
<input type="text" name="billing_state" value="" class="form-field" Placeholder="State">
<input type="text" name="billing_zip" value="" class="form-field" Placeholder="Zipcode">
</div>
<div>
<input type="text" name="billing_country" value="" class="form-field" Placeholder="Country">
<input type="text" name="billing_tel" value="" class="form-field" Placeholder="Phone">
</div>
<div>
<input type="text" name="billing_email" value="" class="form-field" Placeholder="Email">
</div>
<div>
<button class="btn-payment" type="submit">Pay Now</button>
</div>
</form>
</div>
ccavRequestHandler.php
<html>
<head>
<title> CCAvenue Payment Gateway Integration kit</title>
</head>
<body>
<center>
<?php include('Crypto.php')?>
<?php require_once "config.php"; ?>
<?php
error_reporting(0);
$merchant_data='';
$working_key = CCA_WORKING_KEY;
$access_code = CCA_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">
<!-- here set the path of test or live ccavanue -->
<?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>
This file contains the functions to encrypt or decrypt the payment information posted via the HTML form. It also includes util functions for padding and conversion.
crypto.php
<?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;
}
?>
For going live and use the CCAvenue secure production environment, the https://test.ccavenue.com target should be replaced with https://secure.ccavenue.com.
Note: This file uses the deprecated function mcrypt_ function. These functions are deprecated as of PHP version 5.5.0 and it will not work in PHP 7. If you want a alternate for the encrypt decrypt utilities to be supported in latest PHP version, the following code will be helpful.
<?php
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);
$encryptedText = openssl_encrypt($plainText, "AES-128-CBC", $secretKey, OPENSSL_RAW_DATA, $initVector);
$encryptedText = bin2hex($encryptedText);
return $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);
$decryptedText = openssl_decrypt($encryptedText,"AES-128-CBC", $secretKey, OPENSSL_RAW_DATA, $initVector);
return $decryptedText;
}
...
...
?>
config.php
<?php define("CCA_MERCHANT_ID", "YOUR_MERCHANT_ID");
define("CCA_ACCESS_CODE", "YOUR_ACCESS CODE");
define("CCA_WORKING_KEY", "YOUR WORKING KEY");