Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- authorize_expired function added to lessen the code.

- All functions in authorize plugin are named with authorize_ prefix.
  • Loading branch information...
commit 2dc90eb90ed58490baf7d5de85a52151c94884c7 1 parent 243c0e3
ethem authored
View
59 enrol/authorize/authorizenetlib.php
@@ -18,7 +18,7 @@
* @param int $time Processed time, usually now.
* @return int Settlement date
*/
-function getsettletime($time)
+function authorize_getsettletime($time)
{
global $CFG;
@@ -38,13 +38,44 @@ 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
*
* @param object &$order Which transaction data will be sent. See enrol_authorize table.
@@ -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;
@@ -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:
@@ -125,10 +155,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;
}
@@ -142,10 +169,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!";
@@ -171,16 +199,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;
}
@@ -262,7 +287,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;
}
@@ -272,7 +297,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:
View
2  enrol/authorize/const.php
@@ -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.
View
2  enrol/authorize/db/mysql.php
@@ -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);
}
}
}
View
4 enrol/authorize/db/postgres7.php
@@ -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.");
}
@@ -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);
}
}
}
View
18 enrol/authorize/enrol.php
@@ -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;
@@ -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');
@@ -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');
@@ -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: " .
@@ -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";
}
}
@@ -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.
View
26 enrol/authorize/locallib.php
@@ -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);
@@ -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);
@@ -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);
@@ -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)) {
@@ -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)) {
@@ -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();
@@ -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);
}
@@ -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);
}
@@ -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 {
Please sign in to comment.
Something went wrong with that request. Please try again.