10

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!

user1070084
  • 383
  • 1
  • 3
  • 11
  • Might be this link is help to you.... http://stackoverflow.com/questions/14827497/unable-to-getting-response-from-paypal-ipn-sandbox – Nikhil Thombare Aug 19 '15 at 16:33

6 Answers6

11

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);
preinheimer
  • 3,712
  • 20
  • 34
9

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.

user1070084
  • 383
  • 1
  • 3
  • 11
  • If you are looking for the correct answer. For some it is using ssl:// and port 443. I came here with the same problem and switching to ssl protecol and using port 443 fixed it for me. I was getting an empty response in the sandbox environment. Note that stream_get_contents solution didn't work for me, so if that doesn't work for you try some of the other suggestions! Though -> Port 80 is still working from live site, as of June 8th 2012. – Dave Thomas Jun 15 '12 at 21:31
  • Why did this solution work? How did you realized that this was the solution? – nulll Jul 08 '13 at 11:02
3

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.

kitchin
  • 774
  • 6
  • 9
  • That's what I noticed as well. I set up an IPN two years ago and the docs said I could use normal HTTP for sandbox. Now, today, I found it returned 302 and I had to use SSL on post 443. – thomthom Aug 16 '12 at 10:18
0

PayPal test server moved to:

$fp = fsockopen('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30);
jkrcma
  • 1,180
  • 9
  • 14
Ademus
  • 1
  • 3
    Source? I didn't find anything about this URL change at PayPal's site. The PHP5.2 IPN example at their site uses sandbox.paypal.com. – thomthom Aug 16 '12 at 10:21
0

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.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
deepu np
  • 31
  • 5
0

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.

Community
  • 1
  • 1
Ace
  • 26
  • 2
  • Thanks for the help! I tried every combination of everything and still drew a blank, but I'll show you what I tried: Mostly I changed the $fp variable and left $header =."Host:...." alone or commented it out. I'm not sure whether I made it clear, but before I was getting a successful connection (according to my code) to the site I was connecting to, i.e. $fp was valid, and as such the condition if(!$fp) wasn't being met. Every combination I've tried since your post hasn't worked. Oddly, $fp=fsockopen('h ttp://w ww.san...com' 80,,, 30) DIDN'T work (but it does work with the 'http' removed). – user1070084 Nov 28 '11 at 22:44
  • Yes sorry, I tried both 80 and 443 for all combinations of https:// http:// or just www. I also tried it with the /cgi-bin/webscr added on the end, but the only combination that works is www.sand... and port 80. – user1070084 Nov 28 '11 at 22:57
  • could you try using ssl:// instead of https:// (while keeping port 443) to see if that works – Ace Nov 28 '11 at 23:03
  • Thanks again! Although when I add ssl:// it can't send the IPN. It just hangs and says that it can't access the website. – user1070084 Nov 28 '11 at 23:13
  • I did find this forum post: http://www.phpbuilder.com/board/archive/index.php/t-10329375.html which mentions the similar problems you are facing. I really hope you find a solution from that thread, good luck and sorry for not being able to help more! – Ace Nov 28 '11 at 23:36
  • Thanks again for the help! Will have a good old read of your link tomorrow I think...it's getting too late for all this! Will post back if I get a solution. – user1070084 Nov 28 '11 at 23:43
  • Thanks for the help, I managed to get a solution this morning! – user1070084 Nov 30 '11 at 12:04