moodle/enrol/authorize/enrol.php

1013 lines
36 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');
/**
* enrolment_plugin_authorize
*
*/
class enrolment_plugin_authorize
{
2005-11-21 07:33:04 +00:00
/**
2006-08-30 14:06:40 +00:00
* Credit card and Echeck error messages.
2005-11-21 07:33:04 +00:00
*
2006-01-19 14:57:23 +00:00
* @var array
2005-12-14 15:47:37 +00:00
* @access public
2005-11-21 07:33:04 +00:00
*/
2006-08-30 14:06:40 +00:00
var $authorizeerrors = array();
2005-11-21 07:33:04 +00:00
2005-12-14 15:47:37 +00:00
/**
* Cron log.
*
* @var string
* @access public
*/
var $log;
/**
* Returns information about the courses a student has access to
*
* Set the $user->student course array
* Set the $user->timeaccess course array
*
* @param object &$user must contain $user->id already set
*/
function get_student_courses(&$user) {
$manual = enrolment_factory::factory('manual');
$manual->get_student_courses($user);
}
/**
* Returns information about the courses a teacher has access to
*
* Set the $user->teacher course array
* Set the $user->teacheredit course array
* Set the $user->timeaccess course array
*
* @param object &$user must contain $user->id already set
*/
function get_teacher_courses(&$user) {
$manual = enrolment_factory::factory('manual');
$manual->get_teacher_courses($user);
}
2005-11-21 07:33:04 +00:00
/**
* Shows a credit card form for registration.
*
* @param object $course Course info
* @access public
2005-11-21 07:33:04 +00:00
*/
function print_entry($course) {
2005-11-21 07:33:04 +00:00
global $CFG, $USER, $form;
$zerocost = enrolment_plugin_authorize::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-01-19 14:57:23 +00:00
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off') {
2005-11-21 07:33:04 +00:00
if (empty($CFG->loginhttps)) {
2006-01-19 14:57:23 +00:00
error(get_string('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
}
httpsrequired();
2006-01-19 14:57:23 +00:00
$strcourses = get_string('courses');
$strloginto = get_string('loginto', '', $course->shortname);
2006-08-30 14:06:40 +00:00
print_header($strloginto,
$course->fullname,
"<a href=\"$CFG->wwwroot/course/\">$strcourses</a> -> $strloginto");
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
}
print_simple_box_start('center');
2006-06-29 19:07:28 +00:00
if (isguest()) {
$curcost = enrolment_plugin_authorize::get_course_cost($course);
echo '<div align="center">';
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>';
} else {
include($CFG->dirroot.'/enrol/authorize/enrol.html');
}
print_simple_box_end();
if ($course->password) {
$password = '';
$teacher = get_teacher($course->id);
include($CFG->dirroot.'/enrol/manual/enrol.html');
}
2005-11-21 07:33:04 +00:00
print_footer();
}
2005-11-21 07:33:04 +00:00
/**
* Checks form params.
*
* @param object $form Form parameters
* @param object $course Course info
* @access public
2005-11-21 07:33:04 +00:00
*/
2006-08-30 14:06:40 +00:00
function check_entry($form, $course)
{
global $CFG;
if (enrolment_plugin_authorize::zero_cost($course) or
2006-08-30 14:06:40 +00:00
(!empty($course->password) and !empty($form->password))) { // MANUAL ENROLMENT
$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
}
else { // AUTHORIZE.NET ENROLMENT
$paymentmethodsenabled = enrolment_plugin_authorize::get_list_of_payment_methods();
if (in_array(AN_METHOD_CC, $paymentmethodsenabled) and
!empty($form->ccsubmit) and
$this->validate_cc_form($form)) {
$this->cc_submit($form, $course);
}
elseif (in_array(AN_METHOD_ECHECK, $paymentmethodsenabled) and
!empty($form->echecksubmit) and
$this->validate_echeck_form($form)) {
$this->echeck_submit($form, $course);
}
2005-11-21 07:33:04 +00:00
}
2005-05-16 22:22:31 +00:00
}
2005-05-26 13:22:01 +00:00
2005-11-21 07:33:04 +00:00
/**
* Credit card number mode.
* Send to authorize.net.
*
* @param object $form Form parameters
* @param object $course Course info
* @access private
*/
function cc_submit($form, $course)
{
2005-11-21 07:33:04 +00:00
global $CFG, $USER, $SESSION;
require_once('authorizenetlib.php');
enrolment_plugin_authorize::prevent_double_paid($course);
$useripno = getremoteaddr();
$curcost = enrolment_plugin_authorize::get_course_cost($course);
$exp_date = sprintf("%02d", $form->ccexpiremm) . $form->ccexpireyyyy;
// NEW ORDER
$timenow = time();
$order = new stdClass();
$order->cclastfour = substr($form->cc, -4);
$order->ccname = $form->ccfirstname . " " . $form->cclastname;
$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'];
$order->id = insert_record("enrol_authorize", $order);
if (!$order->id) {
enrolment_plugin_authorize::email_to_admin("Error while trying to insert new data", $order);
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['header'] = "Insert record error. Admin has been notified!";
2005-11-21 14:09:52 +00:00
return;
}
$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'];
$extra->x_first_name = $form->ccfirstname;
$extra->x_last_name = $form->cclastname;
$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 = '';
2005-12-22 15:24:05 +00:00
$message = '';
$an_review = !empty($CFG->an_review);
$action = $an_review ? AN_ACTION_AUTH_ONLY : AN_ACTION_AUTH_CAPTURE;
2006-08-31 07:11:31 +00:00
$success = authorize_action($order, $message, $extra, $action, AN_METHOD_CC, $form->cctype);
2005-12-22 15:24:05 +00:00
if (!$success) {
enrolment_plugin_authorize::email_to_admin($message, $order);
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['header'] = $message;
2005-12-22 15:24:05 +00:00
return;
}
2006-01-19 14:57:23 +00:00
$SESSION->ccpaid = 1; // security check: don't duplicate payment
if ($order->transid == 0) { // TEST MODE
2006-01-05 14:30:49 +00:00
if ($an_review) {
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
}
else {
$timestart = $timenow;
$timeend = $timestart + (3600 * 24); // just enrol for 1 days :)
enrol_student($USER->id, $course->id, $timestart, $timeend, 'manual');
2006-01-05 14:30:49 +00:00
redirect("$CFG->wwwroot/course/view.php?id=$course->id");
}
return;
}
if ($an_review) { // review enabled, inform site payment managers and redirect the user who have paid to main page.
$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(authorize_getsettletime($timenow + (30 * 3600 * 24)));
$a->captureon = userdate(authorize_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_SYSTEM, SITEID);
if ($sitepaymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments')) {
foreach ($sitepaymentmanagers as $sitepaymentmanager) {
email_to_user($sitepaymentmanager, $USER, $emailsubject, $emailmessage);
}
2005-11-21 07:33:04 +00:00
}
2005-12-22 15:24:05 +00:00
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
return;
}
// Credit card captured, ENROL student now...
2005-12-22 15:24:05 +00:00
if ($course->enrolperiod) {
$timestart = $timenow;
$timeend = $timestart + $course->enrolperiod;
} else {
$timestart = $timeend = 0;
}
if (enrol_student($USER->id, $course->id, $timestart, $timeend, 'manual')) {
2005-12-22 15:24:05 +00:00
$teacher = get_teacher($course->id);
if (!empty($CFG->enrol_mailstudents)) {
$a = new stdClass;
2005-12-22 15:24:05 +00:00
$a->coursename = "$course->fullname";
$a->profileurl = "$CFG->wwwroot/user/view.php?id=$USER->id";
email_to_user($USER,
$teacher,
get_string("enrolmentnew", '', $course->shortname),
get_string('welcometocoursetext', '', $a));
}
if (!empty($CFG->enrol_mailteachers)) {
$a = new stdClass;
2005-12-22 15:24:05 +00:00
$a->course = "$course->fullname";
$a->user = fullname($USER);
email_to_user($teacher,
$USER,
get_string("enrolmentnew", '', $course->shortname),
get_string('enrolmentnewuser', '', $a));
}
if (!empty($CFG->enrol_mailadmins)) {
$a = new stdClass;
2005-12-22 15:24:05 +00:00
$a->course = "$course->fullname";
$a->user = fullname($USER);
$admins = get_admins();
foreach ($admins as $admin) {
email_to_user($admin,
2005-11-24 13:07:35 +00:00
$USER,
get_string("enrolmentnew", '', $course->shortname),
get_string('enrolmentnewuser', '', $a));
2005-11-21 07:33:04 +00:00
}
}
2005-12-22 15:24:05 +00:00
} else {
enrolment_plugin_authorize::email_to_admin("Error while trying to enrol " .
fullname($USER) . " in '$course->fullname'", $order);
2005-12-22 15:24:05 +00:00
}
2005-11-21 14:09:52 +00:00
2005-12-22 15:24:05 +00:00
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl; unset($SESSION->wantsurl);
2005-11-21 14:09:52 +00:00
} else {
2005-12-22 15:24:05 +00:00
$destination = "$CFG->wwwroot/course/view.php?id=$course->id";
2005-11-21 14:09:52 +00:00
}
2005-12-22 15:24:05 +00:00
redirect($destination);
2005-11-21 07:33:04 +00:00
}
2005-07-16 15:15:41 +00:00
2006-08-30 14:06:40 +00:00
function echeck_submit($form, $course)
{
global $CFG, $USER, $SESSION;
require_once('authorizenetlib.php');
enrolment_plugin_authorize::prevent_double_paid($course);
$useripno = getremoteaddr();
$curcost = enrolment_plugin_authorize::get_course_cost($course);
return; // not implemented yet
2006-08-30 14:06:40 +00:00
}
function validate_cc_form($form)
2006-01-19 14:57:23 +00:00
{
global $CFG;
require_once('ccval.php');
2006-01-19 14:57:23 +00:00
if (empty($form->cc)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cc'] = get_string('missingcc', 'enrol_authorize');
}
if (empty($form->ccexpiremm) || empty($form->ccexpireyyyy)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['ccexpire'] = get_string('missingccexpire', 'enrol_authorize');
2006-01-19 14:57:23 +00:00
}
else {
$expdate = sprintf("%02d", intval($form->ccexpiremm)) . $form->ccexpireyyyy;
$validcc = CCVal($form->cc, $form->cctype, $expdate);
if (!$validcc) {
if ($validcc === 0) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['ccexpire'] = get_string('ccexpired', 'enrol_authorize');
}
else {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cc'] = get_string('ccinvalid', 'enrol_authorize');
}
}
}
2006-01-19 14:57:23 +00:00
if (empty($form->ccfirstname) || empty($form->cclastname)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['ccfirstlast'] = get_string('missingfullname');
2006-01-19 14:57:23 +00:00
}
if (empty($form->cvv) || !is_numeric($form->cvv)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cvv'] = get_string('missingcvv', 'enrol_authorize');
}
if (empty($form->cctype) or
!in_array($form->cctype, array_keys(enrolment_plugin_authorize::get_list_of_creditcards()))) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cctype'] = get_string('missingcctype', 'enrol_authorize');
}
2006-01-19 14:57:23 +00:00
if (!empty($CFG->an_avs)) {
if (empty($form->ccaddress)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['ccaddress'] = get_string('missingaddress', 'enrol_authorize');
}
if (empty($form->cccity)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cccity'] = get_string('missingcity');
}
if (empty($form->cccountry)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cccountry'] = get_string('missingcountry');
}
}
if (empty($form->cczip) || !is_numeric($form->cczip)) {
2006-08-30 14:06:40 +00:00
$this->authorizeerrors['cczip'] = get_string('missingzip', 'enrol_authorize');
2006-01-19 14:57:23 +00:00
}
2006-08-30 14:06:40 +00:00
if (!empty($this->authorizeerrors)) {
$this->authorizeerrors['header'] = get_string('someerrorswerefound');
return false;
2006-01-19 14:57:23 +00:00
}
return true;
2006-01-19 14:57:23 +00:00
}
2006-08-30 14:06:40 +00:00
function validate_echeck_form($form)
{
global $CFG;
require_once('abaval.php');
2006-08-30 14:06:40 +00:00
2006-08-31 07:11:31 +00:00
if (empty($form->abacode) || !is_numeric($form->abacode)) {
$this->authorizeerrors['abacode'] = get_string('missingaba', 'enrol_authorize');
}
elseif (!ABAVal($form->abacode)) {
$this->authorizeerrors['abacode'] = get_string('invalidaba', 'enrol_authorize');
}
if (empty($form->accnum) || !is_numeric($form->accnum)) {
$this->authorizeerrors['accnum'] = get_string('invalidaccnum', 'enrol_authorize');
}
if (empty($form->acctype) || !in_array($form->acctype, array('CHECKING','BUSINESSCHECKING','SAVINGS'))) {
$this->authorizeerrors['acctype'] = get_string('invalidacctype', 'enrol_authorize');
}
if (empty($form->bankname)) {
$this->authorizeerrors['bankname'] = get_string('missingbankname', 'enrol_authorize');
}
if (empty($form->firstname) || empty($form->lastname)) {
$this->authorizeerrors['firstlast'] = get_string('missingfullname');
}
if (!empty($this->authorizeerrors)) {
$this->authorizeerrors['header'] = get_string('someerrorswerefound');
return false;
}
2006-08-30 14:06:40 +00:00
return true;
}
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
*/
function get_access_icons($course) {
$manual = enrolment_factory::factory('manual');
$str = $manual->get_access_icons($course);
$curcost = enrolment_plugin_authorize::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
*/
function config_form($frm)
{
2005-11-21 07:33:04 +00:00
global $CFG;
if (! enrolment_plugin_authorize::check_openssl_loaded()) {
2005-11-21 07:33:04 +00:00
notify('PHP must be compiled with SSL support (--with-openssl)');
}
2005-05-25 16:27:53 +00:00
if (empty($CFG->loginhttps) and substr($CFG->wwwroot, 0, 5) !== 'https') {
2006-04-24 16:31:02 +00:00
notify('loginhttps must be ON');
}
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) {
$mconfig = get_config('enrol/authorize');
if ((time() - intval($mconfig->an_lastcron) > 3600 * 24)) {
notify(get_string('admincronsetup', 'enrol_authorize'));
}
}
}
2006-01-19 14:57:23 +00:00
if ($count = count_records('enrol_authorize', '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($frm->an_login)) {
notify("an_login required");
}
if (empty($frm->an_tran_key) && empty($frm->an_password)) {
notify("an_tran_key or an_password required");
}
}
2005-05-16 22:22:31 +00:00
include($CFG->dirroot.'/enrol/authorize/config.html');
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
*/
function process_config($config)
{
2005-11-21 07:33:04 +00:00
global $CFG;
// 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_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));
2006-08-30 14:06:40 +00:00
$acceptmethods = optional_param('acceptmethods',
enrolment_plugin_authorize::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(enrolment_plugin_authorize::get_list_of_creditcards()),
PARAM_ALPHA);
set_config('an_acceptccs', implode(',', $acceptccs));
$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);
$mconfig = get_config('enrol/authorize');
if ((!empty($reviewval)) &&
($captureday > 0 || $emailexpired > 0) &&
(time() - intval($mconfig->an_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
if ((substr($CFG->wwwroot, 0, 5) !== 'https' and empty($CFG->loginhttps)) or
!enrolment_plugin_authorize::check_openssl_loaded()) {
return false;
}
// required fields
2006-01-19 14:57:23 +00:00
$loginval = optional_param('an_login', '');
if (empty($loginval)) {
2006-08-30 14:16:45 +00:00
return false;
}
set_config('an_login', $loginval);
2006-01-19 14:57:23 +00:00
$tranval = optional_param('an_tran_key', '');
$passwordval = optional_param('an_password', '');
$deletecurrent = optional_param('delete_current', '');
2006-01-19 14:57:23 +00:00
if (!empty($passwordval)) { // password is changing
set_config('an_password', $passwordval);
}
elseif (!empty($deletecurrent) and !empty($tranval)) {
set_config('an_password', '');
$CFG->an_password = '';
}
if (empty($tranval) and empty($CFG->an_password)) {
2006-01-19 14:57:23 +00:00
return false;
}
set_config('an_tran_key', $tranval);
2005-11-24 13:07:35 +00:00
return true;
2005-05-25 16:27:53 +00:00
}
/**
2006-08-30 14:33:31 +00:00
* Whether a course cost is smaller than 0.01
*
2006-08-30 14:33:31 +00:00
* @param object $course Course information
* @return bool true if the course is free cost
* @static
*/
function zero_cost($course) {
$curcost = enrolment_plugin_authorize::get_course_cost($course);
return (abs($curcost['cost']) < 0.01);
}
/**
2006-08-30 14:33:31 +00:00
* Gets course cost
*
* @param object $course
2006-08-30 14:33:31 +00:00
* @return array cost=>'cost', currency=>'currency'
* @static
*/
function get_course_cost($course)
{
global $CFG;
$cost = (float)0;
$currency = (!empty($course->currency))
? $course->currency :( empty($CFG->enrol_currency)
? 'USD' : $CFG->enrol_currency );
if (!empty($course->cost)) {
$cost = (float)(((float)$course->cost) < 0) ? $CFG->enrol_cost : $course->cost;
}
$cost = format_float($cost, 2);
$ret = array('cost' => $cost, 'currency' => $currency);
return $ret;
}
/**
2006-08-30 14:33:31 +00:00
* Sends email to main admin.
* FIXME: Admin ROLES
*
* @param string $subject
* @param mixed $data
* @static
*/
function email_to_admin($subject, $data)
{
global $SITE;
$admin = get_admin();
$data = (array)$data;
$message = "$SITE->fullname: Transaction failed.\n\n$subject\n\n";
$message .= print_r($data, true);
email_to_user($admin, $admin, "$SITE->fullname: Authorize.net ERROR", $message);
2005-05-16 22:22:31 +00:00
}
/**
* prevent_double_paid (static method)
*
* @param object $course
* @static
*/
function prevent_double_paid($course)
{
global $CFG, $SESSION, $USER;
2005-05-16 22:22:31 +00:00
$status = empty($CFG->an_test) ? AN_STATUS_AUTH : AN_STATUS_NONE;
if ($rec=get_record('enrol_authorize','userid',$USER->id,'courseid',$course->id,'status',$status,'id')) {
$a = new stdClass;
$a->orderid = $rec->id;
2006-03-13 17:57:12 +00:00
$a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid";
redirect($a->url, get_string("paymentpending", "enrol_authorize", $a), '10');
return;
}
2005-11-21 07:33:04 +00:00
if (isset($SESSION->ccpaid)) {
unset($SESSION->ccpaid);
redirect($CFG->wwwroot . '/login/logout.php');
return;
}
2005-05-16 22:22:31 +00:00
}
/**
* check_openssl_loaded (static method)
*
* @return bool
* @static
*/
2005-11-21 07:33:04 +00:00
function check_openssl_loaded() {
return extension_loaded('openssl');
2005-05-16 22:22:31 +00:00
}
/**
2006-08-30 14:33:31 +00:00
* Gets list of credits cards
*
2006-08-30 14:33:31 +00:00
* @param bool $getall, true get all of types, false config values
* @return array, Key => Value
* @static
*/
function get_list_of_creditcards($getall = false)
{
global $CFG;
$alltypes = array(
'mcd' => 'Master Card',
'vis' => 'Visa',
'amx' => 'American Express',
'dsc' => 'Discover',
'dnc' => 'Diners Club',
'jcb' => 'JCB',
'swi' => 'Switch',
'dlt' => 'Delta',
'enr' => 'EnRoute'
);
if ($getall or empty($CFG->an_acceptccs)) {
return $alltypes;
}
$ret = array();
$ccs = explode(',', $CFG->an_acceptccs);
foreach ($ccs as $key) {
$ret[$key] = $alltypes[$key];
}
return $ret;
}
2006-08-30 14:33:31 +00:00
/**
* Gets lists of payment methods (CC,ECHECK)
*
* @param bool $getall, get all of types, false config values
* @return array, Key only
* @static
*/
2006-08-30 14:06:40 +00:00
function get_list_of_payment_methods($getall = false)
{
global $CFG;
if ($getall) {
2006-08-30 14:33:31 +00:00
return array(AN_METHOD_CC, AN_METHOD_ECHECK);
2006-08-30 14:06:40 +00:00
}
2006-08-30 14:33:31 +00:00
elseif (empty($CFG->an_acceptmethods)) {
return array(AN_METHOD_CC); // default
2006-08-30 14:06:40 +00:00
}
else {
2006-08-30 14:33:31 +00:00
return explode(',', $CFG->an_acceptmethods);
2006-08-30 14:06:40 +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
*/
function cron()
{
global $CFG, $SITE;
require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
2006-01-19 14:57:23 +00:00
$oneday = 86400;
2005-11-21 14:09:52 +00:00
$timenow = time();
$settlementtime = authorize_getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);
2006-01-19 14:57:23 +00:00
$mconfig = get_config('enrol/authorize');
set_config('an_lastcron', $timenow, 'enrol/authorize');
mtrace("Processing authorize cron...");
if (intval($mconfig->an_dailysettlement) < $settlementtime) {
set_config('an_dailysettlement', $settlementtime, 'enrol/authorize');
mtrace(" daily cron; some cleanups and sending email to admins the count of pending orders expiring", ": ");
$this->cron_daily();
mtrace("done");
2006-01-19 14:57:23 +00:00
}
mtrace(" scheduled capture", ": ");
if (empty($CFG->an_review) or
(!empty($CFG->an_test)) or
(intval($CFG->an_capture_day) < 1) or
(!enrolment_plugin_authorize::check_openssl_loaded())) {
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);
2005-12-28 16:53:23 +00:00
$sql = "SELECT E.*, C.fullname, C.enrolperiod " .
"FROM {$CFG->prefix}enrol_authorize E " .
"INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
"WHERE (E.status = '" .AN_STATUS_AUTH. "') " .
2005-12-28 16:53:23 +00:00
" AND (E.timecreated < '$timediffcnf') AND (E.timecreated > '$timediff30')";
if (!$orders = get_records_sql($sql)) {
mtrace("no pending orders");
return;
}
2006-01-19 14:57:23 +00:00
$eachconn = intval($mconfig->an_eachconnsecs);
if (empty($eachconn)) $eachconn = 3;
elseif ($eachconn > 60) $eachconn = 60;
$ordercount = count((array)$orders);
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;
}
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
foreach ($orders as $order) {
$message = '';
$extra = NULL;
2006-08-31 07:11:31 +00:00
$success = authorize_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE, AN_METHOD_CC);
if ($success) {
$timestart = $timeend = 0;
2005-12-28 16:53:23 +00:00
if ($order->enrolperiod) {
$timestart = $timenow;
$timeend = $order->settletime + $order->enrolperiod;
}
if (enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'manual')) {
$this->log .= "User($order->userid) has been enrolled to course($order->courseid).\n";
2006-01-19 14:57:23 +00:00
if (!empty($CFG->enrol_mailstudents)) {
$sendem[] = $order->id;
}
}
else {
2006-01-02 09:45:07 +00:00
$user = get_record('user', 'id', $order->userid);
$faults .= "Error while trying to enrol ".fullname($user)." in '$order->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";
}
}
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)) {
email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON FAULTS", $faults);
}
if (!empty($CFG->enrol_mailadmins)) {
email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
}
// Send emails to students about which courses have enrolled.
2006-01-19 14:57:23 +00:00
if (empty($sendem)) {
2006-01-02 09:45:07 +00:00
return;
}
mtrace(" sending welcome messages to students", ": ");
2006-01-02 09:45:07 +00:00
$select = "SELECT E.id, E.courseid, E.userid, C.fullname " .
"FROM {$CFG->prefix}enrol_authorize E " .
"INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
"WHERE E.id IN(" . implode(',', $sendem) . ") " .
"ORDER BY E.userid";
$emailinfo = get_records_sql($select);
$emailcount = count($emailinfo);
for($i = 0; $i < $emailcount; ) {
$usercourses = array();
$lastuserid = $emailinfo[$i]->userid;
for ($j=$i; $j < $emailcount and $emailinfo[$j]->userid == $lastuserid; $j++) {
$usercourses[] = $emailinfo[$j]->fullname;
2006-01-02 09:45:07 +00:00
}
$a = new stdClass;
$a->courses = implode("\n", $usercourses);
$a->profileurl = "$CFG->wwwroot/user/view.php?id=$lastuserid";
$a->paymenturl = "$CFG->wwwroot/enrol/authorize/index.php?user=$lastuserid";
$emailmessage = get_string('welcometocoursesemail', 'enrol_authorize', $a);
$user = get_record('user', 'id', $lastuserid);
email_to_user($user,
$adminuser,
get_string("enrolmentnew", '', $SITE->shortname),
$emailmessage);
$i = $j;
}
mtrace("sent");
}
/**
* Daily cron. It executes at settlement time (default is 00:05).
*
* @access private
*/
function cron_daily()
{
global $CFG, $SITE;
require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');
$oneday = 86400;
$timenow = time();
$settlementtime = authorize_getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);
// Delete orders that no transaction was made.
$select = "(status='".AN_STATUS_NONE."') AND (timecreated<'$timediff30')";
delete_records_select('enrol_authorize', $select);
// Pending orders are expired with in 30 days.
$select = "(status='".AN_STATUS_AUTH."') AND (timecreated<'$timediff30')";
execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='".AN_STATUS_EXPIRE."' WHERE $select", false);
// Delete expired orders 60 days later.
$timediff60 = $settlementtime - (60 * $oneday);
$select = "(status='".AN_STATUS_EXPIRE."') AND (timecreated<'$timediff60')";
delete_records_select('enrol_authorize', $select);
// Daily warning email for pending orders expiring.
if (empty($CFG->an_emailexpired)) {
return; // not enabled
}
// Pending orders count will be expired.
$timediffem = $settlementtime - ((30 - intval($CFG->an_emailexpired)) * $oneday);
$select = "(status='". AN_STATUS_AUTH ."') AND (timecreated<'$timediffem') AND (timecreated>'$timediff30')";
$count = count_records_select('enrol_authorize', $select);
if (!$count) {
return;
}
// Email to admin
$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/users.php";
$a->url = $CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH;
$message = get_string('pendingordersemail', 'enrol_authorize', $a);
$adminuser = get_admin();
email_to_user($adminuser, $adminuser, $subject, $message);
// Email to teachers
if (empty($CFG->an_emailexpiredteacher)) {
return; // email feature disabled for teachers.
}
$sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype;
$where = "(E.status='". AN_STATUS_AUTH ."') AND (E.timecreated<'$timediffem') AND (E.timecreated>'$timediff30')";
$sql = "SELECT E.courseid, E.currency, C.fullname, C.shortname, " .
"COUNT(E.courseid) AS cnt, SUM(E.amount) as ttl " .
"FROM {$CFG->prefix}enrol_authorize E " .
"INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
"WHERE $where GROUP BY E.courseid ORDER BY $sorttype DESC";
$courseinfos = get_records_sql($sql);
foreach($courseinfos as $courseinfo) {
$lastcourse = $courseinfo->courseid;
$context = get_context_instance(CONTEXT_COURSE, $lastcourse);
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) {
email_to_user($paymentmanager, $adminuser, $subject, $message);
}
}
2006-01-02 09:45:07 +00:00
}
}
}
?>