]> git.mjollnir.org Git - moodle.git/commitdiff
Moved all static functions in authorize class to localfuncs.php as normal function.
authorethem <ethem>
Sat, 2 Sep 2006 11:49:02 +0000 (11:49 +0000)
committerethem <ethem>
Sat, 2 Sep 2006 11:49:02 +0000 (11:49 +0000)
Removed abaval.php and ccval.php, because functions in these files moved to localfuncs.php.

enrol/authorize/abaval.php [deleted file]
enrol/authorize/authorizenetlib.php
enrol/authorize/ccval.php [deleted file]
enrol/authorize/config.html
enrol/authorize/enrol.html
enrol/authorize/enrol.php
enrol/authorize/localfuncs.php [new file with mode: 0644]

diff --git a/enrol/authorize/abaval.php b/enrol/authorize/abaval.php
deleted file mode 100644 (file)
index 41712d9..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php // $Id$
-
-/**
- * Validates the supplied ABA number
- * using a simple mod 10 check digit routine.
- *
- * @param string $aba Bank ID
- * @return bool true ABA is valid, false otherwise
- */
-function ABAVal($aba)
-{
-    if (ereg("^[0-9]{9}$", $aba)) {
-        $n = 0;
-        for($i = 0; $i < 9; $i += 3) {
-            $n += (substr($aba, $i, 1) * 3) +
-                  (substr($aba, $i + 1, 1) * 7) +
-                  (substr($aba, $i + 2, 1));
-        }
-        if ($n != 0 and $n % 10 == 0) {
-            return true;
-        }
-    }
-    return false;
-}
-
-?>
index aeee50a5e75d6235c5bd013f824050b4feec3572..3036b55f08c4026f927402be91e660946ab66522 100644 (file)
@@ -18,6 +18,7 @@ define('AN_REASON_NOACHTYPE',  245);
 define('AN_REASON_NOACHTYPE2', 246);
 
 require_once($CFG->dirroot.'/enrol/authorize/const.php');
