1

On my PayPal autoReturn page with a known-to-work PHP script to accommodate Payment Data Transfer, no matter what I do I keep getting this error message: "Warning: fgets(): SSL: Connection reset by peer...*(on the line where this is: '$line = fgets($fp, 1024);'* "

Before I ask my question, let me just say that I've tried everything suggested here and in any other forum or article that I've been advised to read, e.g. changing HTTP 1.0 to HTTP 1.1, using $res=stream_get_contents($fp, 1024) instead of the while loop with $line = fgets($fp, 1024), etc., etc. My problem persists.

Here's what I think might be the problem (and I'm hoping someone can tell me if I'm on the right track): My auto return page for PDT is on an add-on site and I'm thinking that PayPal hangs up when the shared SSL (for my primary domain on a shared server) isn't recognized. So I've asked my web host for SSL to be installed specifically for my add-on domain.

Could the add-on domain SSL thing be the reason for my warning message? Again, that message is: "Warning: fgets(): SSL: Connection reset by peer...*(on the line where this is: '$line = fgets($fp, 1024);'* "

Here's my code:

 //look if the parameter 'tx' is set in the GET request and that it  does not have a null or empty value
if(isset($_GET['tx']) && ($_GET['tx'])!=null && ($_GET['tx'])!= "") {
    $tx = $_GET['tx'];

    verifyWithPayPal($tx);
}
else {
    exitCode();
}

function verifyWithPayPal($tx) {
    $req = 'cmd=_notify-synch';
    $tx_token = $tx;
    $auth_token = "MY SANDBOX AUTH_TOKEN HERE";
    $req .= "&tx=$tx_token&at=$auth_token";

    // post back to PayPal system to validate
    $header = "POST /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";

    // url for paypal sandbox
    //$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno,   $errstr, 30);    

    // url for payal
    // $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
    // If possible, securely post back to paypal using HTTPS
    // Your PHP server will need to be SSL enabled.

     $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

    if (!$fp) {
        exitCode();
    } else {
        fputs($fp, $header . $req);        
        // read the body data
        $res = '';
        $headerdone = false;

         while (!feof($fp)) {
            $line = fgets($fp, 1024);
        // $res=stream_get_contents($fp, 1024);
            if (strcmp($line, "\r\n") == 0) {
                // read the header
                $headerdone = true;
            }
            else if ($headerdone) {
                // header has been read. now read the contents
                $res .= $line;
            }
         }

        // parse the data
        $lines = explode("\n", $res);

        $response = array();

        if (strcmp ($lines[0], "SUCCESS") == 0) {

            for ($i=1; $i<count($lines);$i++){
                list($key,$val) = explode("=", $lines[$i]);
                $response[urldecode($key)] = urldecode($val);
            }

            $itemName = $response["item_name"];
            $amount = $response["payment_gross"];
            $myEmail = $response["receiver_email"];
            $userEmailPaypalId = $response["payer_email"];
            $paymentStatus = $response["payment_status"];
            $paypalTxId = $response["txn_id"];
            $currency = $response["mc_currency"];

            // check the payment_status is Completed
            if($paymentStatus!="Completed") {
                payment_complete();
                emailer($userEmailPayPalID);

            } else {
                payment_incomplete($paymentStatus);
            }

            /*
            // check that txn_id has not been previously processed
            checkIfTransactionHasAlreadyBeenProcessed($paypalTxId);
            // check that receiver_email is your Primary PayPal email
            checkThatPaymentIsReceivedAtYourEmailAddress($myEmail);
            // check that payment_amount/payment_currency are correct
            checkPaymentAmountAndCurrency($amount, $currency);
            // process the order
            processOrder();
            } else {
            exitCode();
            */
        }
    }
        fclose ($fp);
}
JamesENL
  • 6,400
  • 6
  • 39
  • 64
pict5000
  • 31
  • 6

1 Answers1

1

I notice you're connecting to www.sandbox.paypal.com. I believe you want to connect to api.sandbox.paypal.com.

tomwhipple
  • 2,850
  • 27
  • 28
  • Thanks for that @tomwhipple. I scrapped that version of my code and used a cURL version, which worked great. It uses a different url altogether. I recommend others do the same. Otherwise it's a minefield of potential issues. – pict5000 Feb 05 '15 at 17:02