Merge branch 'MDL-39518-24' of git://github.com/damyon/moodle into MOODLE_24_STABLE
[moodle.git] / enrol / authorize / locallib.php
blob36ea35a5d412864215c1a983234b8726402302b1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Authorize enrolment plugin.
20 * This plugin allows you to set up paid courses, using authorize.net.
22 * @package enrol
23 * @subpackage authorize
24 * @copyright 2010 Eugene Venter
25 * @author Eugene Venter
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 if (!defined('MOODLE_INTERNAL')) {
30 die('Direct access to this script is forbidden.');
33 define('ORDER_CAPTURE', 'capture');
34 define('ORDER_DELETE', 'delete');
35 define('ORDER_REFUND', 'refund');
36 define('ORDER_VOID', 'void');
38 /**
39 * authorize_print_orders
42 function authorize_print_orders($courseid, $userid) {
43 global $course;
44 global $CFG, $USER, $SITE, $DB, $OUTPUT, $PAGE;
45 global $strs, $authstrs;
47 $plugin = enrol_get_plugin('authorize');
49 require_once($CFG->libdir.'/tablelib.php');
51 $perpage = optional_param('perpage', 10, PARAM_INT);
52 $showonlymy = optional_param('showonlymy', 0, PARAM_BOOL);
53 $searchquery = optional_param('searchquery', '0', PARAM_INT);
54 $searchtype = optional_param('searchtype', 'orderid', PARAM_ALPHA);
55 $status = optional_param('status', AN_STATUS_NONE, PARAM_INT);
57 $coursecontext = context_course::instance($courseid);
59 $searchmenu = array('orderid' => $authstrs->orderid, 'transid' => $authstrs->transid, 'cclastfour' => $authstrs->cclastfour);
60 $buttons = "<form method='post' action='index.php' autocomplete='off'><div>";
61 $buttons .= html_writer::label(get_string('orderdetails', 'enrol_authorize'), 'menusearchtype', false, array('class' => 'accesshide'));
62 $buttons .= html_writer::select($searchmenu, 'searchtype', $searchtype, false);
63 $buttons .= html_writer::label(get_string('search'), 'searchquery', false, array('class' => 'accesshide'));
64 $buttons .= "<input id='searchquery' type='text' size='16' name='searchquery' value='' />";
65 $buttons .= "<input type='submit' value='$strs->search' />";
66 $buttons .= "</div></form>";
68 if (has_capability('enrol/authorize:uploadcsv', context_user::instance($USER->id))) {
69 $buttons .= "<form method='get' action='uploadcsv.php'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></form>";
72 $canmanagepayments = has_capability('enrol/authorize:managepayments', $coursecontext);
73 if ($showonlymy || !$canmanagepayments) {
74 $userid = $USER->id;
77 $baseurl = $CFG->wwwroot.'/enrol/authorize/index.php?user='.$userid;
79 $params = array('userid'=>$userid);
80 $sql = "SELECT c.id, c.fullname FROM {course} c JOIN {enrol_authorize} e ON c.id = e.courseid ";
81 $sql .= ($userid > 0) ? "WHERE (e.userid=:userid) " : '';
82 $sql .= "ORDER BY c.sortorder, c.fullname";
83 if (($popupcrs = $DB->get_records_sql_menu($sql, $params))) {
84 $popupcrs = array($SITE->id => $SITE->fullname) + $popupcrs;
86 $popupmenu = empty($popupcrs) ? '' : $OUTPUT->single_select(new moodle_url($baseurl.'&status='.$status), 'course', $popupcrs, $courseid, null, 'coursesmenu');
87 $popupmenu .= '<br />';
88 $statusmenu = array(
89 AN_STATUS_NONE => $strs->all,
90 AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW => $authstrs->allpendingorders,
91 AN_STATUS_AUTH => $authstrs->authorizedpendingcapture,
92 AN_STATUS_AUTHCAPTURE => $authstrs->authcaptured,
93 AN_STATUS_CREDIT => $authstrs->refunded,
94 AN_STATUS_VOID => $authstrs->cancelled,
95 AN_STATUS_EXPIRE => $authstrs->expired,
96 AN_STATUS_UNDERREVIEW => $authstrs->underreview,
97 AN_STATUS_APPROVEDREVIEW => $authstrs->approvedreview,
98 AN_STATUS_REVIEWFAILED => $authstrs->reviewfailed,
99 AN_STATUS_TEST => $authstrs->tested
102 $popupmenu .= $OUTPUT->single_select(new moodle_url($baseurl.'&course='.$courseid), 'status', $statusmenu, $status, null, 'statusmenu');
103 if ($canmanagepayments) {
104 $popupmenu .= '<br />';
105 $PAGE->requires->js('/enrol/authorize/authorize.js');
106 $aid = $OUTPUT->add_action_handler(new component_action('click', 'authorize_jump_to_mypayments', array('userid' => $USER->id, 'status' => $status)));
107 $popupmenu .= html_writer::checkbox('enrol_authorize', 1, $userid == $USER->id, get_string('mypaymentsonly', 'enrol_authorize'), array('id'=>$aid));
110 if (SITEID != $courseid) {
111 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
112 $PAGE->navbar->add($shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
114 $PAGE->navbar->add($authstrs->paymentmanagement, 'index.php');
115 $PAGE->set_title("$course->shortname: $authstrs->paymentmanagement");
116 $PAGE->set_heading($authstrs->paymentmanagement);
117 $PAGE->set_headingmenu($popupmenu);
118 $PAGE->set_button($buttons);
119 echo $OUTPUT->header();
121 $table = new flexible_table('enrol-authorize');
122 $table->set_attribute('width', '100%');
123 $table->set_attribute('cellspacing', '0');
124 $table->set_attribute('cellpadding', '3');
125 $table->set_attribute('id', 'orders');
126 $table->set_attribute('class', 'generaltable generalbox');
128 if ($perpage > 100) { $perpage = 100; }
129 $perpagemenus = array(5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100);
130 $perpagemenu = $OUTPUT->single_select(new moodle_url($baseurl.'&status='.$status.'&course='.$courseid), 'perpage', $perpagemenus, $perpage, array(''=>'choosedots'), 'perpagemenu');
131 $table->define_columns(array('id', 'userid', 'timecreated', 'status', 'action'));
132 $table->define_headers(array($authstrs->orderid, $authstrs->shopper, $strs->time, $strs->status, $perpagemenu));
133 $table->define_baseurl($baseurl."&amp;status=$status&amp;course=$courseid&amp;perpage=$perpage");
135 $table->no_sorting('action');
136 $table->sortable(true, 'id', SORT_DESC);
137 $table->pageable(true);
138 $table->setup();
140 $select = "SELECT e.id, e.paymentmethod, e.refundinfo, e.transid, e.courseid, e.userid, e.status, e.ccname, e.timecreated, e.settletime ";
141 $from = "FROM {enrol_authorize} e ";
142 $where = "WHERE (1=1) ";
143 $params = array();
145 if (!empty($searchquery)) {
146 switch($searchtype) {
147 case 'orderid':
148 $where = "WHERE (e.id = :searchquery) ";
149 $params['searchquery'] = $searchquery;
150 break;
152 case 'transid':
153 $where = "WHERE (e.transid = :searchquery) ";
154 $params['searchquery'] = $searchquery;
155 break;
157 case 'cclastfour':
158 $searchquery = sprintf("%04d", $searchquery);
159 $where = "WHERE (e.refundinfo = :searchquery) AND (e.paymentmethod=:method) ";
160 $params['searchquery'] = $searchquery;
161 $params['method'] = AN_METHOD_CC;
162 break;
165 else {
166 switch ($status)
168 case AN_STATUS_NONE:
169 if (!$plugin->get_config('an_test')) {
170 $where .= "AND (e.status != :status) ";
171 $params['status'] = AN_STATUS_NONE;
173 break;
175 case AN_STATUS_TEST:
176 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
177 $where .= "AND (e.status = :status) AND (e.transid = '0') AND (e.timecreated < :newordertime) ";
178 $params['status'] = AN_STATUS_NONE;
179 $params['newordertime'] = $newordertime;
180 break;
182 case AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW:
183 $where .= 'AND (e.status IN(:status1,:status2,:status3)) ';
184 $params['status1'] = AN_STATUS_AUTH;
185 $params['status2'] = AN_STATUS_UNDERREVIEW;
186 $params['status3'] = AN_STATUS_APPROVEDREVIEW;
187 break;
189 case AN_STATUS_CREDIT:
190 $from .= "INNER JOIN {enrol_authorize_refunds} r ON e.id = r.orderid ";
191 $where .= "AND (e.status = :status) ";
192 $params['status'] = AN_STATUS_AUTHCAPTURE;
193 break;
195 default:
196 $where .= "AND (e.status = :status) ";
197 $params['status'] = $status;
198 break;
201 if (SITEID != $courseid) {
202 $where .= "AND (e.courseid = :courseid) ";
203 $params['courseid'] = $courseid;
207 // This must be always LAST where!!!
208 if ($userid > 0) {
209 $where .= "AND (e.userid = :userid) ";
210 $params['userid'] = $userid;
213 if (($sort = $table->get_sql_sort())) {
214 $sort = ' ORDER BY ' . $sort;
217 $totalcount = $DB->count_records_sql('SELECT COUNT(*) ' . $from . $where, $params);
218 $table->initialbars($totalcount > $perpage);
219 $table->pagesize($perpage, $totalcount);
221 if (($records = $DB->get_records_sql($select . $from . $where . $sort, $params, $table->get_page_start(), $table->get_page_size()))) {
222 foreach ($records as $record) {
223 $actionstatus = authorize_get_status_action($record);
224 $color = authorize_get_status_color($actionstatus->status);
225 $actions = '';
227 if (empty($actionstatus->actions)) {
228 $actions .= $strs->none;
230 else {
231 foreach ($actionstatus->actions as $val) {
232 $actions .= authorize_print_action_button($record->id, $val);
236 $table->add_data(array(
237 "<a href='index.php?order=$record->id'>$record->id</a>",
238 $record->ccname,
239 userdate($record->timecreated),
240 "<font style='color:$color'>" . $authstrs->{$actionstatus->status} . "</font>",
241 $actions
246 $table->print_html();
247 echo $OUTPUT->footer();
251 * authorize_print_order
253 * @param object $order
255 function authorize_print_order($orderid)
257 global $CFG, $USER, $DB, $OUTPUT, $PAGE;
258 global $strs, $authstrs;
260 $plugin = enrol_get_plugin('authorize');
261 $an_test = $plugin->get_config('an_test');
263 $do = optional_param('do', '', PARAM_ALPHA);
264 $unenrol = optional_param('unenrol', 0, PARAM_BOOL);
265 $confirm = optional_param('confirm', 0, PARAM_BOOL);
267 if (!$order = $DB->get_record('enrol_authorize', array('id'=>$orderid))) {
268 print_error('orderidnotfound', '',
269 "$CFG->wwwroot/enrol/authorize/index.php", $orderid);
272 if (!$course = $DB->get_record('course', array('id'=>$order->courseid))) {
273 print_error('invalidcourseid', '', "$CFG->wwwroot/enrol/authorize/index.php");
276 if (!$user = $DB->get_record('user', array('id'=>$order->userid))) {
277 print_error('nousers', '', "$CFG->wwwroot/enrol/authorize/index.php");
280 $coursecontext = context_course::instance($course->id);
281 if ($USER->id != $order->userid) { // Current user viewing someone else's order
282 require_capability('enrol/authorize:managepayments', $coursecontext);
285 $settled = AuthorizeNet::settled($order);
286 $statusandactions = authorize_get_status_action($order);
287 $color = authorize_get_status_color($statusandactions->status);
289 $buttons = '';
290 if (empty($do))
292 if (empty($statusandactions->actions)) {
293 if ((AN_METHOD_ECHECK == $order->paymentmethod) && has_capability('enrol/authorize:uploadcsv', context_user::instance($USER->id))) {
294 $buttons .= "<form method='get' action='uploadcsv.php'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></form>";
297 else {
298 foreach ($statusandactions->actions as $val) {
299 $buttons .= authorize_print_action_button($orderid, $val);
304 if (SITEID != $course->id) {
305 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
306 $PAGE->navbar->add($shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
308 $PAGE->navbar->add($authstrs->paymentmanagement, 'index.php?course='.$course->id);
309 $PAGE->navbar->add($authstrs->orderid . ': ' . $orderid, 'index.php');
310 $PAGE->set_course($course);
311 $PAGE->set_title("$course->shortname: $authstrs->paymentmanagement");
312 $PAGE->set_heading($authstrs->orderdetails);
313 $PAGE->set_cacheable(false);
314 $PAGE->set_button($buttons);
315 echo $OUTPUT->header();
317 $table = new html_table();
318 $table->width = '100%';
319 $table->size = array('30%', '70%');
320 $table->align = array('right', 'left');
322 if (AN_METHOD_CC == $order->paymentmethod) {
323 $table->data[] = array("<b>$authstrs->paymentmethod:</b>", $authstrs->methodcc);
324 $table->data[] = array("<b>$authstrs->nameoncard:</b>", $order->ccname . ' (<b><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'">'.fullname($user).'</a></b>)');
325 $table->data[] = array("<b>$authstrs->cclastfour:</b>", $order->refundinfo);
327 else {
328 $table->data[] = array("<b>$authstrs->paymentmethod:</b>", $authstrs->methodecheck);
329 $table->data[] = array("<b>$authstrs->echeckfirslasttname:</b>", $order->ccname . ' (<b><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'">'.fullname($user).'</a></b>)');
330 $table->data[] = array("<b>$authstrs->isbusinesschecking:</b>", ($order->refundinfo == 1) ? $strs->yes : $strs->no);
333 $table->data[] = array("<b>$authstrs->amount:</b>", "$order->currency $order->amount");
334 $table->data[] = array("<b>$authstrs->transid:</b>", $order->transid);
335 $table->data[] = array("<b>$strs->time:</b>", userdate($order->timecreated));
336 $table->data[] = array("<b>$authstrs->settlementdate:</b>", $settled ? userdate($order->settletime) : $authstrs->notsettled);
337 $table->data[] = array("<b>$strs->status:</b>", "<b><font style='color:$color'>" . $authstrs->{$statusandactions->status} . "</font></b>");
339 if (ORDER_CAPTURE == $do && in_array(ORDER_CAPTURE, $statusandactions->actions)) {
340 if ($confirm && confirm_sesskey()) {
341 $message = '';
342 $extra = NULL;
343 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
344 if (empty($an_test)) {
345 if (enrol_into_course($course, $user, 'authorize')) {
346 if ($plugin->get_config('enrol_mailstudents')) {
347 send_welcome_messages($orderid);
349 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
351 else {
352 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
353 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", "Error while trying to enrol ".fullname($user)." in '" . $shortname . "'", 20);
356 else {
357 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
360 else {
361 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
364 $table->data[] = array("<b>$strs->confirm:</b>", get_string('captureyes', 'enrol_authorize') . '<br />' .
365 authorize_print_action_button($orderid, ORDER_CAPTURE, 0, true, false, $strs->no));
366 echo html_writer::table($table);
368 elseif (ORDER_REFUND == $do && in_array(ORDER_REFUND, $statusandactions->actions)) {
369 $refunded = 0.0;
370 $sql = "SELECT SUM(amount) AS refunded
371 FROM {enrol_authorize_refunds}
372 WHERE (orderid = ?)
373 AND (status = ?)";
375 if (($refundval = $DB->get_field_sql($sql, array($orderid, AN_STATUS_CREDIT)))) {
376 $refunded = floatval($refundval);
378 $upto = round($order->amount - $refunded, 2);
379 if ($upto <= 0) {
380 print_error('refoundtoorigi', '',
381 "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $order->amount);
383 $amount = round(optional_param('amount', $upto, PARAM_RAW), 2);
384 if ($amount > $upto) {
385 print_error('refoundto', '',
386 "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $upto);
388 if ($confirm && confirm_sesskey()) {
389 $extra = new stdClass;
390 $extra->orderid = $orderid;
391 $extra->amount = $amount;
392 $message = '';
393 $success = AuthorizeNet::process($order, $message, $extra, AN_ACTION_CREDIT);
394 if (AN_APPROVED == $success || AN_REVIEW == $success) {
395 if (empty($an_test)) {
396 if (empty($extra->id)) {
397 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", "insert record error", 20);
399 else {
400 if (!empty($unenrol)) {
401 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
402 $plugin->unenrol_user($pinstance, $order->userid);
403 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
405 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
408 else {
409 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
412 else {
413 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
416 $a = new stdClass;
417 $a->upto = $upto;
418 $inputattrs = array('id' => 'amount', 'type' => 'text', 'size' => '5', 'name' => 'amount', 'value' => $amount);
419 $extrahtml = html_writer::label(get_string('howmuch', 'enrol_authorize'), 'amount'). ' '.
420 html_writer::empty_tag('input', $inputattrs). ' '.
421 get_string('canbecredit', 'enrol_authorize', $a) . '<br />';
422 $table->data[] = array("<b>$strs->confirm:</b>",
423 authorize_print_action_button($orderid, ORDER_REFUND, 0, true, $authstrs->unenrolstudent, $strs->no, $extrahtml));
424 echo html_writer::table($table);
426 elseif (ORDER_DELETE == $do && in_array(ORDER_DELETE, $statusandactions->actions)) {
427 if ($confirm && confirm_sesskey()) {
428 if (!empty($unenrol)) {
429 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
430 $plugin->unenrol_user($pinstance, $order->userid);
431 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
433 $DB->delete_records('enrol_authorize', array('id'=>$orderid));
434 redirect("$CFG->wwwroot/enrol/authorize/index.php");
436 $table->data[] = array("<b>$strs->confirm:</b>",
437 authorize_print_action_button($orderid, ORDER_DELETE, 0, true, $authstrs->unenrolstudent,$strs->no));
438 echo html_writer::table($table);
440 elseif (ORDER_VOID == $do) { // special case: cancel original or refunded transaction?
441 $suborderid = optional_param('suborder', 0, PARAM_INT);
442 if (empty($suborderid) && in_array(ORDER_VOID, $statusandactions->actions)) { // cancel original
443 if ($confirm && confirm_sesskey()) {
444 $extra = NULL;
445 $message = '';
446 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_VOID)) {
447 if (empty($an_test)) {
448 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
450 else {
451 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
454 else {
455 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
458 $table->data[] = array("<b>$strs->confirm:</b>", get_string('voidyes', 'enrol_authorize') . '<br />' .
459 authorize_print_action_button($orderid, ORDER_VOID, 0, true, false, $strs->no));
460 echo html_writer::table($table);
462 elseif (!empty($suborderid)) { // cancel refunded
463 $sql = "SELECT r.*, e.courseid, e.paymentmethod
464 FROM {enrol_authorize_refunds} r
465 INNER JOIN {enrol_authorize} e
466 ON r.orderid = e.id
467 WHERE r.id = ?
468 AND r.orderid = ?
469 AND r.status = ?";
471 $suborder = $DB->get_record_sql($sql, array($suborderid, $orderid, AN_STATUS_CREDIT));
472 if (!$suborder) { // not found
473 print_error('transactionvoid', '', "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
475 $refundedstatus = authorize_get_status_action($suborder);
476 unset($suborder->courseid);
477 if (in_array(ORDER_VOID, $refundedstatus->actions)) {
478 if ($confirm && confirm_sesskey()) {
479 $message = '';
480 $extra = NULL;
481 if (AN_APPROVED == AuthorizeNet::process($suborder, $message, $extra, AN_ACTION_VOID)) {
482 if (empty($an_test)) {
483 if (!empty($unenrol)) {
484 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
485 $plugin->unenrol_user($pinstance, $order->userid);
486 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
488 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
490 else {
491 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
494 else {
495 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
498 $a = new stdClass;
499 $a->transid = $suborder->transid;
500 $a->amount = $suborder->amount;
501 $table->data[] = array("<b>$strs->confirm:</b>", get_string('subvoidyes', 'enrol_authorize', $a) . '<br />' .
502 authorize_print_action_button($orderid, ORDER_VOID, $suborderid, true, $authstrs->unenrolstudent, $strs->no));
503 echo html_writer::table($table);
507 else {
508 echo html_writer::table($table);
510 if ($settled) { // show refunds.
511 $t2 = new html_table();
512 $t2->size = array('45%', '15%', '20%', '10%', '10%');
513 $t2->align = array('right', 'right', 'right', 'right', 'right');
514 $t2->head = array($authstrs->settlementdate, $authstrs->transid, $strs->status, $strs->action, $authstrs->amount);
516 $sql = "SELECT r.*, e.courseid, e.paymentmethod
517 FROM {enrol_authorize_refunds} r
518 INNER JOIN {enrol_authorize} e
519 ON r.orderid = e.id
520 WHERE r.orderid = ?";
522 if (($refunds = $DB->get_records_sql($sql, array($orderid)))) {
523 $sumrefund = floatval(0.0);
524 foreach ($refunds as $rf) {
525 $subactions = '';
526 $substatus = authorize_get_status_action($rf);
527 if (empty($substatus->actions)) {
528 $subactions .= $strs->none;
530 else {
531 foreach ($substatus->actions as $vl) {
532 $subactions .= authorize_print_action_button($orderid, $vl, $rf->id);
535 $sign = '';
536 $color = authorize_get_status_color($substatus->status);
537 if ($substatus->status == 'refunded' or $substatus->status == 'settled') {
538 $sign = '-';
539 $sumrefund += floatval($rf->amount);
541 $t2->data[] = array(
542 userdate($rf->settletime),
543 $rf->transid,
544 "<b><font style='color:$color'>" .$authstrs->{$substatus->status} . "</font></b>",
545 $subactions,
546 format_float($sign . $rf->amount, 2)
549 $t2->data[] = array('','',get_string('total'),$order->currency,format_float('-'.$sumrefund, 2));
551 else {
552 $t2->data[] = array('','',get_string('noreturns', 'enrol_authorize'),'','');
554 echo "<h4>" . get_string('returns', 'enrol_authorize') . "</h4>\n";
555 echo html_writer::table($t2);
559 echo $OUTPUT->footer();
563 * authorize_get_status_action
565 * @param object $order Order details.
566 * @return object
568 function authorize_get_status_action($order)
570 global $CFG;
571 static $newordertime = 0;
573 if (0 == $newordertime) {
574 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
577 $ret = new stdClass();
578 $ret->actions = array();
580 $canmanage = has_capability('enrol/authorize:managepayments', context_course::instance($order->courseid));
582 if (floatval($order->transid) == 0) { // test transaction or new order
583 if ($order->timecreated < $newordertime) {
584 if ($canmanage) {
585 $ret->actions = array(ORDER_DELETE);
587 $ret->status = 'tested';
589 else {
590 $ret->status = 'new';
592 return $ret;
595 switch ($order->status) {
596 case AN_STATUS_AUTH:
597 if (AuthorizeNet::expired($order)) {
598 if ($canmanage) {
599 $ret->actions = array(ORDER_DELETE);
601 $ret->status = 'expired';
603 else {
604 if ($canmanage) {
605 $ret->actions = array(ORDER_CAPTURE, ORDER_VOID);
607 $ret->status = 'authorizedpendingcapture';
609 return $ret;
611 case AN_STATUS_AUTHCAPTURE:
612 if (AuthorizeNet::settled($order)) {
613 if ($canmanage) {
614 if (($order->paymentmethod == AN_METHOD_CC) || ($order->paymentmethod == AN_METHOD_ECHECK && !empty($order->refundinfo))) {
615 $ret->actions = array(ORDER_REFUND);
618 $ret->status = 'settled';
620 else {
621 if ($order->paymentmethod == AN_METHOD_CC && $canmanage) {
622 $ret->actions = array(ORDER_VOID);
624 $ret->status = 'capturedpendingsettle';
626 return $ret;
628 case AN_STATUS_CREDIT:
629 if (AuthorizeNet::settled($order)) {
630 $ret->status = 'settled';
632 else {
633 if ($order->paymentmethod == AN_METHOD_CC && $canmanage) {
634 $ret->actions = array(ORDER_VOID);
636 $ret->status = 'refunded';
638 return $ret;
640 case AN_STATUS_VOID:
641 $ret->status = 'cancelled';
642 return $ret;
644 case AN_STATUS_EXPIRE:
645 if ($canmanage) {
646 $ret->actions = array(ORDER_DELETE);
648 $ret->status = 'expired';
649 return $ret;
651 case AN_STATUS_UNDERREVIEW:
652 $ret->status = 'underreview';
653 return $ret;
655 case AN_STATUS_APPROVEDREVIEW:
656 $ret->status = 'approvedreview';
657 return $ret;
659 case AN_STATUS_REVIEWFAILED:
660 if ($canmanage) {
661 $ret->actions = array(ORDER_DELETE);
663 $ret->status = 'reviewfailed';
664 return $ret;
666 default:
667 return $ret;
672 function authorize_get_status_color($status)
674 $color = 'black';
675 switch ($status)
677 case 'settled':
678 case 'capturedpendingsettle':
679 $color = '#339900'; // green
680 break;
682 case 'underreview':
683 case 'approvedreview':
684 case 'authorizedpendingcapture':
685 $color = '#FF6600'; // orange
686 break;
688 case 'new':
689 case 'tested':
690 $color = '#003366'; // blue
691 break;
693 case 'expired':
694 case 'cancelled':
695 case 'refunded';
696 case 'reviewfailed':
697 $color = '#FF0033'; // red
698 break;
700 return $color;
703 function authorize_print_action_button($orderid, $do, $suborderid=0, $confirm=false, $unenrol=false, $nobutton=false, $extrahtml='')
705 global $CFG, $OUTPUT;
706 global $authstrs;
708 $ret = '<form action="'.$CFG->wwwroot.'/enrol/authorize/index.php'.'" method="post"><div>' .
709 '<input type="hidden" name="order" value="'.$orderid.'" />' .
710 '<input type="hidden" name="do" value="'.$do.'" />' .
711 '<input type="hidden" name="sesskey" value="'. sesskey() . '" />';
712 if (!empty($suborderid)) {
713 $ret .= '<input type="hidden" name="suborder" value="'.$suborderid.'" />';
715 if (!empty($confirm)) {
716 $ret .= '<input type="hidden" name="confirm" value="1" />';
718 if (!empty($unenrol)) {
719 $ret .= html_writer::checkbox('unenrol', 1, false, $unenrol) . '<br />';
721 $ret .= $extrahtml;
722 $ret .= '<input type="submit" value="'.$authstrs->$do.'" />' .
723 '</div></form>';
724 if (!empty($nobutton)) {
725 $ret .= '<form method="get" action="index.php"><div><input type="hidden" name="order" value="'.$orderid.'" /><input type="submit" value="'.$nobutton.'" /></div></form>';
727 return $ret;