+require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
 require_once($CFG->dirroot.'/enrol/authorize/enrol.php');
 
 /**
@@ -330,7 +331,7 @@ function authorize_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE, $
                     $order->settletime = authorize_getsettletime(time());
                 }
                 if (! update_record('enrol_authorize', $order)) {
-                    enrolment_plugin_authorize::email_to_admin("Error while trying to update data " .
+                    email_to_admin("Error while trying to update data " .
                     "in table enrol_authorize. Please edit manually this record: ID=$order->id.", $order);
                 }
                 break;
@@ -344,7 +345,7 @@ function authorize_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE, $
                 $extra->settletime = authorize_getsettletime(time());
                 unset($extra->sum); // this is not used in refunds table.
                 if (! $extra->id = insert_record('enrol_authorize_refunds', $extra)) {
-                    enrolment_plugin_authorize::email_to_admin("Error while trying to insert data " .
+                    email_to_admin("Error while trying to insert data " .
                     "into table enrol_authorize_refunds. Please add manually this record:", $extra);
                 }
                 break;
@@ -359,7 +360,7 @@ function authorize_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE, $
                 $order->status = AN_STATUS_VOID;
                 // don't update order->settletime
                 if (! update_record($tableupdate, $order)) {
-                    enrolment_plugin_authorize::email_to_admin("Error while trying to update data " .
+                    email_to_admin("Error while trying to update data " .
                     "in table $tableupdate. Please edit manually this record: ID=$order->id.", $order);
                 }
                 break;
@@ -388,10 +389,10 @@ function authorize_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE, $
                 case AN_REASON_NOCCTYPE2:
                 {
                     if (!empty($cctype)) {
-                        $ccaccepts = enrolment_plugin_authorize::get_list_of_creditcards();
+                        $ccaccepts = get_list_of_creditcards();
                         unset($ccaccepts[$cctype]);
                         set_config('an_acceptccs', implode(',', array_keys($ccaccepts)));
-                        enrolment_plugin_authorize::email_to_admin("$message ($cctype)" .
+                        email_to_admin("$message ($cctype)" .
                         "This is new config(an_acceptccs):", $ccaccepts);
                     }
                     break;
@@ -400,7 +401,7 @@ function authorize_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE, $
                 case AN_REASON_NOACH:
                 {
                     set_config('an_acceptmethods', AN_METHOD_CC);
-                    enrolment_plugin_authorize::email_to_admin("$message " .
+                    email_to_admin("$message " .
                     "This is new config(an_acceptmethods):", array(AN_METHOD_CC));
                     break;
                 }
diff --git a/enrol/authorize/ccval.php b/enrol/authorize/ccval.php
deleted file mode 100644 (file)
index 26ebda9..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-<?php // $Id$
-/************************************************************************
-*
-* CCVal - Credit Card Validation function.
-*
-* Copyright (c) 1999, 2003 Holotech Enterprises. All rights reserved.
-* You may freely modify and use this function for your own purposes. You
-* may freely distribute it, without modification and with this notice
-* and entire header intact.
-*
-* This function accepts a credit card number and, optionally, a code for
-* a credit card name. If a Name code is specified, the number is checked
-* against card-specific criteria, then validated with the Luhn Mod 10
-* formula. Otherwise it is only checked against the formula. Valid name
-* codes are:
-*
-*    mcd - Master Card
-*    vis - Visa
-*    amx - American Express
-*    dsc - Discover
-*    dnc - Diners Club
-*    jcb - JCB
-*    swi - Switch
-*    dlt - Delta
-*    enr - EnRoute
-*
-* You can also optionally specify an expiration date in the formay mmyy.
-* If the validation fails on the date, the function returns 0. If it
-* fails on the number validation, it returns false.
-*
-* A description of the criteria used in this function can be found at
-* http://www.paylib.net/ccval.html. If you have any questions or
-* comments, please direct them to ccval@holotech.net
-*
-*                                          Alan Little
-*                                          Holotech Enterprises
-*                                          http://www.holotech.net/
-*                                          August 2003
-*
-************************************************************************/
-
-function CCVal($Num, $Name = "n/a", $Exp = "")
-{
-    // Check the expiration date first
-    if (strlen($Exp))
-    {
-        $Month = substr($Exp, 0, 2);
-        $Year  = substr($Exp, -2);
-        $WorkDate = "$Month/01/$Year";
-        $WorkDate = strtotime($WorkDate);
-        $LastDay  = date("t", $WorkDate);
-        $Expires  = strtotime("$Month/$LastDay/$Year 11:59:59");
-        if ($Expires < time()) return 0;
-    }
-
-    //  Innocent until proven guilty
-    $GoodCard = true;
-
-    //  Get rid of any non-digits
-    $Num = ereg_replace("[^0-9]", "", $Num);
-
-    // Perform card-specific checks, if applicable
-    switch ($Name)
-    {
-        case "mcd" :
-        $GoodCard = ereg("^5[1-5].{14}$", $Num);
-        break;
-
-        case "vis" :
-        $GoodCard = ereg("^4.{15}$|^4.{12}$", $Num);
-        break;
-
-        case "amx" :
-        $GoodCard = ereg("^3[47].{13}$", $Num);
-        break;
-
-        case "dsc" :
-        $GoodCard = ereg("^6011.{12}$", $Num);
-        break;
-
-        case "dnc" :
-        $GoodCard = ereg("^30[0-5].{11}$|^3[68].{12}$", $Num);
-        break;
-
-        case "jcb" :
-        $GoodCard = ereg("^3.{15}$|^2131|1800.{11}$", $Num);
-        break;
-
-        case "dlt" :
-        $GoodCard = ereg("^4.{15}$", $Num);
-        break;
-
-        case "swi" :
-        $GoodCard = ereg("^[456].{15}$|^[456].{17,18}$", $Num);
-        break;
-
-        case "enr" :
-        $GoodCard = ereg("^2014.{11}$|^2149.{11}$", $Num);
-        break;
-    }
-
-    // The Luhn formula works right to left, so reverse the number.
-    $Num = strrev($Num);
-    $Total = 0;
-
-    for ($x=0; $x < strlen($Num); $x++)
-    {
-        $digit = substr($Num, $x, 1);
-
-        // If it's an odd digit, double it
-        if ($x/2 != floor($x/2)) {
-            $digit *= 2;
-
-            // If the result is two digits, add them
-            if (strlen($digit) == 2)
-                $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
-        }
-        // Add the current digit, doubled and added if applicable, to the Total
-        $Total += $digit;
-    }
-
-    // If it passed (or bypassed) the card-specific check and the Total is
-    // evenly divisible by 10, it's cool!
-    return ($GoodCard && $Total % 10 == 0);
-}
-?>
index 1020fc1f9a243b577f1c28b7c6277e8f44a0c109..978de40ea141181f02d9ab489066d13dc7f8a3a5 100755 (executable)
@@ -31,12 +31,12 @@ if (!isset($frm->an_cutoff_hour)) {
 }
 
 if (!isset($frm->acceptmethods)) {
-    $frm->acceptmethods = enrolment_plugin_authorize::get_list_of_payment_methods();
+    $frm->acceptmethods = get_list_of_payment_methods();
     $CFG->an_acceptmethods = implode(',', $frm->acceptmethods);
 }
 
 if (!isset($frm->acceptccs)) {
-    $frm->acceptccs = array_keys(enrolment_plugin_authorize::get_list_of_creditcards());
+    $frm->acceptccs = array_keys(get_list_of_creditcards());
     $CFG->an_acceptccs = implode(',', $frm->acceptccs);
 }
 
