PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/MoodleWebRole/enrol/authorize/enrol.php

#
PHP | 793 lines | 600 code | 101 blank | 92 comment | 94 complexity | a6ecd4b9adfd62c772e84e1b11225f46 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php // $Id: enrol.php,v 1.135.2.7 2008/12/10 06:30:23 dongsheng Exp $
  2. require_once($CFG->dirroot.'/enrol/enrol.class.php');
  3. require_once($CFG->dirroot.'/enrol/authorize/const.php');
  4. require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
  5. /**
  6. * Authorize.net Payment Gateway plugin
  7. */
  8. class enrolment_plugin_authorize
  9. {
  10. /**
  11. * Cron log.
  12. *
  13. * @var string
  14. * @access public
  15. */
  16. var $log;
  17. /**
  18. * Presents registration forms.
  19. *
  20. * @param object $course Course info
  21. * @access public
  22. */
  23. function print_entry($course) {
  24. global $CFG, $USER, $form;
  25. $zerocost = zero_cost($course);
  26. if ($zerocost) {
  27. $manual = enrolment_factory::factory('manual');
  28. if (!empty($this->errormsg)) {
  29. $manual->errormsg = $this->errormsg;
  30. }
  31. $manual->print_entry($course);
  32. return;
  33. }
  34. prevent_double_paid($course);
  35. httpsrequired();
  36. if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 443) { // MDL-9836
  37. if (empty($CFG->loginhttps)) {
  38. print_error('httpsrequired', 'enrol_authorize');
  39. } else {
  40. $wwwsroot = str_replace('http:','https:', $CFG->wwwroot);
  41. redirect("$wwwsroot/course/enrol.php?id=$course->id");
  42. exit;
  43. }
  44. }
  45. $strcourses = get_string('courses');
  46. $strloginto = get_string('loginto', '', $course->shortname);
  47. $navlinks = array();
  48. $navlinks[] = array('name' => $strcourses, 'link' => "$CFG->wwwroot/course/", 'type' => 'misc');
  49. $navlinks[] = array('name' => $strloginto, 'link' => null, 'type' => 'misc');
  50. $navigation = build_navigation($navlinks);
  51. print_header($strloginto, $course->fullname, $navigation);
  52. print_course($course, '80%');
  53. if ($course->password) {
  54. print_heading(get_string('choosemethod', 'enrol_authorize'), 'center');
  55. }
  56. print_simple_box_start('center', '80%');
  57. if ($USER->username == 'guest') { // only real guest user, not for users with guest role
  58. $curcost = get_course_cost($course);
  59. echo '<div class="mdl-align">';
  60. echo '<p>'.get_string('paymentrequired').'</p>';
  61. echo '<p><b>'.get_string('cost').": $curcost[currency] $curcost[cost]".'</b></p>';
  62. echo '<p><a href="'.$CFG->httpswwwroot.'/login/">'.get_string('loginsite').'</a></p>';
  63. echo '</div>';
  64. }
  65. else {
  66. require_once($CFG->dirroot.'/enrol/authorize/enrol_form.php');
  67. $frmenrol = new enrol_authorize_form('enrol.php', compact('course'));
  68. if ($frmenrol->get_data()) {
  69. $authorizeerror = '';
  70. switch ($form->paymentmethod) {
  71. case AN_METHOD_CC:
  72. $authorizeerror = $this->cc_submit($form, $course);
  73. break;
  74. case AN_METHOD_ECHECK:
  75. $authorizeerror = $this->echeck_submit($form, $course);
  76. break;
  77. }
  78. if (!empty($authorizeerror)) {
  79. error($authorizeerror);
  80. }
  81. }
  82. $frmenrol->display();
  83. }
  84. print_simple_box_end();
  85. if ($course->password) {
  86. $password = '';
  87. include($CFG->dirroot.'/enrol/manual/enrol.html');
  88. }
  89. print_footer();
  90. }
  91. function print_enrolmentkeyfrom($course)
  92. {
  93. $manual = enrolment_factory::factory('manual');
  94. $manual->print_enrolmentkeyfrom($course);
  95. }
  96. /**
  97. * Validates registration forms and enrols student to course.
  98. *
  99. * @param object $form Form parameters
  100. * @param object $course Course info
  101. * @access public
  102. */
  103. function check_entry($form, $course)
  104. {
  105. global $CFG;
  106. if (zero_cost($course) || (!empty($course->password) && !empty($form->enrol) && $form->enrol == 'manual')) {
  107. $manual = enrolment_factory::factory('manual');
  108. $manual->check_entry($form, $course);
  109. if (!empty($manual->errormsg)) {
  110. $this->errormsg = $manual->errormsg;
  111. }
  112. }
  113. }
  114. /**
  115. * The user submitted credit card form.
  116. *
  117. * @param object $form Form parameters
  118. * @param object $course Course info
  119. * @access private
  120. */
  121. function cc_submit($form, $course)
  122. {
  123. global $CFG, $USER, $SESSION;
  124. require_once('authorizenetlib.php');
  125. prevent_double_paid($course);
  126. $useripno = getremoteaddr();
  127. $curcost = get_course_cost($course);
  128. $exp_date = sprintf("%02d", $form->ccexpiremm) . $form->ccexpireyyyy;
  129. // NEW CC ORDER
  130. $timenow = time();
  131. $order = new stdClass();
  132. $order->paymentmethod = AN_METHOD_CC;
  133. $order->refundinfo = substr($form->cc, -4);
  134. $order->ccname = $form->firstname . " " . $form->lastname;
  135. $order->courseid = $course->id;
  136. $order->userid = $USER->id;
  137. $order->status = AN_STATUS_NONE; // it will be changed...
  138. $order->settletime = 0; // cron changes this.
  139. $order->transid = 0; // Transaction Id
  140. $order->timecreated = $timenow;
  141. $order->amount = $curcost['cost'];
  142. $order->currency = $curcost['currency'];
  143. $order->id = insert_record("enrol_authorize", $order);
  144. if (!$order->id) {
  145. email_to_admin("Error while trying to insert new data", $order);
  146. return "Insert record error. Admin has been notified!";
  147. }
  148. $extra = new stdClass();
  149. $extra->x_card_num = $form->cc;
  150. $extra->x_card_code = $form->cvv;
  151. $extra->x_exp_date = $exp_date;
  152. $extra->x_currency_code = $curcost['currency'];
  153. $extra->x_amount = $curcost['cost'];
  154. $extra->x_first_name = $form->firstname;
  155. $extra->x_last_name = $form->lastname;
  156. $extra->x_country = $form->cccountry;
  157. $extra->x_address = $form->ccaddress;
  158. $extra->x_state = $form->ccstate;
  159. $extra->x_city = $form->cccity;
  160. $extra->x_zip = $form->cczip;
  161. $extra->x_invoice_num = $order->id;
  162. $extra->x_description = $course->shortname;
  163. $extra->x_cust_id = $USER->id;
  164. $extra->x_email = $USER->email;
  165. $extra->x_customer_ip = $useripno;
  166. $extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
  167. $extra->x_phone = '';
  168. $extra->x_fax = '';
  169. if (!empty($CFG->an_authcode) && !empty($form->ccauthcode)) {
  170. $action = AN_ACTION_CAPTURE_ONLY;
  171. $extra->x_auth_code = $form->ccauthcode;
  172. }
  173. elseif (!empty($CFG->an_review)) {
  174. $action = AN_ACTION_AUTH_ONLY;
  175. }
  176. else {
  177. $action = AN_ACTION_AUTH_CAPTURE;
  178. }
  179. $message = '';
  180. if (AN_APPROVED != authorize_action($order, $message, $extra, $action, $form->cctype)) {
  181. email_to_admin($message, $order);
  182. return $message;
  183. }
  184. $SESSION->ccpaid = 1; // security check: don't duplicate payment
  185. if (AN_ACTION_AUTH_ONLY == $action) { // review enabled, inform payment managers and redirect the user who have paid to main page.
  186. $a = new stdClass;
  187. $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$order->id";
  188. $a->orderid = $order->id;
  189. $a->transid = $order->transid;
  190. $a->amount = "$order->currency $order->amount";
  191. $a->expireon = userdate(authorize_getsettletime($timenow + (30 * 3600 * 24)));
  192. $a->captureon = userdate(authorize_getsettletime($timenow + (intval($CFG->an_capture_day) * 3600 * 24)));
  193. $a->course = $course->fullname;
  194. $a->user = fullname($USER);
  195. $a->acstatus = ($CFG->an_capture_day > 0) ? get_string('yes') : get_string('no');
  196. $emailmessage = get_string('adminneworder', 'enrol_authorize', $a);
  197. $a = new stdClass;
  198. $a->course = $course->shortname;
  199. $a->orderid = $order->id;
  200. $emailsubject = get_string('adminnewordersubject', 'enrol_authorize', $a);
  201. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  202. if (($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments'))) {
  203. foreach ($paymentmanagers as $paymentmanager) {
  204. email_to_user($paymentmanager, $USER, $emailsubject, $emailmessage);
  205. }
  206. }
  207. redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
  208. return;
  209. }
  210. // Credit card captured, ENROL student now...
  211. if (enrol_into_course($course, $USER, 'authorize')) {
  212. if (!empty($CFG->enrol_mailstudents)) {
  213. send_welcome_messages($order->id);
  214. }
  215. if (!empty($CFG->enrol_mailteachers)) {
  216. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  217. $paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments', '', '', '0', '1');
  218. $paymentmanager = array_shift($paymentmanagers);
  219. $a = new stdClass;
  220. $a->course = "$course->fullname";
  221. $a->user = fullname($USER);
  222. email_to_user($paymentmanager,
  223. $USER,
  224. get_string("enrolmentnew", '', format_string($course->shortname)),
  225. get_string('enrolmentnewuser', '', $a));
  226. }
  227. if (!empty($CFG->enrol_mailadmins)) {
  228. $a = new stdClass;
  229. $a->course = "$course->fullname";
  230. $a->user = fullname($USER);
  231. $admins = get_admins();
  232. foreach ($admins as $admin) {
  233. email_to_user($admin,
  234. $USER,
  235. get_string("enrolmentnew", '', format_string($course->shortname)),
  236. get_string('enrolmentnewuser', '', $a));
  237. }
  238. }
  239. } else {
  240. email_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
  241. }
  242. if ($SESSION->wantsurl) {
  243. $destination = $SESSION->wantsurl; unset($SESSION->wantsurl);
  244. } else {
  245. $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
  246. }
  247. load_all_capabilities();
  248. redirect($destination, get_string('paymentthanks', 'moodle', $course->fullname), 10);
  249. }
  250. /**
  251. * The user submitted echeck form.
  252. *
  253. * @param object $form Form parameters
  254. * @param object $course Course info
  255. * @access private
  256. */
  257. function echeck_submit($form, $course)
  258. {
  259. global $CFG, $USER, $SESSION;
  260. require_once('authorizenetlib.php');
  261. prevent_double_paid($course);
  262. $useripno = getremoteaddr();
  263. $curcost = get_course_cost($course);
  264. $isbusinesschecking = ($form->acctype == 'BUSINESSCHECKING');
  265. // NEW ECHECK ORDER
  266. $timenow = time();
  267. $order = new stdClass();
  268. $order->paymentmethod = AN_METHOD_ECHECK;
  269. $order->refundinfo = $isbusinesschecking ? 1 : 0;
  270. $order->ccname = $form->firstname . ' ' . $form->lastname;
  271. $order->courseid = $course->id;
  272. $order->userid = $USER->id;
  273. $order->status = AN_STATUS_NONE; // it will be changed...
  274. $order->settletime = 0; // cron changes this.
  275. $order->transid = 0; // Transaction Id
  276. $order->timecreated = $timenow;
  277. $order->amount = $curcost['cost'];
  278. $order->currency = $curcost['currency'];
  279. $order->id = insert_record("enrol_authorize", $order);
  280. if (!$order->id) {
  281. email_to_admin("Error while trying to insert new data", $order);
  282. return "Insert record error. Admin has been notified!";
  283. }
  284. $extra = new stdClass();
  285. $extra->x_bank_aba_code = $form->abacode;
  286. $extra->x_bank_acct_num = $form->accnum;
  287. $extra->x_bank_acct_type = $form->acctype;
  288. $extra->x_echeck_type = $isbusinesschecking ? 'CCD' : 'WEB';
  289. $extra->x_bank_name = $form->bankname;
  290. $extra->x_currency_code = $curcost['currency'];
  291. $extra->x_amount = $curcost['cost'];
  292. $extra->x_first_name = $form->firstname;
  293. $extra->x_last_name = $form->lastname;
  294. $extra->x_country = $USER->country;
  295. $extra->x_address = $USER->address;
  296. $extra->x_city = $USER->city;
  297. $extra->x_state = '';
  298. $extra->x_zip = '';
  299. $extra->x_invoice_num = $order->id;
  300. $extra->x_description = $course->shortname;
  301. $extra->x_cust_id = $USER->id;
  302. $extra->x_email = $USER->email;
  303. $extra->x_customer_ip = $useripno;
  304. $extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
  305. $extra->x_phone = '';
  306. $extra->x_fax = '';
  307. $message = '';
  308. if (AN_REVIEW != authorize_action($order, $message, $extra, AN_ACTION_AUTH_CAPTURE)) {
  309. email_to_admin($message, $order);
  310. return $message;
  311. }
  312. $SESSION->ccpaid = 1; // security check: don't duplicate payment
  313. redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
  314. }
  315. /**
  316. * Gets access icons.
  317. *
  318. * @param object $course
  319. * @return string
  320. * @access public
  321. */
  322. function get_access_icons($course) {
  323. $manual = enrolment_factory::factory('manual');
  324. $str = $manual->get_access_icons($course);
  325. $curcost = get_course_cost($course);
  326. if (abs($curcost['cost']) > 0.00) {
  327. $strrequirespayment = get_string("requirespayment");
  328. $strcost = get_string("cost");
  329. $currency = $curcost['currency'];
  330. switch ($currency) {
  331. case 'USD': $currency = 'US$'; break;
  332. case 'CAD': $currency = 'C$'; break;
  333. case 'EUR': $currency = '&euro;'; break;
  334. case 'GBP': $currency = '&pound;'; break;
  335. case 'JPY': $currency = '&yen;'; break;
  336. }
  337. $str .= '<div class="cost" title="'.$strrequirespayment.'">'.$strcost.': ';
  338. $str .= $currency . ' ' . $curcost['cost'].'</div>';
  339. }
  340. return $str;
  341. }
  342. /**
  343. * Shows config form & errors
  344. *
  345. * @param object $frm
  346. * @access public
  347. */
  348. function config_form($frm)
  349. {
  350. global $CFG;
  351. $mconfig = get_config('enrol/authorize');
  352. if (!check_openssl_loaded()) {
  353. notify('PHP must be compiled with SSL support (--with-openssl)');
  354. }
  355. if (empty($CFG->loginhttps) and substr($CFG->wwwroot, 0, 5) !== 'https') {
  356. $a = new stdClass;
  357. $a->url = "$CFG->wwwroot/$CFG->admin/settings.php?section=httpsecurity";
  358. notify(get_string('adminconfighttps', 'enrol_authorize', $a));
  359. return; // notice breaks the form and xhtml later
  360. }
  361. elseif (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 443) { // MDL-9836
  362. $wwwsroot = qualified_me();
  363. $wwwsroot = str_replace('http:', 'https:', $wwwsroot);
  364. $a = new stdClass;
  365. $a->url = $wwwsroot;
  366. notify(get_string('adminconfighttpsgo', 'enrol_authorize', $a));
  367. return; // notice breaks the form and xhtml later
  368. }
  369. if (!empty($frm->an_review)) {
  370. $captureday = intval($frm->an_capture_day);
  371. $emailexpired = intval($frm->an_emailexpired);
  372. if ($captureday > 0 || $emailexpired > 0) {
  373. $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
  374. if ((time() - intval($lastcron) > 3600 * 24)) {
  375. notify(get_string('admincronsetup', 'enrol_authorize'));
  376. }
  377. }
  378. }
  379. if (($count = count_records('enrol_authorize', 'status', AN_STATUS_AUTH))) {
  380. $a = new stdClass;
  381. $a->count = $count;
  382. $a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=".AN_STATUS_AUTH;
  383. notify(get_string('adminpendingorders', 'enrol_authorize', $a));
  384. }
  385. if (data_submitted()) {
  386. if (empty($mconfig->an_login)) {
  387. notify("an_login required");
  388. }
  389. if (empty($mconfig->an_tran_key) && empty($mconfig->an_password)) {
  390. notify("an_tran_key or an_password required");
  391. }
  392. }
  393. include($CFG->dirroot.'/enrol/authorize/config_form.php');
  394. }
  395. /**
  396. * process_config
  397. *
  398. * @param object $config
  399. * @return bool true if it will be saved.
  400. * @access public
  401. */
  402. function process_config($config)
  403. {
  404. global $CFG;
  405. $mconfig = get_config('enrol/authorize');
  406. // site settings
  407. if (($cost = optional_param('enrol_cost', 5, PARAM_INT)) > 0) {
  408. set_config('enrol_cost', $cost);
  409. }
  410. set_config('enrol_currency', optional_param('enrol_currency', 'USD', PARAM_ALPHA));
  411. set_config('enrol_mailstudents', optional_param('enrol_mailstudents', 0, PARAM_BOOL));
  412. set_config('enrol_mailteachers', optional_param('enrol_mailteachers', 0, PARAM_BOOL));
  413. set_config('enrol_mailadmins', optional_param('enrol_mailadmins', 0, PARAM_BOOL));
  414. // optional authorize.net settings
  415. set_config('an_avs', optional_param('an_avs', 0, PARAM_BOOL));
  416. set_config('an_authcode', optional_param('an_authcode', 0, PARAM_BOOL));
  417. set_config('an_test', optional_param('an_test', 0, PARAM_BOOL));
  418. set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL));
  419. $acceptmethods = optional_param('acceptmethods', get_list_of_payment_methods(), PARAM_ALPHA);
  420. set_config('an_acceptmethods', implode(',', $acceptmethods));
  421. $acceptccs = optional_param('acceptccs', array_keys(get_list_of_creditcards()), PARAM_ALPHA);
  422. set_config('an_acceptccs', implode(',', $acceptccs));
  423. $acceptechecktypes = optional_param('acceptechecktypes', get_list_of_bank_account_types(), PARAM_ALPHA);
  424. set_config('an_acceptechecktypes', implode(',', $acceptechecktypes));
  425. $cutoff_hour = optional_param('an_cutoff_hour', 0, PARAM_INT);
  426. $cutoff_min = optional_param('an_cutoff_min', 5, PARAM_INT);
  427. set_config('an_cutoff', $cutoff_hour * 60 + $cutoff_min);
  428. // cron depencies
  429. $reviewval = optional_param('an_review', 0, PARAM_BOOL);
  430. $captureday = optional_param('an_capture_day', 5, PARAM_INT);
  431. $emailexpired = optional_param('an_emailexpired', 2, PARAM_INT);
  432. $emailexpiredteacher = optional_param('an_emailexpiredteacher', 0, PARAM_BOOL);
  433. $sorttype = optional_param('an_sorttype', 'ttl', PARAM_ALPHA);
  434. $captureday = ($captureday > 29) ? 29 : (($captureday < 0) ? 0 : $captureday);
  435. $emailexpired = ($emailexpired > 5) ? 5 : (($emailexpired < 0) ? 0 : $emailexpired);
  436. if (!empty($reviewval) && ($captureday > 0 || $emailexpired > 0)) {
  437. $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
  438. if (time() - intval($lastcron) > 3600 * 24) {
  439. return false;
  440. }
  441. }
  442. set_config('an_review', $reviewval);
  443. set_config('an_capture_day', $captureday);
  444. set_config('an_emailexpired', $emailexpired);
  445. set_config('an_emailexpiredteacher', $emailexpiredteacher);
  446. set_config('an_sorttype', $sorttype);
  447. // https and openssl library is required
  448. if ((substr($CFG->wwwroot, 0, 5) !== 'https' and empty($CFG->loginhttps)) or !check_openssl_loaded()) {
  449. return false;
  450. }
  451. // REQUIRED fields;
  452. // an_login
  453. $loginval = optional_param('an_login', '');
  454. if (empty($loginval) && empty($mconfig->an_login)) {
  455. return false;
  456. }
  457. $loginval = !empty($loginval) ? rc4encrypt($loginval) : strval($mconfig->an_login);
  458. set_config('an_login', $loginval, 'enrol/authorize');
  459. // an_tran_key, an_password
  460. $tranval = optional_param('an_tran_key', '');
  461. $tranval = !empty($tranval) ? rc4encrypt($tranval) : (isset($mconfig->an_tran_key)?$mconfig->an_tran_key:'');
  462. $passwordval = optional_param('an_password', '');
  463. $passwordval = !empty($passwordval) ? rc4encrypt($passwordval) :(isset($mconfig->an_password)?$mconfig->an_password:'');
  464. $deletecurrent = optional_param('delete_current', '0', PARAM_BOOL);
  465. if (!empty($deletecurrent) and !empty($tranval)) {
  466. unset_config('an_password', 'enrol/authorize');
  467. $passwordval = '';
  468. }
  469. elseif (!empty($passwordval)) {
  470. set_config('an_password', $passwordval, 'enrol/authorize');
  471. }
  472. if (empty($tranval) and empty($passwordval)) {
  473. return false;
  474. }
  475. if (!empty($tranval)) {
  476. set_config('an_tran_key', $tranval, 'enrol/authorize');
  477. }
  478. return true;
  479. }
  480. /**
  481. * This function is run by admin/cron.php every time if admin has enabled this plugin.
  482. *
  483. * Everyday at settlement time (default is 00:05), it cleans up some tables
  484. * and sends email to admin/teachers about pending orders expiring if manual-capture has enabled.
  485. *
  486. * If admin set up 'Order review' and 'Capture day', it captures credits cards and enrols students.
  487. *
  488. * @access public
  489. */
  490. function cron()
  491. {
  492. global $CFG;
  493. require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
  494. $oneday = 86400;
  495. $timenow = time();
  496. $settlementtime = authorize_getsettletime($timenow);
  497. $timediff30 = $settlementtime - (30 * $oneday);
  498. $mconfig = get_config('enrol/authorize');
  499. mtrace("Processing authorize cron...");
  500. if (intval($mconfig->an_dailysettlement) < $settlementtime) {
  501. set_config('an_dailysettlement', $settlementtime, 'enrol/authorize');
  502. mtrace(" daily cron; some cleanups and sending email to admins the count of pending orders expiring", ": ");
  503. $this->cron_daily();
  504. mtrace("done");
  505. }
  506. mtrace(" scheduled capture", ": ");
  507. if (empty($CFG->an_review) or (!empty($CFG->an_test)) or (intval($CFG->an_capture_day) < 1) or (!check_openssl_loaded())) {
  508. mtrace("disabled");
  509. return; // order review disabled or test mode or manual capture or openssl wasn't loaded.
  510. }
  511. $timediffcnf = $settlementtime - (intval($CFG->an_capture_day) * $oneday);
  512. $select = "(status = '" .AN_STATUS_AUTH. "') AND (timecreated < '$timediffcnf') AND (timecreated > '$timediff30')";
  513. if (!($ordercount = count_records_select('enrol_authorize', $select))) {
  514. mtrace("no pending orders");
  515. return;
  516. }
  517. $eachconn = intval($mconfig->an_eachconnsecs);
  518. $eachconn = (($eachconn > 60) ? 60 : (($eachconn <= 0) ? 3 : $eachconn));
  519. if (($ordercount * $eachconn) + intval($mconfig->an_lastcron) > $timenow) {
  520. mtrace("blocked");
  521. return;
  522. }
  523. set_config('an_lastcron', $timenow, 'enrol/authorize');
  524. mtrace(" $ordercount orders are being processed now", ": ");
  525. $faults = '';
  526. $sendem = array();
  527. $elapsed = time();
  528. @set_time_limit(0);
  529. $this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
  530. $lastcourseid = 0;
  531. for ($rs = get_recordset_select('enrol_authorize', $select, 'courseid'); ($order = rs_fetch_next_record($rs)); )
  532. {
  533. $message = '';
  534. $extra = NULL;
  535. if (AN_APPROVED == authorize_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
  536. if ($lastcourseid != $order->courseid) {
  537. $lastcourseid = $order->courseid;
  538. $course = get_record('course', 'id', $lastcourseid);
  539. $role = get_default_course_role($course);
  540. $context = get_context_instance(CONTEXT_COURSE, $lastcourseid);
  541. }
  542. $timestart = $timeend = 0;
  543. if ($course->enrolperiod) {
  544. $timestart = $timenow;
  545. $timeend = $order->settletime + $course->enrolperiod;
  546. }
  547. $user = get_record('user', 'id', $order->userid);
  548. if (role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, 'authorize')) {
  549. $this->log .= "User($user->id) has been enrolled to course($course->id).\n";
  550. if (!empty($CFG->enrol_mailstudents)) {
  551. $sendem[] = $order->id;
  552. }
  553. }
  554. else {
  555. $faults .= "Error while trying to enrol ".fullname($user)." in '$course->fullname' \n";
  556. foreach ($order as $okey => $ovalue) {
  557. $faults .= " $okey = $ovalue\n";
  558. }
  559. }
  560. }
  561. else {
  562. $this->log .= "Error, Order# $order->id: " . $message . "\n";
  563. }
  564. }
  565. rs_close($rs);
  566. mtrace("processed");
  567. $timenow = time();
  568. $elapsed = $timenow - $elapsed;
  569. $eachconn = ceil($elapsed / $ordercount);
  570. set_config('an_eachconnsecs', $eachconn, 'enrol/authorize');
  571. $this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate($timenow);
  572. $adminuser = get_admin();
  573. if (!empty($faults)) {
  574. email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON FAULTS", $faults);
  575. }
  576. if (!empty($CFG->enrol_mailadmins)) {
  577. email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
  578. }
  579. // Send emails to students about which courses have enrolled.
  580. if (!empty($sendem)) {
  581. mtrace(" sending welcome messages to students", ": ");
  582. send_welcome_messages($sendem);
  583. mtrace("sent");
  584. }
  585. }
  586. /**
  587. * Daily cron. It executes at settlement time (default is 00:05).
  588. *
  589. * @access private
  590. */
  591. function cron_daily()
  592. {
  593. global $CFG, $SITE;
  594. require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
  595. $oneday = 86400;
  596. $timenow = time();
  597. $onepass = $timenow - $oneday;
  598. $settlementtime = authorize_getsettletime($timenow);
  599. $timediff30 = $settlementtime - (30 * $oneday);
  600. // Delete orders that no transaction was made.
  601. $select = "(status='".AN_STATUS_NONE."') AND (timecreated<'$timediff30')";
  602. delete_records_select('enrol_authorize', $select);
  603. // Pending orders are expired with in 30 days.
  604. $select = "(status='".AN_STATUS_AUTH."') AND (timecreated<'$timediff30')";
  605. execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='".AN_STATUS_EXPIRE."' WHERE $select", false);
  606. // Delete expired orders 60 days later.
  607. $timediff60 = $settlementtime - (60 * $oneday);
  608. $select = "(status='".AN_STATUS_EXPIRE."') AND (timecreated<'$timediff60')";
  609. delete_records_select('enrol_authorize', $select);
  610. // XXX TODO SEND EMAIL to 'enrol/authorize:uploadcsv'
  611. // get_users_by_capability() does not handling user level resolving
  612. // After user resolving, get_admin() to get_users_by_capability()
  613. $adminuser = get_admin();
  614. $select = "status IN(".AN_STATUS_UNDERREVIEW.",".AN_STATUS_APPROVEDREVIEW.") AND (timecreated<'$onepass') AND (timecreated>'$timediff60')";
  615. $count = count_records_select('enrol_authorize', $select);
  616. if ($count) {
  617. $a = new stdClass;
  618. $a->count = $count;
  619. $a->course = $SITE->shortname;
  620. $subject = get_string('pendingechecksubject', 'enrol_authorize', $a);
  621. $a = new stdClass;
  622. $a->count = $count;
  623. $a->url = $CFG->wwwroot.'/enrol/authorize/uploadcsv.php';
  624. $message = get_string('pendingecheckemail', 'enrol_authorize', $a);
  625. @email_to_user($adminuser, $adminuser, $subject, $message);
  626. }
  627. // Daily warning email for pending orders expiring.
  628. if (empty($CFG->an_emailexpired)) {
  629. return; // not enabled
  630. }
  631. // Pending orders count will be expired.
  632. $timediffem = $settlementtime - ((30 - intval($CFG->an_emailexpired)) * $oneday);
  633. $select = "(status='". AN_STATUS_AUTH ."') AND (timecreated<'$timediffem') AND (timecreated>'$timediff30')";
  634. $count = count_records_select('enrol_authorize', $select);
  635. if (!$count) {
  636. return;
  637. }
  638. // Email to admin
  639. $a = new stdClass;
  640. $a->pending = $count;
  641. $a->days = $CFG->an_emailexpired;
  642. $a->course = $SITE->shortname;
  643. $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
  644. $a = new stdClass;
  645. $a->pending = $count;
  646. $a->days = $CFG->an_emailexpired;
  647. $a->course = $SITE->fullname;
  648. $a->enrolurl = "$CFG->wwwroot/$CFG->admin/enrol_config.php?enrol=authorize";
  649. $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH;
  650. $message = get_string('pendingordersemail', 'enrol_authorize', $a);
  651. email_to_user($adminuser, $adminuser, $subject, $message);
  652. // Email to teachers
  653. if (empty($CFG->an_emailexpiredteacher)) {
  654. return; // email feature disabled for teachers.
  655. }
  656. $sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype;
  657. $sql = "SELECT e.courseid, e.currency, c.fullname, c.shortname,
  658. COUNT(e.courseid) AS cnt, SUM(e.amount) as ttl
  659. FROM {$CFG->prefix}enrol_authorize e
  660. INNER JOIN {$CFG->prefix}course c ON c.id = e.courseid
  661. WHERE (e.status = ". AN_STATUS_AUTH .")
  662. AND (e.timecreated < $timediffem)
  663. AND (e.timecreated > $timediff30)
  664. GROUP BY e.courseid
  665. ORDER BY $sorttype DESC";
  666. for ($rs = get_recordset_sql($sql); ($courseinfo = rs_fetch_next_record($rs)); )
  667. {
  668. $lastcourse = $courseinfo->courseid;
  669. $context = get_context_instance(CONTEXT_COURSE, $lastcourse);
  670. if (($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments'))) {
  671. $a = new stdClass;
  672. $a->course = $courseinfo->shortname;
  673. $a->pending = $courseinfo->cnt;
  674. $a->days = $CFG->an_emailexpired;
  675. $subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
  676. $a = new stdClass;
  677. $a->course = $courseinfo->fullname;
  678. $a->pending = $courseinfo->cnt;
  679. $a->currency = $courseinfo->currency;
  680. $a->sumcost = $courseinfo->ttl;
  681. $a->days = $CFG->an_emailexpired;
  682. $a->url = $CFG->wwwroot.'/enrol/authorize/index.php?course='.$lastcourse.'&amp;status='.AN_STATUS_AUTH;
  683. $message = get_string('pendingordersemailteacher', 'enrol_authorize', $a);
  684. foreach ($paymentmanagers as $paymentmanager) {
  685. email_to_user($paymentmanager, $adminuser, $subject, $message);
  686. }
  687. }
  688. }
  689. rs_close($rs);
  690. }
  691. }
  692. ?>