-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathipn.php
325 lines (271 loc) · 12 KB
/
ipn.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Listens for Instant Payment Notification from PayPal
*
* This script waits for Payment notification from PayPal,
* then double checks that data by sending it back to PayPal.
* If PayPal verifies this then sets the activity as completed.
*
* @package availability_paypal
* @copyright 2010 Eugene Venter
* @copyright 2015 Daniel Neis
* @author Eugene Venter - based on code by others
* @author Daniel Neis - based on code by others
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_MOODLE_COOKIES', 1);
define('NO_DEBUG_DISPLAY', 1);
// This file do not require login because paypal service will use to confirm transactions.
// @codingStandardsIgnoreLine
require(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/filelib.php');
// PayPal does not like when we return error messages here,
// the custom handler just logs exceptions and stops.
set_exception_handler('availability_paypal_ipn_exception_handler');
// Keep out casual intruders.
if (empty($_POST) or !empty($_GET)) {
die("Sorry, you can not use the script that way.");
}
// Read all the data from PayPal and get it ready for later;
// we expect only valid UTF-8 encoding, it is the responsibility
// of user to set it up properly in PayPal business account,
// it is documented in docs wiki.
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$req .= "&$key=" . urlencode($value);
}
$data = new stdclass();
$data->business = optional_param('business', '', PARAM_TEXT);
$data->receiver_email = optional_param('receiver_email', '', PARAM_TEXT);
$data->receiver_id = optional_param('receiver_id', '', PARAM_TEXT);
$data->item_name = optional_param('item_name', '', PARAM_TEXT);
$data->memo = optional_param('memo', '', PARAM_TEXT);
$data->tax = optional_param('tax', '', PARAM_TEXT);
$data->option_name1 = optional_param('option_name1', '', PARAM_TEXT);
$data->option_selection1_x = optional_param('option_selection1_x', '', PARAM_TEXT);
$data->option_name2 = optional_param('option_name2', '', PARAM_TEXT);
$data->option_selection2_x = optional_param('option_selection2_x', '', PARAM_TEXT);
$data->payment_status = optional_param('payment_status', '', PARAM_TEXT);
$data->pending_reason = optional_param('pending_reason', '', PARAM_TEXT);
$data->reason_code = optional_param('reason_code', '', PARAM_TEXT);
$data->txn_id = optional_param('txn_id', '', PARAM_TEXT);
$data->parent_txn_id = optional_param('parent_txn_id', '', PARAM_TEXT);
$data->payment_type = optional_param('payment_type', '', PARAM_TEXT);
$data->payment_gross = optional_param('mc_gross', '', PARAM_TEXT);
$data->payment_currency = optional_param('mc_currency', '', PARAM_TEXT);
$custom = optional_param('custom', '', PARAM_ALPHANUMEXT);
$custom = explode('-', $custom);
if (count($custom) != 4 || $custom[0] !== 'availability_paypal') {
// This is not IPN for this plugin.
debugging('availability_paypal IPN: custom value does not match expected format');
die();
}
$data->userid = (int) ($custom[1] ?? -1);
$data->contextid = (int) ($custom[2] ?? -1);
$data->sectionid = (int) ($custom[3] ?? -1);
$data->timeupdated = time();
debugging('availability_paypal IPN incoming notification: ' . json_encode($data), DEBUG_DEVELOPER);
if (!$user = $DB->get_record("user", array("id" => $data->userid))) {
$PAGE->set_context(context_system::instance());
availability_paypal_message_error("Not a valid user id", $data);
die;
}
if (!$context = context::instance_by_id($data->contextid, IGNORE_MISSING)) {
$PAGE->set_context(context_system::instance());
availability_paypal_message_error("Not a valid context id", $data);
die;
}
$PAGE->set_context($context);
if ($context instanceof context_module) {
$availability = $DB->get_field('course_modules', 'availability', ['id' => $context->instanceid], MUST_EXIST);
} else {
$availability = $DB->get_field('course_sections', 'availability', ['id' => $data->sectionid], MUST_EXIST);
}
$availability = json_decode($availability);
$paypal = null;
if ($availability) {
// There can be multiple conditions specified. Find the first of the type "paypal".
// TODO: Support more than one paypal condition specified.
foreach ($availability->c as $condition) {
if ($condition->type == 'paypal') {
$paypal = $condition;
break;
}
}
}
if (empty($paypal)) {
availability_paypal_message_error("PayPal condition not found while processing incoming IPN", $data);
die();
}
// Make a temporary record of the incoming IPN. It will be deleted once the payment is verified. If the verification
// fails, it will be kept and will allow the admin to debug and/or verify it manually.
$DB->insert_record("availability_paypal_tnx", array_merge((array) $data, [
'payment_status' => 'ToBeVerified',
]), false);
// Open a connection back to PayPal to validate the data.
$paypaladdr = empty($CFG->usepaypalsandbox) ? 'ipnpb.paypal.com' : 'ipnpb.sandbox.paypal.com';
$c = new curl();
$options = array(
'CURLOPT_RETURNTRANSFER' => 1,
'CURLOPT_HTTPHEADER' => [
'Host: ' . $paypaladdr,
'Content-Type: application/x-www-form-urlencoded',
'Connection: Close',
],
'CURLOPT_TIMEOUT' => 30,
'CURLOPT_HTTP_VERSION' => CURL_HTTP_VERSION_1_1,
'CURLOPT_FORBID_REUSE' => 1,
);
$location = "https://{$paypaladdr}/cgi-bin/webscr";
debugging('availability_paypal IPN verification request: ' . json_encode($req), DEBUG_DEVELOPER);
// Number of attempts to verify the payment.
$attempts = 5;
while ($attempts) {
$attempts--;
$result = trim($c->post($location, $req, $options));
$info = $c->get_info();
if ($c->get_errno()) {
availability_paypal_message_error("Could not access paypal.com to verify payment", $data);
die;
}
if ($info['http_code'] == 200) {
break;
} else {
debugging('availability_paypal IPN verification unexpected response code: ' . $info['http_code'], DEBUG_DEVELOPER);
if ($attempts) {
sleep(1);
}
}
}
debugging('availability_paypal IPN verification response: ' . $result, DEBUG_DEVELOPER);
if (strlen($result) > 0) {
if (strcmp($result, "VERIFIED") == 0) {
// Make sure the transaction with the same payment status and same pending reason doesn't exist already.
if ($DB->record_exists("availability_paypal_tnx", [
'txn_id' => $data->txn_id,
'payment_status' => $data->payment_status,
'pending_reason' => $data->pending_reason,
])) {
availability_paypal_message_error("Transaction $data->txn_id is being repeated!", $data);
die;
}
// In case of unexpected status, warn admins.
if ($data->payment_status !== "Completed" && $data->payment_status !== "Pending") {
$str = "Status neither completed nor pending: " . $data->payment_status;
availability_paypal_message_error($str, $data);
die;
}
// If currency is incorrectly set then someone maybe trying to cheat the system.
if ($data->payment_currency !== $paypal->currency) {
$str = "Currency does not match course settings, received: " . $data->payment_currency;
availability_paypal_message_error($str, $data);
die;
}
// If cost is incorrectly set then someone maybe trying to cheat the system.
if ($data->payment_gross != $paypal->cost) {
$str = "Payment gross does not match course settings, received: " . $data->payment_gross;
availability_paypal_message_error($str, $data);
die;
}
// If status is pending and reason is other than echeck, then we are on hold until further notice.
// Email the user to let them know.
if ($data->payment_status === "Pending" && $data->pending_reason !== "echeck") {
$eventdata = new \core\message\message();
$eventdata->component = 'availability_paypal';
$eventdata->name = 'payment_pending';
$eventdata->userfrom = core_user::get_noreply_user();
$eventdata->userto = $user;
$eventdata->subject = get_string('paypalpaymentpendingsubject', 'availability_paypal');
$eventdata->fullmessage = get_string('paypalpaymentpendingmessage', 'availability_paypal');
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = text_to_html($eventdata->fullmessage);
$eventdata->smallmessage = $eventdata->subject;
message_send($eventdata);
}
// At this point we only proceed with a status of completed or pending.
$DB->insert_record("availability_paypal_tnx", $data, false);
} else {
$DB->insert_record("availability_paypal_tnx", array_merge((array) $data, [
'payment_status' => 'Unverified',
]), false);
$data->verification_result = s($result);
availability_paypal_message_error("Payment verification failed", $data);
}
// Remove the temporary transaction record.
$DB->delete_records("availability_paypal_tnx", [
'payment_status' => 'ToBeVerified',
'txn_id' => $data->txn_id,
'userid' => $data->userid,
'contextid' => $data->contextid,
'sectionid' => $data->sectionid,
]);
}
/**
* Sends message to admin about error
*
* @param string $subject
* @param stdClass $data
*/
function availability_paypal_message_error($subject, $data) {
$userfrom = core_user::get_noreply_user();
$recipients = get_users_by_capability(context_system::instance(), 'availability/paypal:receivenotifications');
if (empty($recipients)) {
// Make sure that someone is notified.
$recipients = get_admins();
}
$site = get_site();
$text = "$site->fullname: PayPal transaction problem: {$subject}\n\n";
$text .= "Transaction data:\n";
if ($data) {
foreach ($data as $key => $value) {
$text .= "* {$key} => {$value}\n";
}
}
foreach ($recipients as $recipient) {
$message = new \core\message\message();
$message->component = 'availability_paypal';
$message->name = 'payment_error';
$message->userfrom = $userfrom;
$message->userto = $recipient;
$message->subject = "PayPal ERROR: " . $subject;
$message->fullmessage = $text;
$message->fullmessageformat = FORMAT_PLAIN;
$message->fullmessagehtml = text_to_html($text);
$message->smallmessage = $subject;
// Don't make one error to stop all other notifications.
try {
message_send($message);
} catch (Throwable $t) {
debugging('availability_paypal IPN: exception while sending message: ' . $t->getMessage());
}
}
}
/**
* Silent exception handler.
*
* @param Exception $ex
* @return void - does not return. Terminates execution!
*/
function availability_paypal_ipn_exception_handler($ex) {
$info = get_exception_info($ex);
$logerrmsg = "availability_paypal IPN exception handler: ".$info->message;
if (debugging('', DEBUG_NORMAL)) {
$logerrmsg .= ' Debug: '.$info->debuginfo."\n".format_backtrace($info->backtrace, true);
}
debugging($logerrmsg);
exit(0);
}