@@ -133,8 +133,8 @@ if (!isset($frm->acceptccs)) {
 <tr valign="top">
     <td align="right">an_acceptmethods:</td>
     <td><?php print_string("adminacceptmethods", "enrol_authorize") ?><br /><?php
-    $allpaymentmethods = enrolment_plugin_authorize::get_list_of_payment_methods(true);
-    $paymentmethodsenabled = enrolment_plugin_authorize::get_list_of_payment_methods();
+    $allpaymentmethods = get_list_of_payment_methods(true);
+    $paymentmethodsenabled = get_list_of_payment_methods();
     foreach ($allpaymentmethods as $key) {
         print_checkbox('acceptmethods[]', $key, in_array($key, $paymentmethodsenabled), get_string('method'.strtolower($key),'enrol_authorize')); echo "<br />\n";
     }
@@ -144,7 +144,7 @@ if (!isset($frm->acceptccs)) {
 <tr valign="top">
     <td align="right">an_acceptccs:</td>
     <td><?php print_string("adminacceptccs", "enrol_authorize") ?><br /><?php
-    $allccs = enrolment_plugin_authorize::get_list_of_creditcards(true);
+    $allccs = get_list_of_creditcards(true);
     foreach ($allccs as $key => $val) {
         print_checkbox('acceptccs[]', $key, stristr($CFG->an_acceptccs, $key) !== false, $val); echo "<br />\n";
     }
index e9fe10925810f064767ba45d2552d6588021570c..ecc01beef16ae9f1ffe557b0f4ad9bbe616e42ce 100755 (executable)
@@ -1,10 +1,10 @@
 <?php // $Id$
 
 /// Prevent double paid
-    enrolment_plugin_authorize::prevent_double_paid($course);
+    prevent_double_paid($course);
 
 /// Get payment methods enabled and use the first method as default payment method
-    $paymentmethodsenabled = enrolment_plugin_authorize::get_list_of_payment_methods();       // methods enabled
+    $paymentmethodsenabled = get_list_of_payment_methods(); // methods enabled
     $paymentmethod = optional_param('paymentmethod', $paymentmethodsenabled[0], PARAM_ALPHA); // user's payment preference
 
     if (!in_array($paymentmethod, $paymentmethodsenabled)) {
@@ -41,7 +41,7 @@ function print_cc_form($classreference)
         }
     }
 
-    $curcost = enrolment_plugin_authorize::get_course_cost($course);
+    $curcost = get_course_cost($course);
     $userfirstname = empty($form->ccfirstname) ? $USER->firstname : $form->ccfirstname;
     $userlastname = empty($form->cclastname) ? $USER->lastname : $form->cclastname;
     $useraddress = empty($form->ccaddress) ? $USER->address : $form->ccaddress;
@@ -91,7 +91,7 @@ function print_cc_form($classreference)
     <tr>
       <td align="right"><?php print_string("cctype", "enrol_authorize") ?>: </td>
       <td align="left"><?php
-      choose_from_menu(enrolment_plugin_authorize::get_list_of_creditcards(), 'cctype', $form->cctype);
+      choose_from_menu(get_list_of_creditcards(), 'cctype', $form->cctype);
       if (!empty($classreference->authorizeerrors['cctype'])) { formerr($classreference->authorizeerrors['cctype']); }
       ?>
     </td>
@@ -154,7 +154,7 @@ function print_echeck_form($classreference)
             }
         }
 
-        $curcost = enrolment_plugin_authorize::get_course_cost($course);
+        $curcost = get_course_cost($course);
         $userfirstname = empty($form->firstname) ? $USER->firstname : $form->firstname;
         $userlastname = empty($form->lastname) ? $USER->lastname : $form->lastname;
 ?>
@@ -218,12 +218,12 @@ function print_other_method($currentmethod)
     global $course;
 
     if ($currentmethod == AN_METHOD_CC) {
-        $otheravailable = in_array(AN_METHOD_ECHECK, enrolment_plugin_authorize::get_list_of_payment_methods());
+        $otheravailable = in_array(AN_METHOD_ECHECK, get_list_of_payment_methods());
         $url = 'enrol.php?id='.$course->id.'&amp;paymentmethod='.AN_METHOD_ECHECK;
         $stringtofetch = 'usingecheckmethod';
     }
     else {
-        $otheravailable = in_array(AN_METHOD_CC, enrolment_plugin_authorize::get_list_of_payment_methods());
+        $otheravailable = in_array(AN_METHOD_CC, get_list_of_payment_methods());
         $url = 'enrol.php?id='.$course->id.'&amp;paymentmethod='.AN_METHOD_CC;
         $stringtofetch = 'usingccmethod';
     }
index d0243d35089f47ea4d303fba2c13467306597088..1cec1b9fb9498d7f73beb2712c88b3cab60b0ba4 100755 (executable)
@@ -2,10 +2,10 @@
 
 require_once($CFG->dirroot.'/enrol/enrol.class.php');
 require_once($CFG->dirroot.'/enrol/authorize/const.php');
