Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

New authorize.net module is more powerful. :)

- Review order:

   Review order before processing the credit card.

- Auto-Capture:
   If auto-capture enabled the credit card will be captured
   after an_review_day later, then student will be enrolled
   to course, otherwise it will be expired or
   admin must accept/deny payment.

   NOTE: Set-up Transaction Cut-Off Time correctly.
         When the last transaction is picked up for settlement?

- Payment Management:

   Accept/Deny/Refund payments: cvs:/moodle/enrol/authorize/index.php

- TO DO: Batch upload: It will sync authorize.net and moodle from csv file.
  • Loading branch information...
commit 62d98252ac1b865038ad640c95be6588e274464a 1 parent 8cb3ad1
authored December 26, 2005
15  enrol/authorize/action.php
@@ -46,9 +46,6 @@
46 46
 define('AN_ACTION_VOID', 0x08);
47 47
 
48 48
 
49  
-define('AN_REASON_NONE', 0);
50  
-define('AN_REASON_TRAN_NOT_FOUND', 16);
51  
-
52 49
 /**
53 50
  * Gets settlement date and time
54 51
  *
@@ -82,11 +79,11 @@ function settled($order)
82 79
     static $timenow;
83 80
 
84 81
     if (!isset($timenow)) {
85  
-    	$timenow = time();
  82
+        $timenow = time();
86 83
     }
87 84
 
88 85
     return (($order->status == AN_STATUS_AUTHCAPTURE || $order->status == AN_STATUS_CREDIT)
89  
-            && $order->settletime < $timenow && $order->settletime > 0);
  86
+            && $order->settletime > 0 && $order->settletime < $timenow );
90 87
 }
91 88
 
92 89
 /**
@@ -94,14 +91,13 @@ function settled($order)
94 91
  *
95 92
  * @param object &$order Which transaction data will be send. See enrol_authorize table.
96 93
  * @param string &$message Information about error messages.
97  
- * @param int &$reason Reason subcode
98 94
  * @param object &$extra Extra transaction data.
99 95
  * @param int $action Which action will be performed. See AN_ACTION_*
100 96
  * @return bool true, transaction was successful, false otherwise.
101 97
  * @author Ethem Evlice <ethem a.t evlice d.o.t com>
102 98
  * @uses $CFG
103 99
  */
104  
-function authorizenet_action(&$order, &$message, &$reason, &$extra, $action=AN_ACTION_NONE)
  100
