Merge branch 'w24_MDL-33635_m23_sort' of git://github.com/skodak/moodle
[moodle.git] / enrol / authorize / localfuncs.php
blob6a5a1e574520bb1db661aebeb0134dff5e4b58bf
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Authorize enrolment plugin.
20 * This plugin allows you to set up paid courses, using authorize.net.
22 * @package enrol
23 * @subpackage authorize
24 * @copyright 2010 Eugene Venter
25 * @author Eugene Venter
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once($CFG->libdir.'/eventslib.php');
31 function get_course_cost($plugininstance) {
32 $defaultplugin = enrol_get_plugin('authorize');
34 $cost = (float)0;
35 $currency = (!empty($plugininstance->currency))
36 ? $plugininstance->currency :( empty($defaultplugin->currency)
37 ? 'USD' : $defaultplugin->enrol_currency );
39 if (!empty($plugininstance->cost)) {
40 $cost = (float)(((float)$plugininstance->cost) < 0) ? $defaultplugin->cost : $plugininstance->cost;
43 $cost = format_float($cost, 2);
44 $ret = array(
45 'cost' => $cost,
46 'currency' => $currency
49 return $ret;
52 function zero_cost($plugininstance) {
53 $curcost = get_course_cost($plugininstance);
54 return (abs($curcost['cost']) < 0.01);
57 function prevent_double_paid($plugininstance) {
58 global $CFG, $SESSION, $USER, $DB;
59 $plugin = enrol_get_plugin('authorize');
61 $sql = "SELECT id FROM {enrol_authorize} WHERE userid = ? AND courseid = ? AND instanceid = ?";
62 $params = array($USER->id, $plugininstance->courseid, $plugininstance->id);
64 if (!$plugin->get_config('an_test')) { // Real mode
65 $sql .= ' AND status IN(?,?,?)';
66 $params[] = AN_STATUS_AUTH;
67 $params[] = AN_STATUS_UNDERREVIEW;
68 $params[] = AN_STATUS_APPROVEDREVIEW;
70 else { // Test mode
71 $sql .= ' AND status=?';
72 $params[] = AN_STATUS_NONE;
75 if (($recid = $DB->get_field_sql($sql, $params))) {
76 $a = new stdClass;
77 $a->orderid = $recid;
78 $a->url = "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid";
79 redirect($a->url, get_string("paymentpending", "enrol_authorize", $a), '10');
80 return;
82 if (isset($SESSION->ccpaid)) {
83 unset($SESSION->ccpaid);
84 redirect($CFG->wwwroot . '/login/logout.php?sesskey='.sesskey());
85 return;
89 function get_list_of_creditcards($getall = false) {
90 $plugin = enrol_get_plugin('authorize');
92 $alltypes = array(
93 'mcd' => 'Master Card',
94 'vis' => 'Visa',
95 'amx' => 'American Express',
96 'dsc' => 'Discover',
97 'dnc' => 'Diners Club',
98 'jcb' => 'JCB',
99 'swi' => 'Switch',
100 'dlt' => 'Delta',
101 'enr' => 'EnRoute'
104 if ($getall) {
105 return $alltypes;
108 $ret = array();
109 foreach ($alltypes as $code=>$name) {
110 if ($plugin->get_config("an_acceptcc_{$code}")) {
111 $ret[$code] = $name;
115 return $ret;
118 function get_list_of_payment_methods($getall = false) {
119 $plugin = enrol_get_plugin('authorize');
120 $method_cc = $plugin->get_config('an_acceptmethod_cc');
121 $method_echeck = $plugin->get_config('an_acceptmethod_echeck');
124 if ($getall || (empty($method_cc) && empty($method_echeck))) {
125 return array(AN_METHOD_CC, AN_METHOD_ECHECK);
126 } else {
127 $methods = array();
128 if ($method_cc) {
129 $methods[] = AN_METHOD_CC;
132 if ($method_echeck) {
133 $methods[] = AN_METHOD_ECHECK;
136 return $methods;
140 function get_list_of_bank_account_types($getall = false) {
141 $plugin = enrol_get_plugin('authorize');
142 $alltypes = array('CHECKING', 'BUSINESSCHECKING', 'SAVINGS');
144 if ($getall) {
145 return $alltypes;
146 } else {
147 $types = array();
148 foreach ($alltypes as $type) {
149 if ($plugin->get_config("an_acceptecheck_{$type}")) {
150 $types[] = $type;
154 return $types;
158 function message_to_admin($subject, $data) {
159 global $SITE;
161 $admin = get_admin();
162 $data = (array)$data;
164 $emailmessage = "$SITE->fullname: Transaction failed.\n\n$subject\n\n";
165 $emailmessage .= print_r($data, true);
166 $eventdata = new stdClass();
167 $eventdata->modulename = 'moodle';
168 $eventdata->component = 'enrol_authorize';
169 $eventdata->name = 'authorize_enrolment';
170 $eventdata->userfrom = $admin;
171 $eventdata->userto = $admin;
172 $eventdata->subject = "$SITE->fullname: Authorize.net ERROR";
173 $eventdata->fullmessage = $emailmessage;
174 $eventdata->fullmessageformat = FORMAT_PLAIN;
175 $eventdata->fullmessagehtml = '';
176 $eventdata->smallmessage = '';
177 message_send($eventdata);
180 function send_welcome_messages($orderdata) {
181 global $CFG, $SITE, $DB;
183 if (empty($orderdata)) {
184 return;
187 if (is_numeric($orderdata)) {
188 $orderdata = array($orderdata);
191 $sql = "SELECT e.id, e.courseid, e.userid, c.fullname
192 FROM {enrol_authorize} e
193 JOIN {course} c ON c.id = e.courseid
194 WHERE e.id IN(" . implode(',', $orderdata) . ")
195 ORDER BY e.userid";
197 $rs = $DB->get_recordset_sql($sql);
198 if (!$rs->valid()) {
199 $rs->close(); // Not going to iterate (but exit), close rs
200 return;
203 if ($rs->valid() and $ei = current($rs))
205 if (1 < count($orderdata)) {
206 $sender = get_admin();
208 else {
209 $context = get_context_instance(CONTEXT_COURSE, $ei->courseid);
210 $paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments', '', '', '0', '1');
211 $sender = array_shift($paymentmanagers);
216 $usercourses = array();
217 $lastuserid = $ei->userid;
219 while ($ei && $ei->userid == $lastuserid) {
220 $context = get_context_instance(CONTEXT_COURSE, $ei->courseid);
221 $usercourses[] = format_string($ei->fullname, true, array('context' => $context));
222 if (!$rs->valid()) {
223 break;
225 $rs->next();
226 $ei = $rs->current();
229 if (($user = $DB->get_record('user', array('id'=>$lastuserid)))) {
230 $a = new stdClass;
231 $a->name = $user->firstname;
232 $a->courses = implode("\n", $usercourses);
233 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$lastuserid";
234 $a->paymenturl = "$CFG->wwwroot/enrol/authorize/index.php?user=$lastuserid";
235 $emailmessage = get_string('welcometocoursesemail', 'enrol_authorize', $a);
236 $subject = get_string("enrolmentnew", 'enrol', format_string($SITE->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))));
238 $eventdata = new stdClass();
239 $eventdata->modulename = 'moodle';
240 $eventdata->component = 'enrol_authorize';
241 $eventdata->name = 'authorize_enrolment';
242 $eventdata->userfrom = $sender;
243 $eventdata->userto = $user;
244 $eventdata->subject = $subject;
245 $eventdata->fullmessage = $emailmessage;
246 $eventdata->fullmessageformat = FORMAT_PLAIN;
247 $eventdata->fullmessagehtml = '';
248 $eventdata->smallmessage = '';
249 message_send($eventdata);
252 while ($ei);
254 $rs->close(); // end of iteration, close rs
258 function check_curl_available() {
259 return function_exists('curl_init') &&
260 function_exists('stream_get_wrappers') &&
261 in_array('https', stream_get_wrappers());
264 function authorize_verify_account() {
265 global $USER, $SITE;
266 $plugin = enrol_get_plugin('authorize');
268 require_once('authorizenet.class.php');
270 $original_antest = $plugin->get_config('an_test');
271 $plugin->set_config('an_test', 1); // Test mode
272 $shortname = format_string($SITE->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
274 $order = new stdClass();
275 $order->id = -1;
276 $order->paymentmethod = AN_METHOD_CC;
277 $order->refundinfo = '1111';
278 $order->ccname = 'Test User';
279 $order->courseid = $SITE->id;
280 $order->userid = $USER->id;
281 $order->status = AN_STATUS_NONE;
282 $order->settletime = 0;
283 $order->transid = 0;
284 $order->timecreated = time();
285 $order->amount = '0.01';
286 $order->currency = 'USD';
288 $extra = new stdClass();
289 $extra->x_card_num = '4111111111111111';
290 $extra->x_card_code = '123';
291 $extra->x_exp_date = "12" . intval(date("Y")) + 5;
292 $extra->x_currency_code = $order->currency;
293 $extra->x_amount = $order->amount;
294 $extra->x_first_name = 'Test';
295 $extra->x_last_name = 'User';
296 $extra->x_country = $USER->country;
298 $extra->x_invoice_num = $order->id;
299 $extra->x_description = $shortname . ' - Authorize.net Merchant Account Verification Test';
301 $ret = '';
302 $message = '';
303 if (AN_APPROVED == AuthorizeNet::process($order, $message, $extra, AN_ACTION_AUTH_CAPTURE)) {
304 $ret = get_string('verifyaccountresult', 'enrol_authorize', get_string('success'));
306 else {
307 $ret = get_string('verifyaccountresult', 'enrol_authorize', $message);
310 $plugin->set_config('an_test', $original_antest);
312 return $ret;