难以接收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.0rn";
// $header .= "Host: ssl://www.sandbox.paypal.comrn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";
//$header .= "Connection: Closern";
$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);
}

作为一个编写了几个PHP库来使用PayPal IPN(下面的CodeIgniter和Symfony2库)的人,我不得不说你的脚本远远不足以以安全和健壮的方式处理PayPal IPN的所有可能性。

无需重新发明轮子,只需插入以下库之一,具体取决于您的 PHP 设置:

  • https://github.com/Quixotix/PHP-PayPal-IPN(通用 PHP 5)
  • https://github.com/webtechnick/CakePHP-Paypal-IPN-Plugin
  • https://github.com/orderly/codeigniter-paypal-ipn
  • https://github.com/orderly/symfony2-paypal-ipn

最新更新