From 04f47a89a19505dc5d2707457ce8efbed700b795 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 26 Jun 2004 09:50:19 +0000 Subject: [PATCH] New paypal plugin ... not quite working yet, but very close. Just uploading it to another site for polishing off. --- enrol/enrol.class.php | 53 ++++++++------- enrol/internal/enrol.html | 2 +- enrol/paypal/enrol.html | 18 ++++++ enrol/paypal/enrol.php | 107 ++++++++++++++++++++++++++++++ enrol/paypal/ipn.php | 133 ++++++++++++++++++++++++++++++++++++++ enrol/paypal/thankyou.php | 33 ++++++++++ 6 files changed, 323 insertions(+), 23 deletions(-) create mode 100644 enrol/paypal/enrol.html create mode 100644 enrol/paypal/enrol.php create mode 100644 enrol/paypal/ipn.php create mode 100644 enrol/paypal/thankyou.php diff --git a/enrol/enrol.class.php b/enrol/enrol.class.php index e849b362a5..bb064835e7 100644 --- a/enrol/enrol.class.php +++ b/enrol/enrol.class.php @@ -57,8 +57,8 @@ function get_student_courses(&$user) { /// Is the student enrolment active right now? - if ( (!$student->timestart and !$student->timeend) or - ( $student->timestart < $currenttime and $currenttime < $student->timeend) ) { + if ( ( $student->timestart == 0 or ( $currenttime > $student->timestart )) and + ( $student->timeend == 0 or ( $currenttime < $student->timeend )) ) { $user->student[$student->course] = true; $user->timeaccess[$student->course] = $student->timeaccess; } @@ -86,8 +86,8 @@ function get_teacher_courses(&$user) { /// Is teacher only teaching this course for a specific time period? - if ( (!$teacher->timestart and !$teacher->timeend) or - ( $teacher->timestart < $currenttime and $currenttime < $teacher->timeend) ) { + if ( ( $teacher->timestart == 0 or ( $currenttime > $teacher->timestart )) and + ( $teacher->timeend == 0 or ( $currenttime < $teacher->timeend )) ) { $user->teacher[$teacher->course] = true; @@ -115,28 +115,12 @@ function get_teacher_courses(&$user) { * @param course current course object */ function print_entry($course) { - global $CFG, $USER, $SESSION; + global $CFG, $USER, $SESSION, $THEME; $strloginto = get_string("loginto", "", $course->shortname); $strcourses = get_string("courses"); -/// Double check just in case they are actually enrolled already -/// This might occur if they were manually enrolled during this session - - if (record_exists("user_students", "userid", $USER->id, "course", $course->id)) { - $USER->student[$course->id] = true; - - if ($SESSION->wantsurl) { - $destination = $SESSION->wantsurl; - unset($SESSION->wantsurl); - } else { - $destination = "$CFG->wwwroot/course/view.php?id=$course->id"; - } - - redirect($destination); - } - /// Automatically enrol into courses without password @@ -201,7 +185,7 @@ function print_entry($course) { * @param course the current course, as an object */ function check_entry($form, $course) { - global $CFG, $USER, $SESSION; + global $CFG, $USER, $SESSION, $THEME; if ($form->password == $course->password) { @@ -281,6 +265,31 @@ function cron() { } +/** +* Returns the relevant icons for a course +* +* Returns the relevant icons for a course +* +* @param course the current course, as an object +*/ +function get_access_icons($course) { + global $CFG; + + if ($course->guest) { + $strallowguests = get_string("allowguests"); + $str = "wwwroot/course/view.php?id=$course->id\">"; + $str .= "\"$strallowguests\"pixpath/i/guest.gif\">  "; + } + if ($course->password) { + $strrequireskey = get_string("requireskey"); + $str .= "wwwroot/course/view.php?id=$course->id\">"; + $str .= "\"$strrequireskey\"pixpath/i/key.gif\">"; + } + + return $str; +} + + } /// end of class ?> diff --git a/enrol/internal/enrol.html b/enrol/internal/enrol.html index 3c89b1ccb9..26c941b000 100644 --- a/enrol/internal/enrol.html +++ b/enrol/internal/enrol.html @@ -16,7 +16,7 @@ -
errormsg)) {formerr($this->errormsg);} ?>
+
errormsg)) {formerr($this->errormsg);} ?>
diff --git a/enrol/paypal/enrol.html b/enrol/paypal/enrol.html new file mode 100644 index 0000000000..f275635bba --- /dev/null +++ b/enrol/paypal/enrol.html @@ -0,0 +1,18 @@ + + + + + + + + +wwwroot/enrol/paypal/thankyou.php?courseid=$course->id" ?>"> + + + + + +"> + + + diff --git a/enrol/paypal/enrol.php b/enrol/paypal/enrol.php new file mode 100644 index 0000000000..49f1d3cc03 --- /dev/null +++ b/enrol/paypal/enrol.php @@ -0,0 +1,107 @@ +dirroot/enrol/enrol.class.php"); + + +// $CFG->enrol_paypalmailusers: send email to users when they are enrolled in a course +// $CFG->enrol_paypalmailadmin: email the log from the cron job to the admin + + +// Test data +$CFG->enrol_cost = 5.00; +$CFG->enrol_paypalbusiness = "payment@moodle.com"; +$CFG->enrol_paypalcurrency = "USD"; + +// Accepted PayPal currencies (USD/EUR/JPY/GBP/CAD) + + + + +class enrolment_plugin extends enrolment_base { + + +/// Override the base print_entry() function +function print_entry($course) { + global $CFG; + + + $strloginto = get_string("loginto", "", $course->shortname); + $strcourses = get_string("courses"); + + + $teacher = get_teacher($course->id); + + + if ( (float) $course->cost < 0 ) { + $cost = (float) $CFG->enrol_cost; + } else { + $cost = (float) $course->cost; + } + + + if (abs($cost) < 0.01) { // no cost, default to base class entry to course + + + parent::print_entry($course); + + } else { + + print_header($strloginto, $course->fullname, "$strcourses -> $strloginto"); + print_course($course); + print_simple_box_start("center"); + + printf ("

