I'm trying to get a ipn script working so that I can integrate Paypal into my site.
Once the purchase is complete, I'm aiming for the script to update 3 MYSQL values, the premium value (needs to update to 1 to show the user is premium), and then the start and end date for the registration.
However, when I test, the database does not update. Nor do any errors echo out.
Heres the databse updating part of the code:
//update premium to 1
$query = sprintf("UPDATE `users` SET `premium` = '1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update premium status: ' . mysql_error());
//update startdate to current date
$query = sprintf("UPDATE `users` SET `startdate` = '$startdate1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update licence date details (1): ' . mysql_error());
//update enddate to current date +365 days
$query = sprintf("UPDATE `users` SET `enddate` = '$enddate1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update licence date details (2): ' . mysql_error());
Heres the full ipn code:
<pre class="php"><?php
require 'glob.inc.php';
$posted_data = print_r($_POST,true);
file_put_contents('IPN_data.txt',$posted_data);
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0
";
$header .= "Content-Type: application/x-www-form-urlencoded
";
$header .= "Content-Length: " . strlen($req) . "
";
$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"]; //our users id.
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if ($_POST['payment_status'] == 'completed')
{
//values for date info
$startdate1 = date("Y-m-d");
$enddate1 = date("Y-m-d", strtotime("+ 365 day"));
//update whatever information you need to
// update premium to 1
$query = sprintf("UPDATE `users` SET `premium` = '1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update premium status: ' . mysql_error());
//update startdate to current date
$query = sprintf("UPDATE `users` SET `startdate` = '$startdate1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update licence date details (1): ' . mysql_error());
//update enddate to current date +365 days
$query = sprintf("UPDATE `users` SET `enddate` = '$enddate1' WHERE `user_id` = '".$user_id."'");
mysql_query($query)or die('Could not update licence date details (2): ' . mysql_error());
}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?></pre>
I know im working with depreciated MYSQL, but I have to for this project. Just have to go with me on that one. Im also a beginner, so am learning as I go. the script connects to the database via glob.inc.php, which is required at the start of the ipn script.
I've looked at the IPN data report from Paypal, and the correct user ID (custom field) is being sent and returned. The database does not update though...
Any ideas?