+require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
 
 /**
- * enrolment_plugin_authorize
- *
+ * Authorize.net Payment Gateway plugin
  */
 class enrolment_plugin_authorize
 {
@@ -64,7 +64,7 @@ class enrolment_plugin_authorize
     function print_entry($course) {
         global $CFG, $USER, $form;
 
-        $zerocost = enrolment_plugin_authorize::zero_cost($course);
+        $zerocost = zero_cost($course);
         if ($zerocost) {
             $manual = enrolment_factory::factory('manual');
             if (!empty($this->errormsg)) {
@@ -100,7 +100,7 @@ class enrolment_plugin_authorize
 
         print_simple_box_start('center');
         if (isguest()) {
-            $curcost = enrolment_plugin_authorize::get_course_cost($course);
+            $curcost = get_course_cost($course);
             echo '<div align="center">';
             echo '<p>'.get_string('paymentrequired').'</p>';
             echo '<p><b>'.get_string('cost').": $curcost[currency] $curcost[cost]".'</b></p>';
@@ -132,7 +132,7 @@ class enrolment_plugin_authorize
     {
         global $CFG;
 
-        if (enrolment_plugin_authorize::zero_cost($course) or
+        if (zero_cost($course) or
            (!empty($course->password) and !empty($form->password))) { // MANUAL ENROLMENT
             $manual = enrolment_factory::factory('manual');
             $manual->check_entry($form, $course);
@@ -141,15 +141,15 @@ class enrolment_plugin_authorize
             }
         }
         else { // AUTHORIZE.NET ENROLMENT
-            $paymentmethodsenabled = enrolment_plugin_authorize::get_list_of_payment_methods();
+            $paymentmethodsenabled = get_list_of_payment_methods();
             if (in_array(AN_METHOD_CC, $paymentmethodsenabled) and
                 !empty($form->ccsubmit) and
-                $this->validate_cc_form($form)) {
+                validate_cc_form($form, $this->authorizeerrors)) {
                     $this->cc_submit($form, $course);
             }
             elseif (in_array(AN_METHOD_ECHECK, $paymentmethodsenabled) and
                 !empty($form->echecksubmit) and
-                $this->validate_echeck_form($form)) {
+                validate_echeck_form($form, $this->authorizeerrors)) {
                     $this->echeck_submit($form, $course);
             }
         }
@@ -169,10 +169,10 @@ class enrolment_plugin_authorize
         global $CFG, $USER, $SESSION;
         require_once('authorizenetlib.php');
 
-        enrolment_plugin_authorize::prevent_double_paid($course);
+        prevent_double_paid($course);
 
         $useripno = getremoteaddr();
-        $curcost = enrolment_plugin_authorize::get_course_cost($course);
+        $curcost = get_course_cost($course);
         $exp_date = sprintf("%02d", $form->ccexpiremm) . $form->ccexpireyyyy;
 
         // NEW CC ORDER
@@ -191,7 +191,7 @@ class enrolment_plugin_authorize
         $order->currency = $curcost['currency'];
         $order->id = insert_record("enrol_authorize", $order);
         if (!$order->id) {
-            enrolment_plugin_authorize::email_to_admin("Error while trying to insert new data", $order);
+            email_to_admin("Error while trying to insert new data", $order);
             $this->authorizeerrors['header'] = "Insert record error. Admin has been notified!";
             return;
         }
@@ -225,7 +225,7 @@ class enrolment_plugin_authorize
         $action = $an_review ? AN_ACTION_AUTH_ONLY : AN_ACTION_AUTH_CAPTURE;
         $success = authorize_action($order, $message, $extra, $action, $form->cctype);
         if (!$success) {
-            enrolment_plugin_authorize::email_to_admin($message, $order);
+            email_to_admin($message, $order);
             $this->authorizeerrors['header'] = $message;
             return;
         }
@@ -311,7 +311,7 @@ class enrolment_plugin_authorize
                 }
             }
         } else {
-            enrolment_plugin_authorize::email_to_admin("Error while trying to enrol " .
+            email_to_admin("Error while trying to enrol " .
             fullname($USER) . " in '$course->fullname'", $order);
         }
 
@@ -328,10 +328,10 @@ class enrolment_plugin_authorize
         global $CFG, $USER, $SESSION;
         require_once('authorizenetlib.php');
 
-        enrolment_plugin_authorize::prevent_double_paid($course);
+        prevent_double_paid($course);
 
         $useripno = getremoteaddr();
-        $curcost = enrolment_plugin_authorize::get_course_cost($course);
+        $curcost = get_course_cost($course);
 
         // NEW ECHECK ORDER
         $timenow = time();
@@ -349,7 +349,7 @@ class enrolment_plugin_authorize
         $order->currency = $curcost['currency'];
         $order->id = insert_record("enrol_authorize", $order);
         if (!$order->id) {
-            enrolment_plugin_authorize::email_to_admin("Error while trying to insert new data", $order);
+            email_to_admin("Error while trying to insert new data", $order);
             $this->authorizeerrors['header'] = "Insert record error. Admin has been notified!";
             return;
         }
@@ -382,7 +382,7 @@ class enrolment_plugin_authorize
         $message = ''; // 2 actions only for echecks: auth_capture and credit
         $success = authorize_action($order, $message, $extra, AN_ACTION_AUTH_CAPTURE);
         if (!$success) {
-            enrolment_plugin_authorize::email_to_admin($message, $order);
+            email_to_admin($message, $order);
             $this->authorizeerrors['header'] = $message;
             return;
         }
@@ -436,7 +436,7 @@ class enrolment_plugin_authorize
                 }
             }
         } else {
-            enrolment_plugin_authorize::email_to_admin("Error while trying to enrol " .
+            email_to_admin("Error while trying to enrol " .
             fullname($USER) . " in '$course->fullname'", $order);
         }
 
@@ -448,102 +448,6 @@ class enrolment_plugin_authorize
         redirect($destination);
     }
 
-    function validate_cc_form($form)
-    {
-        global $CFG;
-        require_once('ccval.php');
-
-        if (empty($form->cc)) {
-            $this->authorizeerrors['cc'] = get_string('missingcc', 'enrol_authorize');
-        }
-        if (empty($form->ccexpiremm) || empty($form->ccexpireyyyy)) {
-            $this->authorizeerrors['ccexpire'] = get_string('missingccexpire', 'enrol_authorize');
-        }
-        else {
-            $expdate = sprintf("%02d", intval($form->ccexpiremm)) . $form->ccexpireyyyy;
-            $validcc = CCVal($form->cc, $form->cctype, $expdate);
-            if (!$validcc) {
-                if ($validcc === 0) {
-                    $this->authorizeerrors['ccexpire'] = get_string('ccexpired', 'enrol_authorize');
-                }
-                else {
-                    $this->authorizeerrors['cc'] = get_string('ccinvalid', 'enrol_authorize');
-                }
-            }
-        }
-
-        if (empty($form->ccfirstname) || empty($form->cclastname)) {
-            $this->authorizeerrors['ccfirstlast'] = get_string('missingfullname');
-        }
-
-        if (empty($form->cvv) || !is_numeric($form->cvv)) {
-            $this->authorizeerrors['cvv'] = get_string('missingcvv', 'enrol_authorize');
-        }
-
-        if (empty($form->cctype) or
-            !in_array($form->cctype, array_keys(enrolment_plugin_authorize::get_list_of_creditcards()))) {
-            $this->authorizeerrors['cctype'] = get_string('missingcctype', 'enrol_authorize');
-        }
-
-        if (!empty($CFG->an_avs)) {
-            if (empty($form->ccaddress)) {
-                $this->authorizeerrors['ccaddress'] = get_string('missingaddress', 'enrol_authorize');
-            }
-            if (empty($form->cccity)) {
-                $this->authorizeerrors['cccity'] = get_string('missingcity');
-            }
-            if (empty($form->cccountry)) {
-                $this->authorizeerrors['cccountry'] = get_string('missingcountry');
-            }
-        }
-        if (empty($form->cczip) || !is_numeric($form->cczip)) {
-            $this->authorizeerrors['cczip'] = get_string('missingzip', 'enrol_authorize');
-        }
-
-        if (!empty($this->authorizeerrors)) {
-            $this->authorizeerrors['header'] = get_string('someerrorswerefound');
-            return false;
-        }
-
-        return true;
-    }
-
-    function validate_echeck_form($form)
-    {
-        global $CFG;
-        require_once('abaval.php');
-
-        if (empty($form->abacode) || !is_numeric($form->abacode)) {
-            $this->authorizeerrors['abacode'] = get_string('missingaba', 'enrol_authorize');
-        }
-        elseif (!ABAVal($form->abacode)) {
-            $this->authorizeerrors['abacode'] = get_string('invalidaba', 'enrol_authorize');
-        }
-
-        if (empty($form->accnum) || !is_numeric($form->accnum)) {
-            $this->authorizeerrors['accnum'] = get_string('invalidaccnum', 'enrol_authorize');
-        }
-
-        if (empty($form->acctype) || !in_array($form->acctype, array('CHECKING','BUSINESSCHECKING','SAVINGS'))) {
-            $this->authorizeerrors['acctype'] = get_string('invalidacctype', 'enrol_authorize');
-        }
-
-        if (empty($form->bankname)) {
-            $this->authorizeerrors['bankname'] = get_string('missingbankname', 'enrol_authorize');
-        }
-
-        if (empty($form->firstname) || empty($form->lastname)) {
-            $this->authorizeerrors['firstlast'] = get_string('missingfullname');
-        }
-
-        if (!empty($this->authorizeerrors)) {
-            $this->authorizeerrors['header'] = get_string('someerrorswerefound');
-            return false;
-        }
-
-        return true;
-    }
-
 
     /**
      * Gets access icons.
@@ -556,7 +460,7 @@ class enrolment_plugin_authorize
 
         $manual = enrolment_factory::factory('manual');
         $str = $manual->get_access_icons($course);
-        $curcost = enrolment_plugin_authorize::get_course_cost($course);
+        $curcost = get_course_cost($course);
 
         if (abs($curcost['cost']) > 0.00) {
             $strrequirespayment = get_string("requirespayment");
@@ -589,7 +493,7 @@ class enrolment_plugin_authorize
     {
         global $CFG;
 
-        if (! enrolment_plugin_authorize::check_openssl_loaded()) {
+        if (! check_openssl_loaded()) {
             notify('PHP must be compiled with SSL support (--with-openssl)');
         }
 
@@ -653,14 +557,10 @@ class enrolment_plugin_authorize
         set_config('an_test', optional_param('an_test', 0, PARAM_BOOL));
         set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL));
 
-        $acceptmethods = optional_param('acceptmethods',
-                                        enrolment_plugin_authorize::get_list_of_payment_methods(),
-                                        PARAM_ALPHA);
+        $acceptmethods = optional_param('acceptmethods', get_list_of_payment_methods(), PARAM_ALPHA);
         set_config('an_acceptmethods', implode(',', $acceptmethods));
 
-        $acceptccs = optional_param('acceptccs',
-                                    array_keys(enrolment_plugin_authorize::get_list_of_creditcards()),
-                                    PARAM_ALPHA);
+        $acceptccs = optional_param('acceptccs', array_keys(get_list_of_creditcards()), PARAM_ALPHA);
         set_config('an_acceptccs', implode(',', $acceptccs));
 
         $cutoff_hour = optional_param('an_cutoff_hour', 0, PARAM_INT);
@@ -676,12 +576,12 @@ class enrolment_plugin_authorize
 
         $captureday = ($captureday > 29) ? 29 : (($captureday < 0) ? 0 : $captureday);
         $emailexpired = ($emailexpired > 5) ? 5 : (($emailexpired < 0) ? 0 : $emailexpired);
-        $mconfig = get_config('enrol/authorize');
 
-        if ((!empty($reviewval)) &&
-            ($captureday > 0 || $emailexpired > 0) &&
-            (time() - intval($mconfig->an_lastcron) > 3600 * 24)) {
-            return false;
+        if (!empty($reviewval) && ($captureday > 0 || $emailexpired > 0)) {
+            $mconfig = get_config('enrol/authorize');
+            if (time() - intval($mconfig->an_lastcron) > 3600 * 24) {
+                return false;
+            }
         }
 
         set_config('an_review', $reviewval);
@@ -692,7 +592,7 @@ class enrolment_plugin_authorize
 
         // https and openssl library is required
         if ((substr($CFG->wwwroot, 0, 5) !== 'https' and empty($CFG->loginhttps)) or
-            !enrolment_plugin_authorize::check_openssl_loaded()) {
+            !check_openssl_loaded()) {
             return false;
         }
 
@@ -723,159 +623,6 @@ class enrolment_plugin_authorize
         return true;
     }
 
-    /**
-     * Whether a course cost is smaller than 0.01
-     *
-     * @param object $course Course information
-     * @return bool true if the course is free cost
-     * @static
-     */
-    function zero_cost($course) {
-        $curcost = enrolment_plugin_authorize::get_course_cost($course);
-        return (abs($curcost['cost']) < 0.01);
-    }
-
-
-    /**
-     * Gets course cost
-     *
-     * @param object $course
-     * @return array cost=>'cost', currency=>'currency'
-     * @static
-     */
-    function get_course_cost($course)
-    {
-        global $CFG;
-
-        $cost = (float)0;
-        $currency = (!empty($course->currency))
-                     ? $course->currency :( empty($CFG->enrol_currency)
-                                            ? 'USD' : $CFG->enrol_currency );
-
-        if (!empty($course->cost)) {
-            $cost = (float)(((float)$course->cost) < 0) ? $CFG->enrol_cost : $course->cost;
-        }
-
-        $cost = format_float($cost, 2);
-        $ret = array('cost' => $cost, 'currency' => $currency);
-
-        return $ret;
-    }
-
-    /**
-     * Sends email to main admin.
-     * FIXME: Admin ROLES
-     *
-     * @param string $subject
-     * @param mixed $data
-     * @static
-     */
-    function email_to_admin($subject, $data)
-    {
-        global $SITE;
-
-        $admin = get_admin();
-        $data = (array)$data;
-
-        $message = "$SITE->fullname: Transaction failed.\n\n$subject\n\n";
-        $message .= print_r($data, true);
-        email_to_user($admin, $admin, "$SITE->fullname: Authorize.net ERROR", $message);
-    }
-
-    /**
-     * prevent_double_paid (static method)
-     *
-     * @param object $course
-     * @static
-     */
-    function prevent_double_paid($course)
-    {
-        global $CFG, $SESSION, $USER;
-
-        $status = empty($CFG->an_test) ? AN_STATUS_AUTH : AN_STATUS_NONE;
-
-        if ($rec=get_record('enrol_authorize','userid',$USER->id,'courseid',$course->id,'status',$status,'id')) {
-            $a = new stdClass;
-            $a->orderid = $rec->id;
-            $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid";
-            redirect($a->url, get_string("paymentpending", "enrol_authorize", $a), '10');
-            return;
-        }
-        if (isset($SESSION->ccpaid)) {
-            unset($SESSION->ccpaid);
-            redirect($CFG->wwwroot . '/login/logout.php');
-            return;
-        }
-    }
-
-    /**
-     * check_openssl_loaded (static method)
-     *
-     * @return bool
-     * @static
-     */
-    function check_openssl_loaded() {
-        return extension_loaded('openssl');
-    }
-
-    /**
-     * Gets list of credits cards
-     *
-     * @param bool $getall, true get all of types, false config values
-     * @return array, Key => Value
-     * @static
-     */
-    function get_list_of_creditcards($getall = false)
-    {
-        global $CFG;
-
-        $alltypes = array(
-            'mcd' => 'Master Card',
-            'vis' => 'Visa',
-            'amx' => 'American Express',
-            'dsc' => 'Discover',
-            'dnc' => 'Diners Club',
-            'jcb' => 'JCB',
-            'swi' => 'Switch',
-            'dlt' => 'Delta',
-            'enr' => 'EnRoute'
-        );
-
-        if ($getall or empty($CFG->an_acceptccs)) {
-            return $alltypes;
-        }
-
-        $ret = array();
-        $ccs = explode(',', $CFG->an_acceptccs);
-        foreach ($ccs as $key) {
-            $ret[$key] = $alltypes[$key];
-        }
-        return $ret;
-    }
-
-    /**
-     * Gets lists of payment methods (CC,ECHECK)
-     *
-     * @param bool $getall, get all of types, false config values
-     * @return array, Key only
-     * @static
-     */
-    function get_list_of_payment_methods($getall = false)
-    {
-        global $CFG;
-
-        if ($getall) {
-            return array(AN_METHOD_CC, AN_METHOD_ECHECK);
-        }
-        elseif (empty($CFG->an_acceptmethods)) {
-            return array(AN_METHOD_CC); // default
-        }
-        else {
-            return explode(',', $CFG->an_acceptmethods);
-        }
-    }
-
-
     /**
      * This function is run by admin/cron.php every time if admin has enabled this plugin.
      *
@@ -911,7 +658,7 @@ class enrolment_plugin_authorize
         if (empty($CFG->an_review) or
            (!empty($CFG->an_test)) or
            (intval($CFG->an_capture_day) < 1) or
-           (!enrolment_plugin_authorize::check_openssl_loaded())) {
+           (!check_openssl_loaded())) {
             mtrace("disabled");
             return; // order review disabled or test mode or manual capture or openssl wasn't loaded.
         }
diff --git a/enrol/authorize/localfuncs.php b/enrol/authorize/localfuncs.php
new file mode 100644 (file)
index 0000000..4380a2f
--- /dev/null
@@ -0,0 +1,306 @@
+<?php // $Id$
+
+function get_course_cost($course)
+{
+    global $CFG;
+
+    $cost = (float)0;
+    $currency = (!empty($course->currency))
+                 ? $course->currency :( empty($CFG->enrol_currency)
+                                        ? 'USD' : $CFG->enrol_currency );
+
+    if (!empty($course->cost)) {
+        $cost = (float)(((float)$course->cost) < 0) ? $CFG->enrol_cost : $course->cost;
+    }
+
+    $cost = format_float($cost, 2);
+    $ret = array(
+        'cost' => $cost,
+        'currency' => $currency
+    );
+
+    return $ret;
+}
+
+function zero_cost($course) {
+    $curcost = get_course_cost($course);
+    return (abs($curcost['cost']) < 0.01);
+}
+
+function prevent_double_paid($course)
+{
+    global $CFG, $SESSION, $USER;
+
+    $status = empty($CFG->an_test) ? AN_STATUS_AUTH : AN_STATUS_NONE;
+
+    if ($rec=get_record('enrol_authorize','userid',$USER->id,'courseid',$course->id,'status',$status,'id')) {
+        $a = new stdClass;
+        $a->orderid = $rec->id;
+        $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid";
+        redirect($a->url, get_string("paymentpending", "enrol_authorize", $a), '10');
+        return;
+    }
+    if (isset($SESSION->ccpaid)) {
+        unset($SESSION->ccpaid);
+        redirect($CFG->wwwroot . '/login/logout.php');
+        return;
+    }
+}
+
+function get_list_of_creditcards($getall = false)
+{
+    global $CFG;
+
+    $alltypes = array(
+        'mcd' => 'Master Card',
+        'vis' => 'Visa',
+        'amx' => 'American Express',
+        'dsc' => 'Discover',
+        'dnc' => 'Diners Club',
+        'jcb' => 'JCB',
+        'swi' => 'Switch',
+        'dlt' => 'Delta',
+        'enr' => 'EnRoute'
+    );
+
+    if ($getall or empty($CFG->an_acceptccs)) {
+        return $alltypes;
+    }
+
+    $ret = array();
+    $ccs = explode(',', $CFG->an_acceptccs);
+    foreach ($ccs as $key) {
+        $ret[$key] = $alltypes[$key];
+    }
+    return $ret;
+}
+
+function get_list_of_payment_methods($getall = false)
+{
+    global $CFG;
+
+    if ($getall) {
+        return array(AN_METHOD_CC, AN_METHOD_ECHECK);
+    }
+    elseif (empty($CFG->an_acceptmethods)) {
+        return array(AN_METHOD_CC); // default
+    }
+    else {
+        return explode(',', $CFG->an_acceptmethods);
+    }
+}
+
+function ABAVal($aba)
+{
+    if (ereg("^[0-9]{9}$", $aba)) {
+        $n = 0;
+        for($i = 0; $i < 9; $i += 3) {
+            $n += (substr($aba, $i, 1) * 3) +
+                  (substr($aba, $i + 1, 1) * 7) +
+                  (substr($aba, $i + 2, 1));
+        }
+        if ($n != 0 and $n % 10 == 0) {
+            return true;
+        }
+    }
+    return false;
+}
+
+function CCVal($Num, $Name = "n/a", $Exp = "")
+{
+    // Check the expiration date first
+    if (strlen($Exp))
+    {
+        $Month = substr($Exp, 0, 2);
+        $Year  = substr($Exp, -2);
+        $WorkDate = "$Month/01/$Year";
+        $WorkDate = strtotime($WorkDate);
+        $LastDay  = date("t", $WorkDate);
+        $Expires  = strtotime("$Month/$LastDay/$Year 11:59:59");
+        if ($Expires < time()) return 0;
+    }
+
+    //  Innocent until proven guilty
+    $GoodCard = true;
+
+    //  Get rid of any non-digits
+    $Num = ereg_replace("[^0-9]", "", $Num);
+
+    // Perform card-specific checks, if applicable
+    switch ($Name)
+    {
+        case "mcd" :
+        $GoodCard = ereg("^5[1-5].{14}$", $Num);
+        break;
+
+        case "vis" :
+        $GoodCard = ereg("^4.{15}$|^4.{12}$", $Num);
+        break;
+
+        case "amx" :
+        $GoodCard = ereg("^3[47].{13}$", $Num);
+        break;
+
+        case "dsc" :
+        $GoodCard = ereg("^6011.{12}$", $Num);
+        break;
+
+        case "dnc" :
+        $GoodCard = ereg("^30[0-5].{11}$|^3[68].{12}$", $Num);
+        break;
+
+        case "jcb" :
+        $GoodCard = ereg("^3.{15}$|^2131|1800.{11}$", $Num);
+        break;
+
+        case "dlt" :
+        $GoodCard = ereg("^4.{15}$", $Num);
+        break;
+
+        case "swi" :
+        $GoodCard = ereg("^[456].{15}$|^[456].{17,18}$", $Num);
+        break;
+
+        case "enr" :
+        $GoodCard = ereg("^2014.{11}$|^2149.{11}$", $Num);
+        break;
+    }
+
+    // The Luhn formula works right to left, so reverse the number.
+    $Num = strrev($Num);
+    $Total = 0;
+
+    for ($x=0; $x < strlen($Num); $x++)
+    {
+        $digit = substr($Num, $x, 1);
+
+        // If it's an odd digit, double it
+        if ($x/2 != floor($x/2)) {
+            $digit *= 2;
+
+            // If the result is two digits, add them
+            if (strlen($digit) == 2)
+                $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
+        }
+        // Add the current digit, doubled and added if applicable, to the Total
+        $Total += $digit;
+    }
+
+    // If it passed (or bypassed) the card-specific check and the Total is
+    // evenly divisible by 10, it's cool!
+    return ($GoodCard && $Total % 10 == 0);
+}
+
+function validate_cc_form($form, &$err)
+{
+    global $CFG;
+
+    if (empty($form->cc)) {
+        $err['cc'] = get_string('missingcc', 'enrol_authorize');
+    }
+    if (empty($form->ccexpiremm) || empty($form->ccexpireyyyy)) {
+        $err['ccexpire'] = get_string('missingccexpire', 'enrol_authorize');
+    }
+    else {
+        $expdate = sprintf("%02d", intval($form->ccexpiremm)) . $form->ccexpireyyyy;
+        $validcc = CCVal($form->cc, $form->cctype, $expdate);
+        if (!$validcc) {
+            if ($validcc === 0) {
+                $err['ccexpire'] = get_string('ccexpired', 'enrol_authorize');
+            }
+            else {
+                $err['cc'] = get_string('ccinvalid', 'enrol_authorize');
+            }
+        }
+    }
+
+    if (empty($form->ccfirstname) || empty($form->cclastname)) {
+        $err['ccfirstlast'] = get_string('missingfullname');
+    }
+
+    if (empty($form->cvv) || !is_numeric($form->cvv)) {
+        $err['cvv'] = get_string('missingcvv', 'enrol_authorize');
+    }
+
+    if (empty($form->cctype) or !in_array($form->cctype, array_keys(get_list_of_creditcards()))) {
+        $err['cctype'] = get_string('missingcctype', 'enrol_authorize');
+    }
+
+    if (!empty($CFG->an_avs))
+    {
+        if (empty($form->ccaddress)) {
+            $err['ccaddress'] = get_string('missingaddress', 'enrol_authorize');
+        }
+        if (empty($form->cccity)) {
+            $err['cccity'] = get_string('missingcity');
+        }
+        if (empty($form->cccountry)) {
+            $err['cccountry'] = get_string('missingcountry');
+        }
+    }
+
+    if (empty($form->cczip) || !is_numeric($form->cczip)) {
+        $err['cczip'] = get_string('missingzip', 'enrol_authorize');
+    }
+
+    if (!empty($err)) {
+        $err['header'] = get_string('someerrorswerefound');
+        return false;
+    }
+
+    return true;
+}
+
+function validate_echeck_form($form, &$err)
+{
+    global $CFG;
+
+    if (empty($form->abacode) || !is_numeric($form->abacode)) {
+        $err['abacode'] = get_string('missingaba', 'enrol_authorize');
+    }
+    elseif (!ABAVal($form->abacode)) {
+        $err['abacode'] = get_string('invalidaba', 'enrol_authorize');
+    }
+
+    if (empty($form->accnum) || !is_numeric($form->accnum)) {
+        $err['accnum'] = get_string('invalidaccnum', 'enrol_authorize');
+    }
+
+    if (empty($form->acctype) || !in_array($form->acctype, array('CHECKING', 'BUSINESSCHECKING', 'SAVINGS'))) {
+        $err['acctype'] = get_string('invalidacctype', 'enrol_authorize');
+    }
+
+    if (empty($form->bankname)) {
+        $err['bankname'] = get_string('missingbankname', 'enrol_authorize');
+    }
+
+    if (empty($form->firstname) || empty($form->lastname)) {
+        $err['firstlast'] = get_string('missingfullname');
+    }
+
+    if (!empty($err)) {
+        $err['header'] = get_string('someerrorswerefound');
+        return false;
+    }
+
+    return true;
+}
+
+function email_to_admin($subject, $data)
+{
+    global $SITE;
+
+    $admin = get_admin();
+    $data = (array)$data;
+
+    $message = "$SITE->fullname: Transaction failed.\n\n$subject\n\n";
+    $message .= print_r($data, true);
+    email_to_user($admin, $admin, "$SITE->fullname: Authorize.net ERROR", $message);
+}
+
+function check_openssl_loaded()
+{
+    return extension_loaded('openssl');
+}
+
+?>