Hello!
I implemented a working model for using PayPal’s IPN to verify payment with PHP version 4.1. I’m now using PHP version 5.2.17 and all of a sudden, the code doesn’t work; while my PayPal payments are going through, my database isn’t changing, nor am I receiving an error message. I thought that perhaps it was a magic_quotes issue, and so changed one of the lines below (stripslashes) but to no avail. Might there be some other piece of code that I need to change in order for it to work with my newer version of PHP?
Thanks for looking at the code,
Eric
<?php // PHP 4.1
require_once('../main_scripts/library.php'); /*used for my Zend_mail at the bottom */
require_once('/databaseconfigstuff.php'); /*Not the real location..but my config stuff for my database */
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
/* I changed the following line because no need for MagicQuotes
$value = urlencode(stripslashes($value)); */
$value = urlencode($value);
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\\r\
";
$header .= "Content-Type: application/x-www-form-urlencoded\\r\
";
$header .= "Content-Length: " . strlen($req) . "\\r\
\\r\
";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$user_id = $_POST['custom']; //user's id
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if ($payment_status=='Completed') {
$txn_id_check = mysql_query ("SELECT txn_id FROM log WHERE txn_id='$txn_id'");
if (mysql_num_rows($txn_id_check)!=1) {
if ($receiver_email=='support@myalgebrabook.com') {
if ($payment_amount=='1'&&$payment_currency=='USD') {
//add txn_id to database
$log_query=mysql_query("INSERT INTO log VALUES ('','".$txn_id."','".$payer_email."','')");
//update paid_status to paid
$update_paid_status = mysql_query("UPDATE users SET paid_status=1 WHERE user_id='$user_id'");
$course_id_query=mysql_query("SELECT course_id FROM users WHERE user_id='$user_id'");
$course_id=mysql_result($course_id_query,0,'course_id');
$update_course_enrollment = mysql_query("INSERT INTO course_enrollment (user_id, course_id) VALUES ('".$user_id."','".$course_id."')");
}
}
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
$mail = new Zend_Mail('UTF-8');
$mail->addTo('support@myalgebrabook.com');
$mail->setFrom('support@myalgebrabook.com');
$mail->setSubject('Problem with PayPal');
$text = 'There was a problem here.';
$mail->setBodyHtml($text, 'UTF-8');
$success = $mail->send();
}
}
fclose ($fp);
}
?>