Dysfunctional PayPal IPN PHP script

Discussion in 'PHP' started by TedMaster, Oct 18, 2011.

  1. #1
    My PayPal IPN PHP script has been working just fine until recently. I have not been able to get this solved and is getting quite fustrated. Please help!

    The script return no errors when executed in Chrome. It seems as if the PHP script runs until at a certain point, and then stops. By using the 'fwrite' function I have concluded that the scripts runs as far as the point "//THIS IS HOW FAR IT GETS!" inserted in the script coding below. This I conclude because the 'fwrite' function is executed properly at that point, but the "handleCompleted();" function is never executed. Additonally it seems as if the script never gets as far as "fclose ($fp);".

    This does not make any sense to me, so I hope some brilliant person out there can help..

    Only a relavant part of the code has been inserted below:


    
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    
    // post back to PayPal system to validate
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 80);
    
    $payment_status = $_POST['payment_status'];
    
    if (!$fp) {
    	logToFile($LOGFILE, "HTTP error");
    	// HTTP ERROR
    } else {
        fputs ($fp, $header . $req); //this line posts back to paypal
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp ($res, "VERIFIED") == 0) {
    		if (strcmp($payment_status, "Completed") == 0) {
                          //THIS IS HOW FAR IT GETS!
    				handleCompleted();
    			} else {
    				handleMisc();
    			}
    		} else if (strcmp ($res, "INVALID") == 0) {
    			logToFile($LOGFILE, "invalid - IPN");
    			mail($ADMIN_MAIL, "IPN - Invalid", $req);
    		}
    	}
    	fclose ($fp);
    }
    
    PHP:
     
    TedMaster, Oct 18, 2011 IP
  2. freshdevelopment

    freshdevelopment Notable Member

    Messages:
    1,303
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    240
    #2
    Keep in mind that your comment //THIS IS HOW FAR IT GETS! is inside an if statement, so have you checked it is not going to the else condition?

    I suspect you don't have errors enabled on the server. Add the following to the top of your script:

    error_reporting(E_ALL);
    PHP:
    You should then see any problems with the script reported. Paypal also keeps a log of all IPN callbacks and the messages returned. You can check this in your Paypal account, in the logs area. This could shed some light. Hope that helps :cool:
     
    freshdevelopment, Oct 18, 2011 IP