Pay using ccavenue with webservice in php
- Here is code if you want to pay using cc avenue with web service in php
First Create function in your controller
public function paynow()
{
if (isset($_REQUEST["pay_Request"]) &&
trim($_REQUEST["pay_Request"]) != '')
{
$register_request = $_REQUEST["pay_Request"];
$register_request_decode = json_decode($register_request);
if (isset($register_request_decode -> userid)
&& isset($register_request_decode->address)
)
{
$data=array() // if you want to pas custom parameters from
controler
$userId=$register_request_decode -> userid;
$address=$register_request_decode->address;
$this->load->view('subscription',$data);
}
else
{
echo $this -> common -> missingparamater;
die ;
}
}
else
{
echo $this->common-> invalidparamater;
die ;
}
}
-------- Hers is you View file subscription.php
<html>
<head>
<script>
window.onload = function() {
var d = new Date().getTime();
document.getElementById("tid").value = d;
};
</script>
</head>
<body>
<form method="POST" name="customerData" action="<?php echo base_url('ws_user/ccavRequestHandler'); ?>">
<input type="hidden" name="tid" id="tid" />
<input type="hidden" name="merchant_id" value="114350"/>
<input type="hidden" name="payment_option" value="OPTNBK">
<input type="hidden" name="order_id" value="123654789"/>
<input type="hidden" name="merchant_param1" value="<?php echo $wheretopay; ?>"/>
<input type="hidden" name="merchant_param2" value="<?php echo $userId; ?>"/>
<input type="hidden" name="amount" value="<?php echo $amount; ?>"/>
<input type="hidden" name="currency" value="INR"/>
<input type="hidden" name="redirect_url" value="<?php echo base_url('webservice/payDone');?>"/>
<input type="hidden" name="cancel_url" value="<?php echo base_url('webservice/cancelPayment');?>"/>
<input type="hidden" name="billing_name" value="<?php echo $addressData[0]->name;?>"/>
<input type="hidden" name="billing_address" value="<?php echo $addressData[0]->AddressLine1 . ' '. $addressData[0]->Addressline2;?>"/>
<input type="hidden" name="billing_city" value="<?php echo $addressData[0]->city_name;?>"/>
<input type="hidden" name="billing_state" value="<?php echo $addressData[0]->state_name;?>"/>
<input type="hidden" name="billing_zip" value="<?php echo $addressData[0]->Zipcode;?>"/>
<input type="hidden" name="billing_country" value="<?php echo $addressData[0]->country_name;?>"/>
<input type="hidden" name="billing_tel" value="<?php echo $addressData[0]->contact;?>"/>
</form>
<script language='javascript'>document.customerData.submit();</script>
</body>
</html>
******** request handler function in controler
public function ccavRequestHandler()
{
$this->load->view('ccavRequestHandler');
}
********* here is your ccavRequestHandler.php file
<html>
<head>
</head>
<body>
<center>
<?php include('Crypto.php')?>
<?php
error_reporting(0);
$merchant_data='';
$working_key='Your Key';//Shared by CCAVENUES
$access_code='Your Code';//Shared by CCAVENUES
foreach ($_POST as $key => $value){
$merchant_data.=$key.'='.urlencode($value).'&';
}
$encrypted_data=encrypt($merchant_data,$working_key); // Method for encrypting the data.
?>
<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>
********* here is your 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;
}
?>
***************** here is your function to handle success or cancel from cc avenue payment
public function payDone()
{
// $this->common-> sendCommonEmail_user('nectarbits.haren@gmail.com','Payment Done','Hi you have got new success Done');
$encResp=$_REQUEST['encResp'];
$working_key='85026ACEE65526A581B6EDB1554ACAB1';
$decryptValues=explode('&',$this->common->decrypt($encResp,$working_key));
$dataSize=sizeof($decryptValues);
for($i = 0; $i < $dataSize; $i++)
{
$information=explode('=',$decryptValues[$i]);
if($information[0] == 'merchant_param1')
{
$address = $information[1];
}
if($information[0] == 'merchant_param2')
{
$userid = $information[1];
}
if($information[0] == 'order_status')
{
$order_status = $information[1];
}
}
if($order_status == 'Success')
{
$msg='';
$order = array('user_id' => $userid,
'order_status_id' => 1,
'address'=>$address,
'ip' => $_SERVER['REMOTE_ADDR']);
$tablenew = "order";
$this -> commonmodel -> insertdata($tablenew, $order);
$order_insert_id = $this->db->insert_id();
$order_insert = 'ord_'.$order_insert_id;
$valuesArray = array('invoice_no' => $order_insert);
$whereArray = array('order_id' => $order_insert_id);
$this -> commonmodel -> updateData("order", $valuesArray, $whereArray);
$payamounts='0';
$alias='product_name,product_id,quantity,price,userEventTypeEventsId,for_user_id';
$where=array('user_id'=>$userid);
$cartData= $this->commonmodel->getchartdata($alias,'cart',$where);
// echo "<pre>";print_r($cartData);die;
foreach($cartData as $cdata)
{
$prodcutName=$cdata->product_name;
$product_id=$cdata->product_id;
$quantity=$cdata->quantity;
$price=$cdata->price;
$userEventTypeEventsId=$cdata->userEventTypeEventsId;
$for_user_id=$cdata->for_user_id;
$payamounts += ($quantity * $price);
//check if product is already placed or not
$where=array('user_id'=>$userid,'product_id'=>$product_id);
$checkisExist= $this->commonmodel->getData($alias='order_product.product_id','order_product',$where);
//add order
//add invoice_no in order table
//add order_history
$orderhistory = array('order_id' => $order_insert_id,
'product_id' => $product_id,
'order_status_id' => 1,
'quantity'=>$quantity,
'price'=>$price,
'name'=>$prodcutName
);
$tablenew = "order_history";
$this -> commonmodel -> insertdata($tablenew, $orderhistory);
//add order_product
$order_product = array('order_id' => $order_insert_id,
'price' => $price,
'product_id'=>$product_id,
'name'=>$prodcutName,
'quantity' => $quantity,
'for_user_id'=>$for_user_id,
'userEventTypeEventsId'=>$userEventTypeEventsId,
'user_id'=>$userid
);
$delcondition="user_id = $userid AND product_id = $product_id";
$this -> commonmodel->deleteRequest('cart',$delcondition);
$tablenew = "order_product";
$this -> commonmodel -> insertdata($tablenew, $order_product);
}
// insert into new payment tabel
$tablenew = "paymentInfo";
$payinfo=array('userId'=>$userid,'orderId'=>$order_insert_id,'Amount'=>$payamounts,'PayUsing'=>'Net Banking');
$this -> commonmodel -> insertdata($tablenew,$payinfo);
}
if($order_status == 'Success')
{
redirect('webservice/paymentSuccess');
}
else
{
redirect('webservice/paymentFail');
}
}
public function paymentSuccess()
{
}
public function paymentFail()
{
}
public function cancelPayment()
{
$this -> common -> customFailMessage("You have skip cancelled payment.");
}
Comments
Post a Comment