簡體   English   中英

貝寶 IPN 響應適用於貝寶,但不適用於沙盒貝寶?

[英]Paypal IPN response works for Paypal but not sandbox-Paypal?

因此,我使用 Paypal 提供的示例代碼來正確通信和驗證傳入的 IPN 消息。 使用提供的 Paypal 開發人員工具“IPN Simulator”,我能夠確認我的 IPN 偵聽器代碼能夠與 Paypal 服務器成功握手,但是當需要重新發送信息進行驗證時,我只能成功連接非-paypal 的沙盒版本(它返回無效,因為一切都設置為與沙盒 paypal 通信)。 當我嘗試連接到沙盒版本時,我收到一個 http 錯誤,這意味着我無法連接到服務器。

我想我已經將問題隔離到這行代碼:

$fh = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); //HTTP error

下面的代碼演示了我的測試,看看哪些 url 可以工作:

$fh = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);//works, returns invalid

$fh = fsockopen ('tls://www.paypal.com', 443, $errno, $errstr, 30);//works, returns invalid

$fh = fsockopen ('www.paypal.com', 443, $errno, $errstr, 30);//Does not work but does not throw HTTP Error (does not return Verified or Invalid either)



$fh = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//HTTP error

$fh = fsockopen ('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//HTTP error

$fh = fsockopen ('www.sandbox.paypal.com', 443, $errno, $errstr, 30);//Does not work but does not throw HTTP Error (does not return Verified or Invalid either)

這是我的服務器上當前的 IPN 偵聽器代碼(為簡單起見,未顯示在我的數據庫中記錄 IPN 數據的額外代碼;這里也是此代碼基於的鏈接: https : //developer.paypal.com/docs/經典/ipn/gs_IPN/ ):

<?php

// STEP 1 - acknowledge PayPal's notification

header('HTTP/1.1 200 OK');


// STEP 2 - create the response we need to send back to PayPal for them to confirm that it's legit

$resp = 'cmd=_notify-validate';
foreach ($_POST as $parm => $var) 
    {
    $var = urlencode(stripslashes($var));
    $resp .= "&$parm=$var";
    }


// STEP 3 - Get the HTTP header into a variable and send back the data we received so that PayPal can confirm it's genuine

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

 // Now create a ="file handle" for writing to a URL to paypal.com on Port 443 (the IPN port)

$errno ='';
$errstr='';

$fh = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); //PROBLEM?

// STEP 4 - Nearly done.  Now send the data back to PayPal so it can tell us if the IPN notification was genuine

 if (!$fh) {

// Uh oh. This means that we have not been able to get thru to the PayPal server.  It's an HTTP failure

           } 

// Connection opened, so spit back the response and get PayPal's view whether it was an authentic notification         

else    {
           fputs ($fh, $httphead . $resp);
           while (!feof($fh))
                {
                $readresp =  fgets ($fh, 1024);
               // $readresp = trim($readresp); 
                if (strcmp ($readresp, "VERIFIED") == 0) //yay verified data!
                    {




                    }

                else if (strcmp ($readresp, "INVALID") == 0) //Invalid data! :(
                    {





                    }
                }
fclose ($fh);
        }

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>

    </body>
</html>

這是我的按鈕代碼:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="M8IC3G88S5WFQ">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

我應該使用什么fsockopen()配置有什么幫助嗎?

好吧,我通過在我的域上安裝私有 SSL 證書(必須是 2048 位和 SHA-256)來使其工作,這意味着您必須擁有 HTTPS。

我也開始使用官方 Paypal 監聽器 php 示例代碼,可以在這里找到: https : //github.com/paypal/ipn-code-samples/blob/master/paypal_ipn.php

另外,我添加了curl_setopt($ch, CURLOPT_SSLVERSION, 6); 對於使用 TSLv1.2 通信的示例代碼 curl 語句,這是必需的,因為當前 Paypal 正在進行安全更新。

如果這與任何人有任何相關性,我目前由 Hostgator 托管(私有 ssl 證書自動為 2048 位和 SHA-256,這很好)。

希望我這幾天奮斗的成果也能幫到遇到同樣問題的人,干杯!

根據新的貝寶指南

正確的 HTTPS 端點是:

  • ipnpb.paypal.com
  • ipnpb.sandbox.paypal.com

也許嘗試第二個(ssl://ipnpb.sandbox.paypal.com)?

我認為貝寶沙箱僅適用於 https 協議。

$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 

http 連接失敗。

但是在 https 中連接成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM