2016-03-25 127 views
1

所以我使用的是貝寶提供的示例代碼,以正確溝通和驗證傳入的IPN消息。使用提供的Paypal開發者工具「IPN模擬器」,我可以確認我的IPN監聽器代碼能夠成功與PayPal服務器握手,但是當需要重新發送驗證信息時,我只能成功地與非-sandbox版本的貝寶(無論如何返回無效,因爲一切都設置爲與沙箱貝寶的溝通)。當我嘗試連接到沙箱版本時,收到一個http錯誤,表示我無法連接到服務器。貝寶IPN響應適用於貝寶而不是沙箱 - 貝寶?

我想我已經分離出的問題這行代碼:

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

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

$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數據的附加代碼未顯示;這裏也是此代碼基於的鏈接):

<?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()配置我應該使用任何幫助嗎?

回答

1

嗯,我通過在我的域上安裝私有SSL證書(必須是2048位和SHA-256)來實現它,這意味着您必須具有HTTPS。

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

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

我目前託管Hostgator,如果它與任何人有任何關聯(私人SSL證書自動2048位和SHA-256這是很好)。

我希望我鬥爭的日子的結果也會幫助有人經歷同樣的問題,乾杯!

0

根據new paypal guidelines

適當的HTTPS端點是:

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

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

0

我認爲paypal沙盒只適用於https協議。

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

http中的連接失敗。

但是,連接在https中是成功的。