Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

AVS and Reason messages.

  • Loading branch information...
commit c1a91db06ebace4481a3924dce3c41b38c7ff011 1 parent 9d025f4
authored January 19, 2006
66  enrol/authorize/action.php
... ...
@@ -1,4 +1,4 @@
1  
-<?php // $Id$
  1
+<?php //  $Id$
2 2
 
3 3
 define('AN_HOST', 'secure.authorize.net');
4 4
 define('AN_HOST_TEST', 'certification.authorize.net');
@@ -10,7 +10,7 @@
10 10
 define('AN_DELIM', '|');
11 11
 define('AN_ENCAP', '"');
12 12
 
13  
-require_once("const.php");
  13
+require_once $CFG->dirroot.'/enrol/authorize/const.php';
14 14
 
15 15
 /**
16 16
  * Gets settlement date and time
@@ -42,7 +42,7 @@ function getsettletime($time)
42 42
 function settled($order)
43 43
 {
44 44
     return (($order->status == AN_STATUS_AUTHCAPTURE || $order->status == AN_STATUS_CREDIT) &&
45  
-             $order->settletime > 0 && $order->settletime < time());
  45
+            ($order->settletime > 0) && ($order->settletime < time()));
46 46
 }
47 47
 
48 48
 /**
@@ -87,13 +87,12 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
87 87
 
88 88
     $action = intval($action);
89 89
 
90  
-    // sanity check
91 90
     if (empty($order) || empty($order->id)) {
92  
-        $message = "check order->id!";
  91
+        $message = "Check order->id!";
93 92
         return false;
94 93
     }
95 94
     elseif ($action <= AN_ACTION_NONE || $action > AN_ACTION_VOID) {
96  
-        $message = "no action taken!";
  95
+        $message = "No action taken!";
97 96
         return false;
98 97
     }
99 98
 
@@ -105,11 +104,11 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
105 104
         case AN_ACTION_AUTH_CAPTURE:
106 105
         {
107 106
             if ($order->status != AN_STATUS_NONE) {
108  
-                $message = "order->status must be AN_STATUS_NONE!";
  107
+                $message = "Order status must be AN_STATUS_NONE(0)!";
109 108
                 return false;
110 109
             }
111 110
             if (empty($extra)) {
112  
-                $message = "need extra fields!";
  111
+                $message = "Need extra fields!";
113 112
                 return false;
114 113
             }
115 114
             $ext = (array)$extra;
@@ -124,7 +123,7 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
124 123
         case AN_ACTION_PRIOR_AUTH_CAPTURE:
125 124
         {
126 125
             if ($order->status != AN_STATUS_AUTH) {
127  
-                $message = "order->status must be AN_STATUS_AUTH!";
  126
+                $message = "Order status must be authorized!";
128 127
                 return false;
129 128
             }
130 129
             $timediff = $timenowsettle - (30 * 3600 * 24);
@@ -141,26 +140,23 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
141 140
         case AN_ACTION_CREDIT:
142 141
         {
143 142
             if ($order->status != AN_STATUS_AUTHCAPTURE) {
144  
-                $message = "order->status must be AN_STATUS_AUTHCAPTURE!";
  143
+                $message = "Order status must be authorized/captured!";
145 144
                 return false;
146 145
             }
147 146
             if (!settled($order)) {
148  
-                $message = "Order wasn't settled, try VOID. Check Cut-Off time if it fails!";
  147
+                $message = "Order must be settled. Try VOID, check Cut-Off time if it fails!";
149 148
                 return false;
150 149
             }
151  
-            // 120 days
152 150
             $timediff = $timenowsettle - (120 * 3600 * 24);
153 151
             if ($order->settletime < $timediff) {
154  
-                $message = "Order can be credited within 120 days!";
  152
+                $message = "Order must be credited within 120 days!";
155 153
                 return false;
156 154
             }
157  
-            // extra fields
158 155
             if (empty($extra)) {
159 156
                 $message = "need extra fields for CREDIT!";
160 157
                 return false;
161 158
             }
162  
-            // up to original amount
163  
-            $total = doubleval($extra->sum) + doubleval($extra->amount);
  159
+            $total = floatval($extra->sum) + floatval($extra->amount);
164 160
             if (($extra->amount == 0) || ($total > $order->amount)) {
165 161
                 $message = "Can be credited up to original amount.";
166 162
                 return false;
@@ -175,11 +171,10 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
175 171
         case AN_ACTION_VOID:
176 172
         {
177 173
             if ($order->status == AN_STATUS_AUTH) {
178  
-                // 30 days for authonly, make it expired (**settletime**)
179 174
                 $timediff = $timenowsettle - (30 * 3600 * 24);
180 175
                 $timecreatedsettle = getsettletime($order->timecreated);
181 176
                 if ($timecreatedsettle < $timediff) {
182  
-                    $message = "Auth_only transaction must be voided within 30 days. EXPIRED!";
  177
+                    $message = "Authorized transaction must be voided within 30 days. EXPIRED!";
183 178
                     $order->status = AN_STATUS_EXPIRE;
184 179
                     return false;
185 180
                 }
@@ -191,35 +186,34 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
191 186
                 }
192 187
             }
193 188
             else {
194  
-                $message = "order->status must be AUTH, AUTH_CAPTURE or CREDIT!";
  189
+                $message = "Order status must be authorized, auth/captured or refunded!";
195 190
                 return false;
196 191
             }
197 192
             $poststring .= '&x_type=VOID&x_trans_id=' . urlencode($order->transid);
198 193
             break;
199 194
         }
200 195
 
201  
-        default: { // ???
202  
-            $message = "missing action: $action";
  196
+        default: {
  197
+            $message = "Missing action? $action";
203 198
             return false;
204 199
         }
205 200
     }
206 201
 
207  
-    // referer
208  
-    $anrefererheader = '';
  202
+    $referer = '';
209 203
     if (! (empty($CFG->an_referer) || $CFG->an_referer == "http://")) {
210  
-        $anrefererheader = "Referer: " . $CFG->an_referer . "\r\n";
  204
+        $referer = "Referer: $CFG->an_referer\r\n";
211 205
     }
212 206
 
213 207
     $response = array();
214  
-    $connect_host = $an_test ? AN_HOST_TEST : AN_HOST;
215  
-    $fp = fsockopen("ssl://" . $connect_host, AN_PORT, $errno, $errstr, 60);
  208
+    $host = $an_test ? AN_HOST_TEST : AN_HOST;
  209
+    $fp = fsockopen("ssl://" . $host, AN_PORT, $errno, $errstr, 60);
216 210
     if (!$fp) {
217 211
         $message =  "no connection: $errstr ($errno)";
218 212
         return false;
219 213
     }
220 214
 
221 215
     fwrite($fp, "POST " . AN_PATH . " HTTP/1.0\r\n" .
222  
-                "Host: $connect_host\r\n" . $anrefererheader .
  216
+                "Host: $host\r\n" . $referer .
223 217
                 "Content-type: application/x-www-form-urlencoded\r\n" .
224 218
                 "Connection: close\r\n" .
225 219
                 "Content-length: " . strlen($poststring) . "\r\n\r\n" .
@@ -254,7 +248,8 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
254 248
 
255 249
     if ($response[0] == AN_APPROVED)
256 250
     {
257  
-        if ($an_test || intval($response[6]) == 0) {
  251
+        $transid = intval($response[6]);
  252
+        if ($an_test || $transid == 0) {
258 253
             return true; // don't update original transaction in test mode.
259 254
         }
260 255
         switch ($action) {
@@ -262,7 +257,7 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
262 257
             case AN_ACTION_AUTH_CAPTURE:
263 258
             case AN_ACTION_PRIOR_AUTH_CAPTURE:
264 259
             {
265  
-                $order->transid = strval($response[6]); // TransactionID
  260
+                $order->transid = $transid;
266 261
                 if ($action == AN_ACTION_AUTH_ONLY) {
267 262
                     $order->status = AN_STATUS_AUTH;
268 263
                     // dont't update settletime
@@ -277,7 +272,7 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
277 272
                 // Credit generates new transaction id.
278 273
                 // So, $extra must be updated, not $order.
279 274
                 $extra->status = AN_STATUS_CREDIT;
280  
-                $extra->transid = strval($response[6]);
  275
+                $extra->transid = $transid;
281 276
                 $extra->settletime = getsettletime(time());
282 277
                 break;
283 278
             }
@@ -293,7 +288,16 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
293 288
     }
294 289
     else
295 290
     {
296  
-        $message = isset($response[3]) ? $response[3] : 'unknown error';
  291
+        $reason = "reason" . $response[2];
  292
+        $message = get_string($reason, "enrol_authorize");
  293
+        if ($message == '[[' . $reason . ']]') {
  294
+            $message = isset($response[3]) ? $response[3] : 'unknown error';
  295
+        }
  296
+        if (!empty($CFG->an_avs)) {
  297
+            $avs = "avs" . strtolower($response[5]);
  298
+            $stravs = get_string($avs, "enrol_authorize");
  299
+            $message .= "<br />" . get_string("avsresult", "enrol_authorize") . $stravs;
  300
+        }
297 301
         return false;
298 302
     }
299 303
 }
45  enrol/authorize/enrol.html
... ...
@@ -1,4 +1,24 @@
1  
-<p align="center"><?php if (!empty($this->ccerrormsg)) { formerr($this->ccerrormsg); } ?></p>
  1
+<?php // $Id$
  2
+
  3
+$formvars = array('password', 'ccaddress', 'cccity', 'ccstate', 'cccountry', 'cczip',
  4
+                  'ccfirstname', 'cclastname', 'cc', 'ccexpiremm', 'ccexpireyyyy', 'cctype', 'cvv');
  5
+
  6
+foreach ($formvars as $var) {
  7
+    if (!isset($form->$var)) {
  8
+        $form->$var = '';
  9
+    }
  10
+}
  11
+
  12
+$curcost = $this->get_course_cost($course);
  13
+$userfirstname = empty($form->ccfirstname) ? $USER->firstname : $form->ccfirstname;
  14
+$userlastname = empty($form->cclastname) ? $USER->lastname : $form->cclastname;
  15
+$useraddress = empty($form->ccaddress) ? $USER->address : $form->ccaddress;
  16
+$usercity = empty($form->cccity) ? $USER->city : $form->cccity;
  17
+$usercountry = empty($form->cccountry) ? $USER->country : $form->cccountry;
  18
+
  19
+?>
  20
+
  21
+<p align="center"><?php if (!empty($this->ccerrors['header'])) { formerr($this->ccerrors['header']); } ?></p>
2 22
 
3 23
 <div align="center">
4 24
 
@@ -11,12 +31,14 @@
11 31
 <table align="center" width="100%" border=0>
12 32
 <tr>
13 33
   <td align="right"><?php print_string("ccno", "enrol_authorize") ?>: </td>
14  
-  <td align="left"><input type="text" name="cc" size="16" value="<?php p($form->cc) ?>"  /></td>
  34
+  <td align="left"><input type="text" name="cc" size="16" value="<?php p($form->cc) ?>"  />
  35
+  <?php if (!empty($this->ccerrors['cc'])) { formerr($this->ccerrors['cc']); } ?></td>
15 36
 </tr>
16 37
 <tr>
17 38
   <td align="right"><?php print_string("nameoncard", "enrol_authorize") ?>: </td>
18 39
   <td align="left"><input type="text" name="ccfirstname" size="8" value="<?php p($userfirstname) ?>"  />
19  
-  <input type="text" name="cclastname" size="8" value="<?php p($userlastname) ?>"  /></td>
  40
+  <input type="text" name="cclastname" size="8" value="<?php p($userlastname) ?>"  />
  41
+  <?php if (!empty($this->ccerrors['ccfirstlast'])) { formerr($this->ccerrors['ccfirstlast']); } ?></td>
20 42
 </tr>
21 43
 <tr>
22 44
   <td align="right"><?php print_string("ccexpire", "enrol_authorize") ?>: </td>
@@ -31,6 +53,7 @@
31 53
       $years[$i] = $i;
32 54
   }
33 55
   choose_from_menu($years, 'ccexpireyyyy', $form->ccexpireyyyy);
  56
+  if (!empty($this->ccerrors['ccexpire'])) { formerr($this->ccerrors['ccexpire']); }
34 57
   ?></td>
35 58
 </tr>
36 59
 <tr>
@@ -42,32 +65,38 @@
42 65
     'swi' => 'Switch',      'dlt' => 'Delta',       'enr' => 'EnRoute'
43 66
   );
44 67
   choose_from_menu($CCTYPES, 'cctype', $form->cctype);
  68
+  if (!empty($this->ccerrors['cctype'])) { formerr($this->ccerrors['cctype']); }
45 69
   ?>
46 70
 </td>
47 71
 </tr>
48 72
 <tr>
49 73
   <td align="right"><?php print_string("ccvv", "enrol_authorize") ?>: </td>
50 74
   <td align="left"><input type="text" name="cvv" size="4" maxlength="4" value="<?php p($form->cvv) ?>" />
51  
-  <?php helpbutton('', '', 'enrol_authorize', true, 'cvv', get_string('ccvvhelp', 'enrol_authorize')); ?></td>
  75
+  <?php helpbutton('', '', 'enrol_authorize', true, 'cvv', get_string('ccvvhelp', 'enrol_authorize')); ?>
  76
+  <?php if (!empty($this->ccerrors['cvv'])) { formerr($this->ccerrors['cvv']); } ?></td>
52 77
 </tr>
53 78
 <?php if (!empty($CFG->an_avs)) { /* Address Verification System */ ?>
