Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/paypal/ipn.php
Go to the documentation of this file.
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 
00033 require("../../config.php");
00034 require_once("lib.php");
00035 require_once($CFG->libdir.'/eventslib.php');
00036 require_once($CFG->libdir.'/enrollib.php');
00037 
00038 
00040 if (empty($_POST) or !empty($_GET)) {
00041     print_error("Sorry, you can not use the script that way.");
00042 }
00043 
00048 
00049 $req = 'cmd=_notify-validate';
00050 
00051 $data = new stdClass();
00052 
00053 foreach ($_POST as $key => $value) {
00054     $req .= "&$key=".urlencode($value);
00055     $data->$key = $value;
00056 }
00057 
00058 $custom = explode('-', $data->custom);
00059 $data->userid           = (int)$custom[0];
00060 $data->courseid         = (int)$custom[1];
00061 $data->instanceid       = (int)$custom[2];
00062 $data->payment_gross    = $data->mc_gross;
00063 $data->payment_currency = $data->mc_currency;
00064 $data->timeupdated      = time();
00065 
00066 
00068 
00069 if (! $user = $DB->get_record("user", array("id"=>$data->userid))) {
00070     message_paypal_error_to_admin("Not a valid user id", $data);
00071     die;
00072 }
00073 
00074 if (! $course = $DB->get_record("course", array("id"=>$data->courseid))) {
00075     message_paypal_error_to_admin("Not a valid course id", $data);
00076     die;
00077 }
00078 
00079 if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
00080     message_paypal_error_to_admin("Not a valid context id", $data);
00081     die;
00082 }
00083 
00084 if (! $plugin_instance = $DB->get_record("enrol", array("id"=>$data->instanceid, "status"=>0))) {
00085     message_paypal_error_to_admin("Not a valid instance id", $data);
00086     die;
00087 }
00088 
00089 $plugin = enrol_get_plugin('paypal');
00090 
00092 $header = '';
00093 $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
00094 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
00095 $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
00096 $paypaladdr = empty($CFG->usepaypalsandbox) ? 'www.paypal.com' : 'www.sandbox.paypal.com';
00097 $fp = fsockopen ($paypaladdr, 80, $errno, $errstr, 30);
00098 
00099 if (!$fp) {  
00100     echo "<p>Error: could not access paypal.com</p>";
00101     message_paypal_error_to_admin("Could not access paypal.com to verify payment", $data);
00102     die;
00103 }
00104 
00106 
00107 fputs ($fp, $header.$req);
00108 
00110 
00111 while (!feof($fp)) {
00112     $result = fgets($fp, 1024);
00113     if (strcmp($result, "VERIFIED") == 0) {          // VALID PAYMENT!
00114 
00115 
00116         // check the payment_status and payment_reason
00117 
00118         // If status is not completed or pending then unenrol the student if already enrolled
00119         // and notify admin
00120 
00121         if ($data->payment_status != "Completed" and $data->payment_status != "Pending") {
00122             $plugin->unenrol_user($plugin_instance, $data->userid);
00123             message_paypal_error_to_admin("Status not completed or pending. User unenrolled from course", $data);
00124             die;
00125         }
00126 
00127         // If currency is incorrectly set then someone maybe trying to cheat the system
00128 
00129         if ($data->mc_currency != $plugin_instance->currency) {
00130             message_paypal_error_to_admin("Currency does not match course settings, received: ".$data->mc_currency, $data);
00131             die;
00132         }
00133 
00134         // If status is pending and reason is other than echeck then we are on hold until further notice
00135         // Email user to let them know. Email admin.
00136 
00137         if ($data->payment_status == "Pending" and $data->pending_reason != "echeck") {
00138             $eventdata = new stdClass();
00139             $eventdata->modulename        = 'moodle';
00140             $eventdata->component         = 'enrol_paypal';
00141             $eventdata->name              = 'paypal_enrolment';
00142             $eventdata->userfrom          = get_admin();
00143             $eventdata->userto            = $user;
00144             $eventdata->subject           = "Moodle: PayPal payment";
00145             $eventdata->fullmessage       = "Your PayPal payment is pending.";
00146             $eventdata->fullmessageformat = FORMAT_PLAIN;
00147             $eventdata->fullmessagehtml   = '';
00148             $eventdata->smallmessage      = '';
00149             message_send($eventdata);
00150 
00151             message_paypal_error_to_admin("Payment pending", $data);
00152             die;
00153         }
00154 
00155         // If our status is not completed or not pending on an echeck clearance then ignore and die
00156         // This check is redundant at present but may be useful if paypal extend the return codes in the future
00157 
00158         if (! ( $data->payment_status == "Completed" or
00159                ($data->payment_status == "Pending" and $data->pending_reason == "echeck") ) ) {
00160             die;
00161         }
00162 
00163         // At this point we only proceed with a status of completed or pending with a reason of echeck
00164 
00165 
00166 
00167         if ($existing = $DB->get_record("enrol_paypal", array("txn_id"=>$data->txn_id))) {   // Make sure this transaction doesn't exist already
00168             message_paypal_error_to_admin("Transaction $data->txn_id is being repeated!", $data);
00169             die;
00170 
00171         }
00172 
00173         if ($data->business != $plugin->get_config('paypalbusiness')) {   // Check that the email is the one we want it to be
00174             message_paypal_error_to_admin("Business email is {$data->business} (not ".
00175                     $plugin->get_config('paypalbusiness').")", $data);
00176             die;
00177 
00178         }
00179 
00180         if (!$user = $DB->get_record('user', array('id'=>$data->userid))) {   // Check that user exists
00181             message_paypal_error_to_admin("User $data->userid doesn't exist", $data);
00182             die;
00183         }
00184 
00185         if (!$course = $DB->get_record('course', array('id'=>$data->courseid))) { // Check that course exists
00186             message_paypal_error_to_admin("Course $data->courseid doesn't exist", $data);;
00187             die;
00188         }
00189 
00190         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00191 
00192         // Check that amount paid is the correct amount
00193         if ( (float) $plugin_instance->cost <= 0 ) {
00194             $cost = (float) $plugin->get_config('cost');
00195         } else {
00196             $cost = (float) $plugin_instance->cost;
00197         }
00198 
00199         if ($data->payment_gross < $cost) {
00200             $cost = format_float($cost, 2);
00201             message_paypal_error_to_admin("Amount paid is not enough ($data->payment_gross < $cost))", $data);
00202             die;
00203 
00204         }
00205 
00206         // ALL CLEAR !
00207 
00208         $DB->insert_record("enrol_paypal", $data);
00209 
00210         if ($plugin_instance->enrolperiod) {
00211             $timestart = time();
00212             $timeend   = $timestart + $plugin_instance->enrolperiod;
00213         } else {
00214             $timestart = 0;
00215             $timeend   = 0;
00216         }
00217 
00218         // Enrol user
00219         $plugin->enrol_user($plugin_instance, $user->id, $plugin_instance->roleid, $timestart, $timeend);
00220 
00221         // Pass $view=true to filter hidden caps if the user cannot see them
00222         if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
00223                                              '', '', '', '', false, true)) {
00224             $users = sort_by_roleassignment_authority($users, $context);
00225             $teacher = array_shift($users);
00226         } else {
00227             $teacher = false;
00228         }
00229 
00230         $mailstudents = $plugin->get_config('mailstudents');
00231         $mailteachers = $plugin->get_config('mailteachers');
00232         $mailadmins   = $plugin->get_config('mailadmins');
00233         $shortname = format_string($course->shortname, true, array('context' => $context));
00234 
00235 
00236         if (!empty($mailstudents)) {
00237             $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
00238             $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id";
00239 
00240             $eventdata = new stdClass();
00241             $eventdata->modulename        = 'moodle';
00242             $eventdata->component         = 'enrol_paypal';
00243             $eventdata->name              = 'paypal_enrolment';
00244             $eventdata->userfrom          = $teacher;
00245             $eventdata->userto            = $user;
00246             $eventdata->subject           = get_string("enrolmentnew", '', $shortname);
00247             $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
00248             $eventdata->fullmessageformat = FORMAT_PLAIN;
00249             $eventdata->fullmessagehtml   = '';
00250             $eventdata->smallmessage      = '';
00251             message_send($eventdata);
00252 
00253         }
00254 
00255         if (!empty($mailteachers)) {
00256             $a->course = format_string($course->fullname, true, array('context' => $coursecontext));
00257             $a->user = fullname($user);
00258 
00259             $eventdata = new stdClass();
00260             $eventdata->modulename        = 'moodle';
00261             $eventdata->component         = 'enrol_paypal';
00262             $eventdata->name              = 'paypal_enrolment';
00263             $eventdata->userfrom          = $user;
00264             $eventdata->userto            = $teacher;
00265             $eventdata->subject           = get_string("enrolmentnew", '', $shortname);
00266             $eventdata->fullmessage       = get_string('enrolmentnewuser', '', $a);
00267             $eventdata->fullmessageformat = FORMAT_PLAIN;
00268             $eventdata->fullmessagehtml   = '';
00269             $eventdata->smallmessage      = '';
00270             message_send($eventdata);
00271         }
00272 
00273         if (!empty($mailadmins)) {
00274             $a->course = format_string($course->fullname, true, array('context' => $coursecontext));
00275             $a->user = fullname($user);
00276             $admins = get_admins();
00277             foreach ($admins as $admin) {
00278                 $eventdata = new stdClass();
00279                 $eventdata->modulename        = 'moodle';
00280                 $eventdata->component         = 'enrol_paypal';
00281                 $eventdata->name              = 'paypal_enrolment';
00282                 $eventdata->userfrom          = $user;
00283                 $eventdata->userto            = $admin;
00284                 $eventdata->subject           = get_string("enrolmentnew", '', $shortname);
00285                 $eventdata->fullmessage       = get_string('enrolmentnewuser', '', $a);
00286                 $eventdata->fullmessageformat = FORMAT_PLAIN;
00287                 $eventdata->fullmessagehtml   = '';
00288                 $eventdata->smallmessage      = '';
00289                 message_send($eventdata);
00290             }
00291         }
00292 
00293     } else if (strcmp ($result, "INVALID") == 0) { // ERROR
00294         $DB->insert_record("enrol_paypal", $data, false);
00295         message_paypal_error_to_admin("Received an invalid payment notification!! (Fake payment?)", $data);
00296     }
00297 }
00298 
00299 fclose($fp);
00300 exit;
00301 
00302 
00303 //--- HELPER FUNCTIONS --------------------------------------------------------------------------------------
00304 
00305 
00306 function message_paypal_error_to_admin($subject, $data) {
00307     echo $subject;
00308     $admin = get_admin();
00309     $site = get_site();
00310 
00311     $message = "$site->fullname:  Transaction failed.\n\n$subject\n\n";
00312 
00313     foreach ($data as $key => $value) {
00314         $message .= "$key => $value\n";
00315     }
00316 
00317     $eventdata = new stdClass();
00318     $eventdata->modulename        = 'moodle';
00319     $eventdata->component         = 'enrol_paypal';
00320     $eventdata->name              = 'paypal_enrolment';
00321     $eventdata->userfrom          = $admin;
00322     $eventdata->userto            = $admin;
00323     $eventdata->subject           = "PAYPAL ERROR: ".$subject;
00324     $eventdata->fullmessage       = $message;
00325     $eventdata->fullmessageformat = FORMAT_PLAIN;
00326     $eventdata->fullmessagehtml   = '';
00327     $eventdata->smallmessage      = '';
00328     message_send($eventdata);
00329 }
00330 
00331 
 All Data Structures Namespaces Files Functions Variables Enumerations