PayPal notify_url无法通过strcmp验证

时间:2016-11-15 01:45:44

标签: php paypal

我在PayPal中遇到了一些与notify_url有关的问题。握手很好,但更新我的数据库的代码不起作用。所以要调试,我使用return.php url输出一些数据并尝试调试和回显问题所在。

这是我到目前为止所做的调试(在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>";
            }
        }
    }
}

我的结果是我得到了REQ的回声,我得到了FP工作的回声,我得到回声“WHILE YES”5次,但我没有“验证”的回声。所以脚本似乎没有通过if (strcmp ($res, "VERIFIED") == 0)

我现在被困住,我该如何解决这个问题?不确定是什么问题

这是我的$ req的回声(如果它有助于调试)

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

1 个答案:

答案 0 :(得分:0)

fgets()的结果包含换行符,但"VERIFIED"没有换行符。你需要在比较之前将其剥离。

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

如果您只想测试两个字符串是否相等,那么使用strcmp()也没有意义,只需使用==

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

但是,不要自己尝试使用HTTP协议,而是使用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>";
}