苦苦挣扎接收Paypal Sandbox IPN后期数据

时间:2012-05-07 11:05:32

标签: php paypal paypal-ipn

我的主机已经启用了PHP 5.0和SSL但是经过严格搜索在线论坛和博客后,我不能为我的生活找出为什么即时通过我试图使用的Paypal IPN脚本返回空手。我正在使用沙盒模拟器来测试脚本,因此在检索Post数据时没有骰子。请参阅我的以下代码 -

$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);
}

1 个答案:

答案 0 :(得分:1)

作为编写了几个PHP库来处理PayPal IPN(下面的CodeIgniter和Symfony2)的人,我不得不说你的脚本远远不足以处理保险箱中PayPal IPN的所有可能性。稳健的方式。

而不是重新发明轮子,只需插入其中一个库,具体取决于您的PHP设置: