-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipn_listener.php
283 lines (241 loc) · 6.26 KB
/
ipn_listener.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* @file
* IPN listener to receive PayPal IPN messages.
*/
if(!defined('e107_INIT'))
{
require_once('../../class2.php');
}
if(!e107::isInstalled('paypal_donation'))
{
header('Location: ' . e_BASE . 'index.php');
exit;
}
// [PLUGINS]/paypal_donation/languages/[LANGUAGE]/[LANGUAGE]_front.php
e107::lan('paypal_donation', false, true);
/**
* Class ipn_listener.
*/
class ipn_listener
{
/**
* Store plugin preferences.
*
* @var mixed|null
*/
private $plugPrefs = null;
/**
* Constructor.
*/
public function __construct()
{
// Get plugin preferences.
$this->plugPrefs = e107::getPlugConfig('paypal_donation')->getPref();
$ipn = $this->readIPN();
$valid = $this->validateIPN($ipn);
if($valid === true)
{
$this->processIPN();
}
}
/**
* Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
* Instead, read raw POST data from the input stream.
*
* @return string
*/
private function readIPN()
{
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach($raw_post_array as $keyval)
{
$keyval = explode('=', $keyval);
if(count($keyval) == 2)
{
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
}
// Read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'.
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc'))
{
$get_magic_quotes_exists = true;
}
else
{
$get_magic_quotes_exists = false;
}
foreach($myPost as $key => $value)
{
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)
{
$value = urlencode(stripslashes($value));
}
else
{
$value = urlencode($value);
}
$req .= "&$key=$value";
}
return $req;
}
/**
* POST IPN data back to PayPal to validate.
*
* @param string $req
* @return bool
*/
private function validateIPN($req = null)
{
if($req)
{
if((int) $this->plugPrefs['sandbox_mode'] === 1)
{
$url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
else
{
$url = 'https://www.paypal.com/cgi-bin/webscr';
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if(!($res = curl_exec($ch)))
{
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);
if(strcmp($res, "VERIFIED") == 0)
{
return true;
}
}
return false;
}
/**
* Process IPN data:
* - Done: Check whether the payment_status is Completed.
* - Done: Check that txn_id has not been previously processed.
* - Done: Check that receiver_email is your Primary PayPal email.
* - Done: Check that the "custom" value is a valid menu item.
* - Done: Process the notification.
*/
private function processIPN()
{
$log = e107::getLog();
$db = e107::getDb();
$tp = e107::getParser();
$logging = (bool) $this->plugPrefs['logging_failed_ipn'];
$validate_email = (bool) $this->plugPrefs['validate_email'];
// Check whether the payment_status is Completed.
$payment_status = vartrue($_POST['payment_status']);
if(strtoupper($payment_status) != 'COMPLETED')
{
if($logging)
{
$log->add('Status is not completed', $_POST, E_LOG_INFORMATIVE, 'IPN');
}
exit;
}
// Check that txn_id has not been previously processed.
$txn_id = vartrue($_POST['txn_id'], 0);
$exists = $db->count('paypal_donation_ipn', '(*)', 'pdi_txn_id = "' . $tp->toDB($txn_id) . '" ');
if($exists > 0)
{
if($logging)
{
$log->add('Existing TXN ID', $_POST, E_LOG_INFORMATIVE, 'IPN');
}
exit;
}
// Check that receiver_email is your Primary PayPal email.
if($validate_email)
{
$receiver_email = vartrue($_POST['receiver_email']);
if((int) $this->plugPrefs['sandbox_mode'] === 1)
{
$business = $this->plugPrefs['email_sandbox'];
}
else
{
$business = $this->plugPrefs['email_live'];
}
if($receiver_email != $business)
{
if($logging)
{
$log->add('Invalid receiver email', $_POST, E_LOG_INFORMATIVE, 'IPN');
}
exit;
}
}
// Check that the "custom" value is a valid menu item.
$custom = vartrue($_POST['custom'], '');
$segments = explode('|', $custom);
$pd_id = (int) $segments[0];
$exists = $db->count('paypal_donation', '(*)', 'pd_id = ' . $pd_id . ' ');
if($exists == 0)
{
if($logging)
{
$data = array(
'POST' => $_POST,
'SEGMENTS' => $segments,
'PD_ID' => $pd_id,
);
$log->add('Invalid menu item', $data, E_LOG_INFORMATIVE, 'IPN');
}
exit;
}
$pdi_user = vartrue($segments[1], 0);
$pdi_mc_gross = vartrue($_POST['mc_gross'], 0);
$pdi_mc_fee = vartrue($_POST['mc_fee'], 0);
$pdi_mc_currency = vartrue($_POST['mc_currency'], '');
$pdi_payment_date = vartrue($_POST['payment_date'], '');
$pdi_serialized_ipn = serialize($_POST);
// Process IPN.
$data = array(
'pdi_donation' => (int) $pd_id,
'pdi_user' => (int) $pdi_user,
'pdi_txn_id' => $tp->toDB($txn_id),
'pdi_mc_gross' => (float) $pdi_mc_gross,
'pdi_mc_fee' => (float) $pdi_mc_fee,
'pdi_mc_currency' => $tp->toDB($pdi_mc_currency),
'pdi_payment_date' => strtotime($pdi_payment_date),
'pdi_cancelled' => 0,
'pdi_serialized_ipn' => $pdi_serialized_ipn,
);
$id = $db->insert('paypal_donation_ipn', $data);
if($id)
{
$event = e107::getEvent();
$event->trigger('paypal-donation-ipn-insert', $id);
exit;
}
else
{
if($logging)
{
$log->add('SQL Insert', $_POST, E_LOG_INFORMATIVE, 'IPN');
}
exit;
}
}
}
new ipn_listener();
exit;