4 require_once('../../config.php');
5 require_once($CFG->libdir
.'/uploadlib.php');
6 require_once($CFG->dirroot
.'/enrol/authorize/const.php');
7 require_once($CFG->dirroot
.'/enrol/authorize/localfuncs.php');
9 /// Require capabilites
11 require_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_USER
, $USER->id
));
14 $struploadcsv = get_string('uploadcsv', 'enrol_authorize');
15 print_header_simple($struploadcsv, "", "<a href=\"uploadcsv.php\">$struploadcsv</a>");
16 print_heading_with_help($struploadcsv, 'uploadcsv', 'enrol/authorize');
19 if ($form = data_submitted() && confirm_sesskey()) {
20 $um = new upload_manager('csvfile', false, false, null, false, 0);
21 if ($um->preprocess_files()) {
22 $filename = $um->files
['csvfile']['tmp_name'];
23 // Fix mac/dos newlines
24 $text = file_get_contents($filename);
25 $text = preg_replace('!\r\n?!', "\n", $text);
26 $fp = fopen($filename, "w");
29 authorize_process_csv($filename);
34 $maxuploadsize = get_max_upload_file_size();
35 echo '<center><form method="post" enctype="multipart/form-data" action="uploadcsv.php">
36 <input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />
37 <input type="hidden" name="sesskey" value="'.$USER->sesskey
.'">';
38 upload_print_form_fragment(1, array('csvfile'), array(get_string('file')));
39 echo '<input type="submit" value="'.get_string('upload').'" />';
40 echo '</form></center><br />';
47 function authorize_process_csv($filename)
51 /// We need these fields
53 'Transaction ID', // enrol_authorize.transid or enrol_authorize_refunds.transid; See: Reference Transaction ID
54 'Transaction Status', // Under Review,Approved Review,Review Failed,Settled Successfully
55 'Transaction Type', // Authorization w/ Auto Capture, Authorization Only, Capture Only, Credit, Void, Prior Authorization Capture
56 'Settlement Amount', //
57 'Settlement Currency', //
58 'Settlement Date/Time', //
59 'Authorization Amount', //
60 'Authorization Currency', //
61 'Submit Date/Time', // timecreated
62 'Reference Transaction ID', // enrol_authorize.transid if Transaction Type = Credit
63 'Total Amount', // enrol_authorize.cost
64 'Currency', // enrol_authorize.currency
65 'Invoice Number', // enrol_authorize.id: Don't trust this! Backup/Restore changes this
66 'Customer ID' // enrol_authorize.userid
69 /// Open the file and get first line
70 $handle = fopen($filename, "r");
72 error('CANNOT OPEN CSV FILE');
74 $firstline = fgetcsv($handle, 8192, ",");
75 $numfields = count($firstline);
76 if ($numfields != 49 && $numfields != 70) {
78 error('INVALID CSV FILE; Each line must include 49 or 70 fields');
83 foreach ($myfields as $myfield) {
84 $csvindex = array_search($myfield, $firstline);
85 if ($csvindex === false) {
89 $csvfields[$myfield] = $csvindex;
91 if (empty($csvfields)) {
93 error("<b>INVALID CSV FILE:</b> First line must include 'Header Fields' and
94 the file must be type of <br />'Expanded Fields/Comma Separated'<br />or<br />
95 'Expanded Fields with CAVV Result Code/Comma Separated'");
105 while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
106 if (count($data) != $numfields) {
107 $ignored++
; // ignore empty lines
111 $transid = $data[$csvfields['Transaction ID']];
112 $transtype = $data[$csvfields['Transaction Type']];
113 $transstatus = $data[$csvfields['Transaction Status']];
114 $reftransid = $data[$csvfields['Reference Transaction ID']];
115 $settlementdate = strtotime($data[$csvfields['Settlement Date/Time']]);
117 if ($transstatus == 'Approved Review' ||
$transstatus == 'Review Failed') {
118 if ($order = get_record('enrol_authorize', 'transid', $transid)) {
119 $order->status
= ($transstatus == 'Approved Review') ? AN_STATUS_APPROVEDREVIEW
: AN_STATUS_REVIEWFAILED
;
120 update_record('enrol_authorize', $order);
121 $updated++
; // Updated order status
126 if (!empty($reftransid) && is_numeric($reftransid) && 'Settled Successfully' == $transstatus && 'Credit' == $transtype) {
127 if ($order = get_record('enrol_authorize', 'transid', $reftransid)) {
128 if (AN_METHOD_ECHECK
== $order->paymentmethod
) {
129 $refund = get_record('enrol_authorize_refunds', 'transid', $transid);
131 $refund->status
= AN_STATUS_CREDIT
;
132 $refund->settletime
= $settlementdate;
133 update_record('enrol_authorize_refunds', $refund);
138 $ignoredlines .= $reftransid . ": Not our business(Reference Transaction ID)\n";
144 $ignoredlines .= $reftransid . ": Not our business(Transaction ID)\n";
149 if (! ($transstatus == 'Settled Successfully' && $transtype == 'Authorization w/ Auto Capture')) {
151 $ignoredlines .= $transid . ": Not settled\n";
155 // TransactionId must match
156 $order = get_record('enrol_authorize', 'transid', $transid);
159 $ignoredlines .= $transid . ": Not our business\n";
163 // Authorized/Captured and Settled
164 $order->status
= AN_STATUS_AUTHCAPTURE
;
165 $order->settletime
= $settlementdate;
166 update_record('enrol_authorize', $order);
167 $updated++
; // Updated order status and settlement date
169 if ($order->paymentmethod
!= AN_METHOD_ECHECK
) {
171 $ignoredlines .= $transid . ": The method must be echeck\n";
175 // Get course and context
176 $course = get_record('course', 'id', $order->courseid
);
179 $ignoredlines .= $transid . ": Could not find this course: " . $order->courseid
. "\n";
182 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
183 if (!$coursecontext) {
185 $ignoredlines .= $transid . ": Could not find course context: " . $order->courseid
. "\n";
190 $user = get_record('user', 'id', $order->userid
);
193 $ignoredlines .= $transid . ": Could not find this user: " . $order->userid
. "\n";
197 // If user wasn't enrolled, enrol now. Ignore otherwise. Because admin user might submit this file again.
198 if ($role = get_default_course_role($course)) {
199 if (! user_has_role_assignment($user->id
, $role->id
, $coursecontext->id
)) {
200 $timestart = $timeend = 0;
201 if ($course->enrolperiod
) {
203 $timeend = $timestart +
$course->enrolperiod
;
205 if (role_assign($role->id
, $user->id
, 0, $coursecontext->id
, $timestart, $timeend, 0, 'manual')) {
207 if (!empty($CFG->enrol_mailstudents
)) {
208 $sendem[] = $order->id
;
212 $ignoredlines .= $transid . ": Error while trying to enrol " . fullname($user) . " in '$course->fullname' \n";
219 /// Send email to admin
220 if (!empty($ignoredlines)) {
221 $admin = get_admin();
222 email_to_user($admin, $admin, "$SITE->fullname: Authorize.net CSV ERROR LOG", $ignoredlines);
225 /// Send welcome messages to users
226 if (!empty($sendem)) {
227 send_welcome_messages($sendem);
231 notice("<b>Done...</b><br />Imported: $imported<br />Updated: $updated<br />Ignored: $ignored");