moodle/enrol/authorize/enrol.php

908 lines
38 KiB
PHP
Raw Normal View History

Ported AUTHORIZE_ECHECK changes into HEAD. (cvs update -j HEAD -j AUTHORIZE_ECHECK) * New Feature (Authorize.net eCheck) Authorize.Net provides an exclusive, fully integrated electronic check payment method, eCheck.Net. Using eCheck.Net, merchants can accept and process payments from consumer and corporate bank accounts directly from their Web site or through the Authorize.Net Virtual Terminal. By accepting electronic checks, you expand the payment options available to new and existing customers, enhancing customer loyalty and potentially increasing sales. + Lower Fees - Lower rates than credit cards or PayPal. + More Efficient - eCheck.Net does everything online, eliminating the cost and inconvenience of manually processing paper checks and waiting for checks in the mail. + Fully Integrated Solution - No third-party integration required implementing eCheck.Net is easy for merchants already using the Authorize.Net Payment Gateway. + Integrated Reporting - Provides a combined view of all eCheck.Net and credit card payment transactions. Reconcile payment and billing activity using online reports and statements. + Ship Product Sooner - Improved up-front transaction validation that returns the status of transactions faster. + Security - Authorize.Net uses the latest 128-bit Secure Socket Layer (SSL) technology for secure Internet Protocol (IP) transactions. == TO DO == - Generate Echeck forms. - Show users a echeck option if admin enabled echeck method. - Allow admins/teachers to enrol a student using echeck method (FIX: role consept) ==========
2006-08-30 10:29:10 +00:00
<?php // $Id$
require_once($CFG->dirroot.'/enrol/enrol.class.php');
require_once($CFG->dirroot.'/enrol/authorize/const.php');
require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
2008-03-03 16:04:32 +00:00
require_once($CFG->dirroot.'/enrol/authorize/authorizenet.class.php');
require_once($CFG->libdir.'/eventslib.php');
/**
* Authorize.net Payment Gateway plugin
*/
class enrolment_plugin_authorize
{
2005-11-21 07:33:04 +00:00
2005-12-14 15:47:37 +00:00
/**
* Cron log.
*
* @var string
* @access public
*/
2007-11-07 10:24:35 +00:00
public $log;
2005-12-14 15:47:37 +00:00
2005-11-21 07:33:04 +00:00
/**
* Presents registration forms.
2005-11-21 07:33:04 +00:00
*
* @param object $course Course info
* @access public
2005-11-21 07:33:04 +00:00
*/
2007-11-07 10:24:35 +00:00
public function print_entry($course)
{
2005-11-21 07:33:04 +00:00
global $CFG, $USER, $form;
$zerocost = zero_cost($course);
2006-06-29 19:07:28 +00:00
if ($zerocost) {
$manual = enrolment_factory::factory('manual');
if (!empty($this->errormsg)) {
$manual->errormsg = $this->errormsg;
}
$manual->print_entry($course);
return;
}
2006-11-15 20:44:49 +00:00
prevent_double_paid($course);
httpsrequired();
if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 443) { // MDL-9836
2005-11-21 07:33:04 +00:00
if (empty($CFG->loginhttps)) {
print_error('httpsrequired', 'enrol_authorize');
2005-11-21 07:33:04 +00:00
} else {
2006-01-19 14:57:23 +00:00
$wwwsroot = str_replace('http:','https:', $CFG->wwwroot);
redirect("$wwwsroot/course/enrol.php?id=$course->id");
2005-11-21 07:33:04 +00:00
exit;
}
2005-08-03 10:11:16 +00:00
}
2006-01-19 14:57:23 +00:00
$strcourses = get_string('courses');
$strloginto = get_string('loginto', '', $course->shortname);
$navlinks = array();
$navlinks[] = array('name' => $strcourses, 'link' => "$CFG->wwwroot/course/", 'type' => 'misc');
$navlinks[] = array('name' => $strloginto, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header($strloginto, $course->fullname, $navigation);
2006-01-19 14:57:23 +00:00
print_course($course, '80%');
2006-06-29 19:07:28 +00:00
if ($course->password) {
print_heading(get_string('choosemethod', 'enrol_authorize'), 'center');
2005-11-21 07:33:04 +00:00
}
if ($USER->username == 'guest') { // only real guest user, not for users with guest role
$curcost = get_course_cost($course);
echo '<div class="mdl-align">';
echo '<p>'.get_string('paymentrequired').'</p>';
echo '<p><b>'.get_string('cost').": $curcost[currency] $curcost[cost]".'</b></p>';
echo '<p><a href="'.$CFG->httpswwwroot.'/login/">'.get_string('loginsite').'</a></p>';
echo '</div>';
2006-11-15 20:44:49 +00:00
}
else {
require_once($CFG->dirroot.'/enrol/authorize/enrol_form.php');
$frmenrol = new enrol_authorize_form('enrol.php', compact('course'));
if ($frmenrol->get_data()) {
$authorizeerror = '';
2006-11-15 20:44:49 +00:00
switch ($form->paymentmethod) {
case AN_METHOD_CC:
$authorizeerror = $this->cc_submit($form, $course);
break;
2006-11-15 20:44:49 +00:00
case AN_METHOD_ECHECK:
$authorizeerror = $this->echeck_submit($form, $course);
break;
2006-11-15 20:44:49 +00:00
}
if (!empty($authorizeerror)) {
print_error('authorizeerror', 'enrol_authorize', '', $authorizeerror);
2006-11-16 12:23:40 +00:00
}
2006-11-15 20:44:49 +00:00
}
print_box_start();
$frmenrol->display();
print_box_end();
}
if ($course->password) {
$password = '';
include($CFG->dirroot.'/enrol/manual/enrol.html');
}
2005-11-21 07:33:04 +00:00
print_footer();
}
function print_enrolmentkeyfrom($course)
{
$manual = enrolment_factory::factory('manual');
$manual->print_enrolmentkeyfrom($course);
}
2005-11-21 07:33:04 +00:00
/**
* Validates registration forms and enrols student to course.
2005-11-21 07:33:04 +00:00
*
* @param object $form Form parameters
* @param object $course Course info
* @access public
2005-11-21 07:33:04 +00:00
*/
2007-11-07 10:24:35 +00:00
public function check_entry($form, $course)
2006-08-30 14:06:40 +00:00
{
global $CFG;
if (zero_cost($course) || (!empty($course->password) && !empty($form->enrol) && $form->enrol == 'manual')) {
$manual = enrolment_factory::factory('manual');
$manual->check_entry($form, $course);
2006-06-29 19:07:28 +00:00
if (!empty($manual->errormsg)) {
$this->errormsg = $manual->errormsg;
}
2006-08-30 14:06:40 +00:00
}
2005-05-16 22:22:31 +00:00
}
2005-05-26 13:22:01 +00:00
2006-11-15 20:44:49 +00:00
2005-11-21 07:33:04 +00:00
/**
* The user submitted credit card form.
2005-11-21 07:33:04 +00:00
*
* @param object $form Form parameters
* @param object $course Course info
2008-03-03 16:04:32 +00:00
* @return string NULL if ok, error message otherwise.
2005-11-21 07:33:04 +00:00
* @access private
*/
2007-11-07 10:24:35 +00:00
private function cc_submit($form, $course)
{
2008-06-05 14:06:39 +00:00
global $CFG, $USER, $SESSION, $DB;
prevent_double_paid($course);
$useripno = getremoteaddr();
$curcost = get_course_cost($course);
$exp_date = sprintf("%02d", $form->ccexpiremm) . $form->ccexpireyyyy;
// NEW CC ORDER
$timenow = time();
$order = new stdClass();
$order->paymentmethod = AN_METHOD_CC;
$order->refundinfo = substr($form->cc, -4);
2006-11-15 20:44:49 +00:00
$order->ccname = $form->firstname . " " . $form->lastname;
$order->courseid = $course->id;
$order->userid = $USER->id;
$order->status = AN_STATUS_NONE; // it will be changed...
2005-12-22 15:24:05 +00:00
$order->settletime = 0; // cron changes this.
2006-01-05 14:30:49 +00:00
$order->transid = 0; // Transaction Id
$order->timecreated = $timenow;
$order->amount = $curcost['cost'];
$order->currency = $curcost['currency'];
2008-06-05 14:06:39 +00:00
$order->id = $DB->insert_record("enrol_authorize", $order);
if (!$order->id) {
message_to_admin("Error while trying to insert new data", $order);
return "Insert record error. Admin has been notified!";
2005-11-21 14:09:52 +00:00
}
$extra = new stdClass();
$extra->x_card_num = $form->cc;
$extra->x_card_code = $form->cvv;
2006-01-05 16:28:34 +00:00
$extra->x_exp_date = $exp_date;
$extra->x_currency_code = $curcost['currency'];
$extra->x_amount = $curcost['cost'];
2006-11-15 20:44:49 +00:00
$extra->x_first_name = $form->firstname;
$extra->x_last_name = $form->lastname;
$extra->x_country = $form->cccountry;
$extra->x_address = $form->ccaddress;
$extra->x_state = $form->ccstate;
$extra->x_city = $form->cccity;
$extra->x_zip = $form->cczip;
$extra->x_invoice_num = $order->id;
$extra->x_description = $course->shortname;
2006-01-19 14:57:23 +00:00
$extra->x_cust_id = $USER->id;
$extra->x_email = $USER->email;
$extra->x_customer_ip = $useripno;
2006-01-19 14:57:23 +00:00
$extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
$extra->x_phone = '';
$extra->x_fax = '';
if (!empty($CFG->an_authcode) && !empty($form->ccauthcode)) {
$action = AN_ACTION_CAPTURE_ONLY;
$extra->x_auth_code = $form->ccauthcode;
}
elseif (!empty($CFG->an_review)) {
$action = AN_ACTION_AUTH_ONLY;
}
2008-03-03 16:04:32 +00:00
else {
$action = AN_ACTION_AUTH_CAPTURE;
}
2005-12-22 15:24:05 +00:00
$message = '';
2008-03-03 16:04:32 +00:00
if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, $action, $form->cctype))
{
$SESSION->ccpaid = 1; // security check: don't duplicate payment
switch ($action)
{
// review enabled (authorize but capture: draw money but wait for settlement during 30 days)
// the first step is to inform payment managers and to redirect the user to main page.
// the next step is to accept/deny payment (AN_ACTION_PRIOR_AUTH_CAPTURE/VOID) within 30 days (payment management or scheduled-capture CRON)
// unless you accept payment or enable auto-capture cron, the transaction is expired after 30 days and the user cannot enrol to the course during 30 days.
// see also: admin/cron.php, $this->cron(), $CFG->an_capture_day...
case AN_ACTION_AUTH_ONLY:
{
$a = new stdClass;
$a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$order->id";
$a->orderid = $order->id;
$a->transid = $order->transid;
$a->amount = "$order->currency $order->amount";
$a->expireon = userdate(AuthorizeNet::getsettletime($timenow + (30 * 3600 * 24)));
$a->captureon = userdate(AuthorizeNet::getsettletime($timenow + (intval($CFG->an_capture_day) * 3600 * 24)));
$a->course = $course->fullname;
$a->user = fullname($USER);
$a->acstatus = ($CFG->an_capture_day > 0) ? get_string('yes') : get_string('no');
$emailmessage = get_string('adminneworder', 'enrol_authorize', $a);
$a = new stdClass;
$a->course = $course->shortname;
$a->orderid = $order->id;
$emailsubject = get_string('adminnewordersubject', 'enrol_authorize', $a);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments'))) {
foreach ($paymentmanagers as $paymentmanager) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $paymentmanager;
$eventdata->subject = $emailsubject;
$eventdata->fullmessage = $emailmessage;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
2008-03-03 16:04:32 +00:00
}
}
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
break;
}
2008-03-03 16:04:32 +00:00
case AN_ACTION_CAPTURE_ONLY: // auth code received via phone and the code accepted.
case AN_ACTION_AUTH_CAPTURE: // real time transaction, authorize and capture.
{
// Credit card captured, ENROL student now...
if (enrol_into_course($course, $USER, 'authorize'))
{
if (!empty($CFG->enrol_mailstudents)) {
send_welcome_messages($order->id);
}
if (!empty($CFG->enrol_mailteachers)) {
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments', '', '', '0', '1');
$paymentmanager = array_shift($paymentmanagers);
$a = new stdClass;
$a->course = "$course->fullname";
$a->user = fullname($USER);
2008-08-07 11:42:09 +00:00
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $paymentmanager;
$eventdata->subject = get_string("enrolmentnew", '', format_string($course->shortname));
$eventdata->fullmessage = get_string('enrolmentnewuser', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
2008-03-03 16:04:32 +00:00
}
if (!empty($CFG->enrol_mailadmins)) {
$a = new stdClass;
$a->course = "$course->fullname";
$a->user = fullname($USER);
$admins = get_admins();
foreach ($admins as $admin) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
2008-08-07 11:42:09 +00:00
$eventdata->userto = $admin;
$eventdata->subject = get_string("enrolmentnew", '', format_string($course->shortname));
$eventdata->fullmessage = get_string('enrolmentnewuser', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
2008-03-03 16:04:32 +00:00
}
}
}
else
{
message_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
2008-03-03 16:04:32 +00:00
}
2008-03-03 16:04:32 +00:00
load_all_capabilities();
print_box_start('generalbox', 'notice');
echo '<p>'. get_string('paymentthanks', 'moodle', $course->fullname) .'</p>';
echo '<div class="buttons">';
print_single_button("$CFG->wwwroot/enrol/authorize/index.php", array('order'=>$order->id), get_string('payments'));
print_single_button("$CFG->wwwroot/course/view.php", array('id'=>$course->id), $course->fullname);
echo '</div>';
print_box_end();
print_footer($course);
exit; // break;
2005-11-21 07:33:04 +00:00
}
}
2008-03-03 16:04:32 +00:00
return NULL;
2005-12-22 15:24:05 +00:00
}
2008-03-03 16:04:32 +00:00
else
{
message_to_admin($message, $order);
2008-03-03 16:04:32 +00:00
return $message;
2005-11-21 14:09:52 +00:00
}
2005-11-21 07:33:04 +00:00
}
2005-07-16 15:15:41 +00:00
/**
* The user submitted echeck form.
*
* @param object $form Form parameters
* @param object $course Course info
2008-03-03 16:04:32 +00:00
* @return string NULL if ok, error message otherwise.
* @access private
*/
2007-11-07 10:24:35 +00:00
private function echeck_submit($form, $course)
2006-08-30 14:06:40 +00:00
{
2008-06-05 14:06:39 +00:00
global $CFG, $USER, $SESSION, $DB;
2006-08-30 14:06:40 +00:00
prevent_double_paid($course);
$useripno = getremoteaddr();
$curcost = get_course_cost($course);
$isbusinesschecking = ($form->acctype == 'BUSINESSCHECKING');
// NEW ECHECK ORDER
$timenow = time();
$order = new stdClass();
$order->paymentmethod = AN_METHOD_ECHECK;
$order->refundinfo = $isbusinesschecking ? 1 : 0;
$order->ccname = $form->firstname . ' ' . $form->lastname;
$order->courseid = $course->id;
$order->userid = $USER->id;
$order->status = AN_STATUS_NONE; // it will be changed...
$order->settletime = 0; // cron changes this.
$order->transid = 0; // Transaction Id
$order->timecreated = $timenow;
$order->amount = $curcost['cost'];
$order->currency = $curcost['currency'];
2008-06-05 14:06:39 +00:00
$order->id = $DB->insert_record("enrol_authorize", $order);
if (!$order->id) {
message_to_admin("Error while trying to insert new data", $order);
return "Insert record error. Admin has been notified!";
}
$extra = new stdClass();
$extra->x_bank_aba_code = $form->abacode;
$extra->x_bank_acct_num = $form->accnum;
$extra->x_bank_acct_type = $form->acctype;
$extra->x_echeck_type = $isbusinesschecking ? 'CCD' : 'WEB';
$extra->x_bank_name = $form->bankname;
$extra->x_currency_code = $curcost['currency'];
$extra->x_amount = $curcost['cost'];
$extra->x_first_name = $form->firstname;
$extra->x_last_name = $form->lastname;
$extra->x_country = $USER->country;
$extra->x_address = $USER->address;
$extra->x_city = $USER->city;
$extra->x_state = '';
$extra->x_zip = '';
$extra->x_invoice_num = $order->id;
$extra->x_description = $course->shortname;
$extra->x_cust_id = $USER->id;
$extra->x_email = $USER->email;
$extra->x_customer_ip = $useripno;
$extra->x_email_customer = empty($CFG->enrol_mailstudents) ? 'FALSE' : 'TRUE';
$extra->x_phone = '';
$extra->x_fax = '';
$message = '';
2008-03-03 16:04:32 +00:00
if (AN_REVIEW == AuthorizeNet::process($order, $message, $extra, AN_ACTION_AUTH_CAPTURE)) {
$SESSION->ccpaid = 1; // security check: don't duplicate payment
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
return NULL;
}
else {
message_to_admin($message, $order);
return $message;
}
2006-08-30 14:06:40 +00:00
}
2005-05-16 22:22:31 +00:00
2005-11-21 07:33:04 +00:00
/**
* Gets access icons.
*
* @param object $course
* @return string
* @access public
2005-11-21 07:33:04 +00:00
*/
2007-11-07 10:24:35 +00:00
public function get_access_icons($course)
{
$manual = enrolment_factory::factory('manual');
$str = $manual->get_access_icons($course);
$curcost = get_course_cost($course);
2005-11-21 07:33:04 +00:00
if (abs($curcost['cost']) > 0.00) {
$strrequirespayment = get_string("requirespayment");
$strcost = get_string("cost");
$currency = $curcost['currency'];
switch ($currency) {
case 'USD': $currency = 'US$'; break;
case 'CAD': $currency = 'C$'; break;
case 'EUR': $currency = '&euro;'; break;
case 'GBP': $currency = '&pound;'; break;
case 'JPY': $currency = '&yen;'; break;
}
2005-11-21 07:33:04 +00:00
$str .= '<div class="cost" title="'.$strrequirespayment.'">'.$strcost.': ';
$str .= $currency . ' ' . $curcost['cost'].'</div>';
}
2005-05-16 22:22:31 +00:00
2005-11-21 07:33:04 +00:00
return $str;
2005-05-16 22:22:31 +00:00
}
2005-11-21 07:33:04 +00:00
/**
* Shows config form & errors
*
* @param object $frm
* @access public
2005-11-21 07:33:04 +00:00
*/
2007-11-07 10:24:35 +00:00
public function config_form($frm)
{
2008-06-05 14:06:39 +00:00
global $CFG, $DB;
$mconfig = get_config('enrol/authorize');
2005-11-21 07:33:04 +00:00
2008-02-05 17:05:29 +00:00
if (!check_curl_available()) {
notify('PHP must be compiled with cURL+SSL support (--with-curl --with-openssl)');
2005-11-21 07:33:04 +00:00
}
2005-05-25 16:27:53 +00:00
if (empty($CFG->loginhttps) and substr($CFG->wwwroot, 0, 5) !== 'https') {
$a = new stdClass;
$a->url = "$CFG->wwwroot/$CFG->admin/settings.php?section=httpsecurity";
2007-02-09 07:47:14 +00:00
notify(get_string('adminconfighttps', 'enrol_authorize', $a));
return; // notice breaks the form and xhtml later
}
elseif (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 443) { // MDL-9836
$wwwsroot = qualified_me();
$wwwsroot = str_replace('http:', 'https:', $wwwsroot);
$a = new stdClass;
$a->url = $wwwsroot;
2007-02-09 07:47:14 +00:00
notify(get_string('adminconfighttpsgo', 'enrol_authorize', $a));
return; // notice breaks the form and xhtml later
}
2006-01-19 14:57:23 +00:00
if (optional_param('verifyaccount', 0, PARAM_INT)) {
notify(authorize_verify_account());
2008-01-16 17:19:30 +00:00
}
2006-01-19 14:57:23 +00:00
if (!empty($frm->an_review)) {
$captureday = intval($frm->an_capture_day);
$emailexpired = intval($frm->an_emailexpired);
if ($captureday > 0 || $emailexpired > 0) {
2008-06-05 14:18:21 +00:00
$lastcron = $DB->get_field_sql('SELECT max(lastcron) FROM {modules}');
if ((time() - intval($lastcron) > 3600 * 24)) {
2006-01-19 14:57:23 +00:00
notify(get_string('admincronsetup', 'enrol_authorize'));
}
}
}
2008-06-05 14:06:39 +00:00
if (($count = $DB->count_records('enrol_authorize', array('status'=>AN_STATUS_AUTH)))) {
$a = new stdClass;
2006-01-19 14:57:23 +00:00
$a->count = $count;
$a->url = $CFG->wwwroot."/enrol/authorize/index.php?status=".AN_STATUS_AUTH;
notify(get_string('adminpendingorders', 'enrol_authorize', $a));
}
2005-11-21 07:33:04 +00:00
if (data_submitted()) {
if (empty($mconfig->an_login)) {
2005-11-21 07:33:04 +00:00
notify("an_login required");
}
if (empty($mconfig->an_tran_key) && empty($mconfig->an_password)) {
2005-11-21 07:33:04 +00:00
notify("an_tran_key or an_password required");
}
}
2005-05-16 22:22:31 +00:00
include($CFG->dirroot.'/enrol/authorize/config_form.php');
2005-05-25 16:27:53 +00:00
}
2005-08-03 10:11:16 +00:00
2005-11-21 07:33:04 +00:00
/**
* process_config
*
* @param object $config
* @return bool true if it will be saved.
* @access public
2005-11-21 07:33:04 +00:00
*/
2007-11-07 10:24:35 +00:00
public function process_config($config)
{
2008-06-05 14:18:21 +00:00
global $CFG, $DB;
$mconfig = get_config('enrol/authorize');
// site settings
2006-07-25 17:38:32 +00:00
if (($cost = optional_param('enrol_cost', 5, PARAM_INT)) > 0) {
set_config('enrol_cost', $cost);
}
2006-01-19 14:57:23 +00:00
set_config('enrol_currency', optional_param('enrol_currency', 'USD', PARAM_ALPHA));
set_config('enrol_mailstudents', optional_param('enrol_mailstudents', 0, PARAM_BOOL));
set_config('enrol_mailteachers', optional_param('enrol_mailteachers', 0, PARAM_BOOL));
set_config('enrol_mailadmins', optional_param('enrol_mailadmins', 0, PARAM_BOOL));
// optional authorize.net settings
set_config('an_avs', optional_param('an_avs', 0, PARAM_BOOL));
set_config('an_authcode', optional_param('an_authcode', 0, PARAM_BOOL));
set_config('an_test', optional_param('an_test', 0, PARAM_BOOL));
2006-01-19 14:57:23 +00:00
set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL));
$acceptmethods = optional_param('acceptmethods', get_list_of_payment_methods(), PARAM_ALPHA);
Ported AUTHORIZE_ECHECK changes into HEAD. (cvs update -j HEAD -j AUTHORIZE_ECHECK) * New Feature (Authorize.net eCheck) Authorize.Net provides an exclusive, fully integrated electronic check payment method, eCheck.Net. Using eCheck.Net, merchants can accept and process payments from consumer and corporate bank accounts directly from their Web site or through the Authorize.Net Virtual Terminal. By accepting electronic checks, you expand the payment options available to new and existing customers, enhancing customer loyalty and potentially increasing sales. + Lower Fees - Lower rates than credit cards or PayPal. + More Efficient - eCheck.Net does everything online, eliminating the cost and inconvenience of manually processing paper checks and waiting for checks in the mail. + Fully Integrated Solution - No third-party integration required implementing eCheck.Net is easy for merchants already using the Authorize.Net Payment Gateway. + Integrated Reporting - Provides a combined view of all eCheck.Net and credit card payment transactions. Reconcile payment and billing activity using online reports and statements. + Ship Product Sooner - Improved up-front transaction validation that returns the status of transactions faster. + Security - Authorize.Net uses the latest 128-bit Secure Socket Layer (SSL) technology for secure Internet Protocol (IP) transactions. == TO DO == - Generate Echeck forms. - Show users a echeck option if admin enabled echeck method. - Allow admins/teachers to enrol a student using echeck method (FIX: role consept) ==========
2006-08-30 10:29:10 +00:00
set_config('an_acceptmethods', implode(',', $acceptmethods));
$acceptccs = optional_param('acceptccs', array_keys(get_list_of_creditcards()), PARAM_ALPHA);
set_config('an_acceptccs', implode(',', $acceptccs));
$acceptechecktypes = optional_param('acceptechecktypes', get_list_of_bank_account_types(), PARAM_ALPHA);
set_config('an_acceptechecktypes', implode(',', $acceptechecktypes));
$cutoff_hour = optional_param('an_cutoff_hour', 0, PARAM_INT);
$cutoff_min = optional_param('an_cutoff_min', 5, PARAM_INT);
set_config('an_cutoff', $cutoff_hour * 60 + $cutoff_min);
2006-01-19 14:57:23 +00:00
// cron depencies
$reviewval = optional_param('an_review', 0, PARAM_BOOL);
2006-01-19 14:57:23 +00:00
$captureday = optional_param('an_capture_day', 5, PARAM_INT);
$emailexpired = optional_param('an_emailexpired', 2, PARAM_INT);
$emailexpiredteacher = optional_param('an_emailexpiredteacher', 0, PARAM_BOOL);
$sorttype = optional_param('an_sorttype', 'ttl', PARAM_ALPHA);
2006-01-19 14:57:23 +00:00
$captureday = ($captureday > 29) ? 29 : (($captureday < 0) ? 0 : $captureday);
$emailexpired = ($emailexpired > 5) ? 5 : (($emailexpired < 0) ? 0 : $emailexpired);
if (!empty($reviewval) && ($captureday > 0 || $emailexpired > 0)) {
2008-06-05 14:18:21 +00:00
$lastcron = $DB->get_field_sql('SELECT max(lastcron) FROM {modules}');
if (time() - intval($lastcron) > 3600 * 24) {
return false;
}
2005-07-18 16:42:30 +00:00
}
2006-01-19 14:57:23 +00:00
set_config('an_review', $reviewval);
set_config('an_capture_day', $captureday);
set_config('an_emailexpired', $emailexpired);
set_config('an_emailexpiredteacher', $emailexpiredteacher);
set_config('an_sorttype', $sorttype);
2006-01-19 14:57:23 +00:00
// https and openssl library is required
2008-02-05 17:05:29 +00:00
if ((substr($CFG->wwwroot, 0, 5) !== 'https' and empty($CFG->loginhttps)) or !check_curl_available()) {
return false;
}
// REQUIRED fields;
// an_login
2006-01-19 14:57:23 +00:00
$loginval = optional_param('an_login', '');
if (empty($loginval) && empty($mconfig->an_login)) {
2006-08-30 14:16:45 +00:00
return false;
}
$loginval = !empty($loginval) ? rc4encrypt($loginval) : strval($mconfig->an_login);
set_config('an_login', $loginval, 'enrol/authorize');
// an_tran_key, an_password
2006-01-19 14:57:23 +00:00
$tranval = optional_param('an_tran_key', '');
$tranval = !empty($tranval) ? rc4encrypt($tranval) : (isset($mconfig->an_tran_key)?$mconfig->an_tran_key:'');
2006-01-19 14:57:23 +00:00
$passwordval = optional_param('an_password', '');
$passwordval = !empty($passwordval) ? rc4encrypt($passwordval) :(isset($mconfig->an_password)?$mconfig->an_password:'');
$deletecurrent = optional_param('delete_current', '0', PARAM_BOOL);
if (!empty($deletecurrent) and !empty($tranval)) {
unset_config('an_password', 'enrol/authorize');
$passwordval = '';
}
elseif (!empty($passwordval)) {
set_config('an_password', $passwordval, 'enrol/authorize');
}
if (empty($tranval) and empty($passwordval)) {
2006-01-19 14:57:23 +00:00
return false;
}
if (!empty($tranval)) {
set_config('an_tran_key', $tranval, 'enrol/authorize');
}
2006-01-19 14:57:23 +00:00
2005-11-24 13:07:35 +00:00
return true;
2005-05-25 16:27:53 +00:00
}
/**
* This function is run by admin/cron.php every time if admin has enabled this plugin.
*
* Everyday at settlement time (default is 00:05), it cleans up some tables
* and sends email to admin/teachers about pending orders expiring if manual-capture has enabled.
*
* If admin set up 'Order review' and 'Capture day', it captures credits cards and enrols students.
*
* @access public
*/
2007-11-07 10:24:35 +00:00
public function cron()
{
2008-06-05 14:06:39 +00:00
global $CFG, $DB;
2006-01-19 14:57:23 +00:00
$oneday = 86400;
2005-11-21 14:09:52 +00:00
$timenow = time();
2008-03-03 16:04:32 +00:00
$settlementtime = AuthorizeNet::getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);
2006-01-19 14:57:23 +00:00
$mconfig = get_config('enrol/authorize');
mtrace("Processing authorize cron...");
if (intval($mconfig->an_dailysettlement) < $settlementtime) {
set_config('an_dailysettlement', $settlementtime, 'enrol/authorize');
2008-02-05 17:05:29 +00:00
mtrace(" Daily cron:");
$this->cron_daily();
2008-02-05 17:05:29 +00:00
mtrace(" Done");
2006-01-19 14:57:23 +00:00
}
2008-02-05 17:05:29 +00:00
mtrace(" Scheduled capture", ": ");
if (empty($CFG->an_review) or (!empty($CFG->an_test)) or (intval($CFG->an_capture_day) < 1) or (!check_curl_available())) {
mtrace("disabled");
return; // order review disabled or test mode or manual capture or openssl wasn't loaded.
}
2005-08-24 14:59:42 +00:00
$timediffcnf = $settlementtime - (intval($CFG->an_capture_day) * $oneday);
2008-06-05 14:06:39 +00:00
$select = "(status = ?) AND (timecreated < ?) AND (timecreated > ?)";
$params = array(AN_STATUS_AUTH, $timediffcnf, $timediff30);
if (!($ordercount = $DB->count_records_select('enrol_authorize', $select, $params))) {
mtrace("no pending orders");
return;
}
2006-01-19 14:57:23 +00:00
$eachconn = intval($mconfig->an_eachconnsecs);
$eachconn = (($eachconn > 60) ? 60 : (($eachconn <= 0) ? 3 : $eachconn));
2006-01-19 14:57:23 +00:00
if (($ordercount * $eachconn) + intval($mconfig->an_lastcron) > $timenow) {
mtrace("blocked");
2006-01-19 14:57:23 +00:00
return;
}
set_config('an_lastcron', $timenow, 'enrol/authorize');
mtrace(" $ordercount orders are being processed now", ": ");
$faults = '';
2006-01-02 09:45:07 +00:00
$sendem = array();
$elapsed = time();
@set_time_limit(0);
$this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
2006-03-09 11:35:54 +00:00
2006-09-22 16:19:53 +00:00
$lastcourseid = 0;
$rs = $DB->get_recordset_select('enrol_authorize', $select, $params, 'courseid');
2008-06-05 14:06:39 +00:00
foreach ( $rs as $order)
{
$message = '';
$extra = NULL;
2008-03-03 16:04:32 +00:00
if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
2006-09-22 16:19:53 +00:00
if ($lastcourseid != $order->courseid) {
$lastcourseid = $order->courseid;
2008-06-05 14:06:39 +00:00
$course = $DB->get_record('course', array('id'=>$lastcourseid));
2006-09-22 16:19:53 +00:00
$role = get_default_course_role($course);
$context = get_context_instance(CONTEXT_COURSE, $lastcourseid);
}
2006-09-22 14:04:11 +00:00
$timestart = $timeend = 0;
if ($course->enrolperiod) {
$timestart = $timenow;
$timeend = $order->settletime + $course->enrolperiod;
}
2008-06-05 14:06:39 +00:00
$user = $DB->get_record('user', array('id'=>$order->userid));
2008-02-05 17:34:36 +00:00
if (role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, 'authorize')) {
2006-09-22 14:04:11 +00:00
$this->log .= "User($user->id) has been enrolled to course($course->id).\n";
2006-01-19 14:57:23 +00:00
if (!empty($CFG->enrol_mailstudents)) {
$sendem[] = $order->id;
}
}
else {
2006-09-22 14:04:11 +00:00
$faults .= "Error while trying to enrol ".fullname($user)." in '$course->fullname' \n";
foreach ($order as $okey => $ovalue) {
$faults .= " $okey = $ovalue\n";
}
}
}
2006-01-19 14:57:23 +00:00
else {
$this->log .= "Error, Order# $order->id: " . $message . "\n";
}
}
2008-06-05 14:06:39 +00:00
$rs->close();
mtrace("processed");
$timenow = time();
$elapsed = $timenow - $elapsed;
2006-01-19 14:57:23 +00:00
$eachconn = ceil($elapsed / $ordercount);
set_config('an_eachconnsecs', $eachconn, 'enrol/authorize');
$this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate($timenow);
$adminuser = get_admin();
if (!empty($faults)) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $adminuser;
$eventdata->subject = "AUTHORIZE.NET CRON FAULTS";
$eventdata->fullmessage = $faults;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
}
if (!empty($CFG->enrol_mailadmins)) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $adminuser;
$eventdata->subject = "AUTHORIZE.NET CRON LOG";
$eventdata->fullmessage = $this->log;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
}
// Send emails to students about which courses have enrolled.
if (!empty($sendem)) {
mtrace(" sending welcome messages to students", ": ");
send_welcome_messages($sendem);
mtrace("sent");
2006-01-02 09:45:07 +00:00
}
}
/**
* Daily cron. It executes at settlement time (default is 00:05).
*
* @access private
*/
2007-11-07 10:24:35 +00:00
private function cron_daily()
{
2008-06-05 14:06:39 +00:00
global $CFG, $SITE, $DB;
$oneday = 86400;
$timenow = time();
2006-11-20 14:48:16 +00:00
$onepass = $timenow - $oneday;
2008-03-03 16:04:32 +00:00
$settlementtime = AuthorizeNet::getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);
2008-06-05 14:06:39 +00:00
$select = "(status=?) AND (timecreated<?)";
$params = array(AN_STATUS_NONE, $timediff30);
if ($DB->delete_records_select('enrol_authorize', $select, $params)) {
2008-02-05 17:05:29 +00:00
mtrace(" orders no transaction made have deleted");
}
2008-06-05 14:06:39 +00:00
$select = "(status=?) AND (timecreated<?)";
$params = array(AN_STATUS_EXPIRE, AN_STATUS_AUTH, $timediff30);
if ($DB->execute("UPDATE {enrol_authorize} SET status=? WHERE $select", $params)) {
2008-02-05 17:05:29 +00:00
mtrace(" pending orders to expire have updated");
}
$timediff60 = $settlementtime - (60 * $oneday);
2008-06-05 14:06:39 +00:00
$select = "(status=?) AND (timecreated<?)";
$params = array(AN_STATUS_EXPIRE, $timediff60);
2008-06-05 14:06:39 +00:00
if ($DB->delete_records_select('enrol_authorize', $select, $params)) {
2008-02-05 17:05:29 +00:00
mtrace(" orders expired older than 60 days have deleted");
}
2006-11-20 14:48:16 +00:00
$adminuser = get_admin();
2008-06-05 14:06:39 +00:00
$select = "status IN(?,?) AND (timecreated<?) AND (timecreated>?)";
$params = array(AN_STATUS_UNDERREVIEW, AN_STATUS_APPROVEDREVIEW, $onepass, $timediff60);
2008-06-05 14:06:39 +00:00
if (($count = $DB->count_records_select('enrol_authorize', $select, $params)) &&
2008-02-05 17:05:29 +00:00
($csvusers = get_users_by_capability(get_context_instance(CONTEXT_SYSTEM), 'enrol/authorize:uploadcsv'))) {
2006-11-20 14:48:16 +00:00
$a = new stdClass;
$a->count = $count;
$a->course = $SITE->shortname;
$subject = get_string('pendingechecksubject', 'enrol_authorize', $a);
$a = new stdClass;
$a->count = $count;
$a->url = $CFG->wwwroot.'/enrol/authorize/uploadcsv.php';
$message = get_string('pendingecheckemail', 'enrol_authorize', $a);
2008-02-05 17:05:29 +00:00
foreach($csvusers as $csvuser) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $csvuser;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
2008-02-05 17:05:29 +00:00
}
mtrace(" users who have 'enrol/authorize:uploadcsv' were mailed");
2006-11-20 14:48:16 +00:00
}
2008-02-05 17:05:29 +00:00
mtrace(" early pending order warning email for manual capture", ": ");
if (empty($CFG->an_emailexpired)) {
2008-02-05 17:05:29 +00:00
mtrace("not enabled");
return;
}
2008-02-05 17:05:29 +00:00
$timediffem = $settlementtime - ((30 - intval($CFG->an_emailexpired)) * $oneday);
2008-06-05 14:06:39 +00:00
$select = "(status=?) AND (timecreated<?) AND (timecreated>?)";
$params = array(AN_STATUS_AUTH, $timediffem, $timediff30);
$count = $DB->count_records_select('enrol_authorize', $select, $params);
if (!$count) {
2008-02-05 17:05:29 +00:00
mtrace("no orders prior to $CFG->an_emailexpired days");
return;
}
2008-02-05 17:05:29 +00:00
mtrace("$count orders prior to $CFG->an_emailexpired days");
$a = new stdClass;
$a->pending = $count;
$a->days = $CFG->an_emailexpired;
$a->course = $SITE->shortname;
$subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
$a = new stdClass;
$a->pending = $count;
$a->days = $CFG->an_emailexpired;
$a->course = $SITE->fullname;
$a->enrolurl = "$CFG->wwwroot/$CFG->admin/enrol_config.php?enrol=authorize";
$a->url = $CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH;
$message = get_string('pendingordersemail', 'enrol_authorize', $a);
2008-08-07 11:42:09 +00:00
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $adminuser;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
2008-08-07 11:42:09 +00:00
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
2008-02-05 17:05:29 +00:00
// Email to payment managers
if (empty($CFG->an_emailexpiredteacher)) {
return; // email feature disabled for teachers.
}
$sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype;
$sql = "SELECT e.courseid, e.currency, c.fullname, c.shortname,
COUNT(e.courseid) AS cnt, SUM(e.amount) as ttl
2008-06-05 14:06:39 +00:00
FROM {enrol_authorize} e
INNER JOIN {course} c ON c.id = e.courseid
WHERE (e.status = ?)
AND (e.timecreated < ?)
AND (e.timecreated > ?)
GROUP BY e.courseid
ORDER BY $sorttype DESC";
2008-06-05 14:06:39 +00:00
$params = array(AN_STATUS_AUTH, $timediffem, $timediff30);
2008-06-05 14:06:39 +00:00
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $courseinfo)
{
$lastcourse = $courseinfo->courseid;
$context = get_context_instance(CONTEXT_COURSE, $lastcourse);
2008-02-05 17:05:29 +00:00
if (($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments'))) {
$a = new stdClass;
$a->course = $courseinfo->shortname;
$a->pending = $courseinfo->cnt;
$a->days = $CFG->an_emailexpired;
$subject = get_string('pendingorderssubject', 'enrol_authorize', $a);
$a = new stdClass;
$a->course = $courseinfo->fullname;
$a->pending = $courseinfo->cnt;
$a->currency = $courseinfo->currency;
$a->sumcost = $courseinfo->ttl;
$a->days = $CFG->an_emailexpired;
$a->url = $CFG->wwwroot.'/enrol/authorize/index.php?course='.$lastcourse.'&amp;status='.AN_STATUS_AUTH;
$message = get_string('pendingordersemailteacher', 'enrol_authorize', $a);
foreach ($paymentmanagers as $paymentmanager) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $paymentmanager;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
2008-08-07 11:42:09 +00:00
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
}
}
2006-01-02 09:45:07 +00:00
}
2008-06-05 14:06:39 +00:00
$rs->close();
}
}
?>