54 79
 <tr>
55 80
   <td align="right"><?php print_string("address") ?>: </td>
56  
-  <td align="left"><input type="text" name="ccaddress" size="32" value="<?php p($useraddress) ?>"  /></td>
  81
+  <td align="left"><input type="text" name="ccaddress" size="32" value="<?php p($useraddress) ?>"  />
  82
+  <?php if (!empty($this->ccerrors['ccaddress'])) { formerr($this->ccerrors['ccaddress']); } ?></td>
57 83
 </tr>
58 84
 <tr>
59 85
   <td align="right"><?php print_string("city") ?> / <?php print_string("state") ?>: </td>
60 86
   <td align="left"><input type="text" name="cccity" size="16" value="<?php p($usercity) ?>" /> /
61  
-                   <input type="text" name="ccstate" size="2" maxlength="2" value="<?php p($form->ccstate) ?>" /></td>
  87
+  <input type="text" name="ccstate" size="2" maxlength="2" value="<?php p($form->ccstate) ?>" />
  88
+  <?php if (!empty($this->ccerrors['cccity'])) { formerr($this->ccerrors['cccity']); } ?></td>
62 89
 </tr>
63 90
 <tr>
64 91
   <td align="right"><?php print_string("country") ?>: </td>
65  
-  <td align="left"><?php choose_from_menu(get_list_of_countries(), "cccountry", $usercountry, get_string("selectacountry")."..."); ?></td>
  92
