Skip to content

Commit

Permalink
- authorize_expired function added to lessen the code.
Browse files Browse the repository at this point in the history
- All functions in authorize plugin are named with authorize_ prefix.

Merged from 16stable.
  • Loading branch information
ethem committed Jul 10, 2006
1 parent 6476b0a commit 411df81
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 47 deletions.
59 changes: 42 additions & 17 deletions enrol/authorize/authorizenetlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @param int $time Processed time, usually now.
* @return int Settlement date
*/
function getsettletime($time)
function authorize_getsettletime($time)
{
global $CFG;

Expand All @@ -38,12 +38,43 @@ function getsettletime($time)
* @param object $order Order details
* @return bool true, if settled, false otherwise.
*/
function settled($order)
function authorize_settled($order)
{
return (($order->status == AN_STATUS_AUTHCAPTURE || $order->status == AN_STATUS_CREDIT) &&
($order->settletime > 0) && ($order->settletime < time()));
}

/**
* Is order expired? 'Authorized/Pending Capture' transactions are expired after 30 days.
*
* @param object &$order Order details.
* @return bool true, transaction is expired, false otherwise.
*/
function authorize_expired(&$order)
{
static $timediff30;

if (empty($timediff30)) {
$timediff30 = authorize_getsettletime(time()) - (30 * 24 * 3600);
}

if ($order->status == AN_STATUS_EXPIRE) {
return true;
}
elseif ($order->status != AN_STATUS_AUTH) {
return false;
}

$exp = (authorize_getsettletime($order->timecreated) < $timediff30);

if ($exp) {
$order->status = AN_STATUS_EXPIRE;
update_record('enrol_authorize', $order);
}

return $exp;
}

/**
* Performs an action on authorize.net
*
Expand All @@ -55,7 +86,7 @@ function settled($order)
* @author Ethem Evlice <ethem a.t evlice d.o.t com>
* @uses $CFG
*/
function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE)
function authorize_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE)
{
global $CFG;
static $conststring;
Expand Down Expand Up @@ -96,7 +127,6 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
}

$poststring = $conststring;
$timenowsettle = getsettletime(time());

