|
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 00029 require_once($CFG->libdir.'/eventslib.php'); 00030 00031 function get_course_cost($plugininstance) { 00032 $defaultplugin = enrol_get_plugin('authorize'); 00033 00034 $cost = (float)0; 00035 $currency = (!empty($plugininstance->currency)) 00036 ? $plugininstance->currency :( empty($defaultplugin->currency) 00037 ? 'USD' : $defaultplugin->enrol_currency ); 00038 00039 if (!empty($plugininstance->cost)) { 00040 $cost = (float)(((float)$plugininstance->cost) < 0) ? $defaultplugin->cost : $plugininstance->cost; 00041 } 00042 00043 $cost = format_float($cost, 2); 00044 $ret = array( 00045 'cost' => $cost, 00046 'currency' => $currency 00047 ); 00048 00049 return $ret; 00050 } 00051 00052 function zero_cost($plugininstance) { 00053 $curcost = get_course_cost($plugininstance); 00054 return (abs($curcost['cost']) < 0.01); 00055 } 00056 00057 function prevent_double_paid($plugininstance) { 00058 global $CFG, $SESSION, $USER, $DB; 00059 $plugin = enrol_get_plugin('authorize'); 00060 00061 $sql = "SELECT id FROM {enrol_authorize} WHERE userid = ? AND courseid = ? AND instanceid = ?"; 00062 $params = array($USER->id, $plugininstance->courseid, $plugininstance->id); 00063 00064 if (!$plugin->get_config('an_test')) { // Real mode 00065 $sql .= ' AND status IN(?,?,?)'; 00066 $params[] = AN_STATUS_AUTH; 00067 $params[] = AN_STATUS_UNDERREVIEW; 00068 $params[] = AN_STATUS_APPROVEDREVIEW; 00069 } 00070 else { // Test mode 00071 $sql .= ' AND status=?'; 00072 $params[] = AN_STATUS_NONE; 00073 } 00074 00075 if (($recid = $DB->get_field_sql($sql, $params))) { 00076 $a = new stdClass; 00077 $a->orderid = $recid; 00078 $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid"; 00079 redirect($a->url, get_string("paymentpending", "enrol_authorize", $a), '10'); 00080 return; 00081 } 00082 if (isset($SESSION->ccpaid)) { 00083 unset($SESSION->ccpaid); 00084 redirect($CFG->wwwroot . '/login/logout.php?sesskey='.sesskey()); 00085 return; 00086 } 00087 } 00088 00089 function get_list_of_creditcards($getall = false) { 00090 $plugin = enrol_get_plugin('authorize'); 00091 00092 $alltypes = array( 00093 'mcd' => 'Master Card', 00094 'vis' => 'Visa', 00095 'amx' => 'American Express', 00096 'dsc' => 'Discover', 00097 'dnc' => 'Diners Club', 00098 'jcb' => 'JCB', 00099 'swi' => 'Switch', 00100 'dlt' => 'Delta', 00101 'enr' => 'EnRoute' 00102 ); 00103 00104 if ($getall) { 00105 return $alltypes; 00106 } 00107 00108 $ret = array(); 00109 foreach ($alltypes as $code=>$name) { 00110 if ($plugin->get_config("an_acceptcc_{$code}")) { 00111 $ret[$code] = $name; 00112 } 00113 } 00114 00115 return $ret; 00116 } 00117 00118 function get_list_of_payment_methods($getall = false) { 00119 $plugin = enrol_get_plugin('authorize'); 00120 $method_cc = $plugin->get_config('an_acceptmethod_cc'); 00121 $method_echeck = $plugin->get_config('an_acceptmethod_echeck'); 00122 00123 00124 if ($getall || (empty($method_cc) && empty($method_echeck))) { 00125 return array(AN_METHOD_CC, AN_METHOD_ECHECK); 00126 } else { 00127 $methods = array(); 00128 if ($method_cc) { 00129 $methods[] = AN_METHOD_CC; 00130 } 00131 00132 if ($method_echeck) { 00133 $methods[] = AN_METHOD_ECHECK; 00134 } 00135 00136 return $methods; 00137 } 00138 } 00139 00140 function get_list_of_bank_account_types($getall = false) { 00141 $plugin = enrol_get_plugin('authorize'); 00142 $alltypes = array('CHECKING', 'BUSINESSCHECKING', 'SAVINGS'); 00143 00144 if ($getall) { 00145 return $alltypes; 00146 } else { 00147 $types = array(); 00148 foreach ($alltypes as $type) { 00149 if ($plugin->get_config("an_acceptecheck_{$type}")) { 00150 $types[] = $type; 00151 } 00152 } 00153 00154 return $types; 00155 } 00156 } 00157 00158 function message_to_admin($subject, $data) { 00159 global $SITE; 00160 00161 $admin = get_admin(); 00162 $data = (array)$data; 00163 00164 $emailmessage = "$SITE->fullname: Transaction failed.\n\n$subject\n\n"; 00165 $emailmessage .= print_r($data, true); 00166 $eventdata = new stdClass(); 00167 $eventdata->modulename = 'moodle'; 00168 $eventdata->component = 'enrol_authorize'; 00169 $eventdata->name = 'authorize_enrolment'; 00170 $eventdata->userfrom = $admin; 00171 $eventdata->userto = $admin; 00172 $eventdata->subject = "$SITE->fullname: Authorize.net ERROR"; 00173 $eventdata->fullmessage = $emailmessage; 00174 $eventdata->fullmessageformat = FORMAT_PLAIN; 00175 $eventdata->fullmessagehtml = ''; 00176 $eventdata->smallmessage = ''; 00177 message_send($eventdata); 00178 } 00179 00180 function send_welcome_messages($orderdata) { 00181 global $CFG, $SITE, $DB; 00182 00183 if (empty($orderdata)) { 00184 return; 00185 } 00186 00187 if (is_numeric($orderdata)) { 00188 $orderdata = array($orderdata); 00189 } 00190 00191 $sql = "SELECT e.id, e.courseid, e.userid, c.fullname 00192 FROM {enrol_authorize} e 00193 JOIN {course} c ON c.id = e.courseid 00194 WHERE e.id IN(" . implode(',', $orderdata) . ") 00195 ORDER BY e.userid"; 00196 00197 $rs = $DB->get_recordset_sql($sql); 00198 if (!$rs->valid()) { 00199 $rs->close(); // Not going to iterate (but exit), close rs 00200 return; 00201 } 00202 00203 if ($rs->valid() and $ei = current($rs)) 00204 { 00205 if (1 < count($orderdata)) { 00206 $sender = get_admin(); 00207 } 00208 else { 00209 $context = get_context_instance(CONTEXT_COURSE, $ei->courseid); 00210 $paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments', '', '', '0', '1'); 00211 $sender = array_shift($paymentmanagers); 00212 } 00213 00214 do 00215 { 00216 $usercourses = array(); 00217 $lastuserid = $ei->userid; 00218 00219 while ($ei && $ei->userid == $lastuserid) { 00220 $context = get_context_instance(CONTEXT_COURSE, $ei->courseid); 00221 $usercourses[] = format_string($ei->fullname, true, array('context' => $context)); 00222 if (!$rs->valid()) { 00223 break; 00224 } 00225 $rs->next(); 00226 $ei = $rs->current(); 00227 } 00228 00229 if (($user = $DB->get_record('user', array('id'=>$lastuserid)))) { 00230 $a = new stdClass; 00231 $a->name = $user->firstname; 00232 $a->courses = implode("\n", $usercourses); 00233 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$lastuserid"; 00234 $a->paymenturl = "$CFG->wwwroot/enrol/authorize/index.php?user=$lastuserid"; 00235 $emailmessage = get_string('welcometocoursesemail', 'enrol_authorize', $a); 00236 $subject = get_string("enrolmentnew", '', format_string($SITE->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)))); 00237 00238 $eventdata = new stdClass(); 00239 $eventdata->modulename = 'moodle'; 00240 $eventdata->component = 'enrol_authorize'; 00241 $eventdata->name = 'authorize_enrolment'; 00242 $eventdata->userfrom = $sender; 00243 $eventdata->userto = $user; 00244 $eventdata->subject = $subject; 00245 $eventdata->fullmessage = $emailmessage; 00246 $eventdata->fullmessageformat = FORMAT_PLAIN; 00247 $eventdata->fullmessagehtml = ''; 00248 $eventdata->smallmessage = ''; 00249 message_send($eventdata); 00250 } 00251 } 00252 while ($ei); 00253 00254 $rs->close(); // end of iteration, close rs 00255 } 00256 } 00257 00258 function check_curl_available() { 00259 return function_exists('curl_init') && 00260 function_exists('stream_get_wrappers') && 00261 in_array('https', stream_get_wrappers()); 00262 } 00263 00264 function authorize_verify_account() { 00265 global $USER, $SITE; 00266 $plugin = enrol_get_plugin('authorize'); 00267 00268 require_once('authorizenet.class.php'); 00269 00270 $original_antest = $plugin->get_config('an_test'); 00271 $plugin->set_config('an_test', 1); // Test mode 00272 $shortname = format_string($SITE->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00273 00274 $order = new stdClass(); 00275 $order->id = -1; 00276 $order->paymentmethod = AN_METHOD_CC; 00277 $order->refundinfo = '1111'; 00278 $order->ccname = 'Test User'; 00279 $order->courseid = $SITE->id; 00280 $order->userid = $USER->id; 00281 $order->status = AN_STATUS_NONE; 00282 $order->settletime = 0; 00283 $order->transid = 0; 00284 $order->timecreated = time(); 00285 $order->amount = '0.01'; 00286 $order->currency = 'USD'; 00287 00288 $extra = new stdClass(); 00289 $extra->x_card_num = '4111111111111111'; 00290 $extra->x_card_code = '123'; 00291 $extra->x_exp_date = "12" . intval(date("Y")) + 5; 00292 $extra->x_currency_code = $order->currency; 00293 $extra->x_amount = $order->amount; 00294 $extra->x_first_name = 'Test'; 00295 $extra->x_last_name = 'User'; 00296 $extra->x_country = $USER->country; 00297 00298 $extra->x_invoice_num = $order->id; 00299 $extra->x_description = $shortname . ' - Authorize.net Merchant Account Verification Test'; 00300 00301 $ret = ''; 00302 $message = ''; 00303 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_AUTH_CAPTURE)) { 00304 $ret = get_string('verifyaccountresult', 'enrol_authorize', get_string('success')); 00305 } 00306 else { 00307 $ret = get_string('verifyaccountresult', 'enrol_authorize', $message); 00308 } 00309 00310 $plugin->set_config('an_test', $original_antest); 00311 00312 return $ret; 00313 } 00314 00315