+  <td align="left"><?php choose_from_menu(get_list_of_countries(), "cccountry", $usercountry, get_string("selectacountry")."..."); ?>
  93
+  <?php if (!empty($this->ccerrors['cccountry'])) { formerr($this->ccerrors['cccountry']); } ?></td>
66 94
 </tr>
67 95
 <?php } ?>
68 96
 <tr>
69 97
   <td align="right"><?php print_string("zipcode", "enrol_authorize") ?>: </td>
70  
-  <td align="left"><input type="text" name="cczip" size="5" value="<?php p($form->cczip) ?>" /></td>
  98
+  <td align="left"><input type="text" name="cczip" size="5" value="<?php p($form->cczip) ?>" />
  99
+  <?php if (!empty($this->ccerrors['cczip'])) { formerr($this->ccerrors['cczip']); } ?></td>
71 100
 </tr>
72 101
 </table>
73 102
 <input type="submit" value="<?php print_string("sendpaymentbutton", "enrol_authorize") ?>">
336  enrol/authorize/enrol.php
... ...
@@ -1,7 +1,7 @@
1 1
 <?php  // $Id$
2 2
 
3  
-require_once("$CFG->dirroot/enrol/enrol.class.php");
4  
-require_once("$CFG->dirroot/enrol/authorize/const.php");
  3
+require_once $CFG->dirroot.'/enrol/enrol.class.php';
  4
+require_once $CFG->dirroot.'/enrol/authorize/const.php';
5 5
 
6 6
 /**
7 7
  * enrolment_plugin_authorize
@@ -10,12 +10,12 @@
10 10
 class enrolment_plugin extends enrolment_base
11 11
 {
12 12
     /**
13  
-     * Credit card error message.
  13
+     * Credit card error messages.
14 14
      *
15  
-     * @var string
  15
+     * @var array
16 16
      * @access public
17 17
      */
18  
-    var $ccerrormsg;
  18
+    var $ccerrors = array();
19 19
 
