Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not receiving a response from Paypal IPN Sandbox

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!

like image 219
user1070084 Avatar asked Nov 28 '11 20:11

user1070084


People also ask

Is sandbox PayPal different from PayPal?

The PayPal sandbox mirrors the features on the PayPal production servers. While some PayPal features do not apply to the sandbox, such as closing an account, issuing monthly statements, storing shipping preferences, and PayPal Shops support, the sandbox has the same PayPal API feature set as the live environment.

How do I enable IPN in sandbox PayPal?

Click the settings icon at the top of your PayPal account page and then click Account Settings. On the Notifications page, click the Update link for the Instant payment notifications item. Click Choose IPN Settings to specify your listener's URL and activate the listener.

How do I test failed transactions in the PayPal sandbox?

Go to the developer.paypal.com home page. Log into the Dashboard if you are not already and click the pull-down menu beneath your name to select Dashboard . Under the Sandbox heading in the left navigation column, click on Accounts . Locate the sandbox account for which you wish to enable negative testing.

How do I view my PayPal sandbox transactions?

Viewing Test Payments in PayPal To do this, return to your Sandbox Test Accounts page on PayPal's Developer site. Next, click on the three dots under Manage Accounts for the Personal test email. Then, click on View notifications. This will open a list of any recent actions taken under this account.


2 Answers

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);
like image 81
preinheimer Avatar answered Nov 11 '22 22:11

preinheimer


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.

like image 23
user1070084 Avatar answered Nov 11 '22 22:11

user1070084