10
votes

I'm putting a paypal checkout onto my website but am falling down with the listener. For those of you who are unfamiliar with the Paypal IPN system, basically Paypal sends your script with a message about the transaction, which you send back with a couple of bits added. If Paypal receives the correct reply, it'll reply with 'VERIFIED', and if not it'll say 'INVALID'.

I've succeeded with the first bit. My code is able to receive the info from paypal, add on the extras and post it back. However, I get no response from the Sandbox saying either 'VERIFIED' or 'INVALID'. I've pretty much copied my code from the paypal website so I was hoping this was going to be fairly straightforward, so if you could take a minute to look at my code, perhaps some new eyes could pick out where I've gone wrong.

Here's the code. Nothing special, it literally just gets the info, adjusts it, passes it back and reads the response (which it either isn't getting or doesn't realise it's getting)

<?php

$debug=true;

//Put together postback info

$postback = 'cmd=_notify-validate';

foreach($_POST as $key =>$value){
     $postback .= "&$key=$value";
}

// build the header string to post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postback) . "\r\n\r\n";

$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);//open the connection

if(!$fp){ //no conn
    die();
}

//post data back
fputs($fp, $header . $postback);

while(!feof($fp)){

    $res=fgets ($fp, 1024);

    if((strcmp($res, "VERIFIED")) == 0){ //verified!
        if($debug){         
            $filename = 'debug/debug5_verified.txt'; //create a file telling me we're verified
            $filehandle=fopen($filename, 'w');
            fwrite($filehandle,'VERIFIED!');
            fclose($filehandle);
        }
    }
}

?>

Thanks in advance!

6
Might be this link is help to you.... stackoverflow.com/questions/14827497/…Nikhil Thombare

6 Answers

11
votes

Switch over to using the HTTPS url, I'm not sure when but recently all of my test scripts started failing on the plain HTTP version. They look to be migrating over.

I'm using the same paypal sample code you are:

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

or

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

So I think I found a solution. Turns out it wasn't having trouble with connecting to ssl://sandbox...., it was actually retrieving the answer. The code was getting hung up on the

while(!feof($fp)){
    $res=fgets($fp,1024);
}

bit. All I did was replace it with:

$res=stream_get_contents($fp, 1024);

and it worked first time! Now I can get on with my life. Thanks again for all the help on this one.

3
votes

Perhaps the original code was missing: $header .= "Connection: close\r\n\r\n";

Note that the Paypal sample code uses HTTP/1.0 so does not have that line. And HTTP/1.1 is fine but might need the line.

On another issue, Sandbox may no longer support port 80. I am getting a 302 redirect to https://www.sandbox.paypal.com.

0
votes

I've noticed that the URL you are posting to is a little different than below, could this be it?

this is from the IPN Testing help page:

Check that your are posting your response to the correct URL, which is https://www.sandbox.paypal.com/cgi-bin/webscr or https://www.paypal.com/cgi-bin/webscr, depending on whether you are testing in the Sandbox or you are live, respectively.

Verify that your response contains exactly the same IPN variables and values in the same order, preceded with cmd=_notify-validate.

Ensure that you are encoding your response string and are using the same character encoding as the original message.

EDIT: Sorry I also wanted to mention that the port for HTTP and HTTPS are different, 80 as opposed to 443. I'm not too familiar with Paypal API but could look into it as I see you are using 80.

0
votes

PayPal test server moved to:

$fp = fsockopen('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30);
0
votes

check up php version tls socket. it should be tls 1.2 to get the response from sandbox account. upgrade the php version to 5.5 to get tls 1.2 socket.

paypal has disabled the service of sslv3 and changed to tls 1.2.

if you need to get the response,php version must require tls 1.2, in order to get tls 1.2 php can be upgraded to 5.5 or more.

visit the link.