I have the following code for an IPN receiver.
When the IPN simulator is used, it gives the response "IPN was sent and handshake was verified". However the code after fsockopen seems not to be working. In the code given, I tried to make it send an email, but I also tried writing to a database table. Nothing seems to work. I more or less copied it from phpbuilder and I am at a loss to know how to debug the thing.
<?php
/* I have followed the detail in the bookmarked PHP Builder article */
header('HTTP/1.1 200 OK'); // Tells PayPal that a notification has been received
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$nbg = "NBG";
$req='cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type : application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
fputs ($fp, $header . $req); // Send the message back to PayPal which will either send back "VERIFIED" or "INVALID"
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// PAYMENT VALID
// Send an email announcing the IPN message is VERIFIED
$mail_From = "IPN@example.com";
$mail_To = "dcw.morley@physics.org";
$mail_Subject = "VERIFIED IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
else if (strcmp ($res, "INVALID") == 0) {
// PAYMENT INVALID
}
}
fclose ($fp);
?>