Hi there everyone!

I'm trying to troubleshoot my PayPal IPN page but am having a problem figuring out how to. I've used the IPN simulator and it says that it was successful, but when I get my error report, it says that it failed because !$fp. Could someone help me figure out how I should test why $fp = fsockopen ('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30) is failing? Here's my code:

/* Response from Paypal */
$error = 'The Following issues were encountered: ';
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
	$value = urlencode(stripslashes($value));
	$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
	$req .= "&$key=$value";
}

// assign posted variables to local variables
$txn_id = $_POST['txn_id'];
$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'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$custom = $_POST['custom'];

// post back to PayPal system to validate
$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 ('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
	// HTTP ERROR
	$error .= '<br>Error talking to the payment mothership!';
} else {
	fputs($fp, $header . $req);

    Hi there Clueless, I started with that and moved to tls per Paypal Developer instruction. A lot has happened in the 4 years since that blog post was written in regards to connection details.

    Any other suggestions on how to troubleshoot the connection would be greatly appreciated!

       
       header('HTTP/1.1 200 OK');
      
         if(isset($_POST['item_name'])){
      $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'];
      
      $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('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30); fputs($fp, $header . $req); while (!feof($fp)) {
      $res = fgets($fp, 1024);
      if (strcmp ($res, "VERIFIED") == 0) {
      $mail_From = "IPN@example.com"; $mail_To = "Your-eMail-Address"; $mail_Subject = "VERIFIED IPN"; $mail_Body = $req; mail($mail_To, $mail_Subject, $mail_Body, $mail_From); } else if (strcmp ($res, "INVALID") == 0) { $mail_From = "IPN@example.com"; $mail_To = "<var>Your-eMail-Address</var>"; $mail_Subject = "INVALID IPN"; $mail_Body = $req; mail($mail_To, $mail_Subject, $mail_Body, $mail_From); } } fclose($fp);
      }

      I managed to get this working, let me know how it goes

        Well, you should in theory have some error info right there that you can use to start debugging:

        $fp = fsockopen ('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
        
        if (!$fp) {
            // HTTP ERROR
            $error .= "<br>Error talking to the payment mothership! Error #$errno: '$errstr'";
        }
        

          We've had several tickets here re: Authorize.net, PayPal and others as TLS supersedes SSL. One thing we found is that our Linuxen (CentOS) tend to be hopelessly out of date by default where OpenSSL is concerned. Lots of work done upgrading various pieces and parts....

            6 days later

            Thank you guys for all your help. My server had an unsupported version of OpenSSL so after 5 days, I got to try it with the new version... and got the same "invalid response" with no actual $error that I've been getting all along. I guess since I can't find the issue in my paypal code, I will begin to sort through the shared working examples to see if I can use it to get me up and running. I normally manage to muddle through my issues without much fanfare but anytime I have to rely on an outside entity, it always seems to become an exercise in painful futility.

              I have always found it easier to use cURL to connect to payment gateways and other remote APIs. I've also found that any SSL/HTTPS/TLS connections occasionally break when you update software packages on the server. I would suggest a few things:
              ALWAYS check for error conditions. NogDog is totally spot on when he suggests you look at $errno and $errstr. You should be smart enough to start doing that on your own every time you try to open a socket, database, file, or anything at all that might return some kind of error condition.
              If this socket/database/file connection is really important (like you want to collect money) then you should probably write any resulting errors to a log file (or a database log table) or maybe send yourself an email to let you know there are problems.

                Write a Reply...