$course->fullname has a cost of $CFG->enrol_paypalcurrency %01.2f

", $cost); + + echo "
"; + include("$CFG->dirroot/enrol/paypal/enrol.html"); + echo "
"; + + print_simple_box_end(); + print_footer(); + + } +} // end of function print_entry() + + + + +/// Override the base check_entry() function +/// This should never be called for this type of enrolment anyway +function check_entry($form, $course) { +} + + + +/// Override the get_access_icons() function +function get_access_icons($course) { + global $CFG; + + if ( (float) $course->cost < 0) { + $cost = (float) $CFG->enrol_cost; + } else { + $cost = (float) $course->cost; + } + + if (abs($cost) < 0.01) { + $str = parent::get_access_icons($course); + } else { + + $strrequirespayment = get_string("requirespayment"); + + if (! file_exists("$CFG->dirroot/pix/m/$CFG->enrol_paypalcurrency.gif")) { + $icon = "$CFG->pixpath/m/USD.gif"; + } else { + $icon = "$CFG->pixpath/m/$CFG->enrol_paypalcurrency.gif"; + } + + $str .= "wwwroot/course/view.php?id=$course->id\">"; + $str .= "\"$strrequirespayment\""; + + } + + return $str; +} + + + +} // end of class definition diff --git a/enrol/paypal/ipn.php b/enrol/paypal/ipn.php new file mode 100644 index 0000000000..1b4c122cd8 --- /dev/null +++ b/enrol/paypal/ipn.php @@ -0,0 +1,133 @@ +timeaccess course array +* +* @param user referenced object, must contain $user->id already set +*/ + + +/// Keep out casual intruders + if (empty($_POST)) { + error("Sorry, you can not use the script that way."); + } + +/// Read all the data from Paypal and get it ready for later + + $req = 'cmd=_notify-validate'; + + foreach ($_POST as $key => $value) { + $value = urlencode(stripslashes($value)); + $req .= "&$key=$value"; + $data->$key = $value; + } + + $data->courseid = $data->item_number; + $data->userid = $data->custom; + $data->payment_amount = $data->mc_gross; + $data->payment_currency = $data->mc_currency; + + +/// Open a connection back to PayPal to validate the data + + $header = ''; + $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; + $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; + $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; + $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); + + if (!$fp) { /// Could not open a socket to Paypal - FAIL + echo "

