|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00028 defined('MOODLE_INTERNAL') || die(); 00029 00030 require_once($CFG->libdir.'/formslib.php'); 00031 00032 class enrol_authorize_form extends moodleform 00033 { 00034 protected $instance; 00035 00036 function definition() { 00037 global $USER; 00038 00039 $mform = $this->_form; 00040 $this->instance = $this->_customdata; 00041 $plugin = enrol_get_plugin('authorize'); 00042 00043 $paymentmethodsenabled = get_list_of_payment_methods(); 00044 $paymentmethod = optional_param('paymentmethod', $paymentmethodsenabled[0], PARAM_ALPHA); 00045 if (!in_array($paymentmethod, $paymentmethodsenabled)) { 00046 print_error('invalidpaymentmethod', '', '', $paymentmethod); 00047 } 00048 00049 $othermethodstr = $this->other_method_available($paymentmethod); 00050 if ($othermethodstr) { 00051 $mform->addElement('static', '', '<div class="mdl-right">' . $othermethodstr . '</div>', ''); 00052 } 00053 00054 $mform->addElement('hidden', 'id'); 00055 $mform->setType('id', PARAM_INT); 00056 $mform->setDefault('id', $this->instance->courseid); 00057 00058 $mform->addElement('hidden', 'instanceid'); 00059 $mform->setType('instanceid', PARAM_INT); 00060 $mform->setDefault('instanceid', $this->instance->id); 00061 00062 $mform->addElement('hidden', 'paymentmethod', $paymentmethod); 00063 $mform->setType('paymentmethod', PARAM_ALPHA); 00064 00065 $firstlastnamestr = (AN_METHOD_CC == $paymentmethod) ? get_string('nameoncard', 'enrol_authorize') : get_string('echeckfirslasttname', 'enrol_authorize'); 00066 $mform->addElement('text', 'firstname', get_string('firstnameoncard', 'enrol_authorize'), 'size="16"'); 00067 $mform->addElement('text', 'lastname', get_string('lastnameoncard', 'enrol_authorize'), 'size="16"'); 00068 $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'client'); 00069 $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'client'); 00070 $mform->setType('firstname', PARAM_ALPHANUM); 00071 $mform->setType('lastname', PARAM_ALPHANUM); 00072 $mform->setDefault('firstname', $USER->firstname); 00073 $mform->setDefault('lastname', $USER->lastname); 00074 00075 if (AN_METHOD_CC == $paymentmethod) 00076 { 00077 $mform->addElement('passwordunmask', 'cc', get_string('ccno', 'enrol_authorize'), 'size="20"'); 00078 $mform->setType('cc', PARAM_ALPHANUM); 00079 $mform->setDefault('cc', ''); 00080 $mform->addRule('cc', get_string('missingcc', 'enrol_authorize'), 'required', null, 'client'); 00081 $mform->addRule('cc', get_string('ccinvalid', 'enrol_authorize'), 'numeric', null, 'client'); 00082 00083 $monthsmenu = array('' => get_string('choose')); 00084 for ($i = 1; $i <= 12; $i++) { 00085 $monthsmenu[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), "%B"); 00086 } 00087 $nowdate = getdate(); 00088 $startyear = $nowdate["year"] - 1; 00089 $endyear = $startyear + 20; 00090 $yearsmenu = array('' => get_string('choose')); 00091 for ($i = $startyear; $i < $endyear; $i++) { 00092 $yearsmenu[$i] = $i; 00093 } 00094 $mform->addElement('select', 'ccexpiremm', get_string('expiremonth', 'enrol_authorize'), $monthsmenu); 00095 $mform->addElement('select', 'ccexpireyyyy', get_string('expireyear', 'enrol_authorize'), $yearsmenu); 00096 $mform->addRule('ccexpiremm', get_string('missingccexpiremonth', 'enrol_authorize'), 'required', null, 'client'); 00097 $mform->addRule('ccexpireyyyy', get_string('missingccexpireyear', 'enrol_authorize'), 'required', null, 'client'); 00098 $mform->setType('ccexpiremm', PARAM_INT); 00099 $mform->setType('ccexpireyyyy', PARAM_INT); 00100 $mform->setDefault('ccexpiremm', ''); 00101 $mform->setDefault('ccexpireyyyy', ''); 00102 00103 $creditcardsmenu = array('' => get_string('choose')) + get_list_of_creditcards(); 00104 $mform->addElement('select', 'cctype', get_string('cctype', 'enrol_authorize'), $creditcardsmenu); 00105 $mform->setType('cctype', PARAM_ALPHA); 00106 $mform->addRule('cctype', get_string('missingcctype', 'enrol_authorize'), 'required', null, 'client'); 00107 $mform->setDefault('cctype', ''); 00108 00109 $mform->addElement('text', 'cvv', get_string('ccvv', 'enrol_authorize'), 'size="4"'); 00110 $mform->setType('cvv', PARAM_ALPHANUM); 00111 $mform->setDefault('cvv', ''); 00112 $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'required', null, 'client'); 00113 $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'numeric', null, 'client'); 00114 00115 if ($plugin->get_config('an_authcode')) { 00116 $ccauthgrp = array(); 00117 $ccauthgrp[] = &$mform->createElement('checkbox', 'haveauth', null, get_string('haveauthcode', 'enrol_authorize')); 00118 $ccauthgrp[] = &$mform->createElement('static', 'nextline', null, '<br />'); 00119 $ccauthgrp[] = &$mform->createElement('text', 'ccauthcode', '', 'size="8"'); 00120 $mform->addGroup($ccauthgrp, 'ccauthgrp', get_string('authcode', 'enrol_authorize'), ' ', false); 00121 00122 $ccauthgrprules = array(); 00123 $ccauthgrprules['ccauthcode'][] = array(get_string('missingccauthcode', 'enrol_authorize'), 'numeric', null, 'client'); 00124 $mform->addGroupRule('ccauthgrp', $ccauthgrprules); 00125 $mform->setDefault('haveauth', ''); 00126 $mform->setDefault('ccauthcode', ''); 00127 } 00128 00129 if ($plugin->get_config('an_avs')) { 00130 $mform->addElement('header', '', ' ' . get_string('address'), ''); 00131 00132 $mform->addElement('text', 'ccaddress', get_string('address'), 'size="30"'); 00133 $mform->setType('ccaddress', PARAM_ALPHANUM); 00134 $mform->setDefault('ccaddress', $USER->address); 00135 $mform->addRule('ccaddress', get_string('missingaddress', 'enrol_authorize'), 'required', null, 'client'); 00136 00137 $mform->addElement('text', 'cccity', get_string('cccity', 'enrol_authorize'), 'size="14"'); 00138 $mform->addElement('text', 'ccstate', get_string('ccstate', 'enrol_authorize'), 'size="8"'); 00139 $mform->addRule('cccity', get_string('missingcity'), 'required', null, 'client'); 00140 $mform->setType('cccity', PARAM_ALPHANUM); 00141 $mform->setType('ccstate', PARAM_ALPHANUM); 00142 $mform->setDefault('cccity', $USER->city); 00143 $mform->setDefault('ccstate', ''); 00144 00145 $mform->addElement('select', 'cccountry', get_string('country'), get_string_manager()->get_list_of_countries()); 00146 $mform->addRule('cccountry', get_string('missingcountry'), 'required', null, 'client'); 00147 $mform->setType('cccountry', PARAM_ALPHA); 00148 $mform->setDefault('cccountry', $USER->country); 00149 } 00150 else { 00151 $mform->addElement('hidden', 'ccstate', ''); 00152 $mform->setType('ccstate', PARAM_ALPHANUM); 00153 $mform->addElement('hidden', 'ccaddress', $USER->address); 00154 $mform->setType('ccaddress', PARAM_ALPHANUM); 00155 $mform->addElement('hidden', 'cccity', $USER->city); 00156 $mform->setType('cccity', PARAM_ALPHANUM); 00157 $mform->addElement('hidden', 'cccountry', $USER->country); 00158 $mform->setType('ccountry', PARAM_ALPHA); 00159 $mform->setDefault('cccountry', $USER->country); 00160 } 00161 } elseif (AN_METHOD_ECHECK == $paymentmethod) { 00162 $mform->addElement('text', 'abacode', get_string('echeckabacode', 'enrol_authorize'), 'size="9" maxlength="9"'); 00163 $mform->setType('abacode', PARAM_ALPHANUM); 00164 $mform->setDefault('abacode', ''); 00165 $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'required', null, 'client'); 00166 $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'numeric', null, 'client'); 00167 00168 $mform->addElement('text', 'accnum', get_string('echeckaccnum', 'enrol_authorize'), 'size="20" maxlength="20"'); 00169 $mform->setType('accnum', PARAM_ALPHANUM); 00170 $mform->setDefault('accnum', ''); 00171 $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'required', null, 'client'); 00172 $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'numeric', null, 'client'); 00173 00174 $acctypes = array(); 00175 $acctypesenabled = get_list_of_bank_account_types(); 00176 foreach ($acctypesenabled as $key) { 00177 $acctypes[$key] = get_string("echeck".strtolower($key), "enrol_authorize"); 00178 } 00179 $acctypes = array('' => get_string('choose')) + $acctypes; 00180 $mform->addElement('select', 'acctype', get_string('echeckacctype', 'enrol_authorize'), $acctypes); 00181 $mform->setType('acctype', PARAM_ALPHA); 00182 $mform->addRule('acctype', get_string('invalidacctype', 'enrol_authorize'), 'required', null, 'client'); 00183 $mform->setDefault('acctype', ''); 00184 00185 $mform->addElement('text', 'bankname', get_string('echeckbankname', 'enrol_authorize'), 'size="20" maxlength="50"'); 00186 $mform->setType('bankname', PARAM_ALPHANUM); 00187 $mform->setDefault('bankname', ''); 00188 $mform->addRule('bankname', get_string('missingbankname', 'enrol_authorize'), 'required', null, 'client'); 00189 } 00190 00191 $mform->addElement('text', 'cczip', get_string('zipcode', 'enrol_authorize'), 'size="5"'); 00192 $mform->setType('cczip', PARAM_ALPHANUM); 00193 $mform->setDefault('cczip', ''); 00194 $mform->addRule('cczip', get_string('missingzip', 'enrol_authorize'), 'required', null, 'client'); 00195 00196 $this->add_action_buttons(false, get_string('sendpaymentbutton', 'enrol_authorize')); 00197 } 00198 00199 function validation($data, $files) { 00200 $errors = parent::validation($data, $files); 00201 $plugin = enrol_get_plugin('authorize'); 00202 00203 if (AN_METHOD_CC == $data['paymentmethod']) 00204 { 00205 if (!in_array($data['cctype'], array_keys(get_list_of_creditcards()))) { 00206 $errors['cctype'] = get_string('missingcctype', 'enrol_authorize'); 00207 } 00208 00209 $expdate = sprintf("%02d", intval($data['ccexpiremm'])) . $data['ccexpireyyyy']; 00210 $validcc = $this->validate_cc($data['cc'], $data['cctype'], $expdate); 00211 if (!$validcc) { 00212 if ($validcc === 0) { 00213 $errors['ccexpiremm'] = get_string('ccexpired', 'enrol_authorize'); 00214 } 00215 else { 00216 $errors['cc'] = get_string('ccinvalid', 'enrol_authorize'); 00217 } 00218 } 00219 00220 if ($plugin->get_config('an_authcode') && !empty($data['haveauth']) && empty($data['ccauthcode'])) { 00221 $errors['ccauthgrp'] = get_string('missingccauthcode', 'enrol_authorize'); 00222 } 00223 } 00224 elseif (AN_METHOD_ECHECK == $data['paymentmethod']) 00225 { 00226 if (!$this->validate_aba($data['abacode'])) { 00227 $errors['abacode'] = get_string('invalidaba', 'enrol_authorize'); 00228 } 00229 00230 if (!in_array($data['acctype'], get_list_of_bank_account_types())) { 00231 $errors['acctype'] = get_string('invalidacctype', 'enrol_authorize'); 00232 } 00233 } 00234 00235 return $errors; 00236 } 00237 00238 private function other_method_available($currentmethod) 00239 { 00240 00241 if ($currentmethod == AN_METHOD_CC) { 00242 $otheravailable = in_array(AN_METHOD_ECHECK, get_list_of_payment_methods()); 00243 $url = 'index.php?id='.$this->instance->courseid.'&paymentmethod='.AN_METHOD_ECHECK; 00244 $stringtofetch = 'usingecheckmethod'; 00245 } else { 00246 $otheravailable = in_array(AN_METHOD_CC, get_list_of_payment_methods()); 00247 $url = 'index.php?id='.$this->instance->courseid.'&paymentmethod='.AN_METHOD_CC; 00248 $stringtofetch = 'usingccmethod'; 00249 } 00250 if ($otheravailable) { 00251 $a = new stdClass; 00252 $a->url = $url; 00253 return get_string($stringtofetch, "enrol_authorize", $a); 00254 } 00255 else { 00256 return ''; 00257 } 00258 } 00259 00260 private function validate_aba($aba) 00261 { 00262 if (preg_match("/^[0-9]{9}$/", $aba)) { 00263 $n = 0; 00264 for($i = 0; $i < 9; $i += 3) { 00265 $n += (substr($aba, $i, 1) * 3) + (substr($aba, $i + 1, 1) * 7) + (substr($aba, $i + 2, 1)); 00266 } 00267 if ($n != 0 and $n % 10 == 0) { 00268 return true; 00269 } 00270 } 00271 return false; 00272 } 00273 00274 private function validate_cc($Num, $Name = "n/a", $Exp = "") 00275 { 00276 // Check the expiration date first 00277 if (strlen($Exp)) 00278 { 00279 $Month = substr($Exp, 0, 2); 00280 $Year = substr($Exp, -2); 00281 $WorkDate = "$Month/01/$Year"; 00282 $WorkDate = strtotime($WorkDate); 00283 $LastDay = date("t", $WorkDate); 00284 $Expires = strtotime("$Month/$LastDay/$Year 11:59:59"); 00285 if ($Expires < time()) return 0; 00286 } 00287 00288 // Innocent until proven guilty 00289 $GoodCard = true; 00290 00291 // Get rid of any non-digits 00292 $Num = preg_replace("/[^0-9]~/", "", $Num); 00293 00294 // Perform card-specific checks, if applicable 00295 switch ($Name) 00296 { 00297 case "mcd" : 00298 $GoodCard = preg_match("/^5[1-5].{14}$/", $Num); 00299 break; 00300 00301 case "vis" : 00302 $GoodCard = preg_match("/^4.{15}$|^4.{12}$/", $Num); 00303 break; 00304 00305 case "amx" : 00306 $GoodCard = preg_match("/^3[47].{13}$/", $Num); 00307 break; 00308 00309 case "dsc" : 00310 $GoodCard = preg_match("/^6011.{12}$/", $Num); 00311 break; 00312 00313 case "dnc" : 00314 $GoodCard = preg_match("/^30[0-5].{11}$|^3[68].{12}$/", $Num); 00315 break; 00316 00317 case "jcb" : 00318 $GoodCard = preg_match("/^3.{15}$|^2131|1800.{11}$/", $Num); 00319 break; 00320 00321 case "dlt" : 00322 $GoodCard = preg_match("/^4.{15}$/", $Num); 00323 break; 00324 00325 case "swi" : 00326 $GoodCard = preg_match("/^[456].{15}$|^[456].{17,18}$/", $Num); 00327 break; 00328 00329 case "enr" : 00330 $GoodCard = preg_match("/^2014.{11}$|^2149.{11}$/", $Num); 00331 break; 00332 } 00333 00334 // The Luhn formula works right to left, so reverse the number. 00335 $Num = strrev($Num); 00336 $Total = 0; 00337 00338 for ($x=0; $x < strlen($Num); $x++) 00339 { 00340 $digit = substr($Num, $x, 1); 00341 00342 // If it's an odd digit, double it 00343 if ($x/2 != floor($x/2)) { 00344 $digit *= 2; 00345 00346 // If the result is two digits, add them 00347 if (strlen($digit) == 2) 00348 $digit = substr($digit, 0, 1) + substr($digit, 1, 1); 00349 } 00350 // Add the current digit, doubled and added if applicable, to the Total 00351 $Total += $digit; 00352 } 00353 00354 // If it passed (or bypassed) the card-specific check and the Total is 00355 // evenly divisible by 10, it's cool! 00356 return ($GoodCard && $Total % 10 == 0); 00357 } 00358 00359 } 00360