20 20
     /**
21 21
      * Cron log.
@@ -36,57 +36,38 @@ function print_entry($course)
36 36
     {
37 37
         global $CFG, $USER, $form;
38 38
 
39  
-        if ($this->zero_cost($course) || isguest()) { // No money for guests ;)
  39
+        if ($this->zero_cost($course) || isguest()) {
40 40
             parent::print_entry($course);
41  
-            return;
  41
+            return; // No money for guests ;)
42 42
         }
43 43
 
44  
-        // check payment
45 44
         $this->prevent_double_paid($course);
46 45
 
47  
-        // I want to pay on SSL.
48  
-        if (empty($_SERVER['HTTPS'])) {
  46
+        if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off') {
49 47
             if (empty($CFG->loginhttps)) {
50  
-                error(get_string("httpsrequired", "enrol_authorize"));
  48
+                error(get_string('httpsrequired', 'enrol_authorize'));
51 49
             } else {
52  
-                $wwwsroot = str_replace('http://','https://', $CFG->wwwroot);
53  
-                $sdestination = "$wwwsroot/course/enrol.php?id=$course->id";
54  
-                redirect($sdestination);
  50
+                $wwwsroot = str_replace('http:','https:', $CFG->wwwroot);
  51
+                redirect("$wwwsroot/course/enrol.php?id=$course->id");
55 52
                 exit;
56 53
             }
57 54
         }
58 55
 
59  
-        $formvars = array('password', 'ccfirstname', 'cclastname', 'cc', 'ccexpiremm', 'ccexpireyyyy', 'cctype', 'cvv',
60  
-                          'ccaddress', 'cccity', 'ccstate', 'cccountry', 'cczip');
61  
-
62  
-        foreach ($formvars as $var) {
63  
-            if (!isset($form->$var)) {
64  
-                $form->$var = '';
65  
-            }
66  
-        }
67  
-
68 56
         $teacher = get_teacher($course->id);
69  
-        $strloginto = get_string("loginto", "", $course->shortname);
70  
-        $strcourses = get_string("courses");
71  
-        $curcost = $this->get_course_cost($course);
72  
-
73  
-        $userfirstname = empty($form->ccfirstname) ? $USER->firstname : $form->ccfirstname;
74  
-        $userlastname = empty($form->cclastname) ? $USER->lastname : $form->cclastname;
75  
-        $useraddress = empty($form->ccaddress) ? $USER->address : $form->ccaddress;
76  
-        $usercity = empty($form->cccity) ? $USER->city : $form->cccity;
77  
-        $usercountry = empty($form->cccountry) ? $USER->country : $form->cccountry;
  57
+        $strcourses = get_string('courses');
  58
+        $strloginto = get_string('loginto', '', $course->shortname);
78 59
 
79 60
         print_header($strloginto, $course->fullname, "<a href=\"$CFG->wwwroot/course/\">$strcourses</a> -> $strloginto");
80  
-        print_course($course, "80%");
  61
+        print_course($course, '80%');
81 62
 
82 63
         if ($course->password) {
83 64
             print_simple_box(get_string('choosemethod', 'enrol_authorize'), 'center');
84 65
             $password = '';
85  
-            include($CFG->dirroot . '/enrol/internal/enrol.html');
  66
+            include $CFG->dirroot.'/enrol/internal/enrol.html';
86 67
         }
87 68
 
88  
-        print_simple_box_start("center");
89  
-        include($CFG->dirroot . '/enrol/authorize/enrol.html');
  69
+        print_simple_box_start('center');
  70
+        include $CFG->dirroot.'/enrol/authorize/enrol.html';
90 71
         print_simple_box_end();
91 72
 
92 73
         print_footer();
@@ -101,7 +82,7 @@ function print_entry($course)
101 82
      * @access public
102 83
      */
