-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayuIpnServer.php
170 lines (121 loc) · 3.57 KB
/
PayuIpnServer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
*
* Mehmet Ali UYSAL
* https://github.com/mehmetaliuysal/payu-php-ipn-server.git
*
* */
class PayuIpnServer {
private static $secret = 'XXXXXXXXXXXX'; // secret key
private static $ipMask = '83.96.157.64/27'; // Indicated in Payu Documentations
function __construct() {
}
function init() {
$returnArr = array('status' => false, 'error' => null, 'result' => null);
$checkReq = self::checkRequirements($_POST);
if ($checkReq['status'] == false)
return $checkReq;
$pass = self::$secret;
$result = '';
$return = '';
$signature = $_POST["HASH"];
while (list($key, $val) = each($_POST)) {
$$key = $val;
if ($key != "HASH") {
if (is_array($val))
$result .= self::ArrayExpand($val);
else {
$size = strlen(stripslashes($val));
$result .= $size . stripslashes($val);
}
}
}
$dateReturn = date("YmdGis");
$return = strlen($_POST["IPN_PID"][0]) . $_POST["IPN_PID"][0] . strlen($_POST["IPN_PNAME"][0]) . $_POST["IPN_PNAME"][0];
$return .= strlen($_POST["IPN_DATE"]) . $_POST["IPN_DATE"] . strlen($dateReturn) . $dateReturn;
$hash = self::hmac($pass, $result);
if ($hash != $signature) {
$returnArr['error'] = 'Bad IPN signature';
/* Here can send mail to technical admin*/
return $returnArr;
}
$resultHash = self::hmac($pass, $return);
$returnArr['result'] = "<EPAYMENT>" . $dateReturn . "|" . $resultHash . "</EPAYMENT>";
/*
* Here check equality with your order data.
* if not equal, set $returnArr['error'] wtih your custom error msg and
* not set $returnArr['status'] = true;
*
* */
$returnArr['status'] = true;
return $returnArr;
}
static function checkRequirements($args) {
$returnArr = array('status' => false, 'error' => NULL);
# if u now Payu Ip range in your locale, you can uncomment this control
/*
if (self::cidrMatch(self::getIp(), self::$ipMask) == false) {
$returnArr['error'] = 'Unauthorized ip address:'.self::getIp();
return $returnArr;
}
*/
if (empty($args)) {
$returnArr['error'] = 'Invalid arg';
return $returnArr;
}
if (!isset($args['ORDERSTATUS']) || $args['ORDERSTATUS'] != 'COMPLETE') {
$returnArr['error'] = 'No action required';
return $returnArr;
}
if (!isset($args['REFNOEXT'])) {
$returnArr['error'] = 'RefNo not found';
return $returnArr;
}
$returnArr['status'] = true;
return $returnArr;
}
static private function cidrMatch($ip, $range) {
list($subnet, $bits) = explode('/', $range);
$ip = ip2long($ip);
$subnet = ip2long($subnet);
$mask = -1<<(32 - $bits);
$subnet &= $mask;
return ($ip & $mask) == $subnet;
}
public static function getIp() {
if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
if (strstr($ip, ',')) {
$tmp = explode(',', $ip);
$ip = trim($tmp[0]);
}
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
return $ip;
}
static function ArrayExpand($array) {
$retval = "";
for ($i = 0; $i < sizeof($array); $i++) {
$size = strlen(stripslashes($array[$i]));
$retval .= $size . stripslashes($array[$i]);
}
return $retval;
}
static function hmac($key, $data) {
$b = 64;
// byte length for md5
if (strlen($key) > $b) {
$key = pack("H*", md5($key));
}
$key = str_pad($key, $b, chr(0x00));
$ipad = str_pad('', $b, chr(0x36));
$opad = str_pad('', $b, chr(0x5c));
$k_ipad = $key ^ $ipad;
$k_opad = $key ^ $opad;
return md5($k_opad . pack("H*", md5($k_ipad . $data)));
}
}