CCAvenue Payment Gateway Integration using PHP
CCavenue is most popular Payment gateway for online online e-commerce/shopping and e-transaction.It is designed to help merchants accept online payments through a spectrum of new-age payment options including major Credit Cards, Debit Cards (All MasterCard /Visa /Maestro /RuPay Cards), Amex EzeClick, Net Banking, Prepaid Instruments, UPI and Multi Bank EMI options.This is a very simple example, you can just copy paste and change according to your requirement.
Create Account and Integrate Payment Gateway
- At first Create Merchant Account.
- After successful registration CCavenue team will verify your provided phone no. and email Id.
- Login to Merchant Dashboard
- download CCAvenue payment integration kit
- Go to dashboard and click on setting tab then select API KEYS
- 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.
CCavenue Payment Gateway features:
- All the major payment method like debit,credit card , netbanking
- 7 major Credit Cards
- 98+ Debit Cards (All MasterCard /Visa /Maestro /RuPay Cards), Amex EzeClick
- Accept online payment from 53+ banks
- CCavenue payment gateway is very safe and secure
- Easy and hassle free integration
- Payment gateway kit available on multiple programming language
- 16 Prepaid Instruments
- UPI and Up to 13 Multi Bank EMI options
Following screenshot shows the CCAvenue merchant Sign up form used to create a new account as the first step of CCavenue payment integration.
Before started to implement the CCAvenue Payment Gateway Integration using PHP, look files structure:
- ccavenue-payment-gateway-integration-using-php
- css
- style.css
- images
- templates
- header.php
- footer.php
- constants.php
- index.php
- ccAvenueRequestHandler.php
- Crypto.php
- ccAvenueResponseHandler.php
- css
Step 1: Create a file named constants.php and define Merchant ID, Access Code and Working Key
1 2 3 4 5 6 7 8 9 |
<?php //CCAVENUE MERCHANT ID define("CCAVENUE_MERCHANT_ID", "123212"); // CCAVENUE ACCESS CODE define("CCAVENUE_ACCESS_CODE", "XXXXXXXXXXXXX"); // CCAVENUE WORKING KEY define("CCAVENUE_WORKING_KEY", "XXXXXXXXXXXXX"); ?> |
Step 2: Create HTML form named 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php require_once "constants.php"; ?> <?php include('templates/header.php');?> <div id="ccavenue-payment-form"> <form name="ccavenue_frm_payment" 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/ccavenue-payment-gateway-integration-using-php/ccavResponseHandler.php"> <input type="hidden" name="cancel_url" value="http://localhost/ccavenue-payment-gateway-integration-using-php/ccavResponseHandler.php"> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>CCAvenue Payment Gateway Integration using PHP</h2> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Amt</label> <input type="text" class="form-control" id="amount" name="amount" placeholder="amount" value="1.00" readonly="readonly"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Full Name</label> <input type="text" name="billing_name" class="form-control" Placeholder="Name" required> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Email</label> <input type="email" name="billing_email"class="form-control" Placeholder="Email" required> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Phone</label> <input type="text" name="billing_tel" class="form-control" Placeholder="Phone" required> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Address</label> <input type="text" name="billing_address" class="form-control" Placeholder="Address"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Country</label> <input type="text" name="billing_country" class="form-control" Placeholder="Country"> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">State</label> <input type="text" name="billing_state" class="form-control" Placeholder="State"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Zipcode</label> <input type="text" name="billing_zip" class="form-control" Placeholder="Zipcode"> </div> </div> <div class="row"> <div class="col"> <button type="submit" class="btn btn-success mt-4 float-right"><i class="fa fa-credit-card" aria-hidden="true"></i> Pay</button> </div> </div> </div> </section> </form> </div> <?php include('templates/footer.php');?> |
Step 3: Create a file named 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 4 : Create a file named 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 33 |
<html> <head> <title>CCAvenue Payment Gateway Integration using PHP</title> </head> <body> <center> <?php include('constants.php'); include('Crypto.php'); // config file error_reporting(0); $merchant_data=''; $working_key = CCAVENUE_WORKING_KEY; $access_code = CCAVENUE_ACCESS_CODE; // POST Data 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 5: Create a file named 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 54 55 56 |
<?php include('Crypto.php'); // config file require_once('constants.php'); error_reporting(0); $msg = ''; //Working Key. $workingKey=CCAVENUE_WORKING_KEY; //This is the response sent by the CCAvenue Server $encResponse=$_POST["encResp"]; //Crypto Decryption used as per the specified working key. $rcvdString=decrypt($encResponse,$workingKey); $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>CCAvenue Payment Gateway Integration 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');?> |
Create
header.php
and footer.php
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 42 43 44 45 |
<!DOCTYPE html> <html lang="en"> <head> <link rel="canonical" href="https://www.webhaunt.com/" /> <meta name="author" content="WebHaunt"> <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=""> <link rel="icon" href="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt-150x142.png" sizes="32x32" /> <link rel="icon" href="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png" sizes="192x192" /> <link rel="apple-touch-icon-precomposed" href="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png" /> <meta name="msapplication-TileImage" content="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png" /> <title>CCAvenue Payment Gateway Integration using PHP | Web Haunt</title> <!-- 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://webhaunt.com"><h1>Web Haunt</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://webhaunt.com/">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://webhaunt.com/php-script-free-demo/">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 |
<!-- 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="small mb-4 mb-lg-0" style="color: #E6ECF0;">Copyright © 2017 - <?php print date('Y', time());?> <a style="color: #E6ECF0;" href="https://webhaunt.com/">WEBHAUNT.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> <!-- 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> |