Error: could not access paypal.com

"; + email_paypal_error_to_admin("Could not access paypal.com to verify payment", $data); + die; + } + +/// Connection is OK, so now we post the data to validate it + + fputs ($fp, $header.$req); + +/// Now read the response and check if everything is OK. + + while (!feof($fp)) { + $result = fgets($fp, 1024); + if (strcmp($result, "VERIFIED") == 0) { // VALID PAYMENT! + + // check the payment_status is Completed + + if ($data->payment_status != "Completed") { // Not complete? + email_paypal_error_to_admin("Transaction status is: $data->payment_status", $data); + die; + } + + if ($existing = get_record("enrol_paypal", "txn_id", $data->txn_id)) { // Make sure this transaction doesn't exist already + + } + + if () { // Check that the email is the one we want it to be + + } + + if (!$user = get_record('user', 'id', $data->userid)) { // Check that user exists + email_paypal_error_to_admin("User $data->userid doesn't exist", $data); + } + + if (!$course = get_record('user', 'id', $data->courseid)) { // Check that course exists + email_paypal_error_to_admin("Course $data->courseid doesn't exist", $data); + } + + if () { // Check that amount paid is the correct amount + + } + + // ALL CLEAR ! + + if (!insert_record("enrol_paypal", $data)) { // Insert a transaction record + email_paypal_error_to_admin("Error while trying to insert valid transaction", $data); + } + + if (!enrol_student($user->id, $course->id)) { // Enrol the student + email_paypal_error_to_admin("Error while trying to enrol ".fullname($user)." in '$course->fullname'", $data); + } else { + if (!empty($CFG->enrol_paypalemail)) { + $teacher = get_teacher(); + email_to_user($teacher, $user, get_string("enrolmentnew"), "I have enrolled in your class via Paypal"); + email_to_user($user, $teacher, get_string("enrolmentnew"), get_string('welcometocoursetext')); + } + } + + + } else if (strcmp ($result, "INVALID") == 0) { // ERROR + insert_record("enrol_paypal", $data); + email_paypal_error_to_admin("Received an invalid payment notification!! (Fake payment?)", $data); + } + } + + fclose($fp); + exit; + + + +/// FUNCTIONS ////////////////////////////////////////////////////////////////// + + +function email_paypal_error_to_admin($subject, $data) { + $admin = get_admin(); + $site = get_admin(); + + $message = "$site->fullname: Transaction failed.\n\n$subject\n\n"; + + foreach ($data as $key => $value) { + $message .= "$key => $value\n"; + } + + email_to_user($admin, $admin, "PAYPAL ERROR: ".$subject, $message); + +} + +?> diff --git a/enrol/paypal/thankyou.php b/enrol/paypal/thankyou.php new file mode 100644 index 0000000000..9d28633ffe --- /dev/null +++ b/enrol/paypal/thankyou.php @@ -0,0 +1,33 @@ +wantsurl) { + $destination = $SESSION->wantsurl; + unset($SESSION->wantsurl); +} else { + if ($course) { + $destination = "$CFG->wwwroot/course/view.php?id=$course->id"; + } else { + $destination = "$CFG->wwwroot/course/"; + } +} + + +$str = "Thank you for your payment."; +if ($course) { + $str .= "You should now be able to access $course->fullname"; +} + + + +print_header(); + +notice($str, $destination); + + +?> -- 2.39.5