IPN帖子每次都是空的

时间:2014-09-03 16:41:11

标签: php paypal paypal-ipn

我直接使用documentation

中的IPN示例

我向PayPal支付没有问题,PayPal重定向到我的监听器没有任何问题,但总是提出一个空的$ _POST变量。任何人都可以看到问题吗?

<?php
   // Send an empty HTTP 200 OK response to acknowledge receipt of the notification 
   header('HTTP/1.1 200 OK'); 

    // Assign payment notification values 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'];

    // Build the required acknowledgement message out of the notification just received
    $req = 'cmd=_notify-validate';               // Add 'cmd=_notify-validate' to beginning of the acknowledgement

    echo '<pre>';
    var_dump($_POST);
    echo '</pre><hr/>';

    foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
        $value = urlencode(stripslashes($value));  // Encode these values
        $req  .= "&$key=$value";                   // Add the NV pairs to the acknowledgement
    }

    // Set up the acknowledgement request headers
    $header  = "POST /cgi-bin/webscr HTTP/1.1\r\n";                    // HTTP POST request
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    // Open a socket for the acknowledgement request
    $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);

    // Send the HTTP POST request back to PayPal for validation
    fputs($fp, $header . $req);

    while (!feof($fp)) {                     // While not EOF
        $res = fgets($fp, 1024);               // Get the acknowledgement response

        if (strcmp ($res, "VERIFIED") == 0) {  // Response contains VERIFIED - process notification
            // Authentication protocol is complete - OK to process notification contents
            echo 'thanks for the payment, we are processing your request.';
         } else if (strcmp ($res, "INVALID") == 0) {
            //Response contains INVALID - reject notification
            echo 'something has gone wrong. please try again.';
        }
    }

    fclose($fp);  // Close the file

?>

结果:

array(0) {
}
something has gone wrong. please try again.

更新添加按钮代码:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="business" value="H7G2W8WN4XXXXX">

<input type="hidden" name="item_name_1" value="Item Name">
<input type="hidden" name="amount_1" value="0.25">

<input type="hidden" name="item_name_2" value="Item Name 2">
<input type="hidden" name="amount_2" value="0.25">

<input type="submit" value="Buy Now"/>
</form>

2 个答案:

答案 0 :(得分:1)

您没有看到任何内容的原因是因为var_dump()旨在将$_POST输出到浏览器。因此,当您访问IPN URL时,您没有向页面发布任何内容,因此没有任何内容。此外,脚本的其余部分仍然执行,将空白响应发送到PayPal进行验证,但由于它是空的,因此返回INVALID

如果您想查看IPN向您的收听者发布的内容,我建议您使用error_log()写入IPN日志。这将向您显示PayPal发布给您的听众时发生的事情。

另外,我注意到一些原本搞砸了我的事情。首先是标题信息。 PayPal更新了他们的系统,以便需要更多的头信息。有关更新的通知,请参阅here。 Basicall建议你改变这个:

$header  = "POST /cgi-bin/webscr HTTP/1.1\r\n";                  // HTTP POST request
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

到此:

$header  = "POST /cgi-bin/webscr HTTP/1.1\r\n";                  // HTTP POST request
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$header .= "Host: www.paypal.com:443\r\n";              //<---added this
$header .= "Connection: close\r\n";                     //<---and this

最后一点是我必须在trim()周围添加$res函数,因为我的VERIFIED带有一个尾随空格。

希望有所帮助!

答案 1 :(得分:0)

最后,我发现另一个文件隐藏在PayPal网站的某个地方,并附有一个较新的示例脚本。它开箱即用,开箱即用(原来应该有)。

以下是查找此内容的所有人的链接:https://gist.github.com/xcommerce-gists/3440401#file-completelistener-php