Q:

PayPal IPN Stuck at Payment Verify

I have been working for some time on this, and have yet to find the solution.   I am attempting to get the PayPal IPN to work, but have yet to be successful at doing so. 
First, I was having some trouble connecting with SSL, so I went without it for now.   After that, I seemed to be getting some other issue.   I went ahead and added some debug, and have found that I am getting stuck at the VERIFY check.   That is really all I know about what is going on. 
Below is my PayPal IPN, and below that, my button code. 
<? php
include("inc/database.php");
$debug = true;
$header = '';
$req = "cmd=_notify-validate";
foreach($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

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

if ($debug)
{
    $ourFileName = "pdebug.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    fwrite($ourFileHandle, $req);
    fclose($ourFileHandle);
}

$fp = fsockopen('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
if(!$fp) {
    // HTTP Error;
    $message .= "\n HTTP ERROR.  \n";
} else {
    if ($debug)
    {
        $ourFileName = "debug/debug2_connected.txt";
        $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
        fclose($ourFileHandle);
    }
    fputs($fp, $header .  $req);
    while(!feof($fp)) {
        $res = fgets($fp, 1024);
        if ($debug)
        {
            $ourFileName = "debug/debug3_fgets.txt";
            $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
            fwrite($ourFileHandle, $res);
            fclose($ourFileHandle);
        }
        if(strcmp($res, "VERIFIED") == 0) {
            if ($debug)
            {
                $ourFileName = "debug/debug4_verified.txt";
                $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
                fclose($ourFileHandle);
            }
            $ids = explode("|", $_POST['custom']);
            $item_name = $_POST["item_name"];
            $item_number = $_POST["item_number"];
            $item_username = $ids[0];
            $item_packageid = $ids[1];
            $item_email = $ids[2];
            $payment_status = $_POST["payment_status"];
            $payment_amount = $_POST["mc_gross"];
            $payment_currency = $_POST["mc_currency"];
            $txn_id = $_POST["txn_id"];
            $receiver_email = $_POST["receiver_email"];
            $payer_email = $_POST["payer_email"];

            $prices = array(4, 4, 4);

            if(($payment_status == "Completed") && ($receiver_email == "bking-facilitator@inbox.com") && ($payment_amount == $prices[$item_packageid - 1]) && ($payment_currency == "USD") && (checkTxnId($txn_id) == 0)) {
                addPaypalPayment($item_name, $item_number, $item_username, $item_packageid, $item_email, $payment_status, $payment_amount, $payment_currency, $txn_id, $receiver_email, $payer_email);
                serviceAdd($item_username, $item_number, $item_packageid);
                setServiceActive($item_packageid);
                sendEmailWithUsername("Carwash", "PayPal IPN", "Success!"); 
            } else {
                $mail_To = getUserEmail($item_username);
                $mail_Subject = "Purchase Unsuccessful";
                $mail_Body = "Something went wrong with your recent order.  \n\nThe transaction ID number is :$txn_id \n\nPayment status is: $payment_status \n\nPayment amount is: $payment_amount \n\nIf you believe this is an error on our part, please submit a ticket and we will look into this for you."; 
                mail($mail_To, $mail_Subject, $mail_Body);
                sendEmailWithUsername("Carwash", "PayPal IPN", "Failure!!"); 
            }
        } else if(!strcmp($res, "INVALID")) {
            $mail_To = "bking@inbox.com";
            $mail_Subject = "PayPal - Invalid IPN";
            $mail_Body = "We had an INVALID response.  \n\nThe transaction ID number is $txn_id \n\n username = $item_username";
            mail($mail_To, $mail_Subject, $mail_Body);
            sendEmailWithUsername("Carwash", "PayPal IPN", "Failure!!!!!!!!"); 
        }
    }
    fclose($fp);
}
? >

Button Code:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $ppemail; ? >">
<input type="hidden" name="item_name" value="<?php echo $pname; ? >">
<input type="hidden" name="item_number" value="<?php echo $id; ? >">
<input type="hidden" name="amount" value="<?php echo $prices[$id - 1] . " .00"; ? >">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!" >
<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
<!--  <div style="padding-top:100px; padding-right: 40%; padding-left: 40%;">
    <center><input type="submit" name="submit" class="btn" value="Checkout with PayPal"></center>
</div> -->
<input type="hidden" name="return"
       value="http://bvpn.biz/success.php?id=<?php echo $id; ? >&met=<?php echo $met; ? >">
<input type="hidden" name="cancel_return"
       value="http://bvpn.biz/success.php?id=<?php echo $id; ? >&met=<?php echo $met; ? >&do=2">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="notify_url" value="http://bvpn.biz/ipn.php"/>
<input type="hidden" name="custom" value="<?php echo $_SESSION['username'].'|'.$pid. '|'.getUserEmail($_SESSION["username"]); ? >">
</form>

Any help that you could supply would be great! 

A:

Try if(strcmp(trim($res), "VERIFIED") == 0)...
According to this post here Paypal is sending VERIFIED response followed by \r\n when HTTP1.1 header is used.  trim($res) solved the problem for me.