Paypal IPN没有响应,只有“连接关闭”

时间:2016-02-19 09:16:27

标签: php paypal paypal-ipn

我正在开发一个IPN响应页面,但似乎存在一些问题:

我收到正确的Paypal发布数据,当我发送数据时确认,Paypal没有回复!

这是我的PHP代码:

$paypal_url = "www.sandbox.paypal.com";
$url = "https://" . $paypal_url . "/cgi-bin/webscr";

$req = 'cmd=_notify-validate';                // Add 'cmd=_notify-validate' to beginning of the acknowledgement
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
 }

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

$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
fputs($fp, $header . $req."\r\n\r\n");

$counter = 0;
while (!feof($fp))
    {
    $res = $res . fgets($fp, 1024); 
    $counter++;
    if($counter > 10)break;
    }

fclose($fp);
$tfile=fopen("text.txt","a+");
fwrite($tfile,$req . "\r\n");
fwrite($tfile,$res . "\r\n");
fclose($tfile);

在我的text.txt测试文件中:

cmd=_notify-  validate&mc_gross=0.02&protection_eligibility=Eligible&address_status=unconfirmed&item_number1=&payer_id=M4DXNVMDFB8TY&tax=0.00&address_street=Gandhi+5%0D%0ABO&payment_date=01%3A05%3A50+Feb+19%2C+2016+PST&payment_status=Completed&charset=windows-1252&address_zip=40069&mc_shipping=0.00&mc_handling=0.00&first_name=test&mc_fee=0.02&address_country_code=IT&address_name=test+facilitator&notify_version=3.8&custom=56&payer_status=verified&business=andreafilippini90%40gmail.com&address_country=Italy&num_cart_items=1&mc_handling1=0.00&address_city=Zola+Predosa&verify_sign=AAllE9FA13ABhVwJYLWEzvptA49cACmRumx.SAuIktyCdTEuwgwrBDOF&payer_email=andreafilippini90-facilitator%40gmail.com&mc_shipping1=0.00&tax1=0.00&txn_id=2XL26853PJ6650947&payment_type=instant&payer_business_name=test+facilitator%27s+Test+Store&last_name=facilitator&address_state=&item_name1=lol&receiver_email=andreafilippini90%40gmail.com&payment_fee=&quantity1=1&receiver_id=36AE5DEXKKHSY&txn_type=cart&mc_gross_1=0.02&mc_currency=EUR&residence_country=IT&test_ipn=1&transaction_subject=&payment_gross=&ipn_track_id=436b4ca66c696

HTTP/1.1 200 OK

Date: Fri, 19 Feb 2016 09:05:59 GMT

Server: Apache

X-Frame-Options: SAMEORIGIN

Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=B2WFzbH6YM2Qyocy2U8eFnZLXwNKbE5H6baCy7qZWiquQveA1SY6UXyZGdfVwZhcRZgDRdhebwuzWFRSDj-JjwIGDp30AX8j3Ps5vhehIHBj7BlRSHWnIDfGq1pdF2_4Ffl6U9PpApn2XMxLLOHKuaJ3avnkqrE7ZZBw9dqp1RtWr6t60ZSDuSrek2zMtn08YXZ7thh1wF88X-1wqF1u3e6pDEZKeDuUOvSxut-QuRsEwIcNSJmDkmbdqAUYASBlgTNG1TolLzQgt3FbOVOI-zXfyu1uDk9BWlgAYpxL_XL00vjCVaX6iYkdC98OXbfhj2mnJvlWxFjbPiDjCKamWlD84U_EeV7GhjUbN5qns_F5OSMbXBm0erybHvlGMTCIJ53-j8FSw68imXw6WkM6Fzl_bq67Zle08eDb3mhLuzekpw_Xg-CpL5TMZne; domain=.paypal.com; path=/; Secure; HttpOnly

Set-Cookie: cookie_check=yes; expires=Mon, 16-Feb-2026 09:06:00 GMT; domain=.paypal.com; path=/; Secure; HttpOnly

Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly

Set-Cookie: navlns=0.0; expires=Sun, 18-Feb-2018 09:06:00 GMT; domain=.paypal.com; path=/; Secure; HttpOnly

Set-Cookie: Apache=10.72.108.11.1455872759947539; path=/; expires=Sun, 11-Feb-46 09:05:59 GMT

Vary: Accept-Encoding,User-Agent

Connection: close

任何想法??

为什么没有回复?

谢谢。

1 个答案:

答案 0 :(得分:1)

这是使用IPN的验证请求步骤,以便您获取PayPal已发布到您的IPN脚本并将其发送回PayPal以验证它的数据;即使一切都正确,这一步偶尔会失败,所以你必须准备好处理它。

您可以使用类似于以下内容的cURL调用执行此操作:

//Sandbox URL
$sURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';

//QueryString
$sQueryString = "cmd=_notify-validate";
foreach($_POST as $sPostKey => $sPostData) { $sQueryString .= "&{$sPostKey}={$sPostData}"; }

//Open the cURL session
$hCurl = curl_init();

//set cURL options
curl_setopt($hCurl, CURLOPT_URL, $sURL);                                    //URI
curl_setopt($hCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);           //HTTP version
curl_setopt($hCurl, CURLOPT_HEADER, 0);                                     //no HTTP headers
curl_setopt($hCurl, CURLOPT_POST, 1);                                       //REQUEST => POST
curl_setopt($hCurl, CURLOPT_POSTFIELDS, $sQueryString);                     //POST data
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1);                             //return raw data to script rather than browser
curl_setopt($hCurl, CURLOPT_TIMEOUT, 60);                                   //set timeout
curl_setopt($hCurl, CURLOPT_FORBID_REUSE, 1);
curl_setopt($hCurl, CURLOPT_HTTPHEADER, array('Connection: Close'));

//ensure that cURL is using the server IP address
curl_setopt($hCurl, CURLOPT_INTERFACE, $_SERVER['SERVER_ADDR']);

//The next two lines must be present for the kit to work with newer version of cURL
//You should remove them if you have any problems in earlier versions of cURL
curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($hCurl, CURLOPT_SSL_VERIFYHOST, 2);

//post and retrieve the response
$sRawResponse = trim(curl_exec($hCurl));

//throw Exceptions if cURL failed
if(curl_error($hCurl)) { throw new Exception(curl_error($hCurl), E_USER_ERROR); }
if(!$sRawResponse) { throw new Exception("No response from PayPal - cURL possibly failed"); }

//close the cURL handle
curl_close($hCurl);

结束时,应该从PayPal返回存储在$sRawResponse变量中的响应,如果一切按计划进行,则应该只有在其中验证