MDL-29189 course Minor fixups after code review
[moodle.git] / enrol / authorize / locallib.php
CommitLineData
4317f92f 1<?php
06090362
AB
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.
13//
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/>.
16
17/**
18 * Authorize enrolment plugin.
19 *
20 * This plugin allows you to set up paid courses, using authorize.net.
21 *
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
27 */
c7dac7f5 28
0b0ac967 29if (!defined('MOODLE_INTERNAL')) {
17dd6691 30 die('Direct access to this script is forbidden.');
0b0ac967 31}
7dcf558c 32
c7dac7f5 33define('ORDER_CAPTURE', 'capture');
34define('ORDER_DELETE', 'delete');
35define('ORDER_REFUND', 'refund');
36define('ORDER_VOID', 'void');
37
ece1fbee 38/**
c9ca150d 39 * authorize_print_orders
ece1fbee 40 *
41 */
e738c328 42function authorize_print_orders($courseid, $userid) {
36814b99 43 global $course;
e738c328 44 global $CFG, $USER, $SITE, $DB, $OUTPUT, $PAGE;
c7dac7f5 45 global $strs, $authstrs;
06090362
AB
46
47 $plugin = enrol_get_plugin('authorize');
48
d65af16f 49 require_once($CFG->libdir.'/tablelib.php');
c7dac7f5 50
36814b99 51 $perpage = optional_param('perpage', 10, PARAM_INT);
eb108480 52 $showonlymy = optional_param('showonlymy', 0, PARAM_BOOL);
36814b99 53 $searchquery = optional_param('searchquery', '0', PARAM_INT);
a6c970b8 54 $searchtype = optional_param('searchtype', 'orderid', PARAM_ALPHA);
36814b99 55 $status = optional_param('status', AN_STATUS_NONE, PARAM_INT);
c7dac7f5 56
eb38d45b 57 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
8ebbb06a 58
36814b99 59 $searchmenu = array('orderid' => $authstrs->orderid, 'transid' => $authstrs->transid, 'cclastfour' => $authstrs->cclastfour);
f31d516e 60 $buttons = "<form method='post' action='index.php' autocomplete='off'><div>";
d776d59e 61 $buttons .= html_writer::select($searchmenu, 'searchtype', $searchtype, false);
36814b99 62 $buttons .= "<input type='text' size='16' name='searchquery' value='' />";
63 $buttons .= "<input type='submit' value='$strs->search' />";
64 $buttons .= "</div></form>";
eb108480 65
eb38d45b 66 if (has_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER, $USER->id))) {
36814b99 67 $buttons .= "<form method='get' action='uploadcsv.php'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></form>";
68 }
69
8ebbb06a 70 $canmanagepayments = has_capability('enrol/authorize:managepayments', $coursecontext);
eb108480 71 if ($showonlymy || !$canmanagepayments) {
c7dac7f5 72 $userid = $USER->id;
73 }
74
36814b99 75 $baseurl = $CFG->wwwroot.'/enrol/authorize/index.php?user='.$userid;
c7dac7f5 76
c6307ef2 77 $params = array('userid'=>$userid);
78 $sql = "SELECT c.id, c.fullname FROM {course} c JOIN {enrol_authorize} e ON c.id = e.courseid ";
79 $sql .= ($userid > 0) ? "WHERE (e.userid=:userid) " : '';
bf228101 80 $sql .= "ORDER BY c.sortorder, c.fullname";
c6307ef2 81 if (($popupcrs = $DB->get_records_sql_menu($sql, $params))) {
8174990c 82 $popupcrs = array($SITE->id => $SITE->fullname) + $popupcrs;
36814b99 83 }
f8dab966 84 $popupmenu = empty($popupcrs) ? '' : $OUTPUT->single_select(new moodle_url($baseurl.'&status='.$status), 'course', $popupcrs, $courseid, null, 'coursesmenu');
36814b99 85 $popupmenu .= '<br />';
86 $statusmenu = array(
87 AN_STATUS_NONE => $strs->all,
88 AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW => $authstrs->allpendingorders,
89 AN_STATUS_AUTH => $authstrs->authorizedpendingcapture,
90 AN_STATUS_AUTHCAPTURE => $authstrs->authcaptured,
91 AN_STATUS_CREDIT => $authstrs->refunded,
92 AN_STATUS_VOID => $authstrs->cancelled,
93 AN_STATUS_EXPIRE => $authstrs->expired,
94 AN_STATUS_UNDERREVIEW => $authstrs->underreview,
95 AN_STATUS_APPROVEDREVIEW => $authstrs->approvedreview,
96 AN_STATUS_REVIEWFAILED => $authstrs->reviewfailed,
97 AN_STATUS_TEST => $authstrs->tested
98 );
4317f92f 99
f8dab966 100 $popupmenu .= $OUTPUT->single_select(new moodle_url($baseurl.'&course='.$courseid), 'status', $statusmenu, $status, null, 'statusmenu');
36814b99 101 if ($canmanagepayments) {
102 $popupmenu .= '<br />';
9dec75db 103 $PAGE->requires->js('/enrol/authorize/authorize.js');
c80877aa
PS
104 $aid = $OUTPUT->add_action_handler(new component_action('click', 'authorize_jump_to_mypayments', array('userid' => $USER->id, 'status' => $status)));
105 $popupmenu .= html_writer::checkbox('enrol_authorize', 1, $userid == $USER->id, get_string('mypaymentsonly', 'enrol_authorize'), array('id'=>$aid));
36814b99 106 }
107
36814b99 108 if (SITEID != $courseid) {
8ebbb06a
SH
109 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
110 $PAGE->navbar->add($shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
c7dac7f5 111 }
01d082fa 112 $PAGE->navbar->add($authstrs->paymentmanagement, 'index.php');
113 $PAGE->set_title("$course->shortname: $authstrs->paymentmanagement");
114 $PAGE->set_heading($authstrs->paymentmanagement);
115 $PAGE->set_headingmenu($popupmenu);
116 $PAGE->set_button($buttons);
117 echo $OUTPUT->header();
c7dac7f5 118
119 $table = new flexible_table('enrol-authorize');
120 $table->set_attribute('width', '100%');
121 $table->set_attribute('cellspacing', '0');
122 $table->set_attribute('cellpadding', '3');
123 $table->set_attribute('id', 'orders');
124 $table->set_attribute('class', 'generaltable generalbox');
125
36814b99 126 if ($perpage > 100) { $perpage = 100; }
127 $perpagemenus = array(5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100);
f8dab966 128 $perpagemenu = $OUTPUT->single_select(new moodle_url($baseurl.'&status='.$status.'&course='.$courseid), 'perpage', $perpagemenus, $perpage, array(''=>'choosedots'), 'perpagemenu');
5a2a5331 129 $table->define_columns(array('id', 'userid', 'timecreated', 'status', 'action'));
65f0a8f6 130 $table->define_headers(array($authstrs->orderid, $authstrs->shopper, $strs->time, $strs->status, $perpagemenu));
36814b99 131 $table->define_baseurl($baseurl."&amp;status=$status&amp;course=$courseid&amp;perpage=$perpage");
c7dac7f5 132
5a2a5331 133 $table->no_sorting('action');
17dd6691 134 $table->sortable(true, 'id', SORT_DESC);
c7dac7f5 135 $table->pageable(true);
136 $table->setup();
137
36814b99 138 $select = "SELECT e.id, e.paymentmethod, e.refundinfo, e.transid, e.courseid, e.userid, e.status, e.ccname, e.timecreated, e.settletime ";
1751f48f 139 $from = "FROM {enrol_authorize} e ";
d65af16f 140 $where = "WHERE (1=1) ";
1751f48f 141 $params = array();
c7dac7f5 142
36814b99 143 if (!empty($searchquery)) {
144 switch($searchtype) {
145 case 'orderid':
1751f48f 146 $where = "WHERE (e.id = :searchquery) ";
147 $params['searchquery'] = $searchquery;
36814b99 148 break;
149
150 case 'transid':
1751f48f 151 $where = "WHERE (e.transid = :searchquery) ";
152 $params['searchquery'] = $searchquery;
36814b99 153 break;
154
155 case 'cclastfour':
156 $searchquery = sprintf("%04d", $searchquery);
1751f48f 157 $where = "WHERE (e.refundinfo = :searchquery) AND (e.paymentmethod=:method) ";
158 $params['searchquery'] = $searchquery;
159 $params['method'] = AN_METHOD_CC;
36814b99 160 break;
161 }
162 }
163 else {
f6a7a35c 164 switch ($status)
165 {
36814b99 166 case AN_STATUS_NONE:
06090362 167 if (!$plugin->get_config('an_test')) {
1751f48f 168 $where .= "AND (e.status != :status) ";
169 $params['status'] = AN_STATUS_NONE;
36814b99 170 }
171 break;
172
173 case AN_STATUS_TEST:
174 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
1751f48f 175 $where .= "AND (e.status = :status) AND (e.transid = '0') AND (e.timecreated < :newordertime) ";
176 $params['status'] = AN_STATUS_NONE;
177 $params['newordertime'] = $newordertime;
36814b99 178 break;
179
2c46669b 180 case AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW:
1751f48f 181 $where .= 'AND (e.status IN(:status1,:status2,:status3)) ';
182 $params['status1'] = AN_STATUS_AUTH;
183 $params['status2'] = AN_STATUS_UNDERREVIEW;
184 $params['status3'] = AN_STATUS_APPROVEDREVIEW;
2c46669b 185 break;
186
f6a7a35c 187 case AN_STATUS_CREDIT:
1751f48f 188 $from .= "INNER JOIN {enrol_authorize_refunds} r ON e.id = r.orderid ";
189 $where .= "AND (e.status = :status) ";
190 $params['status'] = AN_STATUS_AUTHCAPTURE;
d65af16f 191 break;
f6a7a35c 192
f6a7a35c 193 default:
1751f48f 194 $where .= "AND (e.status = :status) ";
195 $params['status'] = $status;
d65af16f 196 break;
c7dac7f5 197 }
c7dac7f5 198
36814b99 199 if (SITEID != $courseid) {
d36e6516 200 $where .= "AND (e.courseid = :courseid) ";
1751f48f 201 $params['courseid'] = $courseid;
9c746ce7 202 }
203 }
204
36814b99 205 // This must be always LAST where!!!
9c746ce7 206 if ($userid > 0) {
1751f48f 207 $where .= "AND (e.userid = :userid) ";
208 $params['userid'] = $userid;
9c746ce7 209 }
210
04b8b688 211 if (($sort = $table->get_sql_sort())) {
c7dac7f5 212 $sort = ' ORDER BY ' . $sort;
213 }
c7dac7f5 214
1751f48f 215 $totalcount = $DB->count_records_sql('SELECT COUNT(*) ' . $from . $where, $params);
c7dac7f5 216 $table->initialbars($totalcount > $perpage);
217 $table->pagesize($perpage, $totalcount);
c7dac7f5 218
1751f48f 219 if (($records = $DB->get_records_sql($select . $from . $where . $sort, $params, $table->get_page_start(), $table->get_page_size()))) {
c7dac7f5 220 foreach ($records as $record) {
9b47e001 221 $actionstatus = authorize_get_status_action($record);
516b1385 222 $color = authorize_get_status_color($actionstatus->status);
c7dac7f5 223 $actions = '';
224
225 if (empty($actionstatus->actions)) {
226 $actions .= $strs->none;
227 }
228 else {
36814b99 229 foreach ($actionstatus->actions as $val) {
230 $actions .= authorize_print_action_button($record->id, $val);
c7dac7f5 231 }
232 }
233
234 $table->add_data(array(
235 "<a href='index.php?order=$record->id'>$record->id</a>",
c7dac7f5 236 $record->ccname,
36814b99 237 userdate($record->timecreated),
516b1385 238 "<font style='color:$color'>" . $authstrs->{$actionstatus->status} . "</font>",
c7dac7f5 239 $actions
240 ));
241 }
242 }
243
244 $table->print_html();
2af6a7e5 245 echo $OUTPUT->footer();
c7dac7f5 246}
247
ece1fbee 248/**
36814b99 249 * authorize_print_order
ece1fbee 250 *
36814b99 251 * @param object $order
ece1fbee 252 */
36814b99 253function authorize_print_order($orderid)
362bdbf7 254{
01d082fa 255 global $CFG, $USER, $DB, $OUTPUT, $PAGE;
c7dac7f5 256 global $strs, $authstrs;
257
06090362
AB
258 $plugin = enrol_get_plugin('authorize');
259 $an_test = $plugin->get_config('an_test');
260
36814b99 261 $do = optional_param('do', '', PARAM_ALPHA);
d65af16f 262 $unenrol = optional_param('unenrol', 0, PARAM_BOOL);
263 $confirm = optional_param('confirm', 0, PARAM_BOOL);
743d932c 264
1751f48f 265 if (!$order = $DB->get_record('enrol_authorize', array('id'=>$orderid))) {
6a933f13 266 print_error('orderidnotfound', '',
267 "$CFG->wwwroot/enrol/authorize/index.php", $orderid);
36814b99 268 }
c7dac7f5 269
1751f48f 270 if (!$course = $DB->get_record('course', array('id'=>$order->courseid))) {
6a933f13 271 print_error('invalidcourseid', '', "$CFG->wwwroot/enrol/authorize/index.php");
c7dac7f5 272 }
273
1751f48f 274 if (!$user = $DB->get_record('user', array('id'=>$order->userid))) {
6a933f13 275 print_error('nousers', '', "$CFG->wwwroot/enrol/authorize/index.php");
36814b99 276 }
7dbc351e 277
36814b99 278 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
c7dac7f5 279 if ($USER->id != $order->userid) { // Current user viewing someone else's order
9c746ce7 280 require_capability('enrol/authorize:managepayments', $coursecontext);
c7dac7f5 281 }
282
5a2a5331 283 $settled = AuthorizeNet::settled($order);
36814b99 284 $statusandactions = authorize_get_status_action($order);
285 $color = authorize_get_status_color($statusandactions->status);
c7dac7f5 286
36814b99 287 $buttons = '';
288 if (empty($do))
289 {
290 if (empty($statusandactions->actions)) {
291 if ((AN_METHOD_ECHECK == $order->paymentmethod) && has_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER, $USER->id))) {
292 $buttons .= "<form method='get' action='uploadcsv.php'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></form>";
293 }
0c76d7e3 294 }
295 else {
36814b99 296 foreach ($statusandactions->actions as $val) {
297 $buttons .= authorize_print_action_button($orderid, $val);
298 }
0c76d7e3 299 }
c7dac7f5 300 }
c7dac7f5 301
64d187aa 302 if (SITEID != $course->id) {
8ebbb06a
SH
303 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
304 $PAGE->navbar->add($shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
36814b99 305 }
01d082fa 306 $PAGE->navbar->add($authstrs->paymentmanagement, 'index.php?course='.$course->id);
307 $PAGE->navbar->add($authstrs->orderid . ': ' . $orderid, 'index.php');
06090362 308 $PAGE->set_course($course);
01d082fa 309 $PAGE->set_title("$course->shortname: $authstrs->paymentmanagement");
310 $PAGE->set_heading($authstrs->orderdetails);
311 $PAGE->set_cacheable(false);
312 $PAGE->set_button($buttons);
313 echo $OUTPUT->header();
c7dac7f5 314
7d50e687 315 $table = new html_table();
36814b99 316 $table->width = '100%';
317 $table->size = array('30%', '70%');
318 $table->align = array('right', 'left');
319
320 if (AN_METHOD_CC == $order->paymentmethod) {
321 $table->data[] = array("<b>$authstrs->paymentmethod:</b>", $authstrs->methodcc);
322 $table->data[] = array("<b>$authstrs->nameoncard:</b>", $order->ccname . ' (<b><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'">'.fullname($user).'</a></b>)');
323 $table->data[] = array("<b>$authstrs->cclastfour:</b>", $order->refundinfo);
324 }
325 else {
326 $table->data[] = array("<b>$authstrs->paymentmethod:</b>", $authstrs->methodecheck);
327 $table->data[] = array("<b>$authstrs->echeckfirslasttname:</b>", $order->ccname . ' (<b><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'">'.fullname($user).'</a></b>)');
328 $table->data[] = array("<b>$authstrs->isbusinesschecking:</b>", ($order->refundinfo == 1) ? $strs->yes : $strs->no);
329 }
330
331 $table->data[] = array("<b>$authstrs->amount:</b>", "$order->currency $order->amount");
332 $table->data[] = array("<b>$authstrs->transid:</b>", $order->transid);
333 $table->data[] = array("<b>$strs->time:</b>", userdate($order->timecreated));
334 $table->data[] = array("<b>$authstrs->settlementdate:</b>", $settled ? userdate($order->settletime) : $authstrs->notsettled);
335 $table->data[] = array("<b>$strs->status:</b>", "<b><font style='color:$color'>" . $authstrs->{$statusandactions->status} . "</font></b>");
336
337 if (ORDER_CAPTURE == $do && in_array(ORDER_CAPTURE, $statusandactions->actions)) {
338 if ($confirm && confirm_sesskey()) {
c7dac7f5 339 $message = '';
340 $extra = NULL;
5a2a5331 341 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
06090362 342 if (empty($an_test)) {
ca177540 343 if (enrol_into_course($course, $user, 'authorize')) {
06090362 344 if ($plugin->get_config('enrol_mailstudents')) {
36814b99 345 send_welcome_messages($orderid);
9c746ce7 346 }
36814b99 347 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
c7dac7f5 348 }
349 else {
8ebbb06a
SH
350 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
351 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", "Error while trying to enrol ".fullname($user)." in '" . $shortname . "'", 20);
c7dac7f5 352 }
353 }
354 else {
36814b99 355 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
c7dac7f5 356 }
357 }
36814b99 358 else {
359 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
360 }
c7dac7f5 361 }
36814b99 362 $table->data[] = array("<b>$strs->confirm:</b>", get_string('captureyes', 'enrol_authorize') . '<br />' .
363 authorize_print_action_button($orderid, ORDER_CAPTURE, 0, true, false, $strs->no));
16be8974 364 echo html_writer::table($table);
c7dac7f5 365 }
36814b99 366 elseif (ORDER_REFUND == $do && in_array(ORDER_REFUND, $statusandactions->actions)) {
367 $refunded = 0.0;
368 $sql = "SELECT SUM(amount) AS refunded
1751f48f 369 FROM {enrol_authorize_refunds}
370 WHERE (orderid = ?)
371 AND (status = ?)";
c7dac7f5 372
1751f48f 373 if (($refundval = $DB->get_field_sql($sql, array($orderid, AN_STATUS_CREDIT)))) {
36814b99 374 $refunded = floatval($refundval);
c7dac7f5 375 }
36814b99 376 $upto = round($order->amount - $refunded, 2);
c7dac7f5 377 if ($upto <= 0) {
6a933f13 378 print_error('refoundtoorigi', '',
379 "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $order->amount);
c7dac7f5 380 }
92ebcabe 381 $amount = round(optional_param('amount', $upto, PARAM_RAW), 2);
36814b99 382 if ($amount > $upto) {
6a933f13 383 print_error('refoundto', '',
384 "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $upto);
36814b99 385 }
386 if ($confirm && confirm_sesskey()) {
387 $extra = new stdClass;
388 $extra->orderid = $orderid;
389 $extra->amount = $amount;
390 $message = '';
5a2a5331 391 $success = AuthorizeNet::process($order, $message, $extra, AN_ACTION_CREDIT);
36814b99 392 if (AN_APPROVED == $success || AN_REVIEW == $success) {
06090362 393 if (empty($an_test)) {
36814b99 394 if (empty($extra->id)) {
395 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", "insert record error", 20);
c7dac7f5 396 }
397 else {
36814b99 398 if (!empty($unenrol)) {
06090362
AB
399 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
400 $plugin->unenrol_user($pinstance, $order->userid);
401 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
36814b99 402 }
403 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
c7dac7f5 404 }
405 }
406 else {
36814b99 407 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
c7dac7f5 408 }
409 }
36814b99 410 else {
411 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
412 }
c7dac7f5 413 }
36814b99 414 $a = new stdClass;
415 $a->upto = $upto;
416 $extrahtml = get_string('howmuch', 'enrol_authorize') .
417 ' <input type="text" size="5" name="amount" value="'.$amount.'" /> ' .
418 get_string('canbecredit', 'enrol_authorize', $a) . '<br />';
419 $table->data[] = array("<b>$strs->confirm:</b>",
420 authorize_print_action_button($orderid, ORDER_REFUND, 0, true, $authstrs->unenrolstudent, $strs->no, $extrahtml));
16be8974 421 echo html_writer::table($table);
c7dac7f5 422 }
36814b99 423 elseif (ORDER_DELETE == $do && in_array(ORDER_DELETE, $statusandactions->actions)) {
424 if ($confirm && confirm_sesskey()) {
425 if (!empty($unenrol)) {
06090362
AB
426 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
427 $plugin->unenrol_user($pinstance, $order->userid);
428 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
c7dac7f5 429 }
1751f48f 430 $DB->delete_records('enrol_authorize', array('id'=>$orderid));
36814b99 431 redirect("$CFG->wwwroot/enrol/authorize/index.php");
432 }
433 $table->data[] = array("<b>$strs->confirm:</b>",
434 authorize_print_action_button($orderid, ORDER_DELETE, 0, true, $authstrs->unenrolstudent,$strs->no));
16be8974 435 echo html_writer::table($table);
36814b99 436 }
437 elseif (ORDER_VOID == $do) { // special case: cancel original or refunded transaction?
438 $suborderid = optional_param('suborder', 0, PARAM_INT);
439 if (empty($suborderid) && in_array(ORDER_VOID, $statusandactions->actions)) { // cancel original
440 if ($confirm && confirm_sesskey()) {
c7dac7f5 441 $extra = NULL;
442 $message = '';
5a2a5331 443 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_VOID)) {
06090362 444 if (empty($an_test)) {
36814b99 445 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
c7dac7f5 446 }
447 else {
36814b99 448 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
c7dac7f5 449 }
450 }
451 else {
36814b99 452 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
c7dac7f5 453 }
454 }
36814b99 455 $table->data[] = array("<b>$strs->confirm:</b>", get_string('voidyes', 'enrol_authorize') . '<br />' .
456 authorize_print_action_button($orderid, ORDER_VOID, 0, true, false, $strs->no));
16be8974 457 echo html_writer::table($table);
36814b99 458 }
459 elseif (!empty($suborderid)) { // cancel refunded
460 $sql = "SELECT r.*, e.courseid, e.paymentmethod
1751f48f 461 FROM {enrol_authorize_refunds} r
462 INNER JOIN {enrol_authorize} e
36814b99 463 ON r.orderid = e.id
1751f48f 464 WHERE r.id = ?
465 AND r.orderid = ?
466 AND r.status = ?";
b9c3d818 467
1751f48f 468 $suborder = $DB->get_record_sql($sql, array($suborderid, $orderid, AN_STATUS_CREDIT));
c7dac7f5 469 if (!$suborder) { // not found
6a933f13 470 print_error('transactionvoid', '', "$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
c7dac7f5 471 }
36814b99 472 $refundedstatus = authorize_get_status_action($suborder);
473 unset($suborder->courseid);
474 if (in_array(ORDER_VOID, $refundedstatus->actions)) {
475 if ($confirm && confirm_sesskey()) {
c7dac7f5 476 $message = '';
477 $extra = NULL;
5a2a5331 478 if (AN_APPROVED == AuthorizeNet::process($suborder, $message, $extra, AN_ACTION_VOID)) {
06090362 479 if (empty($an_test)) {
c7dac7f5 480 if (!empty($unenrol)) {
06090362
AB
481 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid));
482 $plugin->unenrol_user($pinstance, $order->userid);
483 //role_unassign_all(array('userid'=>$order->userid, 'contextid'=>$coursecontext->id, 'component'=>'enrol_authorize'), true, true);
c7dac7f5 484 }
36814b99 485 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid");
c7dac7f5 486 }
487 else {
36814b99 488 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", get_string('testwarning', 'enrol_authorize'), 10);
c7dac7f5 489 }
490 }
491 else {
36814b99 492 redirect("$CFG->wwwroot/enrol/authorize/index.php?order=$orderid", $message, 20);
c7dac7f5 493 }
494 }
36814b99 495 $a = new stdClass;
496 $a->transid = $suborder->transid;
497 $a->amount = $suborder->amount;
498 $table->data[] = array("<b>$strs->confirm:</b>", get_string('subvoidyes', 'enrol_authorize', $a) . '<br />' .
499 authorize_print_action_button($orderid, ORDER_VOID, $suborderid, true, $authstrs->unenrolstudent, $strs->no));
16be8974 500 echo html_writer::table($table);
c7dac7f5 501 }
502 }
c7dac7f5 503 }
36814b99 504 else {
16be8974 505 echo html_writer::table($table);
36814b99 506
c7dac7f5 507 if ($settled) { // show refunds.
7d50e687 508 $t2 = new html_table();
be4f7e7f 509 $t2->size = array('45%', '15%', '20%', '10%', '10%');
17dd6691 510 $t2->align = array('right', 'right', 'right', 'right', 'right');
36814b99 511 $t2->head = array($authstrs->settlementdate, $authstrs->transid, $strs->status, $strs->action, $authstrs->amount);
512
513 $sql = "SELECT r.*, e.courseid, e.paymentmethod
1751f48f 514 FROM {enrol_authorize_refunds} r
515 INNER JOIN {enrol_authorize} e
36814b99 516 ON r.orderid = e.id
1751f48f 517 WHERE r.orderid = ?";
36814b99 518
1751f48f 519 if (($refunds = $DB->get_records_sql($sql, array($orderid)))) {
be4f7e7f 520 $sumrefund = floatval(0.0);
c7dac7f5 521 foreach ($refunds as $rf) {
36814b99 522 $subactions = '';
9b47e001 523 $substatus = authorize_get_status_action($rf);
c7dac7f5 524 if (empty($substatus->actions)) {
525 $subactions .= $strs->none;
526 }
527 else {
528 foreach ($substatus->actions as $vl) {
36814b99 529 $subactions .= authorize_print_action_button($orderid, $vl, $rf->id);
c7dac7f5 530 }
531 }
be4f7e7f 532 $sign = '';
516b1385 533 $color = authorize_get_status_color($substatus->status);
534 if ($substatus->status == 'refunded' or $substatus->status == 'settled') {
535 $sign = '-';
536 $sumrefund += floatval($rf->amount);
be4f7e7f 537 }
be4f7e7f 538 $t2->data[] = array(
539 userdate($rf->settletime),
540 $rf->transid,
36814b99 541 "<b><font style='color:$color'>" .$authstrs->{$substatus->status} . "</font></b>",
be4f7e7f 542 $subactions,
516b1385 543 format_float($sign . $rf->amount, 2)
be4f7e7f 544 );
c7dac7f5 545 }
be4f7e7f 546 $t2->data[] = array('','',get_string('total'),$order->currency,format_float('-'.$sumrefund, 2));
c7dac7f5 547 }
548 else {
17dd6691 549 $t2->data[] = array('','',get_string('noreturns', 'enrol_authorize'),'','');
c7dac7f5 550 }
d65af16f 551 echo "<h4>" . get_string('returns', 'enrol_authorize') . "</h4>\n";
16be8974 552 echo html_writer::table($t2);
c7dac7f5 553 }
554 }
36814b99 555
2af6a7e5 556 echo $OUTPUT->footer();
c7dac7f5 557}
558
ece1fbee 559/**
9b47e001 560 * authorize_get_status_action
ece1fbee 561 *
562 * @param object $order Order details.
563 * @return object
564 */
9b47e001 565function authorize_get_status_action($order)
c7dac7f5 566{
17dd6691 567 global $CFG;
64d187aa 568 static $newordertime = 0;
362bdbf7 569
64d187aa 570 if (0 == $newordertime) {
411df816 571 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
362bdbf7 572 }
c7dac7f5 573
574 $ret = new stdClass();
575 $ret->actions = array();
576
a2610c21 577 $canmanage = has_capability('enrol/authorize:managepayments', get_context_instance(CONTEXT_COURSE, $order->courseid));
4503e31d 578
f5eb7e2f 579 if (floatval($order->transid) == 0) { // test transaction or new order
17dd6691 580 if ($order->timecreated < $newordertime) {
a2610c21 581 if ($canmanage) {
17dd6691 582 $ret->actions = array(ORDER_DELETE);
583 }
584 $ret->status = 'tested';
585 }
586 else {
587 $ret->status = 'new';
c7dac7f5 588 }
c7dac7f5 589 return $ret;
590 }
591
592 switch ($order->status) {
36814b99 593 case AN_STATUS_AUTH:
5a2a5331 594 if (AuthorizeNet::expired($order)) {
36814b99 595 if ($canmanage) {
596 $ret->actions = array(ORDER_DELETE);
597 }
598 $ret->status = 'expired';
c7dac7f5 599 }
36814b99 600 else {
601 if ($canmanage) {
602 $ret->actions = array(ORDER_CAPTURE, ORDER_VOID);
603 }
604 $ret->status = 'authorizedpendingcapture';
c7dac7f5 605 }
36814b99 606 return $ret;
c7dac7f5 607
36814b99 608 case AN_STATUS_AUTHCAPTURE:
5a2a5331 609 if (AuthorizeNet::settled($order)) {
36814b99 610 if ($canmanage) {
611 if (($order->paymentmethod == AN_METHOD_CC) || ($order->paymentmethod == AN_METHOD_ECHECK && !empty($order->refundinfo))) {
612 $ret->actions = array(ORDER_REFUND);
613 }
be09f121 614 }
36814b99 615 $ret->status = 'settled';
c7dac7f5 616 }
36814b99 617 else {
618 if ($order->paymentmethod == AN_METHOD_CC && $canmanage) {
619 $ret->actions = array(ORDER_VOID);
620 }
621 $ret->status = 'capturedpendingsettle';
c7dac7f5 622 }
36814b99 623 return $ret;
c7dac7f5 624
36814b99 625 case AN_STATUS_CREDIT:
5a2a5331 626 if (AuthorizeNet::settled($order)) {
36814b99 627 $ret->status = 'settled';
c7dac7f5 628 }
36814b99 629 else {
630 if ($order->paymentmethod == AN_METHOD_CC && $canmanage) {
631 $ret->actions = array(ORDER_VOID);
632 }
633 $ret->status = 'refunded';
634 }
635 return $ret;
c7dac7f5 636
36814b99 637 case AN_STATUS_VOID:
638 $ret->status = 'cancelled';
639 return $ret;
c7dac7f5 640
36814b99 641 case AN_STATUS_EXPIRE:
642 if ($canmanage) {
643 $ret->actions = array(ORDER_DELETE);
644 }
645 $ret->status = 'expired';
646 return $ret;
c7dac7f5 647
36814b99 648 case AN_STATUS_UNDERREVIEW:
649 $ret->status = 'underreview';
650 return $ret;
9c746ce7 651
36814b99 652 case AN_STATUS_APPROVEDREVIEW:
653 $ret->status = 'approvedreview';
654 return $ret;
9c746ce7 655
36814b99 656 case AN_STATUS_REVIEWFAILED:
657 if ($canmanage) {
658 $ret->actions = array(ORDER_DELETE);
659 }
660 $ret->status = 'reviewfailed';
661 return $ret;
9c746ce7 662
36814b99 663 default:
664 return $ret;
c7dac7f5 665 }
c7dac7f5 666}
516b1385 667
668
669function authorize_get_status_color($status)
670{
671 $color = 'black';
9c746ce7 672 switch ($status)
673 {
674 case 'settled':
9c746ce7 675 case 'capturedpendingsettle':
676 $color = '#339900'; // green
677 break;
678
9c746ce7 679 case 'underreview':
36814b99 680 case 'approvedreview':
516b1385 681 case 'authorizedpendingcapture':
682 $color = '#FF6600'; // orange
683 break;
684
36814b99 685 case 'new':
686 case 'tested':
687 $color = '#003366'; // blue
688 break;
689
516b1385 690 case 'expired':
691 case 'cancelled':
692 case 'refunded';
9c746ce7 693 case 'reviewfailed':
516b1385 694 $color = '#FF0033'; // red
695 break;
696 }
697 return $color;
698}
36814b99 699
700function authorize_print_action_button($orderid, $do, $suborderid=0, $confirm=false, $unenrol=false, $nobutton=false, $extrahtml='')
701{
47353153 702 global $CFG, $OUTPUT;
36814b99 703 global $authstrs;
704
705 $ret = '<form action="'.$CFG->wwwroot.'/enrol/authorize/index.php'.'" method="post"><div>' .
706 '<input type="hidden" name="order" value="'.$orderid.'" />' .
707 '<input type="hidden" name="do" value="'.$do.'" />' .
708 '<input type="hidden" name="sesskey" value="'. sesskey() . '" />';
709 if (!empty($suborderid)) {
710 $ret .= '<input type="hidden" name="suborder" value="'.$suborderid.'" />';
711 }
712 if (!empty($confirm)) {
713 $ret .= '<input type="hidden" name="confirm" value="1" />';
714 }
715 if (!empty($unenrol)) {
2f0e96e4 716 $ret .= html_writer::checkbox('unenrol', 1, false, $unenrol) . '<br />';
36814b99 717 }
718 $ret .= $extrahtml;
719 $ret .= '<input type="submit" value="'.$authstrs->$do.'" />' .
720 '</div></form>';
721 if (!empty($nobutton)) {
722 $ret .= '<form method="get" action="index.php"><div><input type="hidden" name="order" value="'.$orderid.'" /><input type="submit" value="'.$nobutton.'" /></div></form>';
723 }
724 return $ret;
725}
4317f92f 726