New paypal plugin ... not quite working yet, but very close.

Just uploading it to another site for polishing off.
This commit is contained in:
moodler 2004-06-26 09:50:19 +00:00
parent 8c5c6133a8
commit 04f47a89a1
6 changed files with 323 additions and 23 deletions

View File

@ -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 = "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
$str .= "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>&nbsp;&nbsp;";
}
if ($course->password) {
$strrequireskey = get_string("requireskey");
$str .= "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
$str .= "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
}
return $str;
}
} /// end of class
?>

View File

@ -16,7 +16,7 @@
</tr>
<tr valign=top>
<th class="generalbox"><center><?php if (! empty($this->errormsg)) {formerr($this->errormsg);} ?></center>
<td class="generalbox" bgcolor="<?php p($THEME->cellcontent) ?>"><center><?php if (! empty($this->errormsg)) {formerr($this->errormsg);} ?></center>
<form name="form" method="post" action="enrol.php">
<table>
<tr>

18
enrol/paypal/enrol.html Normal file
View File

@ -0,0 +1,18 @@
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php p($CFG->enrol_paypalbusiness)?>">
<input type="hidden" name="item_name" value="<?php p($course->fullname) ?>">
<input type="hidden" name="item_number" value="<?php p($course->id) ?>">
<input type="hidden" name="custom" value="<?php p($USER->id) ?>">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="return" value="<?php echo "$CFG->wwwroot/enrol/paypal/thankyou.php?courseid=$course->id" ?>">
<input type="hidden" name="cancel_return" value="<?php echo $CFG->wwwroot ?>">
<input type="hidden" name="currency_code" value="<?php p($CFG->enrol_paypalcurrency) ?>">
<input type="hidden" name="amount" value="<?php p($cost) ?>">
<input type="submit" value="<?php print_string("sendpaymentbutton", "enrol_paypal") ?>">
</form>

107
enrol/paypal/enrol.php Normal file
View File

@ -0,0 +1,107 @@
<?php
require_once("$CFG->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, "<a href=\".\">$strcourses</a> -> $strloginto");
print_course($course);
print_simple_box_start("center");
printf ("<p align=\"center\"><b>$course->fullname</b> has a cost of $CFG->enrol_paypalcurrency %01.2f</p>", $cost);
echo "<div align=\"center\">";
include("$CFG->dirroot/enrol/paypal/enrol.html");
echo "</div>";
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 .= "<a title=\"$strrequirespayment\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
$str .= "<img vspace=4 alt=\"$strrequirespayment\" height=16 width=16 border=0 src=\"$icon\"></a>";
}
return $str;
}
} // end of class definition

133
enrol/paypal/ipn.php Normal file
View File

@ -0,0 +1,133 @@
<?php // $Id$
/**
* Listens for Instant Payment Notification from Paypal
*
* This script waits for Payment notification from Paypal,
* then double checks that data by sending it back to Paypal.
* If Paypal verifies this then it sets up the enrolment for that
*
* Set the $user->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 "<p>Error: could not access paypal.com</p>";
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);
}
?>

33
enrol/paypal/thankyou.php Normal file
View File

@ -0,0 +1,33 @@
<?php
require ("../../config.php");
if (isset($_GET['courseid'])) {
$course = get_record("course", "id", $_GET['courseid']);
}
if ($SESSION->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);
?>