forked from dualcube/moodle-enrol_stripepayment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharge.php
362 lines (297 loc) · 13.1 KB
/
charge.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?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 Stripe
*
* This script waits for Payment notification from Stripe,
* then requires the data from Stripe.
* If Stripe matches what if expected for enrolment, it sets it up
* for that user.
*
* @package enrol_stripepayment
* @copyright 2019 Dualcube, Arkaprava Midya, Parthajeet Chakraborty, Louis Bronne (Natagora)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Disable moodle specific debug messages and any errors in output,
// comment out when debugging or better look into error log!
define('NO_DEBUG_DISPLAY', true);
require('/home/ubuntu/moodle/config.php');
require_once('lib.php');
if($CFG->version < 2018101900)
{
require_once($CFG->libdir.'/eventslib.php');
}
require_once($CFG->libdir.'/enrollib.php');
require_once($CFG->libdir.'/filelib.php');
require_login();
// Stripe does not like when we return error messages here,
// the custom handler just logs exceptions and stops.
set_exception_handler('enrol_stripepayment_charge_exception_handler');
$data = new stdClass();
$param_source_id = optional_param('source', false, PARAM_RAW);
$param_session_id = optional_param('session', false, PARAM_RAW);
$param_course_id = required_param('c',PARAM_INT);
$param_instance_id = required_param('i',PARAM_INT);
$param_user_id = required_param('u',PARAM_INT);
if (! $param_source_id && ! $param_session_id) {
message_stripepayment_error_to_admin("Charge.php called without source or session id.", $data);
redirect($CFG->wwwroot);
}
if (! $user = $DB->get_record("user", array("id" => $param_user_id))) {
message_stripepayment_error_to_admin("Not a valid user id", $data);
redirect($CFG->wwwroot);
}
if (! $course = $DB->get_record("course", array("id" => $param_course_id))) {
message_stripepayment_error_to_admin("Not a valid course id", $data);
redirect($CFG->wwwroot);
}
if (! $plugininstance = $DB->get_record("enrol", array("id" => $param_instance_id, "status" => 0))) {
message_stripepayment_error_to_admin("Not a valid instance id", $data);
redirect($CFG->wwwroot);
}
if (! $context = context_course::instance($course->id, IGNORE_MISSING)) {
message_stripepayment_error_to_admin("Not a valid context id", $data);
redirect($CFG->wwwroot);
}
$shortname = format_string($course->shortname, true, array('context' => $context));
$coursefullname = format_string($course->fullname, true, array('context' => $context));
$courseshortname = $shortname;
$userfullname = fullname($user);
$userfirstname = $user->firstname;
$userlastname = $user->lastname;
$useraddress = $user->address;
$usercity = $user->city;
$useremail = $user->email;
// preparing data values (same template as in earlier versions of enrol_stripepayment)
$data->cmd = "_xclick";
$data->charset = "utf-8";
$data->item_name = $coursefullname;
$data->item_name = $courseshortname; // should be item_number (original error left)
$data->item_name = 1; // should be item_count (original error left)
$data->on0 = get_string("user");
$data->os0 = $userfullname;
$data->custom = "{$param_user_id}-{$param_course_id}-{$param_instance_id}";
$data->currency_code = $plugininstance->currency;
$data->amount = (float) $plugininstance->cost;
$data->for_auction = False;
$data->no_note = 1;
$data->no_shipping = 1;
$data->rm = 2;
$data->cbt = get_string("continuetocourse");
$data->first_name = $userfirstname;
$data->last_name = $userlastname;
$data->address = $useraddress;
$data->city = $usercity;
$data->email = $user->email;
$data->country = $user->country;
$data->stripeToken = "";
$data->stripeTokenType = "";
$data->stripeEmail = "";
$custom = explode('-', $data->custom);
$data->userid = (int)$param_user_id;
$data->courseid = (int)$param_course_id;
$data->instanceid = (int)$param_instance_id;
$data->payment_gross = $data->amount;
$data->payment_currency = $data->currency_code;
$data->timeupdated = time();
// Get the user and course records.
$PAGE->set_context($context);
// If currency is incorrectly set then someone maybe trying to cheat the system.
if ($data->courseid != $plugininstance->courseid) {
message_stripepayment_error_to_admin("Course Id does not match to the course settings, received: ".$data->courseid, $data);
redirect($CFG->wwwroot);
}
$plugin = enrol_get_plugin('stripepayment');
// Check that amount paid is the correct amount.
if ( (float) $plugininstance->cost <= 0 ) {
$cost = (float) $plugin->get_config('cost');
} else {
$cost = (float) $plugininstance->cost;
}
// Use the same rounding of floats as on the enrol form.
$cost = format_float($cost, 2, false);
try {
require_once('Stripe/vendor/autoload.php');
\Stripe\Stripe::setApiKey($plugin->get_config('secretkey'));
if ($param_session_id) {
$session = \Stripe\Checkout\Session::retrieve($param_session_id);
$payment_intent_id = $session["payment_intent"];
$charge_json = \Stripe\Charge::all([
'payment_intent' => $payment_intent_id
]);
$charge = $charge_json->data[0];
} else {
$source = \Stripe\Source::retrieve($param_source_id);
$amount = $source->amount;
$status = $source->status;
$source_id = $source->id;
if ($status != 'chargeable') {
throw new Exception('Payment is not authorized');
}
$charge = \Stripe\Charge::create([
'amount' => $amount,
'currency' => 'eur',
'source' => $source_id
]);
}
$payment_expected = (float)$plugininstance->cost;
$payment_received = (float)$charge->amount / 100.0;
if ((float)$payment_expected < (float)$payment_received - 0.01) {
throw new Exception('Amount paid on Stripe is lower than payment due');
}
if (! $charge->paid) {
throw new Exception('No payment has been made');
}
$data->txn_id = $charge->balance_transaction;
$data->tax = $charge->amount / 100;
$data->memo = $charge->id;
$data->payment_status = $charge->status;
$data->pending_reason = $charge->failure_message;
$data->reason_code = $charge->failure_code;
// ALL CLEAR !
$DB->insert_record("enrol_stripepayment", $data);
if ($plugininstance->enrolperiod) {
$timestart = time();
$timeend = $timestart + $plugininstance->enrolperiod;
} else {
$timestart = 0;
$timeend = 0;
}
// Enrol user.
$plugin->enrol_user($plugininstance, $user->id, $plugininstance->roleid, $timestart, $timeend);
// Pass $view=true to filter hidden caps if the user cannot see them.
if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
'', '', '', '', false, true)) {
$users = sort_by_roleassignment_authority($users, $context);
$teacher = array_shift($users);
} else {
$teacher = false;
}
$mailstudents = $plugin->get_config('mailstudents');
$mailteachers = $plugin->get_config('mailteachers');
$mailadmins = $plugin->get_config('mailadmins');
$shortname = format_string($course->shortname, true, array('context' => $context));
if (!empty($mailstudents)) {
$a = new stdClass();
$a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
$a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id";
$eventdata = new \core\message\message();
$eventdata->modulename = 'moodle';
$eventdata->component = 'enrol_stripepayment';
$eventdata->name = 'stripepayment_enrolment';
$eventdata->userfrom = empty($teacher) ? core_user::get_support_user() : $teacher;
$eventdata->userto = $user;
$eventdata->subject = get_string("enrolmentnew", 'enrol', $shortname);
$eventdata->fullmessage = get_string('welcometocoursetext', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
message_send($eventdata);
}
if (!empty($mailteachers) && !empty($teacher)) {
$a->course = format_string($course->fullname, true, array('context' => $coursecontext));
$a->user = fullname($user);
$eventdata = new \core\message\message();
$eventdata->modulename = 'moodle';
$eventdata->component = 'enrol_stripepayment';
$eventdata->name = 'stripepayment_enrolment';
$eventdata->userfrom = $user;
$eventdata->userto = $teacher;
$eventdata->subject = get_string("enrolmentnew", 'enrol', $shortname);
$eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
message_send($eventdata);
}
if (!empty($mailadmins)) {
$a->course = format_string($course->fullname, true, array('context' => $coursecontext));
$a->user = fullname($user);
$admins = get_admins();
foreach ($admins as $admin) {
$eventdata = new \core\message\message();
$eventdata->modulename = 'moodle';
$eventdata->component = 'enrol_stripepayment';
$eventdata->name = 'stripepayment_enrolment';
$eventdata->userfrom = $user;
$eventdata->userto = $admin;
$eventdata->subject = get_string("enrolmentnew", 'enrol', $shortname);
$eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
message_send($eventdata);
}
}
$destination = "$CFG->wwwroot/course/view.php?id=$course->id";
$fullname = format_string($course->fullname, true, array('context' => $context));
if (is_enrolled($context, null, '', true)) {
redirect($destination, get_string('paymentthanks', '', $fullname));
} else { // Somehow they aren't enrolled yet!
$PAGE->set_url($destination);
echo $OUTPUT->header();
$a = new stdClass();
$a->teacher = get_string('defaultcourseteacher');
$a->fullname = $fullname;
notice(get_string('paymentsorry', '', $a), $destination);
}
} catch (Stripe_CardError $e) {
// Catch the errors in any way you like.
echo 'Error';
}
// Catch the errors in any way you like.
catch (Stripe_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API.
echo 'Invalid parameters were supplied to Stripe\'s API';
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently).
echo 'Authentication with Stripe\'s API failed';
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed.
echo 'Network communication with Stripe failed';
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
// yourself an email.
echo 'Stripe Error';
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe.
echo 'Something else happened, completely unrelated to Stripe ('.$e->getMessage().')';
}
// --- HELPER FUNCTIONS --------------------------------------------------------------------------------------!
/**
* Send payment error message to the admin.
*
* @param string $subject
* @param stdClass $data
*/
function message_stripepayment_error_to_admin($subject, $data) {
$admin = get_admin();
$site = get_site();
$message = "$site->fullname: Transaction failed.\n\n$subject\n\n";
foreach ($data as $key => $value) {
$message .= s($key) ." => ". s($value)."\n";
}
$eventdata = new stdClass();
$eventdata->modulename = 'moodle';
$eventdata->component = 'enrol_stripepayment';
$eventdata->name = 'stripepayment_enrolment';
$eventdata->userfrom = $admin;
$eventdata->userto = $admin;
$eventdata->subject = "STRIPE PAYMENT ERROR: ".$subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
message_send($eventdata);
}