|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 00028 require_once('../../config.php'); 00029 require_once($CFG->dirroot.'/enrol/authorize/const.php'); 00030 require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php'); 00031 require_once($CFG->libdir.'/eventslib.php'); 00032 require_once('import_form.php'); 00033 00035 require_login(); 00036 require_capability('enrol/authorize:uploadcsv', get_context_instance(CONTEXT_SYSTEM)); 00037 00039 $struploadcsv = get_string('uploadcsv', 'enrol_authorize'); 00040 $managebutton = "<form method='get' action='index.php'><input type='submit' value='".get_string('paymentmanagement', 'enrol_authorize')."' /></form>"; 00041 00042 $form = new enrol_authorize_import_form(); 00043 00044 $PAGE->set_url('/enrol/authorize/uploadcsv.php'); 00045 $PAGE->navbar->add(get_string('paymentmanagement', 'enrol_authorize'), 'index.php'); 00046 $PAGE->navbar->add($struploadcsv, 'uploadcsv.php'); 00047 $PAGE->set_title($struploadcsv); 00048 $PAGE->set_cacheable(false); 00049 $PAGE->set_button($managebutton); 00050 echo $OUTPUT->header(); 00051 echo $OUTPUT->heading($struploadcsv); 00052 00054 if (!$form->get_data()) { 00055 $form->display(); 00056 } else { 00057 $filename = $CFG->tempdir . '/enrolauthorize/importedfile_'.time().'.csv'; 00058 make_temp_directory('enrolauthorize'); 00059 // Fix mac/dos newlines 00060 $text = $form->get_file_content('csvfile'); 00061 $text = preg_replace('!\r\n?!', "\n", $text); 00062 $fp = fopen($filename, "w"); 00063 fwrite($fp, $text); 00064 fclose($fp); 00065 authorize_process_csv($filename); 00066 } 00067 00069 echo $OUTPUT->footer(); 00070 00071 function authorize_process_csv($filename) { 00072 global $CFG, $SITE, $DB; 00073 00074 $plugin = enrol_get_plugin('authorize'); 00075 00077 $myfields = array( 00078 'Transaction ID', // enrol_authorize.transid or enrol_authorize_refunds.transid; See: Reference Transaction ID 00079 'Transaction Status', // Under Review,Approved Review,Review Failed,Settled Successfully 00080 'Transaction Type', // Authorization w/ Auto Capture, Authorization Only, Capture Only, Credit, Void, Prior Authorization Capture 00081 'Settlement Amount', // 00082 'Settlement Currency', // 00083 'Settlement Date/Time', // 00084 'Authorization Amount', // 00085 'Authorization Currency', // 00086 'Submit Date/Time', // timecreated 00087 'Reference Transaction ID', // enrol_authorize.transid if Transaction Type = Credit 00088 'Total Amount', // enrol_authorize.cost 00089 'Currency', // enrol_authorize.currency 00090 'Invoice Number', // enrol_authorize.id: Don't trust this! Backup/Restore changes this 00091 'Customer ID' // enrol_authorize.userid 00092 ); 00093 00095 $handle = fopen($filename, "r"); 00096 if (!$handle) { 00097 print_error('cannotopencsv'); 00098 } 00099 $firstline = fgetcsv($handle, 8192, ","); 00100 $numfields = count($firstline); 00101 if ($numfields != 49 && $numfields != 70) { 00102 @fclose($handle); 00103 print_error('csvinvalidcolsnum'); 00104 } 00105 00107 $csvfields = array(); 00108 foreach ($myfields as $myfield) { 00109 $csvindex = array_search($myfield, $firstline); 00110 if ($csvindex === false) { 00111 $csvfields = array(); 00112 break; 00113 } 00114 $csvfields[$myfield] = $csvindex; 00115 } 00116 if (empty($csvfields)) { 00117 @fclose($handle); 00118 print_error('csvinvalidcols'); 00119 } 00120 00122 $sendem = array(); 00123 $ignoredlines = ''; 00124 00125 $imported = 0; 00126 $updated = 0; 00127 $ignored = 0; 00128 while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) { 00129 if (count($data) != $numfields) { 00130 $ignored++; // ignore empty lines 00131 continue; 00132 } 00133 00134 $transid = $data[$csvfields['Transaction ID']]; 00135 $transtype = $data[$csvfields['Transaction Type']]; 00136 $transstatus = $data[$csvfields['Transaction Status']]; 00137 $reftransid = $data[$csvfields['Reference Transaction ID']]; 00138 $settlementdate = strtotime($data[$csvfields['Settlement Date/Time']]); 00139 00140 if ($transstatus == 'Approved Review' || $transstatus == 'Review Failed') { 00141 if (($order = $DB->get_record('enrol_authorize', array('transid'=>$transid)))) { 00142 $order->status = ($transstatus == 'Approved Review') ? AN_STATUS_APPROVEDREVIEW : AN_STATUS_REVIEWFAILED; 00143 $DB->update_record('enrol_authorize', $order); 00144 $updated++; // Updated order status 00145 } 00146 continue; 00147 } 00148 00149 if (!empty($reftransid) && is_numeric($reftransid) && 'Settled Successfully' == $transstatus && 'Credit' == $transtype) { 00150 if (($order = $DB->get_record('enrol_authorize', array('transid'=>$reftransid)))) { 00151 if (AN_METHOD_ECHECK == $order->paymentmethod) { 00152 $refund = $DB->get_record('enrol_authorize_refunds', array('transid'=>$transid)); 00153 if ($refund) { 00154 $refund->status = AN_STATUS_CREDIT; 00155 $refund->settletime = $settlementdate; 00156 $DB->update_record('enrol_authorize_refunds', $refund); 00157 $updated++; 00158 } 00159 else { 00160 $ignored++; 00161 $ignoredlines .= $reftransid . ": Not our business(Reference Transaction ID)\n"; 00162 } 00163 } 00164 } 00165 else { 00166 $ignored++; 00167 $ignoredlines .= $reftransid . ": Not our business(Transaction ID)\n"; 00168 } 00169 continue; 00170 } 00171 00172 if (! ($transstatus == 'Settled Successfully' && $transtype == 'Authorization w/ Auto Capture')) { 00173 $ignored++; 00174 $ignoredlines .= $transid . ": Not settled\n"; 00175 continue; 00176 } 00177 00178 // TransactionId must match 00179 $order = $DB->get_record('enrol_authorize', array('transid'=>$transid)); 00180 if (!$order) { 00181 $ignored++; 00182 $ignoredlines .= $transid . ": Not our business\n"; 00183 continue; 00184 } 00185 00186 // Authorized/Captured and Settled 00187 $order->status = AN_STATUS_AUTHCAPTURE; 00188 $order->settletime = $settlementdate; 00189 $DB->update_record('enrol_authorize', $order); 00190 $updated++; // Updated order status and settlement date 00191 00192 if ($order->paymentmethod != AN_METHOD_ECHECK) { 00193 $ignored++; 00194 $ignoredlines .= $transid . ": The method must be echeck\n"; 00195 continue; 00196 } 00197 00198 // Get course and context 00199 $course = $DB->get_record('course', array('id'=>$order->courseid)); 00200 if (!$course) { 00201 $ignored++; 00202 $ignoredlines .= $transid . ": Could not find this course: " . $order->courseid . "\n"; 00203 continue; 00204 } 00205 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); 00206 if (!$coursecontext) { 00207 $ignored++; 00208 $ignoredlines .= $transid . ": Could not find course context: " . $order->courseid . "\n"; 00209 continue; 00210 } 00211 00212 // Get user 00213 $user = $DB->get_record('user', array('id'=>$order->userid)); 00214 if (!$user) { 00215 $ignored++; 00216 $ignoredlines .= $transid . ": Could not find this user: " . $order->userid . "\n"; 00217 continue; 00218 } 00219 00220 // If user wasn't enrolled, enrol now. Ignore otherwise. Because admin user might submit this file again. 00221 if (($role = get_default_course_role($course))) { 00222 if (! user_has_role_assignment($user->id, $role->id, $coursecontext->id)) { 00223 $timestart = $timeend = 0; 00224 if ($course->enrolperiod) { 00225 $timestart = time(); 00226 $timeend = $timestart + $course->enrolperiod; 00227 } 00228 // Enrol user 00229 $pinstance = $DB->get_record('enrol', array('id'=>$order->instanceid)); 00230 $plugin->enrol_user($pinstance, $user->id, $pinstance->roleid, $timestart, $timeend); 00231 00232 $imported++; 00233 if ($plugin->get_config('enrol_mailstudents')) { 00234 $sendem[] = $order->id; 00235 } 00236 } 00237 } 00238 } 00239 fclose($handle); 00240 00242 if (!empty($ignoredlines)) { 00243 $admin = get_admin(); 00244 00245 $eventdata = new stdClass(); 00246 $eventdata->modulename = 'moodle'; 00247 $eventdata->component = 'enrol_authorize'; 00248 $eventdata->name = 'authorize_enrolment'; 00249 $eventdata->userfrom = $admin; 00250 $eventdata->userto = $admin; 00251 $eventdata->subject = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))).': Authorize.net CSV ERROR LOG'; 00252 $eventdata->fullmessage = $ignoredlines; 00253 $eventdata->fullmessageformat = FORMAT_PLAIN; 00254 $eventdata->fullmessagehtml = ''; 00255 $eventdata->smallmessage = ''; 00256 message_send($eventdata); 00257 } 00258 00260 if (!empty($sendem)) { 00261 send_welcome_messages($sendem); 00262 } 00263 00265 notice("<b>Done...</b><br />Imported: $imported<br />Updated: $updated<br />Ignored: $ignored"); 00266 }