Search code examples
phppaypalpaypal-ipn

Paypal IPN does not respond, only "Connection Close"


I'm developing an IPN response page but seems there're some problem:

I receive correctly Paypal post data, when I send data do get confirm, Paypal does not respond!

here's my PHP code:

$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);

On my text.txt test file :

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

Any idea??

Why it does not respond?

Thank you.


Solution

  • This is the Verify Request step with IPN so you're getting the data that PayPal has POSTed to your IPN script and sending it back to PayPal to verify it; this step does fail occasionally btw even if everything is correct so you have to prepare to handle that.

    You can do this with a cURL call that looks like:

    //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);
    

    At the end of which you should have the response back from PayPal stored in the $sRawResponse variable which, if everything has gone according to plan, should simply have the word VERIFIED in it.