0

I have a PayPal IPN script that I'm testing in the sandbox. I have inserted the mail function everywhere possible just so that I know whats going on, and I always get an "Invalid Response" from PayPal, even when using the IPN tool at The PayPal Developer Site...

Here is my script, where the * character represents censorship of confidential information:

<?php
mysql_connect('localhost', '************', '******************');
mysql_select_db('*************');
$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.sandbox.paypal.com', 443, $errno, $errstr, 30);
$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) {
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if ($payment_status != 'Completed') {
    mail('**********************', 'ERROR', 'PayPal IPN Error: Payment Status INVALID');
    exit();
}
if ($reciever_email != '***********************') {
    mail('***********************', 'ERROR', 'PayPal IPN Error: Reciever Email INVALID');
    exit();
}
if ($payment_currency != 'USD') {
    mail('***********************', 'ERROR', 'PayPal IPN Error: CURRENCY INVALID');
    exit();
}
mail('***********************', 'PAYPAL TRANSACTION COMPLETE', 'PayPal Transaction Complete! $' . $payment_amount);
}
else if (strcmp ($res, "INVALID") == 0) {
mail('***********************', 'ERROR', 'PayPal IPN Error: RESPONSE INVALID');
exit();
}
}
fclose ($fp);
}
?>

The odd thing is, if I replace ssl://www.sandbox.paypal.com with ssl://www.paypal.com, the script seems to work fine. Any assistance here is greatly appreciated!

VCNinc
  • 747
  • 1
  • 9
  • 25
  • please dont use mysqli_* function see this http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя May 15 '13 at 03:22

1 Answers1

0

Have you capture the POST that you are receiving and captured what you are sending back and compared the two to make sure that they are being sent back correctly and exactly as is. Also take a look at the IPN trouble shooting steps posted here. They may help to resolve the issue as well.

Community
  • 1
  • 1
PP_MTS_Chad
  • 7,311
  • 1
  • 15
  • 20