· 6 years ago · Aug 12, 2019, 02:56 PM
1<?php
2
3
4class CCAvenueController extends Controller
5{
6 public function getRSA(Request $request){
7 $url = "https://test.ccavenue.com/transaction/getRSAKey";
8 $fields = array(
9 'access_code'=>'AVVL02GH84AS74LVSA',
10 'order_id'=>$request->order_id
11 );
12
13 //print_r($fields);
14
15 $postvars='';
16 $sep='';
17 foreach($fields as $key=>$value)
18 {
19 $postvars.= $sep.urlencode($key).'='.urlencode($value);
20 $sep='&';
21 }
22
23 $ch = curl_init();
24
25 curl_setopt($ch,CURLOPT_URL,$url);
26 curl_setopt($ch,CURLOPT_POST,count($fields));
27 curl_setopt($ch, CURLOPT_CAINFO, '/home/betanew/betanew/public_html/public/paymentskey/cacert.pem');
28 curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
29 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
30 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
31 $result = curl_exec($ch);
32
33 echo $result;
34 }
35
36 public function ccavResponseHandler(Request $request){
37
38 //print_r($_POST);
39
40 $workingKey='F3E12CBC68B5D4DA36899ACEDB3B36FF'; //Working Key should be provided here.
41 $encResponse=$_POST["encResp"]; //This is the response sent by the CCAvenue Server
42 $rcvdString=$this->decrypt($encResponse,$workingKey); //Crypto Decryption used as per the specified working key.
43 $order_status="";
44 $decryptValues=explode('&', $rcvdString);
45 $dataSize=sizeof($decryptValues);
46 echo "<center>";
47
48 for($i = 0; $i < $dataSize; $i++)
49 {
50 $information=explode('=',$decryptValues[$i]);
51 if($i==3) $order_status=$information[1];
52 }
53
54 if($order_status==="Success")
55 {
56 echo "<br>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.";
57
58 }
59 else if($order_status==="Aborted")
60 {
61 echo "<br>Thank you for shopping with us.We will keep you posted regarding the status of your order through e-mail";
62
63 }
64 else if($order_status==="Failure")
65 {
66 echo "<br>Thank you for shopping with us.However,the transaction has been declined.";
67 }
68 else
69 {
70 echo "<br>Security Error. Illegal access detected";
71
72 }
73
74 echo "<br><br>";
75
76 echo "<table cellspacing=4 cellpadding=4>";
77 for($i = 0; $i < $dataSize; $i++)
78 {
79 $information=explode('=',$decryptValues[$i]);
80 echo '<tr><td>'.$information[0].'</td><td>'.$information[1].'</td></tr>';
81 }
82
83 echo "</table><br>";
84 echo "</center>";
85 }
86
87
88 public function encrypt($plainText,$key)
89 {
90 $secretKey = $this->hextobin(md5($key));
91 $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
92 $openMode = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
93 $blockSize = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
94 $plainPad = $this->pkcs5_pad($plainText, $blockSize);
95 if (@mcrypt_generic_init($openMode, $secretKey, $initVector) != -1)
96 {
97 $encryptedText = @mcrypt_generic($openMode, $plainPad);
98 @mcrypt_generic_deinit($openMode);
99
100 }
101 return bin2hex($encryptedText);
102 }
103
104 public function decrypt($encryptedText,$key)
105 {
106 $secretKey = $this->hextobin(md5($key));
107 $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
108 $encryptedText=$this->hextobin($encryptedText);
109 $openMode = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
110 @mcrypt_generic_init($openMode, $secretKey, $initVector);
111 $decryptedText = @mdecrypt_generic($openMode, $encryptedText);
112 $decryptedText = rtrim($decryptedText, "\0");
113 @mcrypt_generic_deinit($openMode);
114 return $decryptedText;
115
116 }
117 //*********** Padding Function *********************
118
119 public function pkcs5_pad ($plainText, $blockSize)
120 {
121 $pad = $blockSize - (strlen($plainText) % $blockSize);
122 return $plainText . str_repeat(chr($pad), $pad);
123 }
124
125 //********** Hexadecimal to Binary function for php 4.0 version ********
126
127 public function hextobin($hexString)
128 {
129 $length = strlen($hexString);
130 $binString="";
131 $count=0;
132 while($count<$length)
133 {
134 $subString =substr($hexString,$count,2);
135 $packedString = pack("H*",$subString);
136 if ($count==0)
137 {
138 $binString=$packedString;
139 }
140 else
141 {
142 $binString.=$packedString;
143 }
144
145 $count+=2;
146 }
147 return $binString;
148 }
149}