user3011784 user3011784 - 24 days ago 6
PHP Question

paypal notify_url can't get pass strcmp VERIFIED

I'm having some issues with my notify_url in paypal. The handshake is fine, but the code to update my DB doesn't work. So to debug, I'm using the return.php url to output some data and try to debug and echo where the problem is.

Here is what I did so far to debug (on return.php)

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

echo "<br>test REQ: ".$req."<br><br>";;

$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.sandbox.paypal.com', 443, $errno, $errstr, 30);

if (!$fp)
{
echo "FP NOT WORKIGN";
}
else
{
echo "FP WORKIGN<br><br>";

fputs ($fp, $header . $req);

while (!feof($fp))
{
echo "whie: YES<br>";
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0)
{
echo "verified<br>";

if($status == "Completed")
{
echo "<br>####### completed<br>";
}
}
}
}


My result is I get the echo of REQ, I get the echo of FP Working, I get echo "WHILE YES" 5 times, but I don't the echo of "VERIFIED". SO it seems the script doesn't get pass the
if (strcmp ($res, "VERIFIED") == 0)


I'm stuck now, how can I fix this issue? Not sure what's wrong

This is the echo of my $req (if it helps to debug)

test: cmd=_notify-validate&mc_gross=1.00&protection_eligibility=Ineligible&payer_id=3EW5DFETD3E4L&tax=0.00&payment_date=15%3A35%3A43+Nov+14%2C+2016+PST&payment_status=Completed&charset=windows-1252&first_name=test&mc_fee=0.33┬Čify_version=3.8&custom=1000%40%40Katia%40%40213+st-louis%40%40lemoyne%40%40quebec%40%40j4r+2l3%40%40Canada%40%401%40%40CAD%40%400%40%401.00&payer_status=verified&business=louisefrigon1-facilitator%40gmail.com&quantity=1&payer_email=louisefrigon1-buyer%40gmail.com&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31AchhgWj8TBE3e7K7SSx6jHwconuT&txn_id=6E11719159210262F&payment_type=instant&last_name=buyer&receiver_email=louisefrigon1-facilitator%40gmail.com&payment_fee=&receiver_id=LMCZ83V6LRE4S&txn_type=web_accept&item_name=Superfood+Powder&mc_currency=CAD&item_number=&residence_country=CA&test_ipn=1&handling_amount=0.00&transaction_subject=&payment_gross=&shipping=0.00&merchant_return_link=click+here&auth=A1CZ8ecFDP.O-dZHCSU6ouq8Gxn8qrDXHyoUX9qI-CEmFlvuS1Rq8FlqPmP8dCqlTffcxTJX84-huLEvRi5C.fA

Answer

The result of fgets() includes the newline, but "VERIFIED" doesn't have a newline. You need to strip it off before comparing.

$res = trim(fgets($fp, 1024));

There's also no point in using strcmp() if you just want to test if two strings are equal, just use ==.

if ($res == "VERIFIED") {
    ...
}

But instead of trying to do the HTTP protocol yourself, use curl.

$req = 'cmd=_notify-validate&' . http_build_query($_POST);
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
$res = curl_exec($ch);
if ($res == "VERIFIED") {
    echo "verified<br>";
    if($status == "Completed")
    {   
        echo "<br>####### completed<br>";
    }
} else {
    echo "$res<br>";
}