Skip to content

Commit

Permalink
Code readability & documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ethem committed Mar 3, 2008
1 parent c42c63c commit 9088b7d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 92 deletions.
2 changes: 1 addition & 1 deletion enrol/authorize/config_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<td align="right">&nbsp;&nbsp;</td>
<td><?php print_string("logininfo", "enrol_authorize") ?><br />
<?php if (!optional_param('verifyaccount', 0, PARAM_INT) && isset($mconfig->an_login) && (isset($mconfig->an_tran_key) || isset($mconfig->an_password))) { ?>
<br /><a href="enrol_config.php?enrol=authorize&verifyaccount=1"><b><?php print_string("verifyaccount", "enrol_authorize") ?></b></a><br />
<br /><a href="enrol_config.php?enrol=authorize&amp;verifyaccount=1"><b><?php print_string("verifyaccount", "enrol_authorize") ?></b></a><br />
<?php } ?></td>
</tr>

Expand Down
199 changes: 108 additions & 91 deletions enrol/authorize/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once($CFG->dirroot.'/enrol/enrol.class.php');
require_once($CFG->dirroot.'/enrol/authorize/const.php');
require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
require_once($CFG->dirroot.'/enrol/authorize/authorizenet.class.php');

/**
* Authorize.net Payment Gateway plugin
Expand Down Expand Up @@ -141,12 +142,12 @@ public function check_entry($form, $course)
*
* @param object $form Form parameters
* @param object $course Course info
* @return string NULL if ok, error message otherwise.
* @access private
*/
private function cc_submit($form, $course)
{
global $CFG, $USER, $SESSION;
require_once('authorizenetlib.php');

prevent_double_paid($course);

Expand Down Expand Up @@ -198,102 +199,118 @@ private function cc_submit($form, $course)
$extra->x_phone = '';
$extra->x_fax = '';

$revieworder = false;
$action = AN_ACTION_AUTH_CAPTURE;

if (!empty($CFG->an_authcode) && !empty($form->ccauthcode)) {
$action = AN_ACTION_CAPTURE_ONLY;
$extra->x_auth_code = $form->ccauthcode;
}
elseif (!empty($CFG->an_review)) {
$revieworder = true;
$action = AN_ACTION_AUTH_ONLY;
}
else {
$action = AN_ACTION_AUTH_CAPTURE;
}

$message = '';
if (AN_APPROVED != authorize_action($order, $message, $extra, $action, $form->cctype)) {
email_to_admin($message, $order);
return $message;
}
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) {
email_to_user($paymentmanager, $USER, $emailsubject, $emailmessage);
}
}
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
break;
}

$SESSION->ccpaid = 1; // security check: don't duplicate payment
if ($order->transid == 0) { // TEST MODE
if ($revieworder) {
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
}
else {
enrol_into_course($course, $USER, 'authorize');
redirect("$CFG->wwwroot/course/view.php?id=$course->id");
}
return;
}
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);
email_to_user(
$paymentmanager,
$USER,
get_string("enrolmentnew", '', format_string($course->shortname)),
get_string('enrolmentnewuser', '', $a)
);
}
if (!empty($CFG->enrol_mailadmins)) {
$a = new stdClass;
$a->course = "$course->fullname";
$a->user = fullname($USER);
$admins = get_admins();
foreach ($admins as $admin) {
email_to_user(
$admin,
$USER,
get_string("enrolmentnew", '', format_string($course->shortname)),
get_string('enrolmentnewuser', '', $a)
);
}
}
}
else
{
email_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
}

if ($revieworder) { // review enabled, inform 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_COURSE, $course->id);
if (($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments'))) {
foreach ($paymentmanagers as $paymentmanager) {
email_to_user($paymentmanager, $USER, $emailsubject, $emailmessage);
}
}
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
return;
}
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl;
unset($SESSION->wantsurl);
} else {
$destination = "$CFG->wwwroot/course/view.php?id=$course->id";
}

// 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);
email_to_user($paymentmanager,
$USER,
get_string("enrolmentnew", '', format_string($course->shortname)),
get_string('enrolmentnewuser', '', $a));
}
if (!empty($CFG->enrol_mailadmins)) {
$a = new stdClass;
$a->course = "$course->fullname";
$a->user = fullname($USER);
$admins = get_admins();
foreach ($admins as $admin) {
email_to_user($admin,
$USER,
get_string("enrolmentnew", '', format_string($course->shortname)),
get_string('enrolmentnewuser', '', $a));
load_all_capabilities();
redirect($destination, get_string('paymentthanks', 'moodle', $course->fullname), 10);
break;
}
}
} else {
email_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
return NULL;
}

if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl; unset($SESSION->wantsurl);
} else {
$destination = "$CFG->wwwroot/course/view.php?id=$course->id";
else
{
email_to_admin($message, $order);
return $message;
}
load_all_capabilities();
redirect($destination, get_string('paymentthanks', 'moodle', $course->fullname), 10);
}


Expand All @@ -302,12 +319,12 @@ private function cc_submit($form, $course)
*
* @param object $form Form parameters
* @param object $course Course info
* @return string NULL if ok, error message otherwise.
* @access private
*/
private function echeck_submit($form, $course)
{
global $CFG, $USER, $SESSION;
require_once('authorizenetlib.php');

prevent_double_paid($course);

Expand Down Expand Up @@ -362,13 +379,15 @@ private function echeck_submit($form, $course)
$extra->x_fax = '';

$message = '';
if (AN_REVIEW != authorize_action($order, $message, $extra, AN_ACTION_AUTH_CAPTURE)) {
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 {
email_to_admin($message, $order);
return $message;
}

$SESSION->ccpaid = 1; // security check: don't duplicate payment
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
}


Expand Down Expand Up @@ -580,11 +599,10 @@ public function process_config($config)
public function cron()
{
global $CFG;
require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');

$oneday = 86400;
$timenow = time();
$settlementtime = authorize_getsettletime($timenow);
$settlementtime = AuthorizeNet::getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);
$mconfig = get_config('enrol/authorize');
set_config('an_lastcron', $timenow, 'enrol/authorize');
Expand Down Expand Up @@ -638,7 +656,7 @@ public function cron()
foreach ($orders as $order) {
$message = '';
$extra = NULL;
if (AN_APPROVED == authorize_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE)) {
if ($lastcourseid != $order->courseid) {
$lastcourseid = $order->courseid;
$course = get_record('course', 'id', $lastcourseid);
Expand Down Expand Up @@ -702,12 +720,11 @@ public function cron()
private function cron_daily()
{
global $CFG, $SITE;
require_once($CFG->dirroot.'/enrol/authorize/authorizenetlib.php');

$oneday = 86400;
$timenow = time();
$onepass = $timenow - $oneday;
$settlementtime = authorize_getsettletime($timenow);
$settlementtime = AuthorizeNet::getsettletime($timenow);
$timediff30 = $settlementtime - (30 * $oneday);

$select = "(status='".AN_STATUS_NONE."') AND (timecreated<'$timediff30')";
Expand Down

0 comments on commit 9088b7d

Please sign in to comment.