103 84
     function check_entry($form, $course) {
104  
-        if ($this->zero_cost($course) || isguest() || (!empty($form->password))) {
  85
+        if ((!empty($form->password)) || isguest() || $this->zero_cost($course)) {
105 86
             parent::check_entry($form, $course);
106 87
         } else {
107 88
             $this->cc_submit($form, $course);
@@ -120,22 +101,11 @@ function check_entry($form, $course) {
120 101
     function cc_submit($form, $course)
121 102
     {
122 103
         global $CFG, $USER, $SESSION;
123  
-        require_once($CFG->dirroot . '/enrol/authorize/ccval.php');
124  
-        require_once($CFG->dirroot . '/enrol/authorize/action.php');
125  
-
126  
-        if (empty($form->ccfirstname) || empty($form->cclastname) ||
127  
-            empty($form->cc) || empty($form->cvv) || empty($form->cctype) ||
128  
-            empty($form->ccexpiremm) || empty($form->ccexpireyyyy)) {
129  
-                $this->ccerrormsg = get_string("allfieldsrequired");
130  
-                return;
131  
-        }
  104
+        require_once $CFG->dirroot.'/enrol/authorize/ccval.php';
  105
+        require_once $CFG->dirroot.'/enrol/authorize/action.php';
132 106
 
133  
-        if (!empty($CFG->an_avs)) {
134  
-            if (empty($form->ccaddress) || empty($form->cccity) ||
135  
-                empty($form->cccountry) || empty($form->cczip)) {
136  
-                $this->ccerrormsg = get_string("allfieldsrequired");
137  
-                return;
138  
-            }
  107
+        if (!$this->validate_enrol_form($form)) {
  108
+            return;
139 109
         }
140 110
 
141 111
         $this->prevent_double_paid($course);
@@ -147,7 +117,12 @@ function cc_submit($form, $course)
147 117
         $useripno = getremoteaddr(); // HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR, REMOTE_ADDR
148 118
 
149 119
         if (!$valid_cc) {
150  
-            $this->ccerrormsg = get_string( (($valid_cc===0) ? 'ccexpired' : 'ccinvalid'), 'enrol_authorize' );
  120
+            if ($valid_cc === 0) {
  121
+                $this->ccerrors['ccexpire'] = get_string('ccexpired', 'enrol_authorize');
  122
+            }
  123
+            else {
  124
+            	$this->ccerrors['cc'] = get_string('ccinvalid', 'enrol_authorize');
  125
+            }
151 126
             return;
152 127
         }
153 128
 
@@ -167,7 +142,7 @@ function cc_submit($form, $course)
167 142
         $order->id = insert_record("enrol_authorize", $order);
168 143
         if (!$order->id) {
169 144
             $this->email_to_admin("Error while trying to insert new data", $order);
170  
-            $this->ccerrormsg = "Insert record error. Admin has been notified!";
  145
+            $this->ccerrors['header'] = "Insert record error. Admin has been notified!";
171 146
             return;
172 147
         }
173 148
 
@@ -186,12 +161,12 @@ function cc_submit($form, $course)
186 161
         $extra->x_exp_date = $exp_date;
187 162
         $extra->x_currency_code = $curcost['currency'];
188 163
         $extra->x_amount = $curcost['cost'];
189  
-        $extra->x_email = $USER->email;
190  
-        $extra->x_email_customer = 'TRUE';
191  
-        $extra->x_cust_id = $USER->id;
192  
-        $extra->x_customer_ip = $useripno;
193 164
         $extra->x_invoice_num = $order->id;
194 165
         $extra->x_description = $course->shortname;
  166
+        $extra->x_cust_id = $USER->id;
  167
+        $extra->x_customer_ip = $useripno;
  168
+        $extra->x_email = $USER->email;
  169
+        $extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
195 170
 
196 171
         $message = '';
197 172
         $an_review = !empty($CFG->an_review);
@@ -199,10 +174,11 @@ function cc_submit($form, $course)
199 174
         $success = authorizenet_action($order, $message, $extra, $action);
200 175
         if (!$success) {
201 176
             $this->email_to_admin($message, $order);
202  
-            $this->ccerrormsg = $message;
  177
+            $this->ccerrors['header'] = $message;
203 178
             return;
204 179
         }
205 180
 
  181
+        $SESSION->ccpaid = 1; // security check: don't duplicate payment
206 182
         if (intval($order->transid) == 0) { // TEST MODE
207 183
             if ($an_review) {
208 184
                 redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
@@ -216,7 +192,6 @@ function cc_submit($form, $course)
216 192
             return;
217 193
         }
218 194
 
219  
-        $SESSION->ccpaid = 1; // security check: don't duplicate payment
220 195
         if ($an_review) { // review enabled, inform admin and redirect to main page.
221 196
             if (update_record("enrol_authorize", $order)) {
222 197
                 $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$order->id";
@@ -300,6 +275,41 @@ function cc_submit($form, $course)
300 275
         redirect($destination);
301 276
     }
302 277
 
  278
+    /**
  279
+     * validate_enrol_form
  280
+     *
  281
+     * @param object $form Form parameters
  282
+     * @access private
  283
+     */
  284
+    function validate_enrol_form($form)
  285
+    {
  286
+        global $CFG;
  287
+
  288
+        $return = true;
  289
+
  290
+        if (empty($form->ccexpiremm) || empty($form->ccexpireyyyy)) {
  291
+            $this->ccerrors['ccexpire'] = get_string('missingccexpire', 'enrol_authorize');
  292
+        }
  293
+        if (empty($form->ccfirstname) || empty($form->cclastname)) {
  294
+            $this->ccerrors['ccfirstlast'] = get_string('missingfullname');
  295
+        }
  296
+        if (empty($form->cc)) $this->ccerrors['cc'] = get_string('missingcc', 'enrol_authorize');
  297
+        if (empty($form->cvv)) $this->ccerrors['cvv'] = get_string('missingcvv', 'enrol_authorize');
  298
+        if (empty($form->cctype)) $this->ccerrors['cctype'] = get_string('missingcctype', 'enrol_authorize');
  299
+        if (!empty($CFG->an_avs)) {
  300
+            if (empty($form->ccaddress)) $this->ccerrors['ccaddress'] = get_string('missingaddress', 'enrol_authorize');
  301
+            if (empty($form->cccity)) $this->ccerrors['cccity'] = get_string('missingcity');
  302
+            if (empty($form->cccountry)) $this->ccerrors['cccountry'] = get_string('missingcountry');
  303
+            if (empty($form->cczip)) $this->ccerrors['cczip'] = get_string('missingzip', 'enrol_authorize');
  304
+        }
  305
+
  306
+        if (!empty($this->ccerrors)) {
  307
+            $this->ccerrors['header'] = get_string('someerrorswerefound');
  308
+            $return = false;
  309
+        }
  310
+
  311
+        return $return;
  312
+    }
303 313
 
304 314
     /**
305 315
      * zero_cost
@@ -388,37 +398,37 @@ function config_form($frm)
388 398
             notify('PHP must be compiled with SSL support (--with-openssl)');
389 399
         }
390 400
 
391  
-        $ac_enabled = !empty($frm->an_review) && intval($frm->an_capture_day) > 0;
392  
-        if ($ac_enabled) { // Cron must be runnig!!! Check last cron...
393  
-            $mconfig = get_config('enrol/authorize');
394  
-            $lastcron = intval($mconfig->an_lastcron);
395  
-            if (time() - $lastcron > 3600 * 24) {
396  
-                notify(get_string('admincronsetup', 'enrol_authorize'));
397  
-            }
  401
+        if (empty($CFG->loginhttps)) {
  402
+            notify('$CFG->loginhttps must be ON');
398 403
         }
399  
-        else {
400  
-            if ($count = count_records('enrol_authorize', 'status', AN_STATUS_AUTH)) {
401  
-                $a->count = $count;
402  
-                $a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=" . AN_STATUS_AUTH;
403  
-                $message = get_string('adminpendingorders', 'enrol_authorize', $a);
404  
-                notify($message);
  404
+
  405
+        if (!empty($frm->an_review)) {
  406
+            $captureday = intval($frm->an_capture_day);
  407
+            $emailexpired = intval($frm->an_emailexpired);
  408
+            if ($captureday > 0 || $emailexpired > 0) {
  409
+                $mconfig = get_config('enrol/authorize');
  410
+                if ((time() - intval($mconfig->an_lastcron) > 3600 * 24)) {
  411
+                    notify(get_string('admincronsetup', 'enrol_authorize'));
  412
+                }
405 413
             }
406 414
         }
407 415
 
  416
+        if ($count = count_records('enrol_authorize', 'status', AN_STATUS_AUTH)) {
  417
+            $a->count = $count;
  418
+            $a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=".AN_STATUS_AUTH;
  419
+            notify(get_string('adminpendingorders', 'enrol_authorize', $a));
  420
+        }
  421
+
408 422
         if (data_submitted()) {
409  
-            // something POSTed, Some required fields
410 423
             if (empty($frm->an_login)) {
411 424
                 notify("an_login required");
412 425
             }
413 426
             if (empty($frm->an_tran_key) && empty($frm->an_password)) {
414 427
                 notify("an_tran_key or an_password required");
415 428
             }
416  
-            if (empty($CFG->loginhttps)) {
417  
-                notify("\$CFG->loginhttps must be ON");
418  
-            }
419 429
         }
420 430
 
421  
-        include($CFG->dirroot.'/enrol/authorize/config.html');
  431
+        include $CFG->dirroot.'/enrol/authorize/config.html';
422 432
     }
423 433
 
424 434
 
@@ -433,66 +443,47 @@ function process_config($config)
433 443
     {
434 444
         global $CFG;
435 445
 
436  
-        // ENROL config
437  
-        set_config('enrol_cost', optional_param('enrol_cost', 5, PARAM_INT) );
438  
-        set_config('enrol_currency', optional_param('enrol_currency', 'USD', PARAM_ALPHA) );
439  
-        set_config('enrol_mailstudents', optional_param('enrol_mailstudents', '') );
440  
-        set_config('enrol_mailteachers', optional_param('enrol_mailteachers', '') );
441  
-        set_config('enrol_mailadmins', optional_param('enrol_mailadmins', '') );
442  
-
443  
-        // AUTHORIZE.NET config
444  
-
445  
-        // not required!
446  
-        set_config('an_avs', optional_param('an_avs', '') );
447  
-        set_config('an_test', optional_param('an_test', '') );
448  
-        set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL) );
449  
-        set_config('an_cutoff_hour', optional_param('an_cutoff_hour', '0') );
450  
-        set_config('an_cutoff_min', optional_param('an_cutoff_min', '5') );
451  
-
452  
-        // required!
453  
-        // if is it OK, process next config.
454  
-        if (empty($CFG->loginhttps)) return false;
455  
-        if (!$this->check_openssl_loaded()) return false;
456  
-
457  
-        $login_val = optional_param('an_login', '');
458  
-        if (empty($login_val)) return false;
459  
-        set_config('an_login', $login_val);
460  
-
461  
-        $tran_val = optional_param('an_tran_key', '');
462  
-        $password_val = optional_param('an_password', '');
463  
-        if (empty($tran_val) && empty($password_val)) return false;
464  
-        set_config('an_password', $password_val);
465  
-        set_config('an_tran_key', $tran_val);
466  
-
467  
-        // an_review & an_capture_day & cron depencies...
468  
-        $review_val = optional_param('an_review', '');
469  
-        if (empty($review_val)) {
470  
-            // review disabled. cron is not required. AUTH_CAPTURE works.
471  
-            set_config('an_review', $review_val);
472  
-        } else {
473  
-            // REVIEW ENABLED.
474  
-            // an_emailexpired: default=2, min=0, max=5.
475  
-            $an_emailexpired_val = optional_param('an_emailexpired', 2, PARAM_INT);
476  
-            if ($an_emailexpired_val < 0) $an_emailexpired_val = 0;
477  
-            elseif ($an_emailexpired_val > 5) $an_emailexpired_val = 5;
478  
-            set_config('an_emailexpired', $an_emailexpired_val);
479  
-
480  
-            $capture_day_val = optional_param('an_capture_day', 5, PARAM_INT);
481  
-            if ($capture_day_val < 0) $capture_day_val = 0;
482  
-            elseif ($capture_day_val > 29) $capture_day_val = 29;
483  
-            if ($capture_day_val > 0) {
484  
-                // Cron must change an_lastcron. :))
485  
-                $mconfig = get_config('enrol/authorize');
486  
-                $lastcron = intval($mconfig->an_lastcron);
487  
-                if (time() - $lastcron > 3600 * 24) {
488  
-                    // No!!! I am not lucky. No changes please...
489  
-                    return false;
490  
-                }
491  
-            }
492  
-            set_config('an_review', $review_val);
493  
-            set_config('an_capture_day', $capture_day_val);
  446
+        set_config('enrol_cost', optional_param('enrol_cost', 5, PARAM_INT));
  447
+        set_config('enrol_currency', optional_param('enrol_currency', 'USD', PARAM_ALPHA));
  448
+        set_config('enrol_mailstudents', optional_param('enrol_mailstudents', ''));
  449
+        set_config('enrol_mailteachers', optional_param('enrol_mailteachers', ''));
  450
+        set_config('enrol_mailadmins', optional_param('enrol_mailadmins', ''));
  451
+
  452
+        set_config('an_avs', optional_param('an_avs', ''));
  453
+        set_config('an_test', optional_param('an_test', ''));
  454
+        set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL));
  455
+        set_config('an_cutoff_hour', optional_param('an_cutoff_hour', 0, PARAM_INT));
  456
+        set_config('an_cutoff_min', optional_param('an_cutoff_min', 5, PARAM_INT));
  457
+
  458
+        $reviewval = optional_param('an_review', '');
  459
+        $captureday = optional_param('an_capture_day', 5, PARAM_INT);
  460
+        $emailexpired = optional_param('an_emailexpired', 2, PARAM_INT);
  461
+
  462
+        $captureday = ($captureday > 29) ? 29 : (($captureday < 0) ? 0 : $captureday);
  463
+        $emailexpired = ($emailexpired > 5) ? 5 : (($emailexpired < 0) ? 0 : $emailexpired);
  464
+        $mconfig = get_config('enrol/authorize');
  465
+
  466
+        if (time() - intval($mconfig->an_lastcron) > 3600 * 24) {
  467
+            $captureday = $emailexpired = 0;
494 468
         }
495 469
 
  470
+        set_config('an_review', $reviewval);
  471
+        set_config('an_capture_day', $captureday);
  472
+        set_config('an_emailexpired', $emailexpired);
  473
+
  474
+        $loginval = optional_param('an_login', '');
  475
+        $tranval = optional_param('an_tran_key', '');
  476
+        $passwordval = optional_param('an_password', '');
  477
+
  478
+        if (empty($CFG->loginhttps) || (!$this->check_openssl_loaded()) ||
  479
+            empty($loginval) || (empty($tranval) && empty($passwordval))) {
  480
+            return false;
  481
+        }
  482
+
  483
+        set_config('an_login', $loginval);
  484
+        set_config('an_password', $passwordval);
  485
+        set_config('an_tran_key', $tranval);
  486
+
496 487
         return true;
497 488
     }
498 489
 
@@ -559,18 +550,15 @@ function cron()
559 550
     {
560 551
         global $CFG;
561 552
         parent::cron();
562  
-        require_once("$CFG->dirroot/enrol/authorize/action.php");
  553
+        require_once $CFG->dirroot.'/enrol/authorize/action.php';
563 554
 
  555
+        $oneday = 86400;
564 556
         $timenow = time();
565  
-        $timenowsettle = getsettletime($timenow);
566  
-        $timediff30 = $timenowsettle - (30 * 3600 * 24);
567  
-        // These 2 lines must be HERE and must be EXUCUTED. See process_config.
568  
-        // We use an_lastcron when processing AUTOCAPTURE feature.
569  
-        // Order is important. 1. get_config 2. set_config
570  
-        $mconfig = get_config('enrol/authorize'); // MUST be 1st.
571  
-        set_config('an_lastcron', $timenow, 'enrol/authorize'); // MUST be 2nd.
572  
-
573 557
         $random100 = mt_rand(0, 100);
  558
+        $timenowsettle = getsettletime($timenow);
  559
+        $timediff30 = $timenowsettle - (30 * $oneday);
  560
+        $mconfig = get_config('enrol/authorize');
  561
+        set_config('an_lastcron', $timenow, 'enrol/authorize');
574 562
 
575 563
         if ($random100 < 33) {
576 564
             $select = "(status = '" .AN_STATUS_NONE. "') AND (timecreated < '$timediff30')";
@@ -581,30 +569,25 @@ function cron()
581 569
             execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status = '" .AN_STATUS_EXPIRE. "' WHERE $select", false);
582 570
         }
583 571
         else {
584  
-            $timediff60 = $timenowsettle - (60 * 3600 * 24);
  572
+            $timediff60 = $timenowsettle - (60 * $oneday);
585 573
             $select = "(status = '" .AN_STATUS_EXPIRE. "') AND (timecreated < '$timediff60')";
586 574
             delete_records_select('enrol_authorize', $select);
587 575
         }
588 576
 
589 577
         if (empty($CFG->an_review) || !empty($CFG->an_test)) {
590  
-            return; // review disabled, auth_capture works.
  578
+            return;
591 579
         }
592 580
 
593  
-        //CAPTURE-MANUALLY
594 581
         if (intval($CFG->an_capture_day < 1)) {
595  
-            if (empty($CFG->an_emailexpired)) {
596  
-                return; // no information email.
597  
-            }
598  
-            if (intval($mconfig->an_nextmail) > $timenow) {
599  
-                return; // One day must passed.
  582
+            if (empty($CFG->an_emailexpired) || intval($mconfig->an_nextmail) > $timenow) {
  583
+                return;
600 584
             }
601  
-            $timediffem = $timenowsettle - ((30 - intval($CFG->an_emailexpired)) * 3600 * 24);
  585
+            $timediffem = $timenowsettle - ((30 - intval($CFG->an_emailexpired)) * $oneday);
602 586
             $select = "(status = '" . AN_STATUS_AUTH . "') AND " .
603  
-                      "(timecreated < '$timediffem') AND (timecreated > '$timediff30')";
  587
+            "(timecreated < '$timediffem') AND (timecreated > '$timediff30')";
604 588
             if (!$count = count_records_select('enrol_authorize', $select)) {
605 589
                 return;
606 590
             }
607  
-
608 591
             $a->pending = $count;
609 592
             $a->days = $CFG->an_emailexpired;
610 593
             $a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=" . AN_STATUS_AUTH;
@@ -612,12 +595,15 @@ function cron()
612 595
             $message = get_string('pendingordersemail', 'enrol_authorize', $a);
613 596
             $adminuser = get_admin();
614 597
             email_to_user($adminuser, $adminuser, "WARNING: PENDING PAYMENTS", $a);
615  
-            set_config('an_nextmail', $timenow + (3600 * 24), 'enrol/authorize');
  598
+            set_config('an_nextmail', $timenow + $oneday, 'enrol/authorize');
  599
+            return;
  600
+        }
  601
+
  602
+        if (!$this->check_openssl_loaded()) {
616 603
             return;
617 604
         }
618 605
 
619  
-        // CAPTURE-AUTO: Transaction must be captured within 30 days.
620  
-        $timediffcnf = $timenowsettle - (intval($CFG->an_capture_day) * 3600 * 24);
  606
+        $timediffcnf = $timenowsettle - (intval($CFG->an_capture_day) * $oneday);
621 607
         $sql = "SELECT E.*, C.fullname, C.enrolperiod " .
622 608
                "FROM {$CFG->prefix}enrol_authorize E " .
623 609
                "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
@@ -628,12 +614,13 @@ function cron()
628 614
             return;
629 615
         }
630 616
 
631  
-        // Calculate connection speed for each transaction. Default: 3 secs.
632  
-        $everyconnection = empty($mconfig->an_eachconnsecs) ? 3 : intval($mconfig->an_eachconnsecs);
  617
+        $eachconn = intval($mconfig->an_eachconnsecs);
  618
+        if (empty($eachconn)) $eachconn = 3;
  619
+        elseif ($eachconn > 60) $eachconn = 60;
  620
+
633 621
         $ordercount = count((array)$orders);
634  
-        $maxsecs = $everyconnection * $ordercount;
635  
-        if ($maxsecs + intval($mconfig->an_lastcron) > $timenow) {
636  
-            return; // autocapture runs every eachconnsecs*count.
  622
+        if (($ordercount * $eachconn) + intval($mconfig->an_lastcron) > $timenow) {
  623
+            return;
637 624
         }
638 625
 
639 626
         $faults = '';
@@ -658,7 +645,9 @@ function cron()
658 645
                 }
659 646
                 if (enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'authorize')) {
660 647
                     $this->log .= "User($order->userid) has been enrolled to course($order->courseid).\n";
661  
-                    $sendem[] = $order->id;
  648
+                    if (!empty($CFG->enrol_mailstudents)) {
  649
+                        $sendem[] = $order->id;
  650
+                    }
662 651
                 }
663 652
                 else {
664 653
                     $user = get_record('user', 'id', $order->userid);
@@ -668,7 +657,7 @@ function cron()
668 657
                     }
669 658
                 }
670 659
             }
671  
-            else { // not success
  660
+            else {
672 661
                 $this->log .= "Order $order->id: " . $message . "\n";
673 662
                 if ($order->status != $oldstatus) { //expired
674 663
                     update_record("enrol_authorize", $order);
@@ -678,8 +667,8 @@ function cron()
678 667
 
679 668
         $timenow = time();
680 669
         $elapsed = $timenow - $elapsed;
681  
-        $everyconnection = ceil($elapsed / $ordercount);
682  
-        set_config('an_eachconnsecs', $everyconnection, 'enrol/authorize');
  670
+        $eachconn = ceil($elapsed / $ordercount);
  671
+        set_config('an_eachconnsecs', $eachconn, 'enrol/authorize');
683 672
 
684 673
         $this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate($timenow);
685 674
 
@@ -690,20 +679,21 @@ function cron()
690 679
         if (!empty($CFG->enrol_mailadmins)) {
691 680
             email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
692 681
         }
693  
-        // send emails
694  
-        if (empty($sendem) || empty($CFG->enrol_mailstudents)) {
  682
+        if (empty($sendem)) {
695 683
             return;
696 684
         }
  685
+        $lastcourse = 0;
697 686
         $select = "SELECT E.id, E.courseid, E.userid, C.fullname " .
698 687
                   "FROM {$CFG->prefix}enrol_authorize E " .
699 688
                   "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
700 689
                   "WHERE E.id IN(" . implode(',', $sendem) . ") " .
701 690
                   "ORDER BY E.courseid";
702  
-        $lastcourse = 0;
703 691
         $orders = get_records_sql($select);
704  
-        foreach ($orders as $order) {
  692
+        foreach ($orders as $order)
  693
+        {
705 694
             if ($lastcourse != $order->courseid) {
706  
-                $teacher = get_teacher($order->courseid);
  695
+                $lastcourse = $order->courseid;
  696
+                $teacher = get_teacher($lastcourse);
707 697
             }
708 698
             $user = get_record('user', 'id', $order->userid);
709 699
             $a->coursename = $order->fullname;
8  enrol/authorize/index.php
@@ -30,9 +30,9 @@
30 30
                               'transid','settlementdate','notsettled','amount',
31 31
                               'howmuch','captureyes','unenrolstudent'), 'enrol_authorize');
32 32
 
33  
-print_header("$site->shortname: $authstr->paymentmanagement",
  33
+print_header("$site->shortname: $authstrs->paymentmanagement",
34 34
              "$site->fullname",
35  
-             "<a href=\"index.php\">$authstr->paymentmanagement</a>", "");
  35
+             "<a href=\"index.php\">$authstrs->paymentmanagement</a>", "");
36 36
 
37 37
 if (!empty($csv)) {
38 38
     authorize_csv();
@@ -216,7 +216,7 @@ function authorize_order_details($orderno) {
216 216
                "WHERE (orderid = '" . $orderno . "') AND (status = '" . AN_STATUS_CREDIT . "')";
217 217
 
218 218
         if ($refund = get_record_sql($sql)) {
219  
-            $extra->sum = doubleval($refund->refunded);
  219
+            $extra->sum = floatval($refund->refunded);
220 220
         }
221 221
         $upto = format_float($order->amount - $extra->sum, 2);
222 222
         if ($upto <= 0) {
@@ -454,7 +454,7 @@ function get_order_status_desc($order)
454 454
         }
455 455
         return $ret;
456 456
 
457  
-        case AN_STATUS_CREDIT:
  457
+    case AN_STATUS_CREDIT:
458 458
         if (settled($order)) {
459 459
             $ret->actions = array();
460 460
             $ret->status = 'settled';

0 notes on commit c1a91db

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