3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.');
7 require_once('const.php');
8 require_once('authorizenetlib.php');
10 define('ORDER_CAPTURE', 'capture');
11 define('ORDER_DELETE', 'delete');
12 define('ORDER_REFUND', 'refund');
13 define('ORDER_VOID', 'void');
16 * authorize_print_orders
19 function authorize_print_orders($courseid, $userid)
21 global $CFG, $USER, $SITE;
22 global $strs, $authstrs;
23 require_once($CFG->libdir
.'/tablelib.php');
26 $status = optional_param('status', AN_STATUS_NONE
, PARAM_INT
);
27 $searchtype = optional_param('searchtype', 'id', PARAM_ALPHA
);
28 $idortransid = optional_param('idortransid', '0', PARAM_INT
);
29 $showonlymy = optional_param('showonlymy', 0, PARAM_BOOL
);
31 $canmanagepayments = has_capability('enrol/authorize:managepayments', get_context_instance(CONTEXT_COURSE
, $courseid));
33 if ($showonlymy ||
!$canmanagepayments) {
37 $baseurl = $CFG->wwwroot
."/enrol/authorize/index.php?user=$userid";
38 $statusmenu = array(AN_STATUS_NONE
=> $strs->all
,
39 AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW
=> $authstrs->allpendingorders
,
40 AN_STATUS_AUTH
=> $authstrs->authorizedpendingcapture
,
41 AN_STATUS_AUTHCAPTURE
=> $authstrs->authcaptured
,
42 AN_STATUS_CREDIT
=> $authstrs->refunded
,
43 AN_STATUS_VOID
=> $authstrs->cancelled
,
44 AN_STATUS_EXPIRE
=> $authstrs->expired
,
45 AN_STATUS_UNDERREVIEW
=> $authstrs->underreview
,
46 AN_STATUS_APPROVEDREVIEW
=> $authstrs->approvedreview
,
47 AN_STATUS_REVIEWFAILED
=> $authstrs->reviewfailed
,
48 AN_STATUS_TEST
=> $authstrs->tested
51 $sql = "SELECT c.id, c.fullname FROM {$CFG->prefix}course c INNER JOIN {$CFG->prefix}enrol_authorize e ON c.id = e.courseid ";
53 $sql .= "WHERE (e.userid='$userid') ";
55 $sql .= "ORDER BY c.sortorder, c.fullname";
56 if ($popupcrs = get_records_sql_menu($sql)) {
57 $popupcrs = array($SITE->id
=> $SITE->fullname
) +
$popupcrs;
58 echo "<table border='0' width='100%' cellspacing='0' cellpadding='3' class='generaltable generalbox'>";
60 echo "<td width='5%' valign='top'>$strs->status: </td><td width='10%'>";
61 popup_form($baseurl.'&course='.$courseid.'&status=',$statusmenu,'statusmenu',$status,'','','',false);
62 if ($canmanagepayments) {
64 print_checkbox('showonlymy', '1', $userid == $USER->id
, get_string('mypaymentsonly', 'enrol_authorize'), '',
65 "var locationtogo = '{$CFG->wwwroot}/enrol/authorize/index.php?status=$status&course=$courseid';
66 locationtogo += '&user=' + (this.checked ? '$USER->id' : '0');
67 top.location.href=locationtogo;");
70 echo "<td width='5%' valign='top'>$strs->course: </td><td width='10%' valign='top'>";
71 popup_form($baseurl.'&status='.$status.'&course=',$popupcrs,'coursesmenu',$courseid,'','','',false);echo"</td>\n";
72 if (has_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER
, $USER->id
))) {
73 echo "<form method='get' action='uploadcsv.php'>";
74 echo "<td rowspan='2' align='right' valign='middle' width='50%'><div><input type='submit' value='".get_string('uploadcsv', 'enrol_authorize')."' /></div></td>";
78 echo "<td rowspan=2 width='100%'> </td>";
82 echo "<tr><td>$strs->search: </td>"; $searchmenu = array('id' => $authstrs->orderid
, 'transid' => $authstrs->transid
);
83 echo "<form method='POST' action='index.php' autocomplete='off'>";
84 echo "<td colspan='3'>"; choose_from_menu($searchmenu, 'searchtype', $searchtype, '');
85 echo " = <fieldset class=\"invisiblefieldset\"><input type='text' size='14' name='idortransid' value='' /> ";
86 echo "<input type='submit' value='$strs->search' /></fieldset></td>";
92 $table = new flexible_table('enrol-authorize');
93 $table->set_attribute('width', '100%');
94 $table->set_attribute('cellspacing', '0');
95 $table->set_attribute('cellpadding', '3');
96 $table->set_attribute('id', 'orders');
97 $table->set_attribute('class', 'generaltable generalbox');
99 $table->define_columns(array('id', 'timecreated', 'userid', 'status', ''));
100 $table->define_headers(array($authstrs->orderid
, $strs->time
, $authstrs->nameoncard
, $strs->status
, $strs->action
));
101 $table->define_baseurl($baseurl."&status=$status&course=$courseid");
103 $table->sortable(true, 'id', SORT_DESC
);
104 $table->pageable(true);
107 $select = "SELECT e.id, e.paymentmethod, e.transid, e.courseid, e.userid, e.status, e.ccname, e.timecreated, e.settletime ";
108 $from = "FROM {$CFG->prefix}enrol_authorize e ";
109 $where = "WHERE (1=1) ";
111 if ($status > AN_STATUS_NONE
) {
114 case AN_STATUS_AUTH | AN_STATUS_UNDERREVIEW | AN_STATUS_APPROVEDREVIEW
:
115 $where .= 'AND (e.status IN('.AN_STATUS_AUTH
.','.AN_STATUS_UNDERREVIEW
.','.AN_STATUS_APPROVEDREVIEW
.')) ';
118 case AN_STATUS_CREDIT
:
119 $from .= "INNER JOIN {$CFG->prefix}enrol_authorize_refunds r ON e.id = r.orderid ";
120 $where .= "AND (e.status = '" . AN_STATUS_AUTHCAPTURE
. "') ";
124 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
125 $where .= "AND (e.status = '" . AN_STATUS_NONE
. "') AND (e.transid = '0') AND (e.timecreated < $newordertime) ";
129 $where .= "AND (e.status = '$status') ";
134 if (empty($CFG->an_test
)) {
135 $where .= "AND (e.status != '" . AN_STATUS_NONE
. "') ";
139 if ($courseid != SITEID
) {
140 $where .= "AND (e.courseid = '" . $courseid . "') ";
143 if (!empty($idortransid)) {
145 if ($searchtype == 'transid') {
146 $where = "WHERE (e.transid = $idortransid) ";
149 $where = "WHERE (e.id = $idortransid) ";
153 // This must be always last where!!!
155 $where .= "AND (e.userid = '" . $userid . "') ";
158 if ($sort = $table->get_sql_sort()) {
159 $sort = ' ORDER BY ' . $sort;
162 $totalcount = count_records_sql('SELECT COUNT(*) ' . $from . $where);
163 $table->initialbars($totalcount > $perpage);
164 $table->pagesize($perpage, $totalcount);
166 if ($records = get_records_sql($select . $from . $where . $sort, $table->get_page_start(), $table->get_page_size())) {
167 foreach ($records as $record) {
168 $actionstatus = authorize_get_status_action($record);
169 $color = authorize_get_status_color($actionstatus->status
);
172 if (empty($actionstatus->actions
)) {
173 $actions .= $strs->none
;
176 foreach ($actionstatus->actions
as $value) {
177 $actions .= " <a href='index.php?$value=y&sesskey=$USER->sesskey&order=$record->id'>{$authstrs->$value}</a> ";
181 $table->add_data(array(
182 "<a href='index.php?order=$record->id'>$record->id</a>",
183 userdate($record->timecreated
),
185 "<font style='color:$color'>" . $authstrs->{$actionstatus->status
} . "</font>",
191 $table->print_html();
195 * authorize_print_order_details
197 * @param int $orderno
199 function authorize_print_order_details($orderno)
202 global $strs, $authstrs;
204 $cmdcapture = optional_param(ORDER_CAPTURE
, '', PARAM_ALPHA
);
205 $cmddelete = optional_param(ORDER_DELETE
, '', PARAM_ALPHA
);
206 $cmdrefund = optional_param(ORDER_REFUND
, '', PARAM_ALPHA
);
207 $cmdvoid = optional_param(ORDER_VOID
, '', PARAM_ALPHA
);
209 $unenrol = optional_param('unenrol', 0, PARAM_BOOL
);
210 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
212 $table = new stdClass
;
213 $table->width
= '100%';
214 $table->size
= array('30%', '70%');
215 $table->align
= array('right', 'left');
217 $order = get_record('enrol_authorize', 'id', $orderno);
219 notice("Order $orderno not found.", "index.php");
223 $course = get_record('course', 'id', $order->courseid
);
224 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
226 if ($USER->id
!= $order->userid
) { // Current user viewing someone else's order
227 require_capability('enrol/authorize:managepayments', $coursecontext);
230 echo "<form action=\"index.php\" method=\"post\">\n";
232 echo "<input type=\"hidden\" name=\"order\" value=\"$orderno\" />\n";
233 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
235 $settled = authorize_settled($order);
236 $status = authorize_get_status_action($order);
238 $table->data
[] = array("<b>$authstrs->paymentmethod:</b>",
239 ($order->paymentmethod
== AN_METHOD_CC ?
$authstrs->methodcc
: $authstrs->methodecheck
));
240 $table->data
[] = array("<b>$authstrs->orderid:</b>", $orderno);
241 $table->data
[] = array("<b>$authstrs->transid:</b>", $order->transid
);
242 $table->data
[] = array("<b>$authstrs->amount:</b>", "$order->currency $order->amount");
243 if (empty($cmdcapture) and empty($cmdrefund) and empty($cmdvoid) and empty($cmddelete)) {
244 $color = authorize_get_status_color($status->status
);
245 $table->data
[] = array("<b>$strs->course:</b>", format_string($course->shortname
));
246 $table->data
[] = array("<b>$strs->status:</b>", "<font style='color:$color'>" . $authstrs->{$status->status
} . "</font>");
247 if ($order->paymentmethod
== AN_METHOD_CC
) {
248 $table->data
[] = array("<b>$authstrs->nameoncard:</b>", $order->ccname
);
251 $table->data
[] = array("<b>$authstrs->echeckfirslasttname:</b>", $order->ccname
);
253 $table->data
[] = array("<b>$strs->time:</b>", userdate($order->timecreated
));
254 $table->data
[] = array("<b>$authstrs->settlementdate:</b>", $settled ?
255 userdate($order->settletime
) : $authstrs->notsettled
);
257 $table->data
[] = array(" ", "<hr size='1' />\n");
259 if (!empty($cmdcapture) and confirm_sesskey()) { // CAPTURE
260 if (!in_array(ORDER_CAPTURE
, $status->actions
)) {
262 $a->action
= $authstrs->capture
;
263 error(get_string('youcantdo', 'enrol_authorize', $a));
266 if (empty($confirm)) {
267 $strcaptureyes = get_string('captureyes', 'enrol_authorize');
268 $table->data
[] = array("<b>$strs->confirm:</b>",
269 "$strcaptureyes <br />
270 <input type='hidden' name='confirm' value='1' /><input type='submit' name='". ORDER_CAPTURE
."' value='$authstrs->capture' />
271 <a href='index.php?order=$orderno'>$strs->no</a>");
276 if (AN_APPROVED
!= authorize_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE
)) {
277 $table->data
[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
280 if (empty($CFG->an_test
)) {
281 $user = get_record('user', 'id', $order->userid
);
282 if (enrol_into_course($course, $user, 'manual')) {
283 if (!empty($CFG->enrol_mailstudents
)) {
284 send_welcome_messages($order->id
);
286 redirect("index.php?order=$orderno");
289 $table->data
[] = array("<b><font color='red'>$strs->error:</font></b>",
290 "Error while trying to enrol ".fullname($user)." in '" . format_string($course->shortname
) . "'");
294 $table->data
[] = array(get_string('testmode', 'enrol_authorize'),
295 get_string('testwarning', 'enrol_authorize'));
301 elseif (!empty($cmdrefund) and confirm_sesskey()) { // REFUND
302 if (!in_array(ORDER_REFUND
, $status->actions
)) {
304 $a->action
= $authstrs->refund
;
305 error(get_string('youcantdo', 'enrol_authorize', $a));
308 $extra = new stdClass
;
310 $extra->orderid
= $orderno;
312 $sql = "SELECT SUM(amount) AS refunded FROM {$CFG->prefix}enrol_authorize_refunds " .
313 "WHERE (orderid = '" . $orderno . "') AND (status = '" . AN_STATUS_CREDIT
. "')";
315 if ($refund = get_record_sql($sql)) {
316 $extra->sum
= floatval($refund->refunded
);
318 $upto = format_float($order->amount
- $extra->sum
, 2);
320 error("Refunded to original amount.");
323 $amount = format_float(optional_param('amount', $upto), 2);
324 if (($amount > $upto) or empty($confirm)) {
327 $strcanbecredit = get_string('canbecredit', 'enrol_authorize', $a);
328 $strhowmuch = get_string('howmuch', 'enrol_authorize');
329 $cbunenrol = print_checkbox('unenrol', '1', !empty($unenrol), '', '', '', true);
330 $table->data
[] = array("<b>$authstrs->unenrolstudent</b>", $cbunenrol);
331 $table->data
[] = array("<b>$strhowmuch</b>",
332 "<input type='hidden' name='confirm' value='1' />
333 <input type='text' size='5' name='amount' value='$amount' />
334 $strcanbecredit<br /><input type='submit' name='".ORDER_REFUND
."' value='$authstrs->refund' />");
337 $extra->amount
= $amount;
339 $success = authorize_action($order, $message, $extra, AN_ACTION_CREDIT
);
340 if (AN_APPROVED
== $success || AN_REVIEW
== $success) {
341 if (empty($CFG->an_test
)) {
342 if (empty($extra->id
)) {
343 $table->data
[] = array("<b><font color='red'>$strs->error:</font></b>", 'insert record error');
346 if (!empty($unenrol)) {
347 role_unassign(0, $order->userid
, 0, $coursecontext->id
);
349 redirect("index.php?order=$orderno");
353 $table->data
[] = array(get_string('testmode', 'enrol_authorize'),
354 get_string('testwarning', 'enrol_authorize'));
358 $table->data
[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
364 elseif (!empty($cmdvoid) and confirm_sesskey()) { // VOID
365 $suborderno = optional_param('suborder', 0, PARAM_INT
);
366 if (empty($suborderno)) { // cancel original transaction.
367 if (!in_array(ORDER_VOID
, $status->actions
)) {
369 $a->action
= $authstrs->void
;
370 error(get_string('youcantdo', 'enrol_authorize', $a));
372 if (empty($confirm)) {
373 $strvoidyes = get_string('voidyes', 'enrol_authorize');
374 $table->data
[] = array("<b>$strs->confirm:</b>",
375 "$strvoidyes<br /><input type='hidden' name='".ORDER_VOID
."' value='y' />
376 <input type='hidden' name='confirm' value='1' />
377 <input type='submit' value='$authstrs->void' />
378 <a href='index.php?order=$orderno'>$strs->no</a>");
383 if (AN_APPROVED
== authorize_action($order, $message, $extra, AN_ACTION_VOID
)) {
384 if (empty($CFG->an_test
)) {
385 redirect("index.php?order=$orderno");
388 $table->data
[] = array(get_string('testmode', 'enrol_authorize'),
389 get_string('testwarning', 'enrol_authorize'));
393 $table->data
[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
397 else { // cancel refunded transaction
398 $sql = "SELECT r.*, e.courseid, e.paymentmethod FROM {$CFG->prefix}enrol_authorize_refunds r " .
399 "INNER JOIN {$CFG->prefix}enrol_authorize e ON r.orderid = e.id " .
400 "WHERE r.id = '$suborderno' AND r.orderid = '$orderno' AND r.status = '" .AN_STATUS_CREDIT
. "'";
402 $suborder = get_record_sql($sql);
403 if (!$suborder) { // not found
404 error("Transaction can not be voided because of already been voided.");
407 $refundedstatus = authorize_get_status_action($suborder);
408 if (!in_array(ORDER_VOID
, $refundedstatus->actions
)) {
410 $a->action
= $authstrs->void
;
411 error(get_string('youcantdo', 'enrol_authorize', $a));
413 unset($suborder->courseid
);
414 if (empty($confirm)) {
416 $a->transid
= $suborder->transid
;
417 $a->amount
= $suborder->amount
;
418 $strsubvoidyes = get_string('subvoidyes', 'enrol_authorize', $a);
419 $cbunenrol = print_checkbox('unenrol', '1', !empty($unenrol), '', '', '', true);
420 $table->data
[] = array("<b>$authstrs->unenrolstudent</b>", $cbunenrol);
421 $table->data
[] = array("<b>$strs->confirm:</b>",
422 "$strsubvoidyes<br /><input type='hidden' name='".ORDER_VOID
."' value='y' />
423 <input type='hidden' name='confirm' value='1' />
424 <input type='hidden' name='suborder' value='$suborderno' />
425 <input type='submit' value='$authstrs->void' />
426 <a href='index.php?order=$orderno'>$strs->no</a>");
431 if (AN_APPROVED
== authorize_action($suborder, $message, $extra, AN_ACTION_VOID
)) {
432 if (empty($CFG->an_test
)) {
433 if (!empty($unenrol)) {
434 role_unassign(0, $order->userid
, 0, $coursecontext->id
);
436 redirect("index.php?order=$orderno");
439 $table->data
[] = array(get_string('testmode', 'enrol_authorize'),
440 get_string('testwarning', 'enrol_authorize'));
444 $table->data
[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
451 elseif (!empty($cmddelete) and confirm_sesskey()) { // DELETE
452 if (!in_array(ORDER_DELETE
, $status->actions
)) {
454 $a->action
= $authstrs->delete
;
455 error(get_string('youcantdo', 'enrol_authorize', $a));
457 if (empty($confirm)) {
458 $cbunenrol = print_checkbox('unenrol', '1', !empty($unenrol), '', '', '', true);
459 $table->data
[] = array("<b>$authstrs->unenrolstudent</b>", $cbunenrol);
460 $table->data
[] = array("<b>$strs->confirm:</b>",
461 "<input type='hidden' name='".ORDER_DELETE
."' value='y' />
462 <input type='hidden' name='confirm' value='1' />
463 <input type='submit' value='$authstrs->delete' />
464 <a href='index.php?order=$orderno'>$strs->no</a>");
467 if (!empty($unenrol)) {
468 role_unassign(0, $order->userid
, 0, $coursecontext->id
);
470 delete_records('enrol_authorize', 'id', $orderno);
471 redirect("index.php");
477 if (empty($status->actions
)) {
478 if (($order->paymentmethod
== AN_METHOD_ECHECK
) && has_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER
, $USER->id
))) {
479 $actions .= '<a href="uploadcsv.php">'.get_string('uploadcsv', 'enrol_authorize').'</a>';
482 $actions .= $strs->none
;
486 foreach ($status->actions
as $value) {
487 $actions .= "<input type='submit' name='$value' value='{$authstrs->$value}' /> ";
490 $table->data
[] = array("<b>$strs->action</b>", $actions);
492 if ($settled) { // show refunds.
494 $t2->size
= array('45%', '15%', '20%', '10%', '10%');
495 $t2->align
= array('right', 'right', 'right', 'right', 'right');
496 $t2->head
= array($authstrs->settlementdate
,
502 $sql = "SELECT r.*, e.courseid, e.paymentmethod FROM {$CFG->prefix}enrol_authorize_refunds r " .
503 "INNER JOIN {$CFG->prefix}enrol_authorize e ON r.orderid = e.id " .
504 "WHERE r.orderid = '$orderno'";
506 $refunds = get_records_sql($sql);
508 $sumrefund = floatval(0.0);
509 foreach ($refunds as $rf) {
510 $substatus = authorize_get_status_action($rf);
511 $subactions = ' ';
512 if (empty($substatus->actions
)) {
513 $subactions .= $strs->none
;
516 foreach ($substatus->actions
as $vl) {
518 "<a href='index.php?$vl=y&sesskey=$USER->sesskey&order=$orderno&suborder=$rf->id'>{$authstrs->$vl}</a> ";
522 $color = authorize_get_status_color($substatus->status
);
523 if ($substatus->status
== 'refunded' or $substatus->status
== 'settled') {
525 $sumrefund +
= floatval($rf->amount
);
528 userdate($rf->settletime
),
530 "<font style='color:$color'>" .$authstrs->{$substatus->status
} . "</font>",
532 format_float($sign . $rf->amount
, 2)
535 $t2->data
[] = array('','',get_string('total'),$order->currency
,format_float('-'.$sumrefund, 2));
538 $t2->data
[] = array('','',get_string('noreturns', 'enrol_authorize'),'','');
540 echo "<h4>" . get_string('returns', 'enrol_authorize') . "</h4>\n";
549 * authorize_get_status_action
551 * @param object $order Order details.
554 function authorize_get_status_action($order)
557 static $newordertime;
559 if (empty($newordertime)) {
560 $newordertime = time() - 120; // -2 minutes. Order may be still in process.
563 $ret = new stdClass();
564 $ret->actions
= array();
566 $canmanage = has_capability('enrol/authorize:managepayments', get_context_instance(CONTEXT_COURSE
, $order->courseid
));
568 if (intval($order->transid
) == 0) { // test transaction or new order
569 if ($order->timecreated
< $newordertime) {
571 $ret->actions
= array(ORDER_DELETE
);
573 $ret->status
= 'tested';
576 $ret->status
= 'new';
581 switch ($order->status
) {
583 if (authorize_expired($order)) {
585 $ret->actions
= array(ORDER_DELETE
);
587 $ret->status
= 'expired';
591 $ret->actions
= array(ORDER_CAPTURE
, ORDER_VOID
);
593 $ret->status
= 'authorizedpendingcapture';
597 case AN_STATUS_AUTHCAPTURE
:
598 if (authorize_settled($order)) {
600 if (($order->paymentmethod
== AN_METHOD_CC
) ||
($order->paymentmethod
== AN_METHOD_ECHECK
&& !empty($order->refundinfo
))) {
601 $ret->actions
= array(ORDER_REFUND
);
604 $ret->status
= 'settled';
607 if ($order->paymentmethod
== AN_METHOD_CC
&& $canmanage) {
608 $ret->actions
= array(ORDER_VOID
);
610 $ret->status
= 'capturedpendingsettle';
614 case AN_STATUS_CREDIT
:
615 if (authorize_settled($order)) {
616 $ret->status
= 'settled';
619 if ($order->paymentmethod
== AN_METHOD_CC
&& $canmanage) {
620 $ret->actions
= array(ORDER_VOID
);
622 $ret->status
= 'refunded';
627 $ret->status
= 'cancelled';
630 case AN_STATUS_EXPIRE
:
632 $ret->actions
= array(ORDER_DELETE
);
634 $ret->status
= 'expired';
637 case AN_STATUS_UNDERREVIEW
:
638 $ret->status
= 'underreview';
641 case AN_STATUS_APPROVEDREVIEW
:
642 $ret->status
= 'approvedreview';
645 case AN_STATUS_REVIEWFAILED
:
647 $ret->actions
= array(ORDER_DELETE
);
649 $ret->status
= 'reviewfailed';
658 function authorize_get_status_color($status)
664 case 'approvedreview':
665 case 'capturedpendingsettle':
666 $color = '#339900'; // green
672 case 'authorizedpendingcapture':
673 $color = '#FF6600'; // orange
680 $color = '#FF0033'; // red