/** * Daily cron. It executes at settlement time (default is 00:05). * * @access 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); $timediff30 = $settlementtime - 30 * $oneday; // Delete orders that no transaction was made. $select = "(status='" . AN_STATUS_NONE . "') AND (timecreated<'{$timediff30}')"; delete_records_select('enrol_authorize', $select); // Pending orders are expired with in 30 days. $select = "(status='" . AN_STATUS_AUTH . "') AND (timecreated<'{$timediff30}')"; execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='" . AN_STATUS_EXPIRE . "' WHERE {$select}", false); // Delete expired orders 60 days later. $timediff60 = $settlementtime - 60 * $oneday; $select = "(status='" . AN_STATUS_EXPIRE . "') AND (timecreated<'{$timediff60}')"; delete_records_select('enrol_authorize', $select); // XXX TODO SEND EMAIL to 'enrol/authorize:uploadcsv' // get_users_by_capability() does not handling user level resolving // After user resolving, get_admin() to get_users_by_capability() $adminuser = get_admin(); $select = "status IN(" . AN_STATUS_UNDERREVIEW . "," . AN_STATUS_APPROVEDREVIEW . ") AND (timecreated<'{$onepass}') AND (timecreated>'{$timediff60}')"; $count = count_records_select('enrol_authorize', $select); if ($count) { $a = new stdClass(); $a->count = $count; $a->course = $SITE->shortname; $subject = get_string('pendingechecksubject', 'enrol_authorize', $a); $a = new stdClass(); $a->count = $count; $a->url = $CFG->wwwroot . '/enrol/authorize/uploadcsv.php'; $message = get_string('pendingecheckemail', 'enrol_authorize', $a); @email_to_user($adminuser, $adminuser, $subject, $message); } // Daily warning email for pending orders expiring. if (empty($CFG->an_emailexpired)) { return; // not enabled } // Pending orders count will be expired. $timediffem = $settlementtime - (30 - intval($CFG->an_emailexpired)) * $oneday; $select = "(status='" . AN_STATUS_AUTH . "') AND (timecreated<'{$timediffem}') AND (timecreated>'{$timediff30}')"; $count = count_records_select('enrol_authorize', $select); if (!$count) { return; } // Email to admin $a = new stdClass(); $a->pending = $count; $a->days = $CFG->an_emailexpired; $a->course = $SITE->shortname; $subject = get_string('pendingorderssubject', 'enrol_authorize', $a); $a = new stdClass(); $a->pending = $count; $a->days = $CFG->an_emailexpired; $a->course = $SITE->fullname; $a->enrolurl = "{$CFG->wwwroot}/{$CFG->admin}/enrol_config.php?enrol=authorize"; $a->url = $CFG->wwwroot . '/enrol/authorize/index.php?status=' . AN_STATUS_AUTH; $message = get_string('pendingordersemail', 'enrol_authorize', $a); email_to_user($adminuser, $adminuser, $subject, $message); // Email to teachers if (empty($CFG->an_emailexpiredteacher)) { return; // email feature disabled for teachers. } $sorttype = empty($CFG->an_sorttype) ? 'ttl' : $CFG->an_sorttype; $sql = "SELECT e.courseid, e.currency, c.fullname, c.shortname,\n COUNT(e.courseid) AS cnt, SUM(e.amount) as ttl\n FROM {$CFG->prefix}enrol_authorize e\n INNER JOIN {$CFG->prefix}course c ON c.id = e.courseid\n WHERE (e.status = " . AN_STATUS_AUTH . ")\n AND (e.timecreated < {$timediffem})\n AND (e.timecreated > {$timediff30})\n GROUP BY e.courseid\n ORDER BY {$sorttype} DESC"; for ($rs = get_recordset_sql($sql); $courseinfo = rs_fetch_next_record($rs);) { $lastcourse = $courseinfo->courseid; $context = get_context_instance(CONTEXT_COURSE, $lastcourse); if ($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments')) { $a = new stdClass(); $a->course = $courseinfo->shortname; $a->pending = $courseinfo->cnt; $a->days = $CFG->an_emailexpired; $subject = get_string('pendingorderssubject', 'enrol_authorize', $a); $a = new stdClass(); $a->course = $courseinfo->fullname; $a->pending = $courseinfo->cnt; $a->currency = $courseinfo->currency; $a->sumcost = $courseinfo->ttl; $a->days = $CFG->an_emailexpired; $a->url = $CFG->wwwroot . '/enrol/authorize/index.php?course=' . $lastcourse . '&status=' . AN_STATUS_AUTH; $message = get_string('pendingordersemailteacher', 'enrol_authorize', $a); foreach ($paymentmanagers as $paymentmanager) { email_to_user($paymentmanager, $adminuser, $subject, $message); } } } rs_close($rs); }
/** * Performs an action on authorize.net and updates/inserts records. If record update fails, * sends email to admin. * * @param object &$order Which transaction data will be sent. See enrol_authorize table. * @param string &$message Information about error message. * @param object &$extra Extra data that used for refunding and credit card information. * @param int $action Which action will be performed. See AN_ACTION_* * @param string $cctype Used internally to configure credit types automatically. * @return int AN_APPROVED Transaction was successful, AN_RETURNZERO otherwise. Use $message for reason. * @author Ethem Evlice <ethem a.t evlice d.o.t com> * @uses $CFG */ function authorize_action(&$order, &$message, &$extra, $action = AN_ACTION_NONE, $cctype = NULL) { global $CFG; static $conststring; if (!isset($conststring)) { $mconfig = get_config('enrol/authorize'); $constdata = array('x_version' => '3.1', 'x_delim_data' => 'True', 'x_delim_char' => AN_DELIM, 'x_encap_char' => AN_ENCAP, 'x_relay_response' => 'FALSE', 'x_login' => rc4decrypt($mconfig->an_login)); $str = ''; foreach ($constdata as $ky => $vl) { $str .= $ky . '=' . urlencode($vl) . '&'; } $str .= !empty($mconfig->an_tran_key) ? 'x_tran_key=' . urlencode(rc4decrypt($mconfig->an_tran_key)) : 'x_password='******''; } if (empty($order) or empty($order->id)) { $message = "Check order->id!"; return AN_RETURNZERO; } $method = $order->paymentmethod; if (empty($method)) { $method = AN_METHOD_CC; } elseif ($method != AN_METHOD_CC && $method != AN_METHOD_ECHECK) { $message = "Invalid method: {$method}"; return AN_RETURNZERO; } $action = intval($action); if ($method == AN_METHOD_ECHECK) { if ($action != AN_ACTION_AUTH_CAPTURE && $action != AN_ACTION_CREDIT) { $message = "Please perform AUTH_CAPTURE or CREDIT for echecks"; return AN_RETURNZERO; } } $poststring = $conststring; $poststring .= '&x_method=' . $method; $test = !empty($CFG->an_test); $poststring .= '&x_test_request=' . ($test ? 'TRUE' : 'FALSE'); switch ($action) { case AN_ACTION_AUTH_ONLY: case AN_ACTION_CAPTURE_ONLY: case AN_ACTION_AUTH_CAPTURE: if ($order->status != AN_STATUS_NONE) { $message = "Order status must be AN_STATUS_NONE(0)!"; return AN_RETURNZERO; } elseif (empty($extra)) { $message = "Need extra fields!"; return AN_RETURNZERO; } elseif ($action == AN_ACTION_CAPTURE_ONLY and empty($extra->x_auth_code)) { $message = "x_auth_code is required for capture only transactions!"; return AN_RETURNZERO; } $ext = (array) $extra; $poststring .= '&x_type=' . ($action == AN_ACTION_AUTH_ONLY ? 'AUTH_ONLY' : ($action == AN_ACTION_CAPTURE_ONLY ? 'CAPTURE_ONLY' : 'AUTH_CAPTURE')); foreach ($ext as $k => $v) { $poststring .= '&' . $k . '=' . urlencode($v); } break; case AN_ACTION_PRIOR_AUTH_CAPTURE: if ($order->status != AN_STATUS_AUTH) { $message = "Order status must be authorized!"; return AN_RETURNZERO; } if (authorize_expired($order)) { $message = "Transaction must be captured within 30 days. EXPIRED!"; return AN_RETURNZERO; } $poststring .= '&x_type=PRIOR_AUTH_CAPTURE&x_trans_id=' . urlencode($order->transid); break; case AN_ACTION_CREDIT: if ($order->status != AN_STATUS_AUTHCAPTURE) { $message = "Order status must be authorized/captured!"; return AN_RETURNZERO; } if (!authorize_settled($order)) { $message = "Order must be settled. Try VOID, check Cut-Off time if it fails!"; return AN_RETURNZERO; } if (empty($extra->amount)) { $message = "No valid amount!"; return AN_RETURNZERO; } $timenowsettle = authorize_getsettletime(time()); $timediff = $timenowsettle - 120 * 3600 * 24; if ($order->settletime < $timediff) { $message = "Order must be credited within 120 days!"; return AN_RETURNZERO; } $poststring .= '&x_type=CREDIT&x_trans_id=' . urlencode($order->transid); $poststring .= '&x_currency_code=' . urlencode($order->currency); $poststring .= '&x_invoice_num=' . urlencode($extra->orderid); $poststring .= '&x_amount=' . urlencode($extra->amount); if ($method == AN_METHOD_CC) { $poststring .= '&x_card_num=' . sprintf("%04d", intval($order->refundinfo)); } elseif ($method == AN_METHOD_ECHECK && empty($order->refundinfo)) { $message = "Business checkings can be refunded only."; return AN_RETURNZERO; } break; case AN_ACTION_VOID: if (authorize_expired($order) || authorize_settled($order)) { $message = "The transaction cannot be voided due to the fact that it is expired or settled."; return AN_RETURNZERO; } $poststring .= '&x_type=VOID&x_trans_id=' . urlencode($order->transid); break; default: $message = "Invalid action: {$action}"; return AN_RETURNZERO; } $referer = ''; if (!(empty($CFG->an_referer) || $CFG->an_referer == "http://")) { $referer = "Referer: {$CFG->an_referer}\r\n"; } $errno = 0; $errstr = ''; $host = $test ? 'certification.authorize.net' : 'secure.authorize.net'; $fp = fsockopen("ssl://{$host}", 443, $errno, $errstr, 60); if (!$fp) { $message = "no connection: {$errstr} ({$errno})"; return AN_RETURNZERO; } // critical section @ignore_user_abort(true); if (intval(ini_get('max_execution_time')) > 0) { @set_time_limit(300); } fwrite($fp, "POST /gateway/transact.dll HTTP/1.0\r\n" . "Host: {$host}\r\n" . $referer . "Content-type: application/x-www-form-urlencoded\r\n" . "Connection: close\r\n" . "Content-length: " . strlen($poststring) . "\r\n\r\n" . $poststring . "\r\n"); $tmpstr = ''; while (!feof($fp) && !stristr($tmpstr, 'content-length')) { $tmpstr = fgets($fp, 4096); } if (!stristr($tmpstr, 'content-length')) { $message = "content-length error"; @fclose($fp); return AN_RETURNZERO; } $length = trim(substr($tmpstr, strpos($tmpstr, 'content-length') + 15)); fgets($fp, 4096); $data = fgets($fp, $length); @fclose($fp); $response = explode(AN_ENCAP . AN_DELIM . AN_ENCAP, $data); if ($response === false) { $message = "response error"; return AN_RETURNZERO; } $rcount = count($response) - 1; if ($response[0][0] == AN_ENCAP) { $response[0] = substr($response[0], 1); } if (substr($response[$rcount], -1) == AN_ENCAP) { $response[$rcount] = substr($response[$rcount], 0, -1); } $responsecode = intval($response[0]); if ($responsecode == AN_APPROVED || $responsecode == AN_REVIEW) { $transid = floatval($response[6]); if ($test || $transid == 0) { return $responsecode; // don't update original transaction in test mode. } switch ($action) { case AN_ACTION_AUTH_ONLY: case AN_ACTION_CAPTURE_ONLY: case AN_ACTION_AUTH_CAPTURE: case AN_ACTION_PRIOR_AUTH_CAPTURE: $order->transid = $transid; if ($method == AN_METHOD_CC) { if ($action == AN_ACTION_AUTH_ONLY || $responsecode == AN_REVIEW) { $order->status = AN_STATUS_AUTH; } else { $order->status = AN_STATUS_AUTHCAPTURE; $order->settletime = authorize_getsettletime(time()); } } elseif ($method == AN_METHOD_ECHECK) { $order->status = AN_STATUS_UNDERREVIEW; } if (!update_record('enrol_authorize', $order)) { email_to_admin("Error while trying to update data " . "in table enrol_authorize. Please edit manually this record: ID={$order->id}.", $order); } break; case AN_ACTION_CREDIT: // Credit generates new transaction id. // So, $extra must be updated, not $order. $extra->status = AN_STATUS_CREDIT; $extra->transid = $transid; $extra->settletime = authorize_getsettletime(time()); if (!($extra->id = insert_record('enrol_authorize_refunds', $extra))) { unset($extra->id); email_to_admin("Error while trying to insert data " . "into table enrol_authorize_refunds. Please add manually this record:", $extra); } break; case AN_ACTION_VOID: $tableupdate = 'enrol_authorize'; if ($order->status == AN_STATUS_CREDIT) { $tableupdate = 'enrol_authorize_refunds'; unset($order->paymentmethod); } $order->status = AN_STATUS_VOID; if (!update_record($tableupdate, $order)) { email_to_admin("Error while trying to update data " . "in table {$tableupdate}. Please edit manually this record: ID={$order->id}.", $order); } break; } } else { $reasonno = $response[2]; $reasonstr = "reason" . $reasonno; $message = get_string($reasonstr, "enrol_authorize"); if ($message == '[[' . $reasonstr . ']]') { $message = isset($response[3]) ? $response[3] : 'unknown error'; } if ($method == AN_METHOD_CC && !empty($CFG->an_avs) && $response[5] != "P") { $avs = "avs" . strtolower($response[5]); $stravs = get_string($avs, "enrol_authorize"); $message .= "<br />" . get_string("avsresult", "enrol_authorize", $stravs); } if (!$test) { // Autoconfigure :) switch ($reasonno) { // Credit card type isn't accepted case AN_REASON_NOCCTYPE: case AN_REASON_NOCCTYPE2: if (!empty($cctype)) { $ccaccepts = get_list_of_creditcards(); unset($ccaccepts[$cctype]); set_config('an_acceptccs', implode(',', array_keys($ccaccepts))); email_to_admin("{$message} ({$cctype})" . "This is new config(an_acceptccs):", $ccaccepts); } break; // Echecks only // Echecks only case AN_REASON_ACHONLY: set_config('an_acceptmethods', AN_METHOD_ECHECK); email_to_admin("{$message} " . "This is new config(an_acceptmethods):", array(AN_METHOD_ECHECK)); break; // Echecks aren't accepted // Echecks aren't accepted case AN_REASON_NOACH: set_config('an_acceptmethods', AN_METHOD_CC); email_to_admin("{$message} " . "This is new config(an_acceptmethods):", array(AN_METHOD_CC)); break; // This echeck type isn't accepted // This echeck type isn't accepted case AN_REASON_NOACHTYPE: case AN_REASON_NOACHTYPE2: if (!empty($extra->x_echeck_type)) { switch ($extra->x_echeck_type) { // CCD=BUSINESSCHECKING case 'CCD': set_config('an_acceptechecktypes', 'CHECKING,SAVINGS'); email_to_admin("{$message} " . "This is new config(an_acceptechecktypes):", array('CHECKING', 'SAVINGS')); break; // WEB=CHECKING or SAVINGS // WEB=CHECKING or SAVINGS case 'WEB': set_config('an_acceptechecktypes', 'BUSINESSCHECKING'); email_to_admin("{$message} " . "This is new config(an_acceptechecktypes):", array('BUSINESSCHECKING')); break; } } break; } } } return $responsecode; }
function enrol_authorize_upgrade($oldversion = 0) { global $CFG, $THEME, $db; require_once "{$CFG->dirroot}/enrol/authorize/const.php"; $result = true; if ($oldversion < 2005071600) { // Be sure, only last 4 digit is inserted. table_column('enrol_authorize', 'cclastfour', 'cclastfour', 'integer', '4', 'unsigned', '0', 'not null'); table_column('enrol_authorize', 'courseid', 'courseid', 'integer', '10', 'unsigned', '0', 'not null'); table_column('enrol_authorize', 'userid', 'userid', 'integer', '10', 'unsigned', '0', 'not null'); // Add some indexes for speed. execute_sql("ALTER TABLE `{$CFG->prefix}enrol_authorize` ADD INDEX courseid(courseid)", false); execute_sql("ALTER TABLE `{$CFG->prefix}enrol_authorize` ADD INDEX userid(userid)", false); } if ($oldversion && $oldversion < 2005071602) { notify("If you are using the authorize.net enrolment plugin for credit card\n handling, please ensure that you have turned loginhttps ON in Admin >> Variables >> Security."); } if ($oldversion < 2005112100) { table_column('enrol_authorize', '', 'authcode', 'varchar', '6', '', '', '', 'avscode'); // CAPTURE_ONLY table_column('enrol_authorize', '', 'status', 'integer', '10', 'unsigned', '0', 'not null', 'transid'); table_column('enrol_authorize', '', 'timecreated', 'integer', '10', 'unsigned', '0', 'not null', 'status'); table_column('enrol_authorize', '', 'timeupdated', 'integer', '10', 'unsigned', '0', 'not null', 'timecreated'); // status index for speed. execute_sql("ALTER TABLE `{$CFG->prefix}enrol_authorize` ADD INDEX status(status)"); // defaults. $status = AN_STATUS_AUTH | AN_STATUS_CAPTURE; execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='{$status}' WHERE transid<>'0'", false); $timenow = time(); execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET timecreated='{$timenow}', timeupdated='{$timenow}'", false); } if ($oldversion < 2005121200) { // new fields for refund and sales reports. $defaultcurrency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency; table_column('enrol_authorize', '', 'amount', 'varchar', '10', '', '0', 'not null', 'timeupdated'); table_column('enrol_authorize', '', 'currency', 'varchar', '3', '', $defaultcurrency, 'not null', 'amount'); modify_database("", "CREATE TABLE prefix_enrol_authorize_refunds (\n `id` int(10) unsigned NOT NULL auto_increment,\n `orderid` int(10) unsigned NOT NULL default 0,\n `refundtype` int(1) unsigned NOT NULL default 0,\n `amount` varchar(10) NOT NULL default '',\n `transid` int(10) unsigned NULL default 0,\n PRIMARY KEY (`id`),\n KEY `orderid` (`orderid`));"); // defaults. if ($courses = get_records_select('course', '', '', 'id, cost, currency')) { foreach ($courses as $course) { execute_sql("UPDATE {$CFG->prefix}enrol_authorize\n SET amount = '{$course->cost}', currency = '{$course->currency}'\n WHERE courseid = '{$course->id}'", false); } } } if ($oldversion < 2005122200) { // settletime table_column('enrol_authorize_refunds', 'refundtype', 'status', 'integer', '1', 'unsigned', '0', 'not null'); table_column('enrol_authorize_refunds', '', 'settletime', 'integer', '10', 'unsigned', '0', 'not null', 'transid'); table_column('enrol_authorize', 'timeupdated', 'settletime', 'integer', '10', 'unsigned', '0', 'not null'); $status = AN_STATUS_AUTH | AN_STATUS_CAPTURE; if ($settlements = get_records_select('enrol_authorize', "status='{$status}'", '', 'id, settletime')) { include_once "{$CFG->dirroot}/enrol/authorize/authorizenetlib.php"; foreach ($settlements as $settlement) { execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET settletime = '" . authorize_getsettletime($settlement->settletime) . "' WHERE id = '{$settlement->id}'", false); } } } if ($oldversion < 2005122800) { // no need anymore some fields. execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP ccexp", false); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP cvv", false); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP avscode", false); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP authcode", false); } if ($oldversion < 2006010200) { // rename an_review_day if (isset($CFG->an_review_day)) { set_config('an_capture_day', $CFG->an_review_day); delete_records('config', 'name', 'an_review_day'); } } if ($oldversion < 2006020100) { // rename an_cutoff_hour and an_cutoff_min to an_cutoff if (isset($CFG->an_cutoff_hour) && isset($CFG->an_cutoff_min)) { $an_cutoff_hour = intval($CFG->an_cutoff_hour); $an_cutoff_min = intval($CFG->an_cutoff_min); $an_cutoff = $an_cutoff_hour * 60 + $an_cutoff_min; if (set_config('an_cutoff', $an_cutoff)) { delete_records('config', 'name', 'an_cutoff_hour'); delete_records('config', 'name', 'an_cutoff_min'); } } } if ($oldversion < 2006021500) { // transid is int table_column('enrol_authorize', 'transid', 'transid', 'integer', '10', 'unsigned', '0', 'not null'); } if ($oldversion < 2006021501) { // delete an_nextmail record from config_plugins table delete_records('config_plugins', 'name', 'an_nextmail'); } if ($oldversion < 2006050400) { // Create transid indexes for backup & restore speed. execute_sql("ALTER TABLE `{$CFG->prefix}enrol_authorize` ADD INDEX transid(transid)", false); execute_sql("ALTER TABLE `{$CFG->prefix}enrol_authorize_refunds` ADD INDEX transid(transid)", false); } if ($oldversion < 2006060500) { // delete an_nextmail record from config_plugins table delete_records('config_plugins', 'name', 'an_nextmail'); // run twice. } if ($oldversion < 2006081401) { // no need an_teachermanagepay in 1.7 if (isset($CFG->an_teachermanagepay)) { delete_records('config', 'name', 'an_teachermanagepay'); } } if ($oldversion < 2006083100) { // enums are lower case if (isset($CFG->an_acceptmethods)) { set_config('an_acceptmethods', strtolower($CFG->an_acceptmethods)); } // new ENUM field: paymentmethod(cc,echeck) execute_sql("ALTER TABLE `{$CFG->prefix}enrol_authorize` ADD paymentmethod enum('cc', 'echeck') NOT NULL default 'cc' AFTER `id`", true); } ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions. return $result; }
function enrol_authorize_upgrade($oldversion = 0) { global $CFG, $THEME, $db; require_once "{$CFG->dirroot}/enrol/authorize/const.php"; $result = true; if (!($tables = $db->MetaColumns($CFG->prefix . 'enrol_authorize'))) { $installfirst = true; } if ($oldversion == 0 || !empty($installfirst)) { // First time install $result = modify_database("{$CFG->dirroot}/enrol/authorize/db/postgres7.sql"); return $result; // RETURN, sql file contains last upgrades. } // 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\n handling, please ensure that you have turned loginhttps ON in Admin >> Variables >> Security."); } if ($oldversion < 2005080200) { // Be sure, only last 4 digit is inserted. table_column('enrol_authorize', 'cclastfour', 'cclastfour', 'integer', '4', 'unsigned', '0', 'not null'); table_column('enrol_authorize', 'courseid', 'courseid', 'integer', '10', 'unsigned', '0', 'not null'); table_column('enrol_authorize', 'userid', 'userid', 'integer', '10', 'unsigned', '0', 'not null'); // Add some indexes for speed. execute_sql("CREATE INDEX {$CFG->prefix}enrol_authorize_courseid_idx ON {$CFG->prefix}enrol_authorize (courseid);", false); execute_sql("CREATE INDEX {$CFG->prefix}enrol_authorize_userid_idx ON {$CFG->prefix}enrol_authorize (userid);", false); } if ($oldversion < 2005112100) { table_column('enrol_authorize', '', 'authcode', 'varchar', '6', '', '', '', 'avscode'); // CAPTURE_ONLY table_column('enrol_authorize', '', 'status', 'integer', '10', 'unsigned', '0', 'not null', 'transid'); table_column('enrol_authorize', '', 'timecreated', 'integer', '10', 'unsigned', '0', 'not null', 'status'); table_column('enrol_authorize', '', 'timeupdated', 'integer', '10', 'unsigned', '0', 'not null', 'timecreated'); // status index for speed. modify_database('', "CREATE INDEX prefix_enrol_authorize_status_idx ON prefix_enrol_authorize (status);"); // defaults. $status = AN_STATUS_AUTH | AN_STATUS_CAPTURE; execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET status='{$status}' WHERE transid<>'0'", false); $timenow = time(); execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET timecreated='{$timenow}', timeupdated='{$timenow}'", false); } if ($oldversion < 2005121200) { // new fields for refund and sales reports. $defaultcurrency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency; table_column('enrol_authorize', '', 'amount', 'varchar', '10', '', '0', 'not null', 'timeupdated'); table_column('enrol_authorize', '', 'currency', 'varchar', '3', '', $defaultcurrency, 'not null', 'amount'); modify_database("", "CREATE TABLE prefix_enrol_authorize_refunds (\n id SERIAL PRIMARY KEY,\n orderid INTEGER NOT NULL default 0,\n refundtype INTEGER NOT NULL default 0,\n amount varchar(10) NOT NULL default '',\n transid INTEGER NULL default 0\n );"); modify_database("", "CREATE INDEX prefix_enrol_authorize_refunds_orderid_idx ON prefix_enrol_authorize_refunds (orderid);"); // defaults. if ($courses = get_records_select('course', '', '', 'id, cost, currency')) { foreach ($courses as $course) { execute_sql("UPDATE {$CFG->prefix}enrol_authorize\n SET amount = '{$course->cost}', currency = '{$course->currency}'\n WHERE courseid = '{$course->id}'", false); } } } if ($oldversion < 2005122200) { // settletime table_column('enrol_authorize_refunds', 'refundtype', 'status', 'integer', '1', 'unsigned', '0', 'not null'); table_column('enrol_authorize_refunds', '', 'settletime', 'integer', '10', 'unsigned', '0', 'not null', 'transid'); table_column('enrol_authorize', 'timeupdated', 'settletime', 'integer', '10', 'unsigned', '0', 'not null'); $status = AN_STATUS_AUTH | AN_STATUS_CAPTURE; if ($settlements = get_records_select('enrol_authorize', "status='{$status}'", '', 'id, settletime')) { include_once "{$CFG->dirroot}/enrol/authorize/authorizenetlib.php"; foreach ($settlements as $settlement) { execute_sql("UPDATE {$CFG->prefix}enrol_authorize SET settletime = '" . authorize_getsettletime($settlement->settletime) . "' WHERE id = '{$settlement->id}'", false); } } } if ($oldversion < 2005122800) { // no need anymore some fields. execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP ccexp", false); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP cvv", false); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP avscode", false); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize DROP authcode", false); } if ($oldversion < 2006010200) { // rename an_review_day if (isset($CFG->an_review_day)) { set_config('an_capture_day', $CFG->an_review_day); delete_records('config', 'name', 'an_review_day'); } } if ($oldversion < 2006020100) { // rename an_cutoff_hour and an_cutoff_min to an_cutoff if (isset($CFG->an_cutoff_hour) && isset($CFG->an_cutoff_min)) { $an_cutoff_hour = intval($CFG->an_cutoff_hour); $an_cutoff_min = intval($CFG->an_cutoff_min); $an_cutoff = $an_cutoff_hour * 60 + $an_cutoff_min; if (set_config('an_cutoff', $an_cutoff)) { delete_records('config', 'name', 'an_cutoff_hour'); delete_records('config', 'name', 'an_cutoff_min'); } } } if ($oldversion < 2006021500) { // transid is int table_column('enrol_authorize', 'transid', 'transid', 'integer', '10', 'unsigned', '0', 'not null'); } if ($oldversion < 2006021501) { // delete an_nextmail record from config_plugins table delete_records('config_plugins', 'name', 'an_nextmail'); } if ($oldversion < 2006050400) { // Create transid indexes for backup & restore speed. execute_sql("CREATE INDEX {$CFG->prefix}enrol_authorize_transid_idx ON {$CFG->prefix}enrol_authorize(transid);", false); execute_sql("CREATE INDEX {$CFG->prefix}enrol_authorize_refunds_transid_idx ON {$CFG->prefix}enrol_authorize_refunds(transid);", false); } if ($oldversion < 2006060500) { // delete an_nextmail record from config_plugins table delete_records('config_plugins', 'name', 'an_nextmail'); // run twice. } if ($oldversion < 2006081401) { // no need an_teachermanagepay in 1.7 if (isset($CFG->an_teachermanagepay)) { delete_records('config', 'name', 'an_teachermanagepay'); } } if ($oldversion < 2006083100) { // enums are lower case if (isset($CFG->an_acceptmethods)) { set_config('an_acceptmethods', strtolower($CFG->an_acceptmethods)); } // new ENUM field: paymentmethod(cc,echeck) table_column('enrol_authorize', '', 'paymentmethod', 'varchar', '6', '', 'cc', 'not null'); execute_sql("ALTER TABLE {$CFG->prefix}enrol_authorize ADD CONSTRAINT enroauth_pay_ck CHECK (paymentmethod IN ('cc', 'echeck'))", true); } ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions. return $result; }