Hello! I have been working on this script for a while, and I just can't seem to get it to work. I am using paypal's sandbox to test it. Here's the script, with my questions below:
<?php
// 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 https://www.sandbox.paypal.com/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.sandbox.paypal.com', 80, $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'];
if (!$fp) {
// HTTP ERROR
// log for manual investigation
$fso_error = "error";
$ipn_log = '['.date('m/d/Y g:i A').']';
$ipn_log .= "\n HTTP ERROR:\n";
$ipn_log .= "\n FSO:". $fso_error."\n\n";
$ipn_log_file = '/path/to/my/log/file';
// Write to log
$fp=fopen($ipn_log_file,'a');
fwrite($fp, $ipn_log);
exit;
} else {
fputs($fp, $header . $req);
while (!feof($fp)) {
$res = fgets($fp,1024);
$res2 = trim($res);
include ('my config info omitted here'); //connect and select db
if (strcmp ($res2, "VERIFIED") == 0) {
// check the payment_status is Completed
if ($payment_status == 'Completed') {
$total= mysql_query("SELECT SUM(order_total) FROM cart WHERE username='$item_name' AND confirmed='no'");
// check that payment_amount/receiver_email/currency are correct
// check that txn_id has not been previously processed
if ($payment_amount == $total && $receiver_email == 'me@myhome.com' && $payment_currency == 'USD') {
//get the quantity bought of each item.
$sql="SELECT * FROM cart WHERE username='$item_name' AND confirmed='no'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
//rename variables
$pid = $row['product_id'];
$stock = $row['stock'];
$quantity = $row['quantity'];
$newstock = $stock - $quantity;
// process payment
//update the quantity in the products table
$sql="UPDATE products SET quantity='$newstock' WHERE product_id='$pid'";
mysql_query($sql) or die(mysql_error());
//update the quantity in the wish table
$sql2="UPDATE wish SET quantity='$newstock' WHERE product_id='$pid'";
mysql_query($sql2) or die(mysql_error());
//change cart table's confirmed column to yes
$sql3="UPDATE cart SET confirmed='yes' WHERE username='$item_name' AND product_id='$pid'";
mysql_query($sql3) or die(mysql_error());
} //end while
}//end if
}//end if
} else {
//INVALID or PENDING
// log for manual investigation
$ipn_log = '['.date('m/d/Y g:i A').']';
$ipn_log .= "\n IPN Response INVALID:\n";
$ipn_log .= "Response:".$res2."\n\n";
$ipn_log_file = '/path/to/my/log/file';
// Write to log
$fp=fopen($ipn_log_file,'a');
fwrite($fp, $ipn_log);
exit;
}
}//end while
fclose ($fp);
} //end else
?>
This script DOES write to the log, and it writes the value for $res2 right in there, too, along with date, etc. It is doing this because of the last else statement above. In other words, the 'if (strcmp ($res2, "VERIFIED") == 0)' part is not executing, and instead, the script is executing what is after that: the else statement.
Well, here is what the log says:
[10/12/2006 3:38 PM]
IPN Response INVALID:
Response:HTTP/1.1 200 OK
Now, based on this, $res2 == 'HTTP/1.1 200 OK'. So, I have tried setting up the if statement as follows:
'if (strcmp ($res2, "HTTP/1.1 200 OK") == 0)'
(This I did just to test it, I know I can't leave it that way.) I again tested the script, set up like this, yet it still executed the else statement instead of the if statement.
So, my question is: Why is strcmp not yielding a '0', if the two things it is comparing seem to be identical?
Thank you for your help!