2014-09-03 47 views
-1

我使用直接來自documentation的IPN示例。IPN帖子每次都是空的

我付款給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> 
+0

你可以發佈你的按鈕代碼嗎? – Eshan 2014-09-09 19:22:47

+0

@Eshan它現在包括在內。 – 2014-09-09 20:21:21

+0

您是否嘗試過使用[IPN Simulator](https://developer.paypal.com/webapps/developer/applications/ipn_simulator)發送測試請求?我只是用一個簡單的2行腳本(返回頭文件並將$ _POST轉儲到文件)來完成這個工作,並且它按預期工作。 – timclutton 2014-09-09 20:37:35

回答

1

,你看不到任何東西的原因是因爲var_dump()是爲了輸出$_POST到瀏覽器。所以,當你訪問IPN網址時,你不會在網頁上發佈任何內容,因此沒有任何內容。此外,腳本的其餘部分仍然執行,向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 

最後是我不得不添加周圍$restrim()功能,因爲我得到VERIFIED後面有個空格。

希望有幫助!

+0

謝謝你。它確實看起來很有利,但實際上,這些改變還不足以讓腳本正常工作。 – 2014-09-16 20:46:48