2012-08-23 177 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