Paypal IPN fustrations


Im having a issue with my paypal IPN script. For some reason I cannot get the database to update. I cannot see how to export the varables being pulled from paypal as it just contacts the page in the background. I am using the same connection script that i use among the site so I know its connecting.

// 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\\r\
	// If testing on Sandbox use: 
	$header .= "Host:\\r\
//$header .= "Host:\\r\
$header .= "Content-Type: application/x-www-form-urlencoded\\r\
$header .= "Content-Length: " . strlen($req) . "\\r\

	// If testing on Sandbox use:
	$fp = fsockopen ('ssl://', 443, $errno, $errstr, 30);
//$fp = fsockopen ('ssl://', 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 = mysql_real_escape_string($_POST['custom']);
$today = date("F j, Y, g:i a"); 

if (!$fp) {
} 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 l'og' WHERE 'txn_id' ='".$txn_id."'");
			if (mysql_num_rows($txn_id_check) !=1) {
					if ($payment_amount=='29.95' && $payment_currency=='USD') {
					$log_query = mysql_query("INSERT INTO 'log' VALUES ('','".$txn_id."','".$payer_email."')");
					$update_premium = mysql_query("UPDATE 'users' SET authLevel='1', startDate='".$today."'  WHERE 'fbID'='".$user_id."'");


    else if (strcmp ($res, "INVALID") == 0) {
         // log for manual investigation
  fclose ($fp);

Here is the code for my button from paypal.

    <form action="" method="post">
    <input type="hidden" name="cmd" value="_xclick-subscriptions">
    <input type="hidden" name="business" value="HiddenForQUestion">
    <input type="hidden" name="lc" value="US">
    <input type="hidden" name="item_name" value="HiddenForQUestion">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="no_shipping" value="2">
    <input type="hidden" name="rm" value="1">
    <input type="hidden" name="return" value="HiddenForQUestion">
    <input type="hidden" name="cancel_return" value="HiddenForQUestion">
    <input type="hidden" name="src" value="1">
    <input type="hidden" name="a3" value="29.95">
    <input type="hidden" name="p3" value="1">
    <input type="hidden" name="t3" value="Y">
    <input type="hidden" name="notify_url" value="HiddenForQUestion" />
          <input type="hidden" name="custom" value="<?php echo $userId ?>" />
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="bn" value="PP-SubscriptionsBF:btn_subscribeCC_LG.gif:NonHosted">
    <input type="image" src="" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" style="border:none;">

Can anyone see how i went wrong?

This query looks wrong to me:

SELECT 'txn_id' FROM l'og' WHERE 'txn_id' ='".$txn_id."'

Check the quoting of ‘log’ (viz. log’)

Thank but that was not it. Still no go.

Why do you have

if (mysql_num_rows($txn_id_check) !=1) 


Shouldn’t that be ==1 ?

When I write PayPal IPN (or similar) scripts, I always find it helps if you log stuff to a file.
Just create a function at the top

function myLog($text)
  $fp=fopen('./paypal.log', 'a+');
  fputs($fp, $text);

And then every time you want to log something:

myLog('There is one order found: '.$orderId);


You can’t call this function “log”, as that’s an existing function in PHP.

Then when the call ended open up paypal.log and see what happened.
Makes it less of black box :slight_smile: