Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New authorize.net module is more powerful. :)

- Review order:

   Review order before processing the credit card.

- Auto-Capture:
   If auto-capture enabled the credit card will be captured
   after an_review_day later, then student will be enrolled
   to course, otherwise it will be expired or
   admin must accept/deny payment.

   NOTE: Set-up Transaction Cut-Off Time correctly.
         When the last transaction is picked up for settlement?

- Payment Management:

   Accept/Deny/Refund payments: cvs:/moodle/enrol/authorize/index.php

- TO DO: Batch upload: It will sync authorize.net and moodle from csv file.
  • Loading branch information...
commit 62d98252ac1b865038ad640c95be6588e274464a 1 parent 8cb3ad1
ethem authored
View
15 enrol/authorize/action.php
@@ -46,9 +46,6 @@
define('AN_ACTION_VOID', 0x08);
-define('AN_REASON_NONE', 0);
-define('AN_REASON_TRAN_NOT_FOUND', 16);
-
/**
* Gets settlement date and time
*
@@ -82,11 +79,11 @@ function settled($order)
static $timenow;
if (!isset($timenow)) {
- $timenow = time();
+ $timenow = time();
}
return (($order->status == AN_STATUS_AUTHCAPTURE || $order->status == AN_STATUS_CREDIT)
- && $order->settletime < $timenow && $order->settletime > 0);
+ && $order->settletime > 0 && $order->settletime < $timenow );
}
/**
@@ -94,14 +91,13 @@ function settled($order)
*
* @param object &$order Which transaction data will be send. See enrol_authorize table.
* @param string &$message Information about error messages.
- * @param int &$reason Reason subcode
* @param object &$extra Extra transaction data.
* @param int $action Which action will be performed. See AN_ACTION_*
* @return bool true, transaction was successful, false otherwise.
* @author Ethem Evlice <ethem a.t evlice d.o.t com>
* @uses $CFG
*/
-function authorizenet_action(&$order, &$message, &$reason, &$extra, $action=AN_ACTION_NONE)
+function authorizenet_action(&$order, &$message, &$extra, $action=AN_ACTION_NONE)
{
global $CFG;
static $conststring;
@@ -297,10 +293,11 @@ function authorizenet_action(&$order, &$message, &$reason, &$extra, $action=AN_A
$response[$rcount] = substr($response[$rcount], 0, -1);
}
- $reason = intval($response[2]);
-
if ($response[0] == AN_APPROVED)
{
+ if ($an_test || intval($response[6]) == 0) {
+ return true; // don't update original transaction in test mode.
+ }
switch ($action) {
case AN_ACTION_AUTH_ONLY:
case AN_ACTION_AUTH_CAPTURE:
View
189 enrol/authorize/enrol.php
@@ -163,6 +163,11 @@ function cc_submit($form, $course)
return;
}
+ if (!empty($CFG->an_test)) {
+ error("Credit card module cannot be present because of test mode");
+ return;
+ }
+
$this->prevent_double_paid($course);
$exp_date = ($form->ccexpiremm < 10) ? strval('0'.$form->ccexpiremm) : strval($form->ccexpiremm);
@@ -221,21 +226,40 @@ function cc_submit($form, $course)
$extra->x_description = $course->shortname;
$message = '';
- $reason = AN_REASON_NONE;
$an_review = !empty($CFG->an_review);
$action = $an_review ? AN_ACTION_AUTH_ONLY : AN_ACTION_AUTH_CAPTURE;
- $success = authorizenet_action($order, $message, $reason, $extra, $action);
+ $success = authorizenet_action($order, $message, $extra, $action);
if (!$success) {
$this->email_to_admin($message, $order);
$this->ccerrormsg = $message;
return;
}
+ if (intval($order->transid) == 0) { // I know it is test mode. :)
+ error("Credit card module cannot be present because of test mode");
+ return;
+ }
+
$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)) {
- // notification: new transaction (AUTH_ONLY)
- // see order details: index.php?order=$order->id
+ $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);
+ }
}
else {
$this->email_to_admin("Error while trying to update data. Please edit manually this record: " .
@@ -404,7 +428,8 @@ function config_form($frm)
if (!(empty($frm->an_review) || $frm->an_review_day < 1)) {
// ++ENABLED++
// Cron must be runnig!!! Check last cron...
- $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
+ $mconfig = get_config('enrol/authorize');
+ $lastcron = intval($mconfig->an_lastcron);
if (time() - $lastcron > 3600 * 24) {
// Cron must be enabled if you want to use autocapture feature.
// Setup cron or disable an_review again...
@@ -480,8 +505,9 @@ function process_config($config)
if ($review_day_val < 0) $review_day_val = 0;
elseif ($review_day_val > 29) $review_day_val = 29;
if ($review_day_val > 0) {
- // cron is required.
- $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
+ // Cron must change an_lastcron. :))
+ $mconfig = get_config('enrol/authorize');
+ $lastcron = intval($mconfig->an_lastcron);
if (time() - $lastcron > 3600 * 24) {
// No!!! I am not lucky. No changes please...
return false;
@@ -525,17 +551,16 @@ function prevent_double_paid($course)
{
global $CFG, $SESSION, $USER;
+ 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;
+ }
if (isset($SESSION->ccpaid)) {
unset($SESSION->ccpaid);
redirect($CFG->wwwroot . '/login/logout.php');
return;
}
-
- 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;
- }
}
@@ -560,78 +585,106 @@ function cron()
parent::cron();
require_once("$CFG->dirroot/enrol/authorize/action.php");
- srand((double)microtime() * 10000000);
- $random100 = rand(0, 100);
$timenow = time();
$timenowsettle = getsettletime($timenow);
$timediff30 = $timenowsettle - (30 * 3600 * 24);
+ // 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.
- if ($random100 < 15) { // delete very old records: status=AN_STATUS_NONE & timecreated=-60day.
- // no credit card transaction is made in status AN_STATUS_NONE.
- $timediff60 = $timenowsettle - (60 * 3600 * 24);
- $select = "(status = '" .AN_STATUS_NONE. "') AND (timecreated < '$timediff60')";
- if (count_records_select('enrol_authorize', $select)) {
- mtrace("Deleting records in authorize table older than 60 days (status=AN_STATUS_NONE).");
- delete_records_select('enrol_authorize', $select);
- }
+ $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);
}
- elseif ($random100 > 80) { // EXPIRED: Transactions with auth_only will be expired 30 days later.
- $select = "(status = '" .AN_STATUS_AUTH. "') AND (settletime = '0') AND (timecreated < '$timediff30')";
- execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET settletime = '$timenowsettle', status = '" .AN_STATUS_EXPIRE. "' WHERE $select", false);
+ else {
+ $timediff60 = $timenowsettle - (60 * 3600 * 24);
+ $select = "(status = '" .AN_STATUS_EXPIRE. "') AND (timecreated < '$timediff60')";
+ delete_records_select('enrol_authorize', $select);
}
+ if (!empty($CFG->an_test)) {
+ return; // AUTOCAPTURE doesn't work in test mode.
+ }
if (empty($CFG->an_review) || empty($CFG->an_review_day) || $CFG->an_review_day < 1) {
- // AUTOCAPTURE disabled. admin, teacher review it manually
- return;
+ return; // AUTOCAPTURE disabled. admin, teacher review it manually
}
- // AUTO-CAPTURE: it must be captured within 30 days. Otherwise it will expired.
+ // AUTO-CAPTURE: Transaction must be captured within 30 days. Otherwise it will expired.
$timediffcnf = $timenowsettle - (intval($CFG->an_review_day) * 3600 * 24);
- $select = "(status = '" . AN_STATUS_AUTH . "') AND (settletime = '0') AND (timecreated < '$timediffcnf') AND (timecreated > '$timediff30')";
- if ($orders = get_records_select('enrol_authorize', $select)) {
- $this->log = "AUTHORIZE.NET AUTOCAPTURE CRON: " . userdate($timenow) . "\n";
- @set_time_limit(0);
- $faults = '';
- foreach ($orders as $order) {
- $message = NULL;
- $extra = NULL;
- $reason = AN_REASON_NONE;
- $success = authorizenet_action($order, $message, $reason, $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;
- if ($course = get_record_sql("SELECT enrolperiod FROM {$CFG->prefix}course WHERE id='$order->courseid'")) {
- if ($course->enrolperiod) {
- $timestart = $timenow;
- $timeend = $timestart + $course->enrolperiod;
- }
- }
- 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";
- }
+ $select = "status = '" .AN_STATUS_AUTH. "' AND timecreated < '$timediffcnf' AND timecreated > '$timediff30'";
+ if (!$orders = get_records('enrol_authorize', $select)) {
+ 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;
+ if ($course = get_record_sql("SELECT enrolperiod FROM {$CFG->prefix}course WHERE id='$order->courseid'")) {
+ if ($course->enrolperiod) {
+ $timestart = $timenow;
+ $timeend = $timestart + $course->enrolperiod;
}
}
- else { // not success
- $this->log .= $message . "\n";
+ 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";
+ }
}
}
- $this->log .= "AUTHORIZE.NET CRON FINISHED: " . userdate(time());
- $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);
+ else { // not success
+ $this->log .= "Order $order->id: " . $message . "\n";
+ if ($order->status != $oldstatus) { //expired
+ update_record("enrol_authorize", $order);
+ }
}
}
+
+ $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);
+ }
}
}
?>
View
451 enrol/authorize/index.php
@@ -0,0 +1,451 @@
+<?php // $Id$
+
+require_once("../../config.php");
+require_once("enrol.php");
+require_once("action.php");
+
+define('ORDER_CAPTURE', 'capture');
+define('ORDER_DELETE', 'delete');
+define('ORDER_REFUND', 'refund');
+define('ORDER_VOID', 'void');
+
+if (!($site = get_site())) {
+ error("Could not find a site!");
+}
+
+require_login();
+
+if (!isadmin()) {
+ error("You must be an administrator to use this page.");
+}
+
+$csv = optional_param('csv', '', PARAM_ALPHA);
+$orderid = optional_param('order', 0, PARAM_INT);
+
+$strs = get_strings(array('user', 'status', 'action', 'delete', 'time',
+ 'course', 'confirm', 'yes', 'no', 'none', 'error'));
+
+$authstrs = get_strings(array('paymentmanagement', 'orderid', 'void', 'capture', 'refund',
+ 'authorizedpendingcapture','capturedpendingsettle', 'capturedsettled',
+ 'settled', 'refunded', 'cancelled', 'expired', 'tested',
+ 'transid', 'settlementdate', 'notsettled', 'returns', 'noreturns', 'amount',
+ 'howmuch', 'captureyes', 'unenrolstudent'), 'enrol_authorize');
+
+print_header("$site->shortname: $authstr->paymentmanagement", "$site->fullname", "<a href=\"index.php\">$authstr->paymentmanagement</a>", "");
+
+if (!empty($csv)) {
+ authorize_csv();
+}
+elseif (!empty($orderid)) {
+ authorize_order_details($orderid);
+}
+else {
+ authorize_orders();
+}
+print_footer();
+
+
+function authorize_orders()
+{
+ global $CFG;
+ global $strs, $authstrs;
+ require_once($CFG->libdir.'/tablelib.php');
+
+ $perpage = 10;
+ $userid = optional_param('user', 0, PARAM_INT);
+ $courseid = optional_param('course', 0, PARAM_INT);
+
+ $table = new flexible_table('enrol-authorize');
+ $table->set_attribute('width', '100%');
+ $table->set_attribute('cellspacing', '0');
+ $table->set_attribute('cellpadding', '3');
+ $table->set_attribute('id', 'orders');
+ $table->set_attribute('class', 'generaltable generalbox');
+
+ $table->define_columns(array('id', 'timecreated', 'userid', 'status', ''));
+ $table->define_headers(array($authstrs->orderid, $strs->time, $strs->user, $strs->status, $strs->action));
+ $table->define_baseurl($CFG->wwwroot."/enrol/authorize/index.php?course=$courseid&amp;user=$userid");
+
+ $table->sortable(true);
+ $table->pageable(true);
+ $table->setup();
+
+ $where = "WHERE (status != '" . AN_STATUS_NONE . "') ";
+ if ($userid > 0) { $where .= "AND (userid = '" . $userid . "') "; }
+ if ($courseid > 0) { $where .= "AND (courseid = '" . $courseid . "') "; }
+
+ $select = "SELECT id, transid, courseid, userid, status, ccname, timecreated, settletime ";
+ $from = " FROM {$CFG->prefix}enrol_authorize ";
+ if ($sort = $table->get_sql_sort()) {
+ $sort = ' ORDER BY ' . $sort;
+ }
+ else {
+ $sort = ' ORDER BY id DESC ';
+ }
+
+ $totalcount = count_records_sql('SELECT COUNT(*) ' . $from . $where);
+ $table->initialbars($totalcount > $perpage);
+ $table->pagesize($perpage, $totalcount);
+ if ($table->get_page_start() !== '' && $table->get_page_size() !== '') {
+ $limit = ' ' . sql_paging_limit($table->get_page_start(), $table->get_page_size());
+ }
+ else {
+ $limit = '';
+ }
+
+ if ($records = get_records_sql($select . $from . $where . $sort . $limit)) {
+ foreach ($records as $record) {
+ $actionstatus = get_order_status_desc($record);
+ $actions = '&nbsp;';
+ foreach ($actionstatus->actions as $value) {
+ $actions .= "&nbsp;&nbsp;<a href='index.php?$value=yes&amp;order=$record->id'>{$authstrs->$value}</a> ";
+ }
+ $table->add_data(array(
+ "<a href='index.php?order=$record->id'>$record->id</a>",
+ userdate($record->timecreated),
+ $record->ccname,
+ $authstrs->{$actionstatus->status},
+ $actions
+ ));
+ }
+ }
+
+ $table->print_html();
+}
+
+
+function authorize_order_details($orderno) {
+ global $CFG;
+ global $strs, $authstrs;
+
+ $unenrol = optional_param('unenrol', '');
+ $cmdconfirm = optional_param('confirm', '', PARAM_ALPHA);
+
+ $cmdcapture = optional_param('capture', '', PARAM_ALPHA);
+ $cmddelete = optional_param('delete', '', PARAM_ALPHA);
+ $cmdrefund = optional_param('refund', '', PARAM_ALPHA);
+ $cmdvoid = optional_param('void', '', PARAM_ALPHA);
+
+ $table->width = '100%';
+ $table->size = array('30%', '70%');
+ $table->align = array('right', 'left');
+
+ $sql = "SELECT E.*, C.shortname " .
+ "FROM {$CFG->prefix}enrol_authorize E " .
+ "INNER JOIN {$CFG->prefix}course C ON C.id = E.courseid " .
+ "WHERE E.id = '$orderno'";
+
+ $order = get_record_sql($sql);
+ if (!$order) {
+ notice("Order $orderno not found.", "index.php");
+ return;
+ }
+
+ echo "<form action='index.php' method='post'>\n";
+ echo "<input type='hidden' name='order' value='$order->id'>\n";
+
+ $settled = settled($order);
+ $status = get_order_status_desc($order);
+
+ $table->data[] = array("<b>$authstrs->orderid:</b>", $order->id);
+ $table->data[] = array("<b>$authstrs->transid:</b>", $order->transid);
+ $table->data[] = array("<b>$authstrs->amount:</b>", "$order->currency $order->amount");
+ if ((empty($cmdcapture) and empty($cmdrefund) and empty($cmdvoid))) {
+ $table->data[] = array("<b>$strs->course:</b>", $order->shortname);
+ $table->data[] = array("<b>$strs->status:</b>", $authstrs->{$status->status});
+ $table->data[] = array("<b>$strs->user:</b>", $order->ccname);
+ $table->data[] = array("<b>$strs->time:</b>", userdate($order->timecreated));
+ $table->data[] = array("<b>$authstrs->settlementdate:</b>", $settled ? userdate($order->settletime) : $authstrs->notsettled);
+ }
+ $table->data[] = array("&nbsp;", "<hr size='1' noshade>\n");
+
+ if (!empty($cmdcapture)) { // CAPTURE
+ if (empty($cmdconfirm)) {
+ $table->data[] = array("<b>$strs->confirm:</b>",
+ "$authstrs->captureyes<br /><a href='index.php?order=$orderno&amp;capture=yes&amp;confirm=yes'>$strs->yes</a>
+ &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>$strs->no</a>");
+ }
+ else {
+ $message = '';
+ $extra = NULL;
+ $success = authorizenet_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);
+ }
+ else {
+ if (empty($CFG->an_test)) {
+ $timestart = $timeend = 0;
+ if ($course = get_record_sql("SELECT enrolperiod FROM {$CFG->prefix}course WHERE id='$order->courseid'")) {
+ if ($course->enrolperiod) {
+ $timestart = $order->settletime;
+ $timeend = $timestart + $course->enrolperiod;
+ }
+ }
+ enrol_student($order->userid, $order->courseid, $timestart, $timeend, 'authorize');
+ // To do: inform user. You will access to course on date $order->settletime
+ redirect("index.php?order=$order->id");
+ }
+ else {
+ $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('capturetestwarn', 'enrol_authorize'));
+ }
+ }
+ }
+ print_table($table);
+ }
+ elseif (!empty($cmdrefund)) { // REFUND
+ $extra = new stdClass();
+ $extra->sum = 0.0;
+ $extra->orderid = $order->id;
+
+ $sql = "SELECT SUM(amount) AS refunded FROM {$CFG->prefix}enrol_authorize_refunds " .
+ "WHERE (orderid = '" . $order->id . "') AND (status = '" . AN_STATUS_CREDIT . "')";
+
+ if ($refund = get_record_sql($sql)) {
+ $extra->sum = doubleval($refund->refunded);
+ }
+ $upto = format_float($order->amount - $extra->sum, 2);
+ if ($upto <= 0) {
+ error("Refunded to original amount.");
+ }
+ else {
+ $amount = format_float(optional_param('amount', $upto), 2);
+ if (($amount > $upto) || empty($cmdconfirm)) {
+ $a->upto = $upto;
+ $strcanbecredit = get_string('canbecredit', 'enrol_authorize', $a);
+ $table->data[] = array("<b>$authstrs->unenrolstudent</b>",
+ "<input type='checkbox' name='unenrol' value='yes'" . (!empty($unenrol) ? " checked" : "") . ">");
+ $table->data[] = array("<b>$authstrs->howmuch</b>",
+ "<input type='hidden' name='confirm' value='yes'>
+ <input type='text' size='5' name='amount' value='$amount'>
+ $strcanbecredit<br /><input type='submit' name='refund' value='$authstrs->refund'>");
+ }
+ else {
+ $extra->amount = $amount;
+ $message = '';
+ $success = authorizenet_action($order, $message, $extra, AN_ACTION_CREDIT);
+ if ($success) {
+ if (empty($CFG->an_test)) {
+ $extra->id = insert_record("enrol_authorize_refunds", $extra);
+ if (!$extra->id) {
+ // to do: email admin
+ }
+ if (!empty($unenrol)) {
+ unenrol_student($order->userid, $order->courseid);
+ }
+ redirect("index.php?order=$order->id");
+ }
+ else {
+ $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('credittestwarn', 'enrol_authorize'));
+ }
+ }
+ else {
+ $table->data[] = array("<b><font color=red>$strs->error:</font></b>", $message);
+ }
+ }
+ }
+ print_table($table);
+ }
+ elseif (!empty($cmdvoid)) { // VOID
+ $suborderno = optional_param('suborder', 0, PARAM_INT);
+ if (empty($suborderno)) { // cancel original transaction.
+ if (empty($cmdconfirm)) {
+ $strvoidyes = get_string('voidyes', 'enrol_authorize');
+ $table->data[] = array("<b>$strs->confirm:</b>",
+ "$strvoidyes<br /><input type='hidden' name='void' value='yes'>
+ <input type='hidden' name='confirm' value='yes'>
+ <input type='submit' value='$strs->yes'>
+ &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>$strs->no</a>");
+ }
+ else {
+ $extra = NULL;
+ $message = '';
+ $success = authorizenet_action($order, $message, $extra, AN_ACTION_VOID);
+ update_record("enrol_authorize", $order); // May be expired.
+ if ($success) {
+ if (empty($CFG->an_test)) {
+ redirect("index.php?order=$order->id");
+ }
+ else {
+ $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('voidtestwarn', 'enrol_authorize'));
+ }
+ }
+ else {
+ $table->data[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
+ }
+ }
+ }
+ else { // cancel refunded transaction
+ $suborder = get_record('enrol_authorize_refunds', 'id', $suborderno, 'status', AN_STATUS_CREDIT);
+ if (!$suborder) { // not found
+ error("Transaction can not be voided because of already been voided.");
+ }
+ else {
+ if (empty($cmdconfirm)) {
+ $a->transid = $suborder->transid;
+ $a->amount = $suborder->amount;
+ $strsubvoidyes = get_string('subvoidyes', 'enrol_authorize', $a);
+
+ $table->data[] = array("<b>$authstrs->unenrolstudent</b>",
+ "<input type='checkbox' name='unenrol' value='yes'" . (!empty($unenrol) ? " checked" : "") . ">");
+
+ $table->data[] = array("<b>$strs->confirm:</b>",
+ "$strsubvoidyes<br /><input type='hidden' name='void' value='yes'>
+ <input type='hidden' name='confirm' value='yes'>
+ <input type='hidden' name='suborder' value='$suborderno'>
+ <input type='submit' value='$strs->yes'>
+ &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>$strs->no</a>");
+ }
+ else {
+ $message = '';
+ $extra = NULL;
+ $success = authorizenet_action($suborder, $message, $extra, AN_ACTION_VOID);
+ update_record("enrol_authorize", $suborder); // May be expired.
+ if ($success) {
+ if (empty($CFG->an_test)) {
+ if (!empty($unenrol)) {
+ unenrol_student($order->userid, $order->courseid);
+ }
+ redirect("index.php?order=$order->id");
+ }
+ else {
+ $table->data[] = array(get_string('testmode', 'enrol_authorize'), get_string('voidtestwarn', 'enrol_authorize'));
+ }
+ }
+ else {
+ $table->data[] = array("<b><font color='red'>$strs->error:</font></b>", $message);
+ }
+ }
+ }
+ }
+ print_table($table);
+ }
+ elseif (!empty($cmddelete)) { // DELETE
+ if (!in_array(ORDER_DELETE, $status->actions)) {
+ error("Order $order->id cannot be deleted. Status must be expired.");
+ }
+ if (empty($cmdconfirm)) {
+ $table->data[] = array('<b>Delete?: </b>',
+ "<a href='index.php?order=$orderno&amp;delete=yes&amp;confirm=yes'>YES</a>
+ &nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?order=$orderno'>No</a>");
+ }
+ else {
+ delete_records('enrol_authorize', 'id', $orderno);
+ redirect("index.php");
+ }
+ print_table($table);
+ }
+ else { // SHOW
+ $actions = '';
+ if (empty($status->actions)) {
+ $actions .= $strs->none;
+ }
+ else {
+ foreach ($status->actions as $value) {
+ $actions .= "<input type='submit' name='$value' value='{$authstrs->$value}'> ";
+ }
+ }
+ $table->data[] = array("<b>$strs->action</b>", $actions);
+ print_table($table);
+ if ($settled) { // show refunds.
+ echo "<h4>$authstrs->returns</h4>\n";
+ $table2->size = array('15%', '15%', '20%', '35%', '15%');
+ $table2->align = array('right', 'right', 'right', 'left', 'right');
+ $table2->head = array($authstrs->transid, $authstrs->amount, $strs->status, $authstrs->settlementdate, $strs->action);
+ $refunds = get_records('enrol_authorize_refunds', 'orderid', $orderno);
+ if ($refunds) {
+ foreach ($refunds as $rfnd) {
+ $substatus = get_order_status_desc($rfnd);
+ $subactions = '&nbsp;';
+ if (empty($substatus->actions)) {
+ $subactions .= $strs->none;
+ }
+ else {
+ foreach ($substatus->actions as $value) {
+ $subactions .= "<a href='index.php?$value=yes&amp;order=$orderno&amp;suborder=$rfnd->id'>{$authstrs->$value}</a> ";
+ }
+ }
+ $table2->data[] = array($rfnd->transid, $rfnd->amount, $authstrs->{$substatus->status}, userdate($rfnd->settletime), $subactions);
+ }
+ }
+ else {
+ $table2->data[] = array($authstrs->noreturns);
+ }
+ print_table($table2);
+ }
+ }
+ echo '</form>';
+}
+
+function authorize_csv()
+{
+ return;
+}
+
+function get_order_status_desc($order)
+{
+ global $CFG;
+ static $timediff30;
+
+ $ret = new stdClass();
+
+ if (intval($order->transid) == 0) { // test transaction
+ $ret->actions = array(ORDER_DELETE);
+ $ret->status = 'tested';
+ return $ret;
+ }
+
+ switch ($order->status) {
+ case AN_STATUS_AUTH:
+ if (empty($timediff30)) {
+ $timediff30 = getsettletime(time()) - (30 * 3600 * 24);
+ }
+
+ if (getsettletime($order->timecreated) < $timediff30) {
+ $ret->actions = array(ORDER_DELETE);
+ $ret->status = 'expired';
+ }
+ else {
+ $ret->actions = array(ORDER_CAPTURE, ORDER_VOID);
+ $ret->status = 'authorizedpendingcapture';
+ }
+ return $ret;
+
+ case AN_STATUS_AUTHCAPTURE:
+ if (settled($order)) {
+ $ret->actions = array(ORDER_REFUND);
+ $ret->status = 'capturedsettled';
+ }
+ else {
+ $ret->actions = array(ORDER_VOID);
+ $ret->status = 'capturedpendingsettle';
+ }
+ return $ret;
+
+ case AN_STATUS_CREDIT:
+ if (settled($order)) {
+ $ret->actions = array();
+ $ret->status = 'settled';
+ }
+ else {
+ $ret->actions = array(ORDER_VOID);
+ $ret->status = 'refunded';
+ }
+ return $ret;
+
+ case AN_STATUS_VOID:
+ $ret->actions = array();
+ $ret->status = 'cancelled';
+ return $ret;
+
+ case AN_STATUS_EXPIRE:
+ $ret->actions = array(ORDER_DELETE);
+ $ret->status = 'expired';
+ return $ret;
+
+ default:
+ return $ret;
+ }
+
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.