switch ($action) {
case AN_ACTION_AUTH_ONLY:
Expand Down Expand Up @@ -127,10 +157,7 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
$message = "Order status must be authorized!";
return false;
}
$timediff = $timenowsettle - (30 * 3600 * 24);
$timecreatedsettle = getsettletime($order->timecreated);
if ($timecreatedsettle < $timediff) {
$order->status = AN_STATUS_EXPIRE;
if (authorize_expired($order)) {
$message = "Transaction must be captured within 30 days. EXPIRED!";
return false;
}
Expand All @@ -144,10 +171,11 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
$message = "Order status must be authorized/captured!";
return false;
}
if (!settled($order)) {
if (!authorize_settled($order)) {
$message = "Order must be settled. Try VOID, check Cut-Off time if it fails!";
return false;
}
$timenowsettle = authorize_getsettletime(time());
$timediff = $timenowsettle - (120 * 3600 * 24);
if ($order->settletime < $timediff) {
$message = "Order must be credited within 120 days!";
Expand All @@ -173,16 +201,13 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
case AN_ACTION_VOID:
{
if ($order->status == AN_STATUS_AUTH) {
$timediff = $timenowsettle - (30 * 3600 * 24);
$timecreatedsettle = getsettletime($order->timecreated);
if ($timecreatedsettle < $timediff) {
if (authorize_expired($order)) {
$message = "Authorized transaction must be voided within 30 days. EXPIRED!";
$order->status = AN_STATUS_EXPIRE;
return false;
}
}
elseif ($order->status == AN_STATUS_AUTHCAPTURE || $order->status == AN_STATUS_CREDIT) {
if (settled($order)) {
elseif ($order->status == AN_STATUS_AUTHCAPTURE or $order->status == AN_STATUS_CREDIT) {
if (authorize_settled($order)) {
$message = "Settled transaction cannot be voided. Check Cut-Off time!";
return false;
}
Expand Down Expand Up @@ -265,7 +290,7 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
// dont't update settletime
} else {
$order->status = AN_STATUS_AUTHCAPTURE;
$order->settletime = getsettletime(time());
$order->settletime = authorize_getsettletime(time());
}
break;
}
Expand All @@ -275,7 +300,7 @@ function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE
// So, $extra must be updated, not $order.
$extra->status = AN_STATUS_CREDIT;
$extra->transid = $transid;
$extra->settletime = getsettletime(time());
$extra->settletime = authorize_getsettletime(time());
break;
}
case AN_ACTION_VOID:
Expand Down
2 changes: 1 addition & 1 deletion enrol/authorize/const.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**#@-*/

/**#@+
* Actions used in authorizenet_action function.
* Actions used in authorize_action function.
*
* NONE: No action. Function always returns false.
* AUTH_ONLY: Used to authorize only, don't capture.
Expand Down
2 changes: 1 addition & 1 deletion enrol/authorize/db/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function enrol_authorize_upgrade($oldversion=0) {
include_once("$CFG->dirroot/enrol/authorize/authorizenetlib.php");
foreach ($settlements as $settlement) {
execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET settletime = '" .
getsettletime($settlement->settletime) . "' WHERE id = '$settlement->id'", false);
authorize_getsettletime($settlement->settletime) . "' WHERE id = '$settlement->id'", false);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions enrol/authorize/db/postgres7.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function enrol_authorize_upgrade($oldversion=0) {
// Authorize module was installed before. Upgrades must be applied to SQL file.

if ($oldversion && $oldversion < 2005071602) {
notify("If you are using the authorize.net enrolment plugin for credit card
notify("If you are using the authorize.net enrolment plugin for credit card
handling, please ensure that you have turned loginhttps ON in Admin >> Variables >> Security.");
}

Expand Down Expand Up @@ -80,7 +80,7 @@ function enrol_authorize_upgrade($oldversion=0) {
include_once("$CFG->dirroot/enrol/authorize/authorizenetlib.php");
foreach ($settlements as $settlement) {
execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET settletime = '" .
getsettletime($settlement->settletime) . "' WHERE id = '$settlement->id'", false);
authorize_getsettletime($settlement->settletime) . "' WHERE id = '$settlement->id'", false);
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions enrol/authorize/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function cc_submit($form, $course)
$message = '';
$an_review = !empty($CFG->an_review);
$action = $an_review ? AN_ACTION_AUTH_ONLY : AN_ACTION_AUTH_CAPTURE;
$success = authorizenet_action($order, $message, $extra, $action);
$success = authorize_action($order, $message, $extra, $action);
if (!$success) {
$this->email_to_admin($message, $order);
$this->ccerrors['header'] = $message;
Expand Down Expand Up @@ -268,8 +268,8 @@ function cc_submit($form, $course)
$a->orderid = $order->id;
$a->transid = $order->transid;
$a->amount = "$order->currency $order->amount";
$a->expireon = userdate(getsettletime($timenow + (30 * 3600 * 24)));
$a->captureon = userdate(getsettletime($timenow + (intval($CFG->an_capture_day) * 3600 * 24)));
$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');
Expand Down Expand Up @@ -688,7 +688,7 @@ function cron()

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

$mconfig = get_config('enrol/authorize');
Expand Down Expand Up @@ -745,8 +745,7 @@ function cron()
foreach ($orders as $order) {
$message = '';
$extra = NULL;
$oldstatus = $order->status;
$success = authorizenet_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
$success = authorize_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: " .
Expand All @@ -772,10 +771,7 @@ function cron()
}
}
else {
$this->log .= "Order $order->id: " . $message . "\n";
if ($order->status != $oldstatus) { //expired
update_record("enrol_authorize", $order);
}
$this->log .= "Error, Order# $order->id: " . $message . "\n";
}
}

Expand Down Expand Up @@ -840,7 +836,7 @@ function cron_daily()

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

// Delete orders that no transaction was made.
Expand Down
26 changes: 11 additions & 15 deletions enrol/authorize/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function authorize_print_order_details($orderno)
echo "<input type=\"hidden\" name=\"order\" value=\"$orderno\">\n";
echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";

$settled = settled($order);
$settled = authorize_settled($order);
$status = authorize_get_status_action($order);

$table->data[] = array("<b>$authstrs->orderid:</b>", $orderno);
Expand Down Expand Up @@ -223,7 +223,7 @@ function authorize_print_order_details($orderno)
else {
$message = '';
$extra = NULL;
$success = authorizenet_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
$success = authorize_action($order, $message, $extra, AN_ACTION_PRIOR_AUTH_CAPTURE);
update_record("enrol_authorize", $order); // May be expired.
if (!$success) {
$table->data[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
Expand Down Expand Up @@ -298,7 +298,7 @@ function authorize_print_order_details($orderno)
else {
$extra->amount = $amount;
$message = '';
$success = authorizenet_action($order, $message, $extra, AN_ACTION_CREDIT);
$success = authorize_action($order, $message, $extra, AN_ACTION_CREDIT);
if ($success) {
if (empty($CFG->an_test)) {
$extra->id = insert_record("enrol_authorize_refunds", $extra);
Expand Down Expand Up @@ -352,7 +352,7 @@ function authorize_print_order_details($orderno)
else {
$extra = NULL;
$message = '';
$success = authorizenet_action($order, $message, $extra, AN_ACTION_VOID);
$success = authorize_action($order, $message, $extra, AN_ACTION_VOID);
update_record("enrol_authorize", $order); // May be expired.
if ($success) {
if (empty($CFG->an_test)) {
Expand Down Expand Up @@ -394,7 +394,7 @@ function authorize_print_order_details($orderno)
else {
$message = '';
$extra = NULL;
$success = authorizenet_action($suborder, $message, $extra, AN_ACTION_VOID);
$success = authorize_action($suborder, $message, $extra, AN_ACTION_VOID);
update_record("enrol_authorize_refunds", $suborder); // May be expired.
if ($success) {
if (empty($CFG->an_test)) {
Expand Down Expand Up @@ -501,12 +501,10 @@ function authorize_print_order_details($orderno)
function authorize_get_status_action($order)
{
global $CFG;
static $timediff30, $newordertime;
static $newordertime;

if (empty($timediff30)) {
$timenow = time();
$timediff30 = getsettletime($timenow) - (30 * 3600 * 24);
$newordertime = $timenow - 120; // -2 minutes. Order may be still in process.
if (empty($newordertime)) {
$newordertime = time() - 120; // -2 minutes. Order may be still in process.
}

$ret = new stdClass();
Expand All @@ -527,9 +525,7 @@ function authorize_get_status_action($order)

switch ($order->status) {
case AN_STATUS_AUTH:
if (getsettletime($order->timecreated) < $timediff30) {
$order->status = AN_STATUS_EXPIRE;
update_record("enrol_authorize", $order);
if (authorize_expired($order)) {
if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) {
$ret->actions = array(ORDER_DELETE);
}
Expand All @@ -544,7 +540,7 @@ function authorize_get_status_action($order)
return $ret;

case AN_STATUS_AUTHCAPTURE:
if (settled($order)) {
if (authorize_settled($order)) {
if (isadmin() || (!empty($CFG->an_teachermanagepay) && isteacher($order->courseid))) {
$ret->actions = array(ORDER_REFUND);
}
Expand All @@ -559,7 +555,7 @@ function authorize_get_status_action($order)
return $ret;

case AN_STATUS_CREDIT:
if (settled($order)) {
if (authorize_settled($order)) {
$ret->status = 'settled';
}
else {
Expand Down

0 comments on commit 411df81

Please sign in to comment.