2005-05-13 09:05:39 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
// Authorize.net
|
|
|
|
define('AN_HOST', 'secure.authorize.net');
|
2005-11-21 14:09:52 +00:00
|
|
|
define('AN_HOST_TEST', 'certification.authorize.net');
|
2005-05-13 09:05:39 +00:00
|
|
|
define('AN_PORT', 443);
|
|
|
|
define('AN_PATH', '/gateway/transact.dll');
|
|
|
|
define('AN_APPROVED', '1');
|
|
|
|
define('AN_DECLINED', '2');
|
|
|
|
define('AN_ERROR', '3');
|
2005-05-16 15:37:11 +00:00
|
|
|
define('AN_DELIM', '|');
|
|
|
|
define('AN_ENCAP', '"');
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-11-21 14:09:52 +00:00
|
|
|
/**
|
|
|
|
* New order. No transaction was made.
|
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
define('AN_STATUS_NONE', 0x00);
|
2005-11-21 14:09:52 +00:00
|
|
|
/**
|
|
|
|
* Authorized.
|
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
define('AN_STATUS_AUTH', 0x01);
|
2005-11-21 14:09:52 +00:00
|
|
|
/**
|
|
|
|
* Captured.
|
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
define('AN_STATUS_CAPTURE', 0x02);
|
2005-12-22 15:24:05 +00:00
|
|
|
/**
|
|
|
|
* Auth_Captured.
|
|
|
|
*/
|
|
|
|
define('AN_STATUS_AUTHCAPTURE', AN_STATUS_AUTH|AN_STATUS_CAPTURE);
|
2005-12-12 17:32:00 +00:00
|
|
|
/**
|
|
|
|
* Refunded.
|
|
|
|
*/
|
|
|
|
define('AN_STATUS_CREDIT', 0x04);
|
|
|
|
/**
|
|
|
|
* Voided.
|
|
|
|
*/
|
|
|
|
define('AN_STATUS_VOID', 0x08);
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* Expired.
|
|
|
|
*/
|
2005-12-12 17:32:00 +00:00
|
|
|
define('AN_STATUS_EXPIRE', 0x10);
|
2005-11-21 14:09:52 +00:00
|
|
|
|
2005-05-13 09:05:39 +00:00
|
|
|
require_once("$CFG->dirroot/enrol/enrol.class.php");
|
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* enrolment_plugin_authorize
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class enrolment_plugin extends enrolment_base
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
/**
|
|
|
|
* Credit card error message.
|
|
|
|
*
|
|
|
|
* @var string
|
2005-12-14 15:47:37 +00:00
|
|
|
* @access public
|
2005-11-21 07:33:04 +00:00
|
|
|
*/
|
|
|
|
var $ccerrormsg;
|
|
|
|
|
2005-12-14 15:47:37 +00:00
|
|
|
/**
|
|
|
|
* Cron log.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $log;
|
|
|
|
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
/**
|
|
|
|
* Shows a credit card form for registration.
|
|
|
|
*
|
|
|
|
* @param object $course Course info
|
2005-12-09 12:00:28 +00:00
|
|
|
* @access public
|
2005-11-21 07:33:04 +00:00
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
function print_entry($course)
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
global $CFG, $USER, $form;
|
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
if ($this->zero_cost($course) || isguest()) { // No money for guests ;)
|
2005-11-21 07:33:04 +00:00
|
|
|
parent::print_entry($course);
|
|
|
|
return;
|
2005-08-05 15:05:05 +00:00
|
|
|
}
|
2005-07-18 16:42:30 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
// check payment
|
2005-12-09 12:00:28 +00:00
|
|
|
$this->prevent_double_paid($course);
|
2005-11-21 07:33:04 +00:00
|
|
|
|
2005-12-14 15:47:37 +00:00
|
|
|
// I want to pay on SSL.
|
2005-11-21 07:33:04 +00:00
|
|
|
if (empty($_SERVER['HTTPS'])) {
|
|
|
|
if (empty($CFG->loginhttps)) {
|
|
|
|
error(get_string("httpsrequired", "enrol_authorize"));
|
|
|
|
} else {
|
|
|
|
$wwwsroot = str_replace('http://','https://', $CFG->wwwroot);
|
|
|
|
$sdestination = "$wwwsroot/course/enrol.php?id=$course->id";
|
|
|
|
redirect($sdestination);
|
|
|
|
exit;
|
|
|
|
}
|
2005-08-03 10:11:16 +00:00
|
|
|
}
|
2005-07-13 20:26:16 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$formvars = array('password','ccfirstname','cclastname','cc','ccexpiremm','ccexpireyyyy','cctype','cvv','cczip');
|
|
|
|
foreach ($formvars as $var) {
|
|
|
|
if (!isset($form->$var)) {
|
|
|
|
$form->$var = '';
|
|
|
|
}
|
|
|
|
}
|
2005-07-13 20:26:16 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$teacher = get_teacher($course->id);
|
|
|
|
$strloginto = get_string("loginto", "", $course->shortname);
|
|
|
|
$strcourses = get_string("courses");
|
|
|
|
$userfirstname = empty($form->ccfirstname) ? $USER->firstname : $form->ccfirstname;
|
|
|
|
$userlastname = empty($form->cclastname) ? $USER->lastname : $form->cclastname;
|
|
|
|
$curcost = $this->get_course_cost($course);
|
2005-07-13 20:26:16 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
print_header($strloginto, $course->fullname, "<a href=\"$CFG->wwwroot/course/\">$strcourses</a> -> $strloginto");
|
|
|
|
print_course($course, "80%");
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
if ($course->password) {
|
|
|
|
print_simple_box(get_string('choosemethod', 'enrol_authorize'), 'center');
|
|
|
|
$password = '';
|
|
|
|
include($CFG->dirroot . '/enrol/internal/enrol.html');
|
|
|
|
}
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
print_simple_box_start("center");
|
|
|
|
include($CFG->dirroot . '/enrol/authorize/enrol.html');
|
|
|
|
print_simple_box_end();
|
2005-05-16 22:22:31 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
print_footer();
|
2005-08-01 14:34:30 +00:00
|
|
|
}
|
2005-12-09 12:00:28 +00:00
|
|
|
|
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
/**
|
|
|
|
* Checks form params.
|
|
|
|
*
|
|
|
|
* @param object $form Form parameters
|
|
|
|
* @param object $course Course info
|
2005-12-09 12:00:28 +00:00
|
|
|
* @access public
|
2005-11-21 07:33:04 +00:00
|
|
|
*/
|
|
|
|
function check_entry($form, $course) {
|
|
|
|
if ($this->zero_cost($course) || isguest() || (!empty($form->password))) {
|
|
|
|
parent::check_entry($form, $course);
|
|
|
|
} else {
|
|
|
|
$this->cc_submit($form, $course);
|
|
|
|
}
|
2005-05-16 22:22:31 +00:00
|
|
|
}
|
2005-05-26 13:22:01 +00:00
|
|
|
|
2005-12-12 17:32:00 +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
|
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
function cc_submit($form, $course)
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
global $CFG, $USER, $SESSION;
|
|
|
|
require_once($CFG->dirroot . '/enrol/authorize/ccval.php');
|
2005-12-09 12:00:28 +00:00
|
|
|
require_once($CFG->dirroot . '/enrol/authorize/action.php');
|
2005-11-21 07:33:04 +00:00
|
|
|
|
|
|
|
if (empty($form->ccfirstname) || empty($form->cclastname) ||
|
|
|
|
empty($form->cc) || empty($form->cvv) || empty($form->cctype) ||
|
|
|
|
empty($form->ccexpiremm) || empty($form->ccexpireyyyy) || empty($form->cczip)) {
|
|
|
|
$this->ccerrormsg = get_string("allfieldsrequired");
|
|
|
|
return;
|
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-26 19:21:37 +00:00
|
|
|
if (!empty($CFG->an_test)) {
|
|
|
|
error("Credit card module cannot be present because of test mode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
$this->prevent_double_paid($course);
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$exp_date = ($form->ccexpiremm < 10) ? strval('0'.$form->ccexpiremm) : strval($form->ccexpiremm);
|
|
|
|
$exp_date .= $form->ccexpireyyyy;
|
|
|
|
$valid_cc = CCVal($form->cc, $form->cctype, $exp_date);
|
|
|
|
$curcost = $this->get_course_cost($course);
|
|
|
|
$useripno = getremoteaddr(); // HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR, REMOTE_ADDR
|
2005-06-02 17:56:31 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
if (!$valid_cc) {
|
|
|
|
$this->ccerrormsg = get_string( (($valid_cc===0) ? 'ccexpired' : 'ccinvalid'), 'enrol_authorize' );
|
2005-07-25 15:03:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
// 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.
|
2005-12-09 12:00:28 +00:00
|
|
|
$order->timecreated = $timenow;
|
2005-12-12 17:42:05 +00:00
|
|
|
$order->amount = $curcost['cost'];
|
|
|
|
$order->currency = $curcost['currency'];
|
2005-12-09 12:00:28 +00:00
|
|
|
$order->id = insert_record("enrol_authorize", $order);
|
|
|
|
if (!$order->id) {
|
|
|
|
$this->email_to_admin("Error while trying to insert new data", $order);
|
2005-11-21 14:09:52 +00:00
|
|
|
$this->ccerrormsg = "Insert record error. Admin has been notified!";
|
|
|
|
return;
|
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
$extra = new stdClass();
|
|
|
|
$extra->x_first_name = $form->ccfirstname;
|
|
|
|
$extra->x_last_name = $form->cclastname;
|
|
|
|
$extra->x_address = $USER->address;
|
|
|
|
$extra->x_city = $USER->city;
|
|
|
|
$extra->x_zip = $form->cczip;
|
|
|
|
$extra->x_country = $USER->country;
|
|
|
|
$extra->x_state = '';
|
|
|
|
$extra->x_card_num = $form->cc;
|
|
|
|
$extra->x_card_code = $form->cvv;
|
|
|
|
$extra->x_currency_code = $curcost['currency'];
|
|
|
|
$extra->x_amount = $curcost['cost'];
|
|
|
|
$extra->x_exp_date = $exp_date;
|
|
|
|
$extra->x_email = $USER->email;
|
|
|
|
$extra->x_email_customer = 'TRUE';
|
|
|
|
$extra->x_cust_id = $USER->id;
|
|
|
|
$extra->x_customer_ip = $useripno;
|
|
|
|
$extra->x_phone = '';
|
|
|
|
$extra->x_fax = '';
|
|
|
|
$extra->x_invoice_num = $order->id;
|
|
|
|
$extra->x_description = $course->shortname;
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-22 15:24:05 +00:00
|
|
|
$message = '';
|
2005-12-09 12:00:28 +00:00
|
|
|
$an_review = !empty($CFG->an_review);
|
2005-12-12 17:32:00 +00:00
|
|
|
$action = $an_review ? AN_ACTION_AUTH_ONLY : AN_ACTION_AUTH_CAPTURE;
|
2005-12-26 19:21:37 +00:00
|
|
|
$success = authorizenet_action($order, $message, $extra, $action);
|
2005-12-22 15:24:05 +00:00
|
|
|
if (!$success) {
|
|
|
|
$this->email_to_admin($message, $order);
|
|
|
|
$this->ccerrormsg = $message;
|
|
|
|
return;
|
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-26 19:21:37 +00:00
|
|
|
if (intval($order->transid) == 0) { // I know it is test mode. :)
|
|
|
|
error("Credit card module cannot be present because of test mode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-12-22 15:24:05 +00:00
|
|
|
$SESSION->ccpaid = 1; // security check: don't duplicate payment
|
|
|
|
if ($an_review) { // review enabled, inform admin and redirect to main page.
|
|
|
|
if (update_record("enrol_authorize", $order)) {
|
2005-12-26 19:21:37 +00:00
|
|
|
$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 = getsettletime($timenow + (30 * 3600 * 24));
|
|
|
|
$a->captureon = getsettletime($timenow + (intval($CFG->an_review_day) * 3600 * 24));
|
|
|
|
$a->course = $course->fullname;
|
|
|
|
$a->user = fullname($USER);
|
|
|
|
$a->acstatus = ($CFG->an_review_day > 0) ? get_string('yes') : get_string('no');
|
|
|
|
$emailmessage = get_string('adminneworder', 'enrol_authorize', $a);
|
|
|
|
$a->course = $course->shortname;
|
|
|
|
$a->orderid = $order->id;
|
|
|
|
$emailsubject = get_string('adminnewordersubject', 'enrol_authorize', $a);
|
|
|
|
$admins = get_admins();
|
|
|
|
foreach ($admins as $admin) {
|
|
|
|
email_to_user($admin, $USER, $emailsubject, $emailmessage);
|
|
|
|
}
|
2005-11-21 14:09:52 +00:00
|
|
|
}
|
2005-12-22 15:24:05 +00:00
|
|
|
else {
|
|
|
|
$this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
|
|
|
|
"ID=$order->id in enrol_authorize table.", $order);
|
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;
|
|
|
|
}
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-12-22 15:24:05 +00:00
|
|
|
// credit card captured, ENROL student...
|
|
|
|
if (!update_record("enrol_authorize", $order)) {
|
|
|
|
$this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
|
|
|
|
"ID=$order->id in enrol_authorize table.", $order);
|
|
|
|
// no error occured??? enrol student??? return??? Database busy???
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($course->enrolperiod) {
|
|
|
|
$timestart = $timenow;
|
|
|
|
$timeend = $timestart + $course->enrolperiod;
|
|
|
|
} else {
|
|
|
|
$timestart = $timeend = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enrol_student($USER->id, $course->id, $timestart, $timeend, 'authorize')) {
|
|
|
|
$teacher = get_teacher($course->id);
|
|
|
|
if (!empty($CFG->enrol_mailstudents)) {
|
|
|
|
$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->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->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 {
|
|
|
|
$this->email_to_admin("Error while trying to enrol ".fullname($USER)." in '$course->fullname'", $order);
|
|
|
|
}
|
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
|
|
|
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* zero_cost
|
|
|
|
*
|
|
|
|
* @param unknown_type $course
|
|
|
|
* @return number
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-11-21 07:33:04 +00:00
|
|
|
function zero_cost($course) {
|
|
|
|
$curcost = $this->get_course_cost($course);
|
|
|
|
return (abs($curcost['cost']) < 0.01);
|
2005-07-14 16:17:28 +00:00
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* get_course_cost
|
|
|
|
*
|
|
|
|
* @param unknown_type $course
|
|
|
|
* @return unknown
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function get_course_cost($course)
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
global $CFG;
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$cost = (float)0;
|
|
|
|
$currency = (!empty($course->currency))
|
|
|
|
? $course->currency :( empty($CFG->enrol_currency)
|
|
|
|
? 'USD' : $CFG->enrol_currency );
|
2005-07-14 16:17:28 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
if (!empty($course->cost)) {
|
|
|
|
$cost = (float)(((float)$course->cost) < 0) ? $CFG->enrol_cost : $course->cost;
|
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$cost = format_float($cost, 2);
|
|
|
|
$ret = array('cost' => $cost, 'currency' => $currency);
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
2005-05-16 22:22:31 +00:00
|
|
|
|
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
/**
|
|
|
|
* Gets access icons.
|
|
|
|
*
|
|
|
|
* @param object $course
|
|
|
|
* @return string
|
2005-12-09 12:00:28 +00:00
|
|
|
* @access public
|
2005-11-21 07:33:04 +00:00
|
|
|
*/
|
|
|
|
function get_access_icons($course) {
|
|
|
|
|
|
|
|
$str = parent::get_access_icons($course);
|
|
|
|
$curcost = $this->get_course_cost($course);
|
|
|
|
|
|
|
|
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 = '€'; break;
|
|
|
|
case 'GBP': $currency = '£'; break;
|
|
|
|
case 'JPY': $currency = '¥'; break;
|
|
|
|
}
|
2005-08-01 14:34:30 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$str .= '<div class="cost" title="'.$strrequirespayment.'">'.$strcost.': ';
|
|
|
|
$str .= $currency . ' ' . $curcost['cost'].'</div>';
|
2005-08-01 14:34:30 +00:00
|
|
|
}
|
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-08-01 14:34:30 +00:00
|
|
|
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
/**
|
|
|
|
* Shows config form & errors
|
|
|
|
*
|
|
|
|
* @param object $frm
|
2005-12-09 12:00:28 +00:00
|
|
|
* @access public
|
2005-11-21 07:33:04 +00:00
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
function config_form($frm)
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!$this->check_openssl_loaded()) {
|
|
|
|
notify('PHP must be compiled with SSL support (--with-openssl)');
|
|
|
|
}
|
2005-05-25 16:27:53 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
if (data_submitted()) {
|
|
|
|
// something POSTed, Some required fields
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
if (empty($CFG->loginhttps)) {
|
|
|
|
notify("\$CFG->loginhttps must be ON");
|
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
// ******************* AUTOCAPTURE *******************
|
2005-11-24 13:07:35 +00:00
|
|
|
if (!(empty($frm->an_review) || $frm->an_review_day < 1)) {
|
2005-11-21 07:33:04 +00:00
|
|
|
// ++ENABLED++
|
|
|
|
// Cron must be runnig!!! Check last cron...
|
2005-12-26 19:21:37 +00:00
|
|
|
$mconfig = get_config('enrol/authorize');
|
|
|
|
$lastcron = intval($mconfig->an_lastcron);
|
2005-11-21 07:33:04 +00:00
|
|
|
if (time() - $lastcron > 3600 * 24) {
|
|
|
|
// Cron must be enabled if you want to use autocapture feature.
|
|
|
|
// Setup cron or disable an_review again...
|
|
|
|
// Otherwise, transactions will be cancelled unless you review it within 30 days.
|
|
|
|
notify(get_string('cronwarning', 'admin'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// --DISABLED--
|
|
|
|
// Cron will NOT run anymore, because autocapture runs with cron.
|
|
|
|
// Transactions with AN_STATUS_AUTH will be cancelled and we can display this warning to admin!
|
|
|
|
// Admin can check (Accept|Deny) new transactions manually.
|
2005-12-09 12:00:28 +00:00
|
|
|
|
|
|
|
if ($count = count_records('enrol_authorize', 'status', AN_STATUS_AUTH)) {
|
2005-12-14 15:47:37 +00:00
|
|
|
notify("CRON DISABLED. TRANSACTIONS WITH A STATUS OF AN_STATUS_AUTH WILL BE CANCELLED UNLESS YOU CHECK IT. TOTAL $count");
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
2005-11-21 07:33:04 +00:00
|
|
|
}
|
|
|
|
// ***************************************************
|
2005-07-13 20:26:16 +00:00
|
|
|
}
|
2005-05-16 22:22:31 +00:00
|
|
|
|
2005-11-21 07:33:04 +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-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
/**
|
|
|
|
* process_config
|
|
|
|
*
|
|
|
|
* @param object $config
|
|
|
|
* @return bool true if it will be saved.
|
2005-12-09 12:00:28 +00:00
|
|
|
* @access public
|
2005-11-21 07:33:04 +00:00
|
|
|
*/
|
2005-12-09 12:00:28 +00:00
|
|
|
function process_config($config)
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
global $CFG;
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
// ENROL config
|
|
|
|
set_config('enrol_cost', optional_param('enrol_cost', 5, PARAM_INT) );
|
|
|
|
set_config('enrol_currency', optional_param('enrol_currency', 'USD', PARAM_ALPHA) );
|
|
|
|
set_config('enrol_mailstudents', optional_param('enrol_mailstudents', '') );
|
|
|
|
set_config('enrol_mailteachers', optional_param('enrol_mailteachers', '') );
|
|
|
|
set_config('enrol_mailadmins', optional_param('enrol_mailadmins', '') );
|
|
|
|
|
|
|
|
// AUTHORIZE.NET config
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
// not required!
|
|
|
|
set_config('an_test', optional_param('an_test', '') );
|
|
|
|
set_config('an_referer', optional_param('an_referer', 'http://', PARAM_URL) );
|
2005-12-22 15:24:05 +00:00
|
|
|
set_config('an_cutoff_hour', optional_param('an_cutoff_hour', '0') );
|
|
|
|
set_config('an_cutoff_min', optional_param('an_cutoff_min', '5') );
|
2005-11-21 07:33:04 +00:00
|
|
|
|
|
|
|
// required!
|
|
|
|
// if is it OK, process next config.
|
|
|
|
if (empty($CFG->loginhttps)) return false;
|
|
|
|
if (!$this->check_openssl_loaded()) return false;
|
|
|
|
|
|
|
|
$login_val = optional_param('an_login', '');
|
|
|
|
if (empty($login_val)) return false;
|
|
|
|
set_config('an_login', $login_val);
|
|
|
|
|
|
|
|
$tran_val = optional_param('an_tran_key', '');
|
2005-12-09 12:00:28 +00:00
|
|
|
$password_val = optional_param('an_password', '');
|
2005-11-21 07:33:04 +00:00
|
|
|
if (empty($tran_val) && empty($password_val)) return false;
|
|
|
|
set_config('an_password', $password_val);
|
|
|
|
set_config('an_tran_key', $tran_val);
|
|
|
|
|
|
|
|
// an_review & an_review_day & cron depencies...
|
|
|
|
$review_val = optional_param('an_review', '');
|
|
|
|
if (empty($review_val)) {
|
|
|
|
// review disabled. cron is not required. AUTH_CAPTURE works.
|
|
|
|
set_config('an_review', $review_val);
|
|
|
|
} else {
|
|
|
|
// review enabled.
|
|
|
|
$review_day_val = optional_param('an_review_day', 5, PARAM_INT);
|
2005-11-24 13:07:35 +00:00
|
|
|
if ($review_day_val < 0) $review_day_val = 0;
|
|
|
|
elseif ($review_day_val > 29) $review_day_val = 29;
|
2005-11-21 07:33:04 +00:00
|
|
|
if ($review_day_val > 0) {
|
2005-12-26 19:21:37 +00:00
|
|
|
// Cron must change an_lastcron. :))
|
|
|
|
$mconfig = get_config('enrol/authorize');
|
|
|
|
$lastcron = intval($mconfig->an_lastcron);
|
2005-11-21 07:33:04 +00:00
|
|
|
if (time() - $lastcron > 3600 * 24) {
|
|
|
|
// No!!! I am not lucky. No changes please...
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_config('an_review', $review_val);
|
|
|
|
set_config('an_review_day', $review_day_val);
|
2005-07-18 16:42:30 +00:00
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-11-24 13:07:35 +00:00
|
|
|
return true;
|
2005-05-25 16:27:53 +00:00
|
|
|
}
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* email_to_admin
|
|
|
|
*
|
|
|
|
* @param string $subject
|
|
|
|
* @param mixed $data
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-11-21 07:33:04 +00:00
|
|
|
function email_to_admin($subject, $data) {
|
|
|
|
$admin = get_admin();
|
|
|
|
$site = get_site();
|
2005-12-09 12:00:28 +00:00
|
|
|
$data = (array)$data;
|
2005-05-13 09:05:39 +00:00
|
|
|
|
2005-11-21 07:33:04 +00:00
|
|
|
$message = "$site->fullname: Transaction failed.\n\n$subject\n\n";
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$message .= "$key => $value\n";
|
|
|
|
}
|
|
|
|
email_to_user($admin, $admin, "CC ERROR: ".$subject, $message);
|
2005-05-16 22:22:31 +00:00
|
|
|
}
|
2005-12-12 17:32:00 +00:00
|
|
|
|
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* prevent_double_paid
|
|
|
|
*
|
|
|
|
* @param object $course
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function prevent_double_paid($course)
|
|
|
|
{
|
|
|
|
global $CFG, $SESSION, $USER;
|
2005-05-16 22:22:31 +00:00
|
|
|
|
2005-12-26 19:21:37 +00:00
|
|
|
if ($rec = get_record('enrol_authorize', 'userid', $USER->id, 'courseid', $course->id, 'status', AN_STATUS_AUTH, 'id')) {
|
|
|
|
$a->orderid = $rec->id;
|
|
|
|
redirect($CFG->wwwroot, get_string("paymentpending", "enrol_authorize", $a), '20');
|
|
|
|
return;
|
|
|
|
}
|
2005-11-21 07:33:04 +00:00
|
|
|
if (isset($SESSION->ccpaid)) {
|
|
|
|
unset($SESSION->ccpaid);
|
|
|
|
redirect($CFG->wwwroot . '/login/logout.php');
|
2005-12-09 12:00:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-16 22:22:31 +00:00
|
|
|
}
|
2005-05-16 15:37:11 +00:00
|
|
|
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* check_openssl_loaded
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @access private
|
|
|
|
*/
|
2005-11-21 07:33:04 +00:00
|
|
|
function check_openssl_loaded() {
|
|
|
|
return extension_loaded('openssl');
|
2005-05-16 22:22:31 +00:00
|
|
|
}
|
2005-05-16 15:37:11 +00:00
|
|
|
|
2005-12-12 17:32:00 +00:00
|
|
|
|
2005-12-09 12:00:28 +00:00
|
|
|
/**
|
|
|
|
* cron
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function cron()
|
|
|
|
{
|
2005-11-21 07:33:04 +00:00
|
|
|
global $CFG;
|
|
|
|
parent::cron();
|
2005-12-22 15:24:05 +00:00
|
|
|
require_once("$CFG->dirroot/enrol/authorize/action.php");
|
2005-12-09 12:00:28 +00:00
|
|
|
|
2005-11-21 14:09:52 +00:00
|
|
|
$timenow = time();
|
2005-12-22 15:24:05 +00:00
|
|
|
$timenowsettle = getsettletime($timenow);
|
|
|
|
$timediff30 = $timenowsettle - (30 * 3600 * 24);
|
2005-12-26 19:21:37 +00:00
|
|
|
// These 2 lines must be HERE and must be EXUCUTED. See process_config.
|
|
|
|
// We use an_lastcron when processing AUTOCAPTURE feature.
|
|
|
|
// Order is important. 1. get_config 2. set_config
|
|
|
|
$mconfig = get_config('enrol/authorize'); // MUST be 1st.
|
|
|
|
set_config('an_lastcron', $timenow, 'enrol/authorize'); // MUST be 2nd.
|
2005-12-09 12:00:28 +00:00
|
|
|
|
2005-12-26 19:21:37 +00:00
|
|
|
$random100 = mt_rand(0, 100);
|
|
|
|
|
|
|
|
if ($random100 < 33) {
|
|
|
|
$select = "(status = '" .AN_STATUS_NONE. "') AND (timecreated < '$timediff30')";
|
|
|
|
delete_records_select('enrol_authorize', $select);
|
|
|
|
}
|
|
|
|
elseif ($random100 > 66) {
|
|
|
|
$select = "(status = '" .AN_STATUS_AUTH. "') AND (timecreated < '$timediff30')";
|
|
|
|
execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status = '" .AN_STATUS_EXPIRE. "' WHERE $select", false);
|
2005-11-21 14:09:52 +00:00
|
|
|
}
|
2005-12-26 19:21:37 +00:00
|
|
|
else {
|
|
|
|
$timediff60 = $timenowsettle - (60 * 3600 * 24);
|
|
|
|
$select = "(status = '" .AN_STATUS_EXPIRE. "') AND (timecreated < '$timediff60')";
|
|
|
|
delete_records_select('enrol_authorize', $select);
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
2005-11-21 14:09:52 +00:00
|
|
|
|
2005-12-26 19:21:37 +00:00
|
|
|
if (!empty($CFG->an_test)) {
|
|
|
|
return; // AUTOCAPTURE doesn't work in test mode.
|
|
|
|
}
|
2005-12-09 12:00:28 +00:00
|
|
|
if (empty($CFG->an_review) || empty($CFG->an_review_day) || $CFG->an_review_day < 1) {
|
2005-12-26 19:21:37 +00:00
|
|
|
return; // AUTOCAPTURE disabled. admin, teacher review it manually
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
2005-08-24 14:59:42 +00:00
|
|
|
|
2005-12-26 19:21:37 +00:00
|
|
|
// AUTO-CAPTURE: Transaction must be captured within 30 days. Otherwise it will expired.
|
2005-12-22 15:24:05 +00:00
|
|
|
$timediffcnf = $timenowsettle - (intval($CFG->an_review_day) * 3600 * 24);
|
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 (status = '" .AN_STATUS_AUTH. "') " .
|
|
|
|
" AND (E.timecreated < '$timediffcnf') AND (E.timecreated > '$timediff30')";
|
|
|
|
|
|
|
|
if (!$orders = get_records_sql($sql)) {
|
2005-12-26 19:21:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate connection speed for each transaction. Default: 3 secs.
|
|
|
|
$everyconnection = empty($mconfig->an_eachconnsecs) ? 3 : intval($mconfig->an_eachconnsecs);
|
|
|
|
$ordercount = count((array)$orders);
|
|
|
|
$maxsecs = $everyconnection * $ordercount;
|
|
|
|
if ($maxsecs + intval($mconfig->an_lastcron) > $timenow) {
|
|
|
|
return; // autocapture runs every eachconnsecs*count.
|
|
|
|
}
|
|
|
|
|
|
|
|
$faults = '';
|
|
|
|
$elapsed = time();
|
|
|
|
@set_time_limit(0);
|
|
|
|
$this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
$message = '';
|
|
|
|
$extra = NULL;
|
|
|
|
$oldstatus = $order->status;
|
|
|
|
$success = authorizenet_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
|
|
|
|
if ($success) {
|
|
|
|
if (!update_record("enrol_authorize", $order)) {
|
|
|
|
$this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
|
|
|
|
"ID=$order->id in enrol_authorize table.", $order);
|
|
|
|
}
|
|
|
|
$timestart = $timeend = 0;
|
2005-12-28 16:53:23 +00:00
|
|
|
if ($order->enrolperiod) {
|
|
|
|
$timestart = $timenow;
|
|
|
|
$timeend = $order->settletime + $order->enrolperiod;
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
2005-12-26 19:21:37 +00:00
|
|
|
if (enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'authorize')) {
|
|
|
|
$this->log .= "User($order->userid) has been enrolled to course($order->courseid).\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$faults .= "Error while trying to enrol ".fullname($USER)." in '$course->fullname' \n";
|
|
|
|
foreach ($order as $okey => $ovalue) {
|
|
|
|
$faults .= " $okey = $ovalue\n";
|
|
|
|
}
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-26 19:21:37 +00:00
|
|
|
else { // not success
|
|
|
|
$this->log .= "Order $order->id: " . $message . "\n";
|
|
|
|
if ($order->status != $oldstatus) { //expired
|
|
|
|
update_record("enrol_authorize", $order);
|
|
|
|
}
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-26 19:21:37 +00:00
|
|
|
|
|
|
|
$timenow = time();
|
|
|
|
$elapsed = $timenow - $elapsed;
|
|
|
|
$everyconnection = ceil($elapsed / $ordercount);
|
|
|
|
set_config('an_eachconnsecs', $everyconnection, '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);
|
|
|
|
}
|
2005-12-09 12:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|