From e3949d06af91f849ce2076af56afdfcac6c9aaf9 Mon Sep 17 00:00:00 2001 From: ethem Date: Sat, 2 Sep 2006 11:49:02 +0000 Subject: [PATCH] Moved all static functions in authorize class to localfuncs.php as normal function. Removed abaval.php and ccval.php, because functions in these files moved to localfuncs.php. --- enrol/authorize/abaval.php | 26 --- enrol/authorize/authorizenetlib.php | 13 +- enrol/authorize/ccval.php | 126 ----------- enrol/authorize/config.html | 10 +- enrol/authorize/enrol.html | 14 +- enrol/authorize/enrol.php | 311 +++------------------------- enrol/authorize/localfuncs.php | 306 +++++++++++++++++++++++++++ 7 files changed, 354 insertions(+), 452 deletions(-) delete mode 100644 enrol/authorize/abaval.php delete mode 100644 enrol/authorize/ccval.php create mode 100644 enrol/authorize/localfuncs.php diff --git a/enrol/authorize/abaval.php b/enrol/authorize/abaval.php deleted file mode 100644 index 41712d90333..00000000000 --- a/enrol/authorize/abaval.php +++ /dev/null @@ -1,26 +0,0 @@ - diff --git a/enrol/authorize/authorizenetlib.php b/enrol/authorize/authorizenetlib.php index aeee50a5e75..3036b55f08c 100644 --- a/enrol/authorize/authorizenetlib.php +++ b/enrol/authorize/authorizenetlib.php @@ -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 index 26ebda999fc..00000000000 --- a/enrol/authorize/ccval.php +++ /dev/null @@ -1,126 +0,0 @@ - diff --git a/enrol/authorize/config.html b/enrol/authorize/config.html index 1020fc1f9a2..978de40ea14 100755 --- a/enrol/authorize/config.html +++ b/enrol/authorize/config.html @@ -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)) { an_acceptmethods:
\n"; } @@ -144,7 +144,7 @@ if (!isset($frm->acceptccs)) { an_acceptccs:
$val) { print_checkbox('acceptccs[]', $key, stristr($CFG->an_acceptccs, $key) !== false, $val); echo "
\n"; } diff --git a/enrol/authorize/enrol.html b/enrol/authorize/enrol.html index e9fe1092581..ecc01beef16 100755 --- a/enrol/authorize/enrol.html +++ b/enrol/authorize/enrol.html @@ -1,10 +1,10 @@ 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) : cctype); + choose_from_menu(get_list_of_creditcards(), 'cctype', $form->cctype); if (!empty($classreference->authorizeerrors['cctype'])) { formerr($classreference->authorizeerrors['cctype']); } ?> @@ -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.'&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.'&paymentmethod='.AN_METHOD_CC; $stringtofetch = 'usingccmethod'; } diff --git a/enrol/authorize/enrol.php b/enrol/authorize/enrol.php index d0243d35089..1cec1b9fb94 100755 --- a/enrol/authorize/enrol.php +++ b/enrol/authorize/enrol.php @@ -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 '
'; echo '

'.get_string('paymentrequired').'

'; echo '

'.get_string('cost').": $curcost[currency] $curcost[cost]".'

'; @@ -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 index 00000000000..4380a2fc65b --- /dev/null +++ b/enrol/authorize/localfuncs.php @@ -0,0 +1,306 @@ +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'); +} + +?>