+function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE)
105 101
 {
106 102
     global $CFG;
107 103
     static $conststring;
@@ -297,10 +293,11 @@ function authorizenet_action(&$order, &$message, &$reason, &$extra, $action=AN_A
297 293
         $response[$rcount] = substr($response[$rcount], 0, -1);
298 294
     }
299 295
 
300  
-    $reason = intval($response[2]);
301  
-
302 296
     if ($response[0] == AN_APPROVED)
303 297
     {
  298
+        if ($an_test || intval($response[6]) == 0) {
  299
+            return true; // don't update original transaction in test mode.
  300
+        }
304 301
         switch ($action) {
305 302
             case AN_ACTION_AUTH_ONLY:
306 303
             case AN_ACTION_AUTH_CAPTURE:
189  enrol/authorize/enrol.php
@@ -163,6 +163,11 @@ function cc_submit($form, $course)
163 163
                 return;
164 164
         }
165 165
 
  166
+        if (!empty($CFG->an_test)) {
  167
+            error("Credit card module cannot be present because of test mode");
  168
+            return;
  169
+        }
  170
+
166 171
         $this->prevent_double_paid($course);
167 172
 
168 173
         $exp_date = ($form->ccexpiremm < 10) ? strval('0'.$form->ccexpiremm) : strval($form->ccexpiremm);
@@ -221,21 +226,40 @@ function cc_submit($form, $course)
221 226
         $extra->x_description = $course->shortname;
222 227
 
223 228
         $message = '';
224  
-        $reason = AN_REASON_NONE;
225 229
         $an_review = !empty($CFG->an_review);
226 230
         $action = $an_review ? AN_ACTION_AUTH_ONLY : AN_ACTION_AUTH_CAPTURE;
227  
-        $success = authorizenet_action($order, $message, $reason, $extra, $action);
  231
+        $success = authorizenet_action($order, $message, $extra, $action);
228 232
         if (!$success) {
229 233
             $this->email_to_admin($message, $order);
230 234
             $this->ccerrormsg = $message;
231 235
             return;
232 236
         }
233 237
 
  238
+        if (intval($order->transid) == 0) { // I know it is test mode. :)
  239
+            error("Credit card module cannot be present because of test mode");
  240
+            return;
  241
+        }
  242
+
234 243
         $SESSION->ccpaid = 1; // security check: don't duplicate payment
235 244
         if ($an_review) { // review enabled, inform admin and redirect to main page.
236 245
             if (update_record("enrol_authorize", $order)) {
237  
-                // notification: new transaction (AUTH_ONLY)
238  
-                // see order details: index.php?order=$order->id
  246
+                $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$order->id";
  247
+                $a->orderid = $order->id;
  248
+                $a->transid = $order->transid;
  249
+                $a->amount = "$order->currency $order->amount";
  250
+                $a->expireon = getsettletime($timenow + (30 * 3600 * 24));
  251
+                $a->captureon = getsettletime($timenow + (intval($CFG->an_review_day) * 3600 * 24));
  252
+                $a->course = $course->fullname;
  253
+                $a->user = fullname($USER);
  254
+                $a->acstatus = ($CFG->an_review_day > 0) ? get_string('yes') : get_string('no');
  255
+                $emailmessage = get_string('adminneworder', 'enrol_authorize', $a);
  256
+                $a->course = $course->shortname;
  257
+                $a->orderid = $order->id;
  258
+                $emailsubject = get_string('adminnewordersubject', 'enrol_authorize', $a);
  259
+                $admins = get_admins();
  260
+                foreach ($admins as $admin) {
  261
+                    email_to_user($admin, $USER, $emailsubject, $emailmessage);
  262
+                }
239 263
             }
240 264
             else {
241 265
                 $this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
@@ -404,7 +428,8 @@ function config_form($frm)
404 428
             if (!(empty($frm->an_review) || $frm->an_review_day < 1)) {
405 429
                 // ++ENABLED++
406 430
                 // Cron must be runnig!!! Check last cron...
407  
-                $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
  431
+                $mconfig = get_config('enrol/authorize');
  432
+                $lastcron = intval($mconfig->an_lastcron);
408 433
                 if (time() - $lastcron > 3600 * 24) {
409 434
                     // Cron must be enabled if you want to use autocapture feature.
410 435
                     // Setup cron or disable an_review again...
@@ -480,8 +505,9 @@ function process_config($config)
480 505
             if ($review_day_val < 0) $review_day_val = 0;
481 506
             elseif ($review_day_val > 29) $review_day_val = 29;
482 507
             if ($review_day_val > 0) {
483  
-                // cron is required.
484  
-                $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
  508
+                // Cron must change an_lastcron. :))
  509
+                $mconfig = get_config('enrol/authorize');
  510
+                $lastcron = intval($mconfig->an_lastcron);
485 511
                 if (time() - $lastcron > 3600 * 24) {
486 512
                     // No!!! I am not lucky. No changes please...
487 513
                     return false;
@@ -525,17 +551,16 @@ function prevent_double_paid($course)
525 551
     {
526 552
         global $CFG, $SESSION, $USER;
527 553
 
  554
+        if ($rec = get_record('enrol_authorize', 'userid', $USER->id, 'courseid', $course->id, 'status', AN_STATUS_AUTH, 'id')) {
  555
+            $a->orderid = $rec->id;
  556
+            redirect($CFG->wwwroot, get_string("paymentpending", "enrol_authorize", $a), '20');
  557
+            return;
  558
+        }
528 559
         if (isset($SESSION->ccpaid)) {
529 560
             unset($SESSION->ccpaid);
530 561
             redirect($CFG->wwwroot . '/login/logout.php');
531 562
             return;
532 563
         }
533  
-
534  
-        if ($rec = get_record('enrol_authorize', 'userid',$USER->id, 'courseid',$course->id, 'status',AN_STATUS_AUTH, 'id')) {
535  
-            $a->orderid = $rec->id;
536  
-            redirect($CFG->wwwroot, get_string("paymentpending", "enrol_authorize", $a), '20');
537  
-            return;
538  
-        }
539 564
     }
540 565
 
541 566
 
@@ -560,78 +585,106 @@ function cron()
560 585
         parent::cron();
561 586
         require_once("$CFG->dirroot/enrol/authorize/action.php");
562 587
 
563  
-        srand((double)microtime() * 10000000);
564  
-        $random100 = rand(0, 100);
565 588
         $timenow = time();
566 589
         $timenowsettle = getsettletime($timenow);
567 590
         $timediff30 = $timenowsettle - (30 * 3600 * 24);
  591
+        // These 2 lines must be HERE and must be EXUCUTED. See process_config.
  592
+        // We use an_lastcron when processing AUTOCAPTURE feature.
  593
+        // Order is important. 1. get_config 2. set_config
  594
+        $mconfig = get_config('enrol/authorize'); // MUST be 1st.
  595
+        set_config('an_lastcron', $timenow, 'enrol/authorize'); // MUST be 2nd.
568 596
 
569  
-        if ($random100 < 15) { // delete very old records: status=AN_STATUS_NONE & timecreated=-60day.
570  
-            // no credit card transaction is made in status AN_STATUS_NONE.
571  
-            $timediff60 = $timenowsettle - (60 * 3600 * 24);
572  
-            $select = "(status = '" .AN_STATUS_NONE. "') AND (timecreated < '$timediff60')";
573  
-            if (count_records_select('enrol_authorize', $select)) {
574  
-                mtrace("Deleting records in authorize table older than 60 days (status=AN_STATUS_NONE).");
575  
-                delete_records_select('enrol_authorize', $select);
576  
-            }
  597
+        $random100 = mt_rand(0, 100);
  598
+
  599
+        if ($random100 < 33) {
  600
+            $select = "(status = '" .AN_STATUS_NONE. "') AND (timecreated < '$timediff30')";
  601
+            delete_records_select('enrol_authorize', $select);
  602
+        }
  603
+        elseif ($random100 > 66) {
  604
+            $select = "(status = '" .AN_STATUS_AUTH. "') AND (timecreated < '$timediff30')";
  605
+            execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status = '" .AN_STATUS_EXPIRE. "' WHERE $select", false);
577 606
         }
578  
-        elseif ($random100 > 80) { // EXPIRED: Transactions with auth_only will be expired 30 days later.
579  
-            $select = "(status = '" .AN_STATUS_AUTH. "') AND (settletime = '0') AND (timecreated < '$timediff30')";
580  
-            execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET settletime = '$timenowsettle', status = '" .AN_STATUS_EXPIRE. "' WHERE $select", false);
  607
+        else {
  608
+            $timediff60 = $timenowsettle - (60 * 3600 * 24);
  609
+            $select = "(status = '" .AN_STATUS_EXPIRE. "') AND (timecreated < '$timediff60')";
  610
+            delete_records_select('enrol_authorize', $select);
581 611
         }
582 612
 
  613
+        if (!empty($CFG->an_test)) {
  614
+            return; // AUTOCAPTURE doesn't work in test mode.
  615
+        }
583 616
         if (empty($CFG->an_review) || empty($CFG->an_review_day) || $CFG->an_review_day < 1) {
584  
-            // AUTOCAPTURE disabled. admin, teacher review it manually
585  
-            return;
  617
+            return; // AUTOCAPTURE disabled. admin, teacher review it manually
586 618
         }
587 619
 
588  
-        // AUTO-CAPTURE: it must be captured within 30 days. Otherwise it will expired.
  620
+        // AUTO-CAPTURE: Transaction must be captured within 30 days. Otherwise it will expired.
589 621
         $timediffcnf = $timenowsettle - (intval($CFG->an_review_day) * 3600 * 24);
590  
-        $select = "(status = '" . AN_STATUS_AUTH . "') AND (settletime = '0') AND (timecreated < '$timediffcnf') AND (timecreated > '$timediff30')";
591  
-        if ($orders = get_records_select('enrol_authorize', $select)) {
592  
-            $this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
593  
-            @set_time_limit(0);
594  
-            $faults = '';
595  
-            foreach ($orders as $order) {
596  
-                $message = NULL;
597  
-                $extra = NULL;
598  
-                $reason = AN_REASON_NONE;
599  
-                $success = authorizenet_action($order, $message, $reason, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
600  
-                if ($success) {
601  
-                    if (!update_record("enrol_authorize", $order)) {
602  
-                        $this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
603  
-                        "ID=$order->id in enrol_authorize table.", $order);
604  
-                    }
605  
-                    $timestart = $timeend = 0;
606  
-                    if ($course = get_record_sql("SELECT enrolperiod FROM {$CFG->prefix}course WHERE id='$order->courseid'")) {
607  
-                        if ($course->enrolperiod) {
608  
-                            $timestart = $timenow;
609  
-                            $timeend = $timestart + $course->enrolperiod;
610  
-                        }
611  
-                    }
612  
-                    if (enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'authorize')) {
613  
-                        $this->log .= "User($order->userid) has been enrolled to course($order->courseid).\n";
614  
-                    }
615  
-                    else {
616  
-                        $faults .= "Error while trying to enrol ".fullname($USER)." in '$course->fullname' \n";
617  
-                        foreach ($order as $okey => $ovalue) {
618  
-                            $faults .= "   $okey = $ovalue\n";
619  
-                        }
  622
+        $select = "status = '" .AN_STATUS_AUTH. "' AND timecreated < '$timediffcnf' AND timecreated > '$timediff30'";
  623
+        if (!$orders = get_records('enrol_authorize', $select)) {
  624
+            return;
  625
+        }
  626
+
  627
+        // Calculate connection speed for each transaction. Default: 3 secs.
  628
+        $everyconnection = empty($mconfig->an_eachconnsecs) ? 3 : intval($mconfig->an_eachconnsecs);
  629
+        $ordercount = count((array)$orders);
  630
+        $maxsecs = $everyconnection * $ordercount;
  631
+        if ($maxsecs + intval($mconfig->an_lastcron) > $timenow) {
  632
+            return; // autocapture runs every eachconnsecs*count.
  633
+        }
  634
+
  635
+        $faults = '';
  636
+        $elapsed = time();
  637
+        @set_time_limit(0);
  638
+        $this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
  639
+        foreach ($orders as $order) {
  640
+            $message = '';
  641
+            $extra = NULL;
  642
+            $oldstatus = $order->status;
  643
+            $success = authorizenet_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
  644
+            if ($success) {
  645
+                if (!update_record("enrol_authorize", $order)) {
  646
+                    $this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
  647
+                    "ID=$order->id in enrol_authorize table.", $order);
  648
+                }
  649
+                $timestart = $timeend = 0;
  650
+                if ($course = get_record_sql("SELECT enrolperiod FROM {$CFG->prefix}course WHERE id='$order->courseid'")) {
  651
+                    if ($course->enrolperiod) {
  652
+                        $timestart = $timenow;
  653
+                        $timeend = $timestart + $course->enrolperiod;
620 654
                     }
621 655
                 }
622  
-                else { // not success
623  
-                    $this->log .= $message . "\n";
  656
+                if (enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'authorize')) {
  657
+                    $this->log .= "User($order->userid) has been enrolled to course($order->courseid).\n";
  658
+                }
  659
+                else {
  660
+                    $faults .= "Error while trying to enrol ".fullname($USER)." in '$course->fullname' \n";
  661
+                    foreach ($order as $okey => $ovalue) {
  662
+                        $faults .= "   $okey = $ovalue\n";
  663
+                    }
624 664
                 }
625 665
             }
626  
-            $this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate(time());
627  
-            $adminuser = get_admin();
628  
-            if (!empty($faults)) {
629  
-                email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON FAULTS", $faults);
630  
-            }
631  
-            if (!empty($CFG->enrol_mailadmins)) {
632  
-                email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
  666
+            else { // not success
  667
+                $this->log .= "Order $order->id: " . $message . "\n";
  668
+                if ($order->status != $oldstatus) { //expired
  669
+                    update_record("enrol_authorize", $order);
  670
+                }
633 671
             }
634 672
         }
  673
+
  674
+        $timenow = time();
  675
+        $elapsed = $timenow - $elapsed;
  676
+        $everyconnection = ceil($elapsed / $ordercount);
  677
+        set_config('an_eachconnsecs', $everyconnection, 'enrol/authorize');
  678
+
  679
+        $this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate($timenow);
  680
+
  681
+        $adminuser = get_admin();
  682
+        if (!empty($faults)) {
  683
+            email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON FAULTS", $faults);
  684
+        }
  685
+        if (!empty($CFG->enrol_mailadmins)) {
  686
+            email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
  687
+        }
635 688
     }
636 689
 }
637 690
 ?>
451  enrol/authorize/index.php
... ...
@@ -0,0 +1,451 @@
  1
+<?php // $Id$
  2
+
  3
+require_once("../../config.php");
  4
+require_once("enrol.php");
  5
+require_once("action.php");
  6
+
  7
+define('ORDER_CAPTURE', 'capture');
  8
+define('ORDER_DELETE', 'delete');
  9
+define('ORDER_REFUND', 'refund');
  10
+define('ORDER_VOID', 'void');
  11
+
  12
+if (!($site = get_site())) {
  13
+    error("Could not find a site!");
  14
+}
  15
+
  16
+require_login();
  17
+
  18
+if (!isadmin()) {
  19
+    error("You must be an administrator to use this page.");
  20
+}
  21
+
  22
+$csv = optional_param('csv', '', PARAM_ALPHA);
  23
+$orderid = optional_param('order', 0, PARAM_INT);
  24
+
  25
+$strs = get_strings(array('user', 'status', 'action', 'delete', 'time',
  26
+                   'course', 'confirm', 'yes', 'no', 'none', 'error'));
  27
+
  28
+$authstrs = get_strings(array('paymentmanagement', 'orderid', 'void', 'capture', 'refund',
  29
+                      'authorizedpendingcapture','capturedpendingsettle', 'capturedsettled',
  30
+                      'settled', 'refunded', 'cancelled', 'expired', 'tested',
  31
+                      'transid', 'settlementdate', 'notsettled', 'returns', 'noreturns', 'amount',
  32
+                      'howmuch', 'captureyes', 'unenrolstudent'), 'enrol_authorize');
  33
+
  34
+print_header("$site->shortname: $authstr->paymentmanagement", "$site->fullname", "<a href=\"index.php\">$authstr->paymentmanagement</a>", "");
  35
+
  36
+if (!empty($csv)) {
  37
+    authorize_csv();
  38
+}
  39
+elseif (!empty($orderid)) {
  40
+    authorize_order_details($orderid);
  41
+}
  42
+else {
  43
+    authorize_orders();
  44
+}
  45
+print_footer();
  46
+
  47
+
  48
+function authorize_orders()
  49
+{
  50
+    global $CFG;
  51
+    global $strs, $authstrs;
  52
+    require_once($CFG->libdir.'/tablelib.php');
  53
+
  54
+    $perpage = 10;
  55
+    $userid = optional_param('user', 0, PARAM_INT);
  56
+    $courseid = optional_param('course', 0, PARAM_INT);
  57
+
  58
+    $table = new flexible_table('enrol-authorize');
  59
+    $table->set_attribute('width', '100%');
  60
+    $table->set_attribute('cellspacing', '0');
  61
+    $table->set_attribute('cellpadding', '3');
  62
+    $table->set_attribute('id', 'orders');
  63
+    $table->set_attribute('class', 'generaltable generalbox');
  64
+
  65
+    $table->define_columns(array('id', 'timecreated', 'userid', 'status', ''));
  66
+    $table->define_headers(array($authstrs->orderid, $strs->time, $strs->user, $strs->status, $strs->action));
  67
+    $table->define_baseurl($CFG->wwwroot."/enrol/authorize/index.php?course=$courseid&amp;user=$userid");
  68
+
  69
+    $table->sortable(true);
  70
+    $table->pageable(true);
  71
+    $table->setup();
  72
+
  73
+    $where = "WHERE (status != '" . AN_STATUS_NONE . "') ";
  74
+    if ($userid > 0) { $where .= "AND (userid = '" . $userid . "') "; }
  75
+    if ($courseid > 0) { $where .= "AND (courseid = '" . $courseid . "') "; }
  76
+
  77
+    $select = "SELECT id, transid, courseid, userid, status, ccname, timecreated, settletime ";
  78
+    $from = " FROM {$CFG->prefix}enrol_authorize ";
  79
+    if ($sort = $table->get_sql_sort()) {
  80
+        $sort = ' ORDER BY ' . $sort;
  81
+    }
  82
+    else {
  83
+        $sort = ' ORDER BY id DESC ';
  84
+    }
  85
+
  86
+    $totalcount = count_records_sql('SELECT COUNT(*) ' . $from . $where);
  87
+    $table->initialbars($totalcount > $perpage);
  88
+    $table->pagesize($perpage, $totalcount);
  89
+    if ($table->get_page_start() !== '' && $table->get_page_size() !== '') {
  90
+        $limit = ' ' . sql_paging_limit($table->get_page_start(), $table->get_page_size());
  91
+    }
  92
+    else {
  93
+        $limit = '';
  94
+    }
  95
+
  96
+    if ($records = get_records_sql($select . $from . $where . $sort . $limit)) {
  97
+        foreach ($records as $record) {
  98
+            $actionstatus = get_order_status_desc($record);
  99
+            $actions = '&nbsp;';
  100
+               foreach ($actionstatus->actions as $value) {
  101
+                $actions .= "&nbsp;&nbsp;<a href='index.php?$value=yes&amp;order=$record->id'>{$authstrs->$value}</a> ";
  102
+            }
  103
+            $table->add_data(array(
  104
+                "<a href='index.php?order=$record->id'>$record->id</a>",
  105
+                userdate($record->timecreated),
  106
+                $record->ccname,
  107
+                $authstrs->{$actionstatus->status},
  108
+                $actions
  109
+            ));
  110
+        }
  111
+    }
  112
+
  113
+    $table->print_html();
  114
+}
  115
+
  116
+
  117
+function authorize_order_details($orderno) {
  118
+    global $CFG;
  119
+    global $strs, $authstrs;
  120
+
  121
+    $unenrol = optional_param('unenrol', '');
  122
+    $cmdconfirm = optional_param('confirm', '', PARAM_ALPHA);
  123
+
  124
+    $cmdcapture = optional_param('capture', '', PARAM_ALPHA);
  125
+    $cmddelete = optional_param('delete', '', PARAM_ALPHA);
  126
+    $cmdrefund = optional_param('refund', '', PARAM_ALPHA);
  127
+    $cmdvoid = optional_param('void', '', PARAM_ALPHA);
  128
+
  129
+    $table->width = '100%';
  130
+    $table->size = array('30%', '70%');
  131
+    $table->align = array('right', 'left');
  132
+
  133
+    $sql = "SELECT E.*, C.shortname " .
  134
+    "FROM {$CFG->prefix}enrol_authorize E " .
  135
+    "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
  136
+    "WHERE E.id = '$orderno'";
  137
+
  138
+    $order = get_record_sql($sql);
  139
+    if (!$order) {
  140
+        notice("Order $orderno not found.", "index.php");
  141
+        return;
  142
+    }
  143
+
  144
+    echo "<form action='index.php' method='post'>\n";
  145
+    echo "<input type='hidden' name='order' value='$order->id'>\n";
  146
+
  147
+    $settled = settled($order);
  148
+    $status = get_order_status_desc($order);
  149
+
  150
+    $table->data[] = array("<b>$authstrs->orderid:</b>", $order->id);
  151
+    $table->data[] = array("<b>$authstrs->transid:</b>", $order->transid);
  152
+    $table->data[] = array("<b>$authstrs->amount:</b>", "$order->currency $order->amount");
  153
+    if ((empty($cmdcapture) and empty($cmdrefund) and empty($cmdvoid))) {
  154
+        $table->data[] = array("<b>$strs->course:</b>", $order->shortname);
  155
+        $table->data[] = array("<b>$strs->status:</b>", $authstrs->{$status->status});
  156
+        $table->data[] = array("<b>$strs->user:</b>", $order->ccname);
  157
+        $table->data[] = array("<b>$strs->time:</b>", userdate($order->timecreated));
  158
+        $table->data[] = array("<b>$authstrs->settlementdate:</b>", $settled ? userdate($order->settletime) : $authstrs->notsettled);
  159
+    }
  160
+    $table->data[] = array("&nbsp;", "<hr size='1' noshade>\n");
  161
+
  162
+    if (!empty($cmdcapture)) { // CAPTURE
  163
+        if (empty($cmdconfirm)) {
  164
+            $table->data[] = array("<b>$strs->confirm:</b>",
  165
+            "$authstrs->captureyes<br /><a href='index.php?order=$orderno&amp;capture=yes&amp;confirm=yes'>$strs->yes</a>
  166
+            &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>$strs->no</a>");
  167
+        }
  168
+        else {
  169
+            $message = '';
  170
+            $extra = NULL;
  171
+            $success = authorizenet_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
  172
+            update_record("enrol_authorize", $order); // May be expired.
  173
+            if (!$success) {
  174
+                $table->data[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
  175
+            }
  176
+            else {
  177
+                if (empty($CFG->an_test)) {
  178
+                    $timestart = $timeend = 0;
  179
+                    if ($course = get_record_sql("SELECT enrolperiod FROM {$CFG->prefix}course WHERE id='$order->courseid'")) {
  180
+                        if ($course->enrolperiod) {
  181
+                            $timestart = $order->settletime;
  182
+                            $timeend = $timestart + $course->enrolperiod;
  183
+                        }
  184
+                    }
  185
+                    enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'authorize');
  186
+                    // To do: inform user. You will access to course on date $order->settletime
  187
+                    redirect("index.php?order=$order->id");
  188
+                }
  189
+                else {
  190
+                    $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('capturetestwarn', 'enrol_authorize'));
  191
+                }
  192
+            }
  193
+        }
  194
+        print_table($table);
  195
+    }
  196
+    elseif (!empty($cmdrefund)) { // REFUND
  197
+        $extra = new stdClass();
  198
+        $extra->sum = 0.0;
  199
+        $extra->orderid = $order->id;
  200
+
  201
+        $sql = "SELECT SUM(amount) AS refunded FROM {$CFG->prefix}enrol_authorize_refunds " .
  202
+               "WHERE (orderid = '" . $order->id . "') AND (status = '" . AN_STATUS_CREDIT . "')";
  203
+
  204
+        if ($refund = get_record_sql($sql)) {
  205
+            $extra->sum = doubleval($refund->refunded);
  206
+        }
  207
+        $upto = format_float($order->amount - $extra->sum, 2);
  208
+        if ($upto <= 0) {
  209
+            error("Refunded to original amount.");
  210
+        }
  211
+        else {
  212
+            $amount = format_float(optional_param('amount', $upto), 2);
  213
+            if (($amount > $upto) || empty($cmdconfirm)) {
  214
+                $a->upto = $upto;
  215
+                $strcanbecredit = get_string('canbecredit', 'enrol_authorize', $a);
  216
+                $table->data[] = array("<b>$authstrs->unenrolstudent</b>",
  217
+                    "<input type='checkbox' name='unenrol' value='yes'" . (!empty($unenrol) ? " checked" : "") . ">");
  218
+                $table->data[] = array("<b>$authstrs->howmuch</b>",
  219
+                    "<input type='hidden' name='confirm' value='yes'>
  220
+                     <input type='text' size='5' name='amount' value='$amount'>
  221
+                     $strcanbecredit<br /><input type='submit' name='refund' value='$authstrs->refund'>");
  222
+            }
  223
+            else {
  224
+                $extra->amount = $amount;
  225
+                $message = '';
  226
+                $success = authorizenet_action($order, $message, $extra, AN_ACTION_CREDIT);
  227
+                if ($success) {
  228
+                    if (empty($CFG->an_test)) {
  229
+                        $extra->id = insert_record("enrol_authorize_refunds", $extra);
  230
+                        if (!$extra->id) {
  231
+                            // to do: email admin
  232
+                        }
  233
+                        if (!empty($unenrol)) {
  234
+                            unenrol_student($order->userid, $order->courseid);
  235
+                        }
  236
+                        redirect("index.php?order=$order->id");
  237
+                    }
  238
+                    else {
  239
+                        $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('credittestwarn', 'enrol_authorize'));
  240
+                    }
  241
+                }
  242
+                else {
  243
+                    $table->data[] = array("<b><font color=red>$strs->error:</font></b>", $message);
  244
+                }
  245
+            }
  246
+        }
  247
+        print_table($table);
  248
+    }
  249
+    elseif (!empty($cmdvoid)) { // VOID
  250
+        $suborderno = optional_param('suborder', 0, PARAM_INT);
  251
+        if (empty($suborderno)) { // cancel original transaction.
  252
+            if (empty($cmdconfirm)) {
  253
+                $strvoidyes = get_string('voidyes', 'enrol_authorize');
  254
+                $table->data[] = array("<b>$strs->confirm:</b>",
  255
+                    "$strvoidyes<br /><input type='hidden' name='void' value='yes'>
  256
+                     <input type='hidden' name='confirm' value='yes'>
  257
+                     <input type='submit' value='$strs->yes'>
  258
+                     &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>$strs->no</a>");
  259
+            }
  260
+            else {
  261
+                $extra = NULL;
  262
+                $message = '';
  263
+                $success = authorizenet_action($order, $message, $extra, AN_ACTION_VOID);
  264
+                update_record("enrol_authorize", $order); // May be expired.
  265
+                if ($success) {
  266
+                    if (empty($CFG->an_test)) {
  267
+                        redirect("index.php?order=$order->id");
  268
+                    }
  269
+                    else {
  270
+                       $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('voidtestwarn', 'enrol_authorize'));
  271
+                    }
  272
+                }
  273
+                else {
  274
+                    $table->data[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
  275
+                }
  276
+            }
  277
+        }
  278
+        else { // cancel refunded transaction
  279
+            $suborder = get_record('enrol_authorize_refunds', 'id', $suborderno, 'status', AN_STATUS_CREDIT);
  280
+            if (!$suborder) { // not found
  281
+                error("Transaction can not be voided because of already been voided.");
  282
+            }
  283
+            else {
  284
+                if (empty($cmdconfirm)) {
  285
+                    $a->transid = $suborder->transid;
  286
+                    $a->amount = $suborder->amount;
  287
+                    $strsubvoidyes = get_string('subvoidyes', 'enrol_authorize', $a);
  288
+
  289
+                    $table->data[] = array("<b>$authstrs->unenrolstudent</b>",
  290
+                        "<input type='checkbox' name='unenrol' value='yes'" . (!empty($unenrol) ? " checked" : "") . ">");
  291
+
  292
+                    $table->data[] = array("<b>$strs->confirm:</b>",
  293
+                        "$strsubvoidyes<br /><input type='hidden' name='void' value='yes'>
  294
+                         <input type='hidden' name='confirm' value='yes'>
  295
+                         <input type='hidden' name='suborder' value='$suborderno'>
  296
+                         <input type='submit' value='$strs->yes'>
  297
+                         &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>$strs->no</a>");
  298
+                }
  299
+                else {
  300
+                    $message = '';
  301
+                    $extra = NULL;
  302
+                    $success = authorizenet_action($suborder, $message, $extra, AN_ACTION_VOID);
  303
+                    update_record("enrol_authorize", $suborder); // May be expired.
  304
+                    if ($success) {
  305
+                        if (empty($CFG->an_test)) {
  306
+                            if (!empty($unenrol)) {
  307
+                                unenrol_student($order->userid, $order->courseid);
  308
+                            }
  309
+                            redirect("index.php?order=$order->id");
  310
+                        }
  311
+                        else {
  312
+                            $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('voidtestwarn', 'enrol_authorize'));
  313
+                        }
  314
+                    }
  315
+                    else {
  316
+                        $table->data[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
  317
+                    }
  318
+                }
  319
+            }
  320
+        }
  321
+        print_table($table);
  322
+    }
  323
+    elseif (!empty($cmddelete)) { // DELETE
  324
+        if (!in_array(ORDER_DELETE, $status->actions)) {
  325
+            error("Order $order->id cannot be deleted. Status must be expired.");
  326
+        }
  327
+        if (empty($cmdconfirm)) {
  328
+            $table->data[] = array('<b>Delete?: </b>',
  329
+            "<a href='index.php?order=$orderno&amp;delete=yes&amp;confirm=yes'>YES</a>
  330
+            &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>No</a>");
  331
+        }
  332
+        else {
  333
+            delete_records('enrol_authorize', 'id', $orderno);
  334
+            redirect("index.php");
  335
+        }
  336
+        print_table($table);
  337
+    }
  338
+    else { // SHOW
  339
+        $actions = '';
  340
+        if (empty($status->actions)) {
  341
+            $actions .= $strs->none;
  342
+        }
  343
+        else {
  344
+            foreach ($status->actions as $value) {
  345
+                $actions .= "<input type='submit' name='$value' value='{$authstrs->$value}'> ";
  346
+            }
  347
+        }
  348
+        $table->data[] = array("<b>$strs->action</b>", $actions);
  349
+        print_table($table);
  350
+        if ($settled) { // show refunds.
  351
+            echo "<h4>$authstrs->returns</h4>\n";
  352
+            $table2->size = array('15%', '15%', '20%', '35%', '15%');
  353
+            $table2->align = array('right', 'right', 'right', 'left', 'right');
  354
+            $table2->head = array($authstrs->transid, $authstrs->amount, $strs->status, $authstrs->settlementdate, $strs->action);
  355
+            $refunds = get_records('enrol_authorize_refunds', 'orderid', $orderno);
  356
+            if ($refunds) {
  357
+                foreach ($refunds as $rfnd) {
  358
+                    $substatus = get_order_status_desc($rfnd);
  359
+                    $subactions = '&nbsp;';
  360
+                    if (empty($substatus->actions)) {
  361
+                        $subactions .= $strs->none;
  362
+                    }
  363
+                    else {
  364
+                        foreach ($substatus->actions as $value) {
  365
+                            $subactions .= "<a href='index.php?$value=yes&amp;order=$orderno&amp;suborder=$rfnd->id'>{$authstrs->$value}</a> ";
  366
+                        }
  367
+                    }
  368
+                    $table2->data[] = array($rfnd->transid, $rfnd->amount, $authstrs->{$substatus->status}, userdate($rfnd->settletime), $subactions);
  369
+                }
  370
+            }
  371
+            else {
  372
+                $table2->data[] = array($authstrs->noreturns);
  373
+            }
  374
+            print_table($table2);
  375
+        }
  376
+    }
  377
+    echo '</form>';
  378
+}
  379
+
  380
+function authorize_csv()
  381
+{
  382
+    return;
  383
+}
  384
+
  385
+function get_order_status_desc($order)
  386
+{
  387
+    global $CFG;
  388
+    static $timediff30;
  389
+
  390
+    $ret = new stdClass();
  391
+
  392
+    if (intval($order->transid) == 0) { // test transaction
  393
+        $ret->actions = array(ORDER_DELETE);
  394
+        $ret->status = 'tested';
  395
+        return $ret;
  396
+    }
  397
+
  398
+    switch ($order->status) {
  399
+    case AN_STATUS_AUTH:
  400
+        if (empty($timediff30)) {
  401
+            $timediff30 = getsettletime(time()) - (30 * 3600 * 24);
  402
+        }
  403
+
  404
+        if (getsettletime($order->timecreated) < $timediff30) {
  405
+            $ret->actions = array(ORDER_DELETE);
  406
+            $ret->status = 'expired';
  407
+        }
  408
+        else {
  409
+            $ret->actions = array(ORDER_CAPTURE, ORDER_VOID);
  410
+            $ret->status = 'authorizedpendingcapture';
  411
+        }
  412
+        return $ret;
  413
+
  414
+    case AN_STATUS_AUTHCAPTURE:
  415
+        if (settled($order)) {
  416
+            $ret->actions = array(ORDER_REFUND);
  417
+            $ret->status = 'capturedsettled';
  418
+        }
  419
+        else {
  420
+            $ret->actions = array(ORDER_VOID);
  421
+            $ret->status = 'capturedpendingsettle';
  422
+        }
  423
+        return $ret;
  424
+
  425
+        case AN_STATUS_CREDIT:
  426
+        if (settled($order)) {
  427
+            $ret->actions = array();
  428
+            $ret->status = 'settled';
  429
+        }
  430
+        else {
  431
+            $ret->actions = array(ORDER_VOID);
  432
+            $ret->status = 'refunded';
  433
+        }
  434
+        return $ret;
  435
+
  436
+    case AN_STATUS_VOID:
  437
+        $ret->actions = array();
  438
+        $ret->status = 'cancelled';
  439
+        return $ret;
  440
+
  441
+    case AN_STATUS_EXPIRE:
  442
+        $ret->actions = array(ORDER_DELETE);
  443
+        $ret->status = 'expired';
  444
+        return $ret;
  445
+
  446
+    default:
  447
+        return $ret;
  448
+    }
  449
+
  450
+}
  451
+?>

0 notes on commit 62d9825

Please sign in to comment.
Something went wrong with that request. Please try again.