2012-05-09 47 views
1

我使用貝寶IPN下面的代碼:貝寶IPN沒有得到證實

<?php 

mysql_connect("localhost", "user", "password") or die(mysql_error()); 
mysql_select_db("PayPal") or die(mysql_error()); 

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
foreach ($_POST as $key => $value) { 
$value = urlencode(stripslashes($value)); 
$req .= "&$key=$value"; 
} 
// 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.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) { 

// PAYMENT VALIDATED & VERIFIED! 

} 

else if (strcmp ($res, "INVALID") == 0) { 

// PAYMENT INVALID & INVESTIGATE MANUALY! 

} 
} 
fclose ($fp); 
} 

    ?> 

在所有可能的方法測試後,我得到的一切,除了上班的時候:

if (strcmp ($res, "VERIFIED") == 0) 

不工作

if (strcmp ($res, "VERIFIED") == 1) 

WORKS

很明顯,它沒有得到驗證,因爲我從沙箱發送IPN。

什麼可能會丟失?

+0

如果您處於沙箱模式,請在'fsockopen的hostname參數中使用'ssl:// www.sandbox.paypal.com'而不是'ssl:// www.paypal.com' '。 – Josh

回答

3

既然你們都說strcmp($res, "VERIFIED") == 1是真的,$ res是比字符串VERIFIED大1個字符「大」。我的猜測是$ res在最後有一個\ n字符,或者其他需要被剝離的東西。在使用strcmp調用行之前,請嘗試執行類似str_replace('\n', '', $res)的操作。儘管大聲思考。如果這不起作用,請告訴我。

Fyi,PayPal在線樣本代碼使用cURL驗證PHP中的IPN。 鏈接:http://www.x.com/developers/paypal/documentation-tools/paypal-code-samples#instantpaymentnotification