2013-12-18 100 views
1

我的paypal ipn代碼停止工作後,或似乎沒有收集正確的信息,或者信息沒有設置。有沒有人有任何想法,應該爲這行代碼輸入正確的內容。代碼如下:貝寶Ipn不工作代碼停止

while (!feof($fp)) { 
$res = fgets ($fp, 1024); 

以上代碼是否正常運行,代碼如下。下面是代碼:

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 

這裏的大部分代碼:

// Response from Paypal 

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
foreach ($_POST as $key => $value) { 
    $value = urlencode(stripslashes($value)); 
    $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix 
    $req .= "&$key=$value"; 
} 

// assign posted variables to local variables 
$data['item_name']   = $_POST['item_name']; 
$data['item_number']  = $_POST['item_number']; 
$data['payment_status']  = $_POST['payment_status']; 
$data['payment_amount']  = $_POST['mc_gross']; 
$data['payment_currency'] = $_POST['mc_currency']; 
$data['txn_id']    = $_POST['txn_id']; 
$data['receiver_email']  = $_POST['receiver_email']; 
$data['payer_email']  = $_POST['payer_email']; 
$data['custom']    = $_POST['custom']; 

// post back to PayPal system to validate 
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 

if (!$fp) { 
    // HTTP ERROR 
} else {  

    fputs ($fp, $header . $req); 
    while (!feof($fp)) { 
     $res = fgets ($fp, 1024); 
     if (strcmp($res, "VERIFIED") == 0) { 

有誰知道爲什麼它不通過貝寶IPN代碼的這一部分?

+0

'ssl'應該是'https'嗎?此外,這不起作用?它停在哪裏? – halfer

+0

$ res = fgets($ fp,1024); – user3109009

回答

0

您應該使用curl而不是ssl套接字。你需要建立你的請求(在我的例子中,$ req),作爲對值字符串,即:$req="key1=value1&key2=value2";

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); 
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); 

    if(!($res = curl_exec($ch))) { 
     curl_close($ch); 
     exit; 
    } 
    curl_close($ch); 

而且您將在$res var。中獲得Paypal結果。

0

改變這一行:

$value = urlencode(stripslashes($value)); 

要這樣:

$value = urlencode($value); 

你只需要使用stripslashes那裏,如果你捉迷藏PHP與魔術引號上。從PHP 5.4開始,這是(幸好)不再可能。

貝寶允許用戶數據包含反斜槓(我看到它在address_street變量中的IPN數據中)。如果你從IPN數據中去掉反斜槓,貝寶將返回一個INVALID響應。