2012-08-23 245 views
0

我爲這個問題掙扎了幾天,但沒有成功,我相對 新的paypal ipn,但已經用它在過去幾個monhts成功,現在也許我是 做一些愚蠢的錯誤或PayPal沙箱ipn服務器是不負責任的。paypal ipn和mysql

付款處理正常,錢從買家帳戶轉到賣家,但仍沒有在數據庫中輸入詳細信息。

所以這是HTML表單代碼:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST"> 
    <input type="hidden" name="cmd" value="_xclick"> 
    <input type="hidden" name="business" value="sanboxselleremail"> 
    <input type="hidden" name="item_name" value="Product"> 
    <input type="hidden" name="item_number" value="1"> 
    <input type="hidden" name="amount" value="15"> 
    <input type="hidden" name="no_shipping" value="1"> 
    <input type="hidden" name="no_note" value="1"> 
    <input type="hidden" name="currency_code" value="USD"> 
    <input type="hidden" name="lc" value="EN"> 
    <input type="hidden" name="bn" value="PP-BuyNowBF"> 
    <input type="hidden" name="return" value="http://mysite.com/testipn/"> 
    <input type="hidden" name="cancel_return" value="http://mysite.com/testipn/"> 
    <input type="hidden" name="rm" value="2"> 
    <input type="hidden" name="notify_url" value="http://mysite.com/testipn/ipn.php" /> 
    <input type="submit" value="submit" /> 
</form> 

這是IPN的代碼,我發現在貝寶:

include('db.php'); 

// 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.sandbox.paypal.com', 443, $errno, $errstr, 30); 

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


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

if($payment_status=='Completed'){ 

$paylog = $db->query("INSERT INTO....); 

} 
else if (strcmp ($res, "INVALID") == 0) { 
// log for manual investigation 
} 
} 
fclose ($fp); 
} 

我仔細檢查過的一切,我知道你必須回發所有的變量,所以我也檢查了它們。 sql語法沒有問題,因爲我測試了它,並且它輸入了我想要的數據庫表的 值。

您能否快速查看並指出您可能發現的任何錯誤?

謝謝。 這個問題造成了我很多時間和壓力... :(

回答

0

你代碼已過時,並且不包含需要(現在)需要的HTTP'主機'頭。
所以使用此代碼,它永遠不會獲得bac k'VERIFIED',而是從PayPal返回一個HTTP/1.1 400'Bad Request'。

爲了解決這個問題,只需更改:
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";

要:
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";

並添加:
$header .= "Host: www.sandbox.paypal.com\r\n";

如果你只是想使用更新的示例代碼,你可以找到這在https://www.paypal.com/ipn/

+0

謝謝。我現在會測試這個代碼:)並且會讓你知道。謝謝。 – inrob

+0

我一直在試圖讓大家覺得不舒服。謝謝你的建議。對此,我真的非常感激。上帝保佑你,上帝保佑stackoverflow太:) – inrob