libdir.'/uploadlib.php');
require_once($CFG->dirroot.'/enrol/authorize/const.php');
require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
/// Require capabilites
require_login();
require_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER, $USER->id));
/// Print header
$struploadcsv = get_string('uploadcsv', 'enrol_authorize');
$navlinks = array();
$navlinks[] = array('name' => $struploadcsv, 'link' => "uploadcsv.php", 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header_simple($struploadcsv, "", $navigation);
print_heading_with_help($struploadcsv, 'uploadcsv', 'enrol/authorize');
/// Handle CSV file
if (($form = data_submitted()) && confirm_sesskey()) {
$um = new upload_manager('csvfile', false, false, null, false, 0);
if ($um->preprocess_files()) {
$filename = $um->files['csvfile']['tmp_name'];
// Fix mac/dos newlines
$text = file_get_contents($filename);
$text = preg_replace('!\r\n?!', "\n", $text);
$fp = fopen($filename, "w");
fwrite($fp, $text);
fclose($fp);
authorize_process_csv($filename);
}
}
/// Print submit form
$maxuploadsize = get_max_upload_file_size();
echo '
';
/// Print footer
print_footer();
?>INVALID CSV FILE: First line must include 'Header Fields' and
the file must be type of
'Expanded Fields/Comma Separated'
or
'Expanded Fields with CAVV Result Code/Comma Separated'");
}
/// Read lines
$sendem = array();
$ignoredlines = '';
$imported = 0;
$updated = 0;
$ignored = 0;
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
if (count($data) != $numfields) {
$ignored++; // ignore empty lines
continue;
}
$transid = $data[$csvfields['Transaction ID']];
$transtype = $data[$csvfields['Transaction Type']];
$transstatus = $data[$csvfields['Transaction Status']];
$reftransid = $data[$csvfields['Reference Transaction ID']];
$settlementdate = strtotime($data[$csvfields['Settlement Date/Time']]);
if ($transstatus == 'Approved Review' || $transstatus == 'Review Failed') {
if (($order = get_record('enrol_authorize', 'transid', $transid))) {
$order->status = ($transstatus == 'Approved Review') ? AN_STATUS_APPROVEDREVIEW : AN_STATUS_REVIEWFAILED;
update_record('enrol_authorize', $order);
$updated++; // Updated order status
}
continue;
}
if (!empty($reftransid) && is_numeric($reftransid) && 'Settled Successfully' == $transstatus && 'Credit' == $transtype) {
if (($order = get_record('enrol_authorize', 'transid', $reftransid))) {
if (AN_METHOD_ECHECK == $order->paymentmethod) {
$refund = get_record('enrol_authorize_refunds', 'transid', $transid);
if ($refund) {
$refund->status = AN_STATUS_CREDIT;
$refund->settletime = $settlementdate;
update_record('enrol_authorize_refunds', $refund);
$updated++;
}
else {
$ignored++;
$ignoredlines .= $reftransid . ": Not our business(Reference Transaction ID)\n";
}
}
}
else {
$ignored++;
$ignoredlines .= $reftransid . ": Not our business(Transaction ID)\n";
}
continue;
}
if (! ($transstatus == 'Settled Successfully' && $transtype == 'Authorization w/ Auto Capture')) {
$ignored++;
$ignoredlines .= $transid . ": Not settled\n";
continue;
}
// TransactionId must match
$order = get_record('enrol_authorize', 'transid', $transid);
if (!$order) {
$ignored++;
$ignoredlines .= $transid . ": Not our business\n";
continue;
}
// Authorized/Captured and Settled
$order->status = AN_STATUS_AUTHCAPTURE;
$order->settletime = $settlementdate;
update_record('enrol_authorize', $order);
$updated++; // Updated order status and settlement date
if ($order->paymentmethod != AN_METHOD_ECHECK) {
$ignored++;
$ignoredlines .= $transid . ": The method must be echeck\n";
continue;
}
// Get course and context
$course = get_record('course', 'id', $order->courseid);
if (!$course) {
$ignored++;
$ignoredlines .= $transid . ": Could not find this course: " . $order->courseid . "\n";
continue;
}
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
if (!$coursecontext) {
$ignored++;
$ignoredlines .= $transid . ": Could not find course context: " . $order->courseid . "\n";
continue;
}
// Get user
$user = get_record('user', 'id', $order->userid);
if (!$user) {
$ignored++;
$ignoredlines .= $transid . ": Could not find this user: " . $order->userid . "\n";
continue;
}
// If user wasn't enrolled, enrol now. Ignore otherwise. Because admin user might submit this file again.
if (($role = get_default_course_role($course))) {
if (! user_has_role_assignment($user->id, $role->id, $coursecontext->id)) {
$timestart = $timeend = 0;
if ($course->enrolperiod) {
$timestart = time();
$timeend = $timestart + $course->enrolperiod;
}
if (role_assign($role->id, $user->id, 0, $coursecontext->id, $timestart, $timeend, 0, 'authorize')) {
$imported++;
if (!empty($CFG->enrol_mailstudents)) {
$sendem[] = $order->id;
}
}
else {
$ignoredlines .= $transid . ": Error while trying to enrol " . fullname($user) . " in '$course->fullname' \n";
}
}
}
}
fclose($handle);
/// Send email to admin
if (!empty($ignoredlines)) {
$admin = get_admin();
email_to_user($admin, $admin, "$SITE->fullname: Authorize.net CSV ERROR LOG", $ignoredlines);
}
/// Send welcome messages to users
if (!empty($sendem)) {
send_welcome_messages($sendem);
}
/// Show result
notice("Done...
Imported: $imported
Updated: $updated
Ignored: $ignored");
}
?>