2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Authorize enrolment plugin.
20 * This plugin allows you to set up paid courses, using authorize.net.
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');
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);
46 'currency' => $currency
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
;
71 $sql .= ' AND status=?';
72 $params[] = AN_STATUS_NONE
;
75 if (($recid = $DB->get_field_sql($sql, $params))) {
78 $a->url
= "$CFG->wwwroot/enrol/authorize/index.php?order=$a->orderid";
79 redirect($a->url
, get_string("paymentpending", "enrol_authorize", $a), '10');
82 if (isset($SESSION->ccpaid
)) {
83 unset($SESSION->ccpaid
);
84 redirect($CFG->wwwroot
. '/login/logout.php?sesskey='.sesskey());
89 function get_list_of_creditcards($getall = false) {
90 $plugin = enrol_get_plugin('authorize');
93 'mcd' => 'Master Card',
95 'amx' => 'American Express',
97 'dnc' => 'Diners Club',
109 foreach ($alltypes as $code=>$name) {
110 if ($plugin->get_config("an_acceptcc_{$code}")) {
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
);
129 $methods[] = AN_METHOD_CC
;
132 if ($method_echeck) {
133 $methods[] = AN_METHOD_ECHECK
;
140 function get_list_of_bank_account_types($getall = false) {
141 $plugin = enrol_get_plugin('authorize');
142 $alltypes = array('CHECKING', 'BUSINESSCHECKING', 'SAVINGS');
148 foreach ($alltypes as $type) {
149 if ($plugin->get_config("an_acceptecheck_{$type}")) {
158 function message_to_admin($subject, $data) {
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)) {
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) . ")
197 $rs = $DB->get_recordset_sql($sql);
199 $rs->close(); // Not going to iterate (but exit), close rs
203 if ($rs->valid() and $ei = current($rs))
205 if (1 < count($orderdata)) {
206 $sender = get_admin();
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));
226 $ei = $rs->current();
229 if (($user = $DB->get_record('user', array('id'=>$lastuserid)))) {
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);
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() {
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();
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;
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';
303 if (AN_APPROVED
== AuthorizeNet
::process($order, $message, $extra, AN_ACTION_AUTH_CAPTURE
)) {
304 $ret = get_string('verifyaccountresult', 'enrol_authorize', get_string('success'));
307 $ret = get_string('verifyaccountresult', 'enrol_authorize', $message);
310 $plugin->set_config('an_test', $original_antest);