PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/authorize/enrol_form.php

http://github.com/moodle/moodle
PHP | 360 lines | 271 code | 56 blank | 33 comment | 44 complexity | 5b11a37544bb9c2c2d8343af8888def3 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Authorize.Net enrol plugin implementation.
  18. *
  19. * @package enrol
  20. * @subpackage authorize
  21. * @copyright 2010 Eugene Venter
  22. * @author Eugene Venter
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->libdir.'/formslib.php');
  27. class enrol_authorize_form extends moodleform
  28. {
  29. protected $instance;
  30. function definition() {
  31. global $USER;
  32. $mform = $this->_form;
  33. $this->instance = $this->_customdata;
  34. $plugin = enrol_get_plugin('authorize');
  35. $paymentmethodsenabled = get_list_of_payment_methods();
  36. $paymentmethod = optional_param('paymentmethod', $paymentmethodsenabled[0], PARAM_ALPHA);
  37. if (!in_array($paymentmethod, $paymentmethodsenabled)) {
  38. print_error('invalidpaymentmethod', '', '', $paymentmethod);
  39. }
  40. $othermethodstr = $this->other_method_available($paymentmethod);
  41. if ($othermethodstr) {
  42. $mform->addElement('static', '', '<div class="mdl-right">' . $othermethodstr . '</div>', '');
  43. }
  44. $mform->addElement('hidden', 'id');
  45. $mform->setType('id', PARAM_INT);
  46. $mform->setDefault('id', $this->instance->courseid);
  47. $mform->addElement('hidden', 'instanceid');
  48. $mform->setType('instanceid', PARAM_INT);
  49. $mform->setDefault('instanceid', $this->instance->id);
  50. $mform->addElement('hidden', 'paymentmethod', $paymentmethod);
  51. $mform->setType('paymentmethod', PARAM_ALPHA);
  52. $firstlastnamestr = (AN_METHOD_CC == $paymentmethod) ? get_string('nameoncard', 'enrol_authorize') : get_string('echeckfirslasttname', 'enrol_authorize');
  53. $mform->addElement('text', 'firstname', get_string('firstnameoncard', 'enrol_authorize'), 'size="16"');
  54. $mform->addElement('text', 'lastname', get_string('lastnameoncard', 'enrol_authorize'), 'size="16"');
  55. $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'client');
  56. $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'client');
  57. $mform->setType('firstname', PARAM_ALPHANUM);
  58. $mform->setType('lastname', PARAM_ALPHANUM);
  59. $mform->setDefault('firstname', $USER->firstname);
  60. $mform->setDefault('lastname', $USER->lastname);
  61. if (AN_METHOD_CC == $paymentmethod)
  62. {
  63. $mform->addElement('passwordunmask', 'cc', get_string('ccno', 'enrol_authorize'), 'size="20"');
  64. $mform->setType('cc', PARAM_ALPHANUM);
  65. $mform->setDefault('cc', '');
  66. $mform->addRule('cc', get_string('missingcc', 'enrol_authorize'), 'required', null, 'client');
  67. $mform->addRule('cc', get_string('ccinvalid', 'enrol_authorize'), 'numeric', null, 'client');
  68. $monthsmenu = array('' => get_string('choose'));
  69. for ($i = 1; $i <= 12; $i++) {
  70. $monthsmenu[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), "%B");
  71. }
  72. $nowdate = getdate();
  73. $startyear = $nowdate["year"] - 1;
  74. $endyear = $startyear + 20;
  75. $yearsmenu = array('' => get_string('choose'));
  76. for ($i = $startyear; $i < $endyear; $i++) {
  77. $yearsmenu[$i] = $i;
  78. }
  79. $mform->addElement('select', 'ccexpiremm', get_string('expiremonth', 'enrol_authorize'), $monthsmenu);
  80. $mform->addElement('select', 'ccexpireyyyy', get_string('expireyear', 'enrol_authorize'), $yearsmenu);
  81. $mform->addRule('ccexpiremm', get_string('missingccexpiremonth', 'enrol_authorize'), 'required', null, 'client');
  82. $mform->addRule('ccexpireyyyy', get_string('missingccexpireyear', 'enrol_authorize'), 'required', null, 'client');
  83. $mform->setType('ccexpiremm', PARAM_INT);
  84. $mform->setType('ccexpireyyyy', PARAM_INT);
  85. $mform->setDefault('ccexpiremm', '');
  86. $mform->setDefault('ccexpireyyyy', '');
  87. $creditcardsmenu = array('' => get_string('choose')) + get_list_of_creditcards();
  88. $mform->addElement('select', 'cctype', get_string('cctype', 'enrol_authorize'), $creditcardsmenu);
  89. $mform->setType('cctype', PARAM_ALPHA);
  90. $mform->addRule('cctype', get_string('missingcctype', 'enrol_authorize'), 'required', null, 'client');
  91. $mform->setDefault('cctype', '');
  92. $mform->addElement('text', 'cvv', get_string('ccvv', 'enrol_authorize'), 'size="4"');
  93. $mform->setType('cvv', PARAM_ALPHANUM);
  94. $mform->setDefault('cvv', '');
  95. $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'required', null, 'client');
  96. $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'numeric', null, 'client');
  97. if ($plugin->get_config('an_authcode')) {
  98. $ccauthgrp = array();
  99. $ccauthgrp[] = &$mform->createElement('checkbox', 'haveauth', null, get_string('haveauthcode', 'enrol_authorize'));
  100. $ccauthgrp[] = &$mform->createElement('static', 'nextline', null, '<br />');
  101. $ccauthgrp[] = &$mform->createElement('text', 'ccauthcode', '', 'size="8"');
  102. $mform->addGroup($ccauthgrp, 'ccauthgrp', get_string('authcode', 'enrol_authorize'), '&nbsp;', false);
  103. $ccauthgrprules = array();
  104. $ccauthgrprules['ccauthcode'][] = array(get_string('missingccauthcode', 'enrol_authorize'), 'numeric', null, 'client');
  105. $mform->addGroupRule('ccauthgrp', $ccauthgrprules);
  106. $mform->setDefault('haveauth', '');
  107. $mform->setDefault('ccauthcode', '');
  108. }
  109. if ($plugin->get_config('an_avs')) {
  110. $mform->addElement('header', '', '&nbsp;&nbsp;' . get_string('address'), '');
  111. $mform->addElement('text', 'ccaddress', get_string('address'), 'size="30"');
  112. $mform->setType('ccaddress', PARAM_ALPHANUM);
  113. $mform->setDefault('ccaddress', $USER->address);
  114. $mform->addRule('ccaddress', get_string('missingaddress', 'enrol_authorize'), 'required', null, 'client');
  115. $mform->addElement('text', 'cccity', get_string('cccity', 'enrol_authorize'), 'size="14"');
  116. $mform->addElement('text', 'ccstate', get_string('ccstate', 'enrol_authorize'), 'size="8"');
  117. $mform->addRule('cccity', get_string('missingcity'), 'required', null, 'client');
  118. $mform->setType('cccity', PARAM_ALPHANUM);
  119. $mform->setType('ccstate', PARAM_ALPHANUM);
  120. $mform->setDefault('cccity', $USER->city);
  121. $mform->setDefault('ccstate', '');
  122. $mform->addElement('select', 'cccountry', get_string('country'), get_string_manager()->get_list_of_countries());
  123. $mform->addRule('cccountry', get_string('missingcountry'), 'required', null, 'client');
  124. $mform->setType('cccountry', PARAM_ALPHA);
  125. $mform->setDefault('cccountry', $USER->country);
  126. }
  127. else {
  128. $mform->addElement('hidden', 'ccstate', '');
  129. $mform->setType('ccstate', PARAM_ALPHANUM);
  130. $mform->addElement('hidden', 'ccaddress', $USER->address);
  131. $mform->setType('ccaddress', PARAM_ALPHANUM);
  132. $mform->addElement('hidden', 'cccity', $USER->city);
  133. $mform->setType('cccity', PARAM_ALPHANUM);
  134. $mform->addElement('hidden', 'cccountry', $USER->country);
  135. $mform->setType('ccountry', PARAM_ALPHA);
  136. $mform->setDefault('cccountry', $USER->country);
  137. }
  138. } elseif (AN_METHOD_ECHECK == $paymentmethod) {
  139. $mform->addElement('text', 'abacode', get_string('echeckabacode', 'enrol_authorize'), 'size="9" maxlength="9"');
  140. $mform->setType('abacode', PARAM_ALPHANUM);
  141. $mform->setDefault('abacode', '');
  142. $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'required', null, 'client');
  143. $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'numeric', null, 'client');
  144. $mform->addElement('text', 'accnum', get_string('echeckaccnum', 'enrol_authorize'), 'size="20" maxlength="20"');
  145. $mform->setType('accnum', PARAM_ALPHANUM);
  146. $mform->setDefault('accnum', '');
  147. $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'required', null, 'client');
  148. $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'numeric', null, 'client');
  149. $acctypes = array();
  150. $acctypesenabled = get_list_of_bank_account_types();
  151. foreach ($acctypesenabled as $key) {
  152. $acctypes[$key] = get_string("echeck".strtolower($key), "enrol_authorize");
  153. }
  154. $acctypes = array('' => get_string('choose')) + $acctypes;
  155. $mform->addElement('select', 'acctype', get_string('echeckacctype', 'enrol_authorize'), $acctypes);
  156. $mform->setType('acctype', PARAM_ALPHA);
  157. $mform->addRule('acctype', get_string('invalidacctype', 'enrol_authorize'), 'required', null, 'client');
  158. $mform->setDefault('acctype', '');
  159. $mform->addElement('text', 'bankname', get_string('echeckbankname', 'enrol_authorize'), 'size="20" maxlength="50"');
  160. $mform->setType('bankname', PARAM_ALPHANUM);
  161. $mform->setDefault('bankname', '');
  162. $mform->addRule('bankname', get_string('missingbankname', 'enrol_authorize'), 'required', null, 'client');
  163. }
  164. $mform->addElement('text', 'cczip', get_string('zipcode', 'enrol_authorize'), 'size="5"');
  165. $mform->setType('cczip', PARAM_ALPHANUM);
  166. $mform->setDefault('cczip', '');
  167. $mform->addRule('cczip', get_string('missingzip', 'enrol_authorize'), 'required', null, 'client');
  168. $this->add_action_buttons(false, get_string('sendpaymentbutton', 'enrol_authorize'));
  169. }
  170. function validation($data, $files) {
  171. $errors = parent::validation($data, $files);
  172. $plugin = enrol_get_plugin('authorize');
  173. if (AN_METHOD_CC == $data['paymentmethod'])
  174. {
  175. if (!in_array($data['cctype'], array_keys(get_list_of_creditcards()))) {
  176. $errors['cctype'] = get_string('missingcctype', 'enrol_authorize');
  177. }
  178. $expdate = sprintf("%02d", intval($data['ccexpiremm'])) . $data['ccexpireyyyy'];
  179. $validcc = $this->validate_cc($data['cc'], $data['cctype'], $expdate);
  180. if (!$validcc) {
  181. if ($validcc === 0) {
  182. $errors['ccexpiremm'] = get_string('ccexpired', 'enrol_authorize');
  183. }
  184. else {
  185. $errors['cc'] = get_string('ccinvalid', 'enrol_authorize');
  186. }
  187. }
  188. if ($plugin->get_config('an_authcode') && !empty($data['haveauth']) && empty($data['ccauthcode'])) {
  189. $errors['ccauthgrp'] = get_string('missingccauthcode', 'enrol_authorize');
  190. }
  191. }
  192. elseif (AN_METHOD_ECHECK == $data['paymentmethod'])
  193. {
  194. if (!$this->validate_aba($data['abacode'])) {
  195. $errors['abacode'] = get_string('invalidaba', 'enrol_authorize');
  196. }
  197. if (!in_array($data['acctype'], get_list_of_bank_account_types())) {
  198. $errors['acctype'] = get_string('invalidacctype', 'enrol_authorize');
  199. }
  200. }
  201. return $errors;
  202. }
  203. private function other_method_available($currentmethod)
  204. {
  205. if ($currentmethod == AN_METHOD_CC) {
  206. $otheravailable = in_array(AN_METHOD_ECHECK, get_list_of_payment_methods());
  207. $url = 'index.php?id='.$this->instance->courseid.'&amp;paymentmethod='.AN_METHOD_ECHECK;
  208. $stringtofetch = 'usingecheckmethod';
  209. } else {
  210. $otheravailable = in_array(AN_METHOD_CC, get_list_of_payment_methods());
  211. $url = 'index.php?id='.$this->instance->courseid.'&amp;paymentmethod='.AN_METHOD_CC;
  212. $stringtofetch = 'usingccmethod';
  213. }
  214. if ($otheravailable) {
  215. $a = new stdClass;
  216. $a->url = $url;
  217. return get_string($stringtofetch, "enrol_authorize", $a);
  218. }
  219. else {
  220. return '';
  221. }
  222. }
  223. private function validate_aba($aba)
  224. {
  225. if (preg_match("/^[0-9]{9}$/", $aba)) {
  226. $n = 0;
  227. for($i = 0; $i < 9; $i += 3) {
  228. $n += (substr($aba, $i, 1) * 3) + (substr($aba, $i + 1, 1) * 7) + (substr($aba, $i + 2, 1));
  229. }
  230. if ($n != 0 and $n % 10 == 0) {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. private function validate_cc($Num, $Name = "n/a", $Exp = "")
  237. {
  238. // Check the expiration date first
  239. if (strlen($Exp))
  240. {
  241. $Month = substr($Exp, 0, 2);
  242. $Year = substr($Exp, -2);
  243. $WorkDate = "$Month/01/$Year";
  244. $WorkDate = strtotime($WorkDate);
  245. $LastDay = date("t", $WorkDate);
  246. $Expires = strtotime("$Month/$LastDay/$Year 11:59:59");
  247. if ($Expires < time()) return 0;
  248. }
  249. // Innocent until proven guilty
  250. $GoodCard = true;
  251. // Get rid of any non-digits
  252. $Num = preg_replace("/[^0-9]~/", "", $Num);
  253. // Perform card-specific checks, if applicable
  254. switch ($Name)
  255. {
  256. case "mcd" :
  257. $GoodCard = preg_match("/^5[1-5].{14}$/", $Num);
  258. break;
  259. case "vis" :
  260. $GoodCard = preg_match("/^4.{15}$|^4.{12}$/", $Num);
  261. break;
  262. case "amx" :
  263. $GoodCard = preg_match("/^3[47].{13}$/", $Num);
  264. break;
  265. case "dsc" :
  266. $GoodCard = preg_match("/^6011.{12}$/", $Num);
  267. break;
  268. case "dnc" :
  269. $GoodCard = preg_match("/^30[0-5].{11}$|^3[68].{12}$/", $Num);
  270. break;
  271. case "jcb" :
  272. $GoodCard = preg_match("/^3.{15}$|^2131|1800.{11}$/", $Num);
  273. break;
  274. case "dlt" :
  275. $GoodCard = preg_match("/^4.{15}$/", $Num);
  276. break;
  277. case "swi" :
  278. $GoodCard = preg_match("/^[456].{15}$|^[456].{17,18}$/", $Num);
  279. break;
  280. case "enr" :
  281. $GoodCard = preg_match("/^2014.{11}$|^2149.{11}$/", $Num);
  282. break;
  283. }
  284. // The Luhn formula works right to left, so reverse the number.
  285. $Num = strrev($Num);
  286. $Total = 0;
  287. for ($x=0; $x < strlen($Num); $x++)
  288. {
  289. $digit = substr($Num, $x, 1);
  290. // If it's an odd digit, double it
  291. if ($x/2 != floor($x/2)) {
  292. $digit *= 2;
  293. // If the result is two digits, add them
  294. if (strlen($digit) == 2)
  295. $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
  296. }
  297. // Add the current digit, doubled and added if applicable, to the Total
  298. $Total += $digit;
  299. }
  300. // If it passed (or bypassed) the card-specific check and the Total is
  301. // evenly divisible by 10, it's cool!
  302. return ($GoodCard && $Total % 10 == 0);
  303. }
  304. }