user1379549
user1379549

Reputation: 21

Struggling To Receive Paypal Sandbox IPN Post Data

My hosting has php 5.0 and SSL enabled but after rigorously scouring online forums and blogs i cant for the life of me figure out why im returning empty handed with the Paypal IPN script im trying to use. I am using the sandbox simulator to test the script and so for no dice in retrieving Post data. Refer to my following code -

$header = '';

$req = 'cmd=_notify-validate';

foreach($_POST as $key => $value) {
    //All PayPal reqs must be URL encoded
    $value = urlencode(stripslashes($value));
    //Append key => value pair to CMD string
    $req .= "&$key=$value";
} 


//Post info back to paypal for verification
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
// $header .= "Host: ssl://www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//$header .= "Connection: Close\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

$con=mysql_connect("localhost","quadsour_xswitch","xswitch");
mysql_select_db("quadsour_xswitch",$con);//$data="NULL"

if(!$fp) {
    //Process HTTP Error
     $filename = 'debug.txt';
    $filehandle=fopen($filename, 'w');
    fwrite($filehandle, $errstr.'('.$errno.')');
    fclose($filehandle);
    die();

 $message .= "\n HTTP ERROR. \n";

} else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
        $res = stream_get_contents($fp, 1024);
        /* $data="INSERT INTO ipn SET
        complete='".$errstr."',invoice='".$errno."'";
        $query=mysql_query($data);
        */

        //VERIFIED TRANSACTION
        //PAYMENT
        if($_POST['payment_status'] == 'completed') {
            $data="INSERT INTO ipn SET
            complete='cond'";
            $query=mysql_query($data);

            $data="INSERT INTO ipn SET
            complete='true',
            invoice='".$_POST['invoice']."',
            email='".$_POST['payer_email']."'
            ";
            $query=mysql_query($data);

            //$subscription = //Do SQL to retrieve the subscription based on $_POST['invoice']


        } 

    }

    fclose ($fp);
}

Upvotes: 2

Views: 996

Answers (1)

Alex Dean
Alex Dean

Reputation: 16075

As someone who has written a couple of PHP libraries to work with PayPal IPN (the CodeIgniter and Symfony2 ones below), I have to say that your script is nowhere near sufficient to handle all the eventualities of PayPal IPN in a safe and robust manner.

Rather than reinvent the wheel, just plug in one of these libraries, depending on your PHP setup:

Upvotes: 1

Related Questions