最新的2016 Paypal IPN代码总是返回INVALID响应


Latest 2016 Paypal IPN Code always return INVALID response

我知道这个查询有很多问题,但我找不到正确的答案来评估我的需求。这是我的项目的当前代码。我总是使用贝宝沙盒IPN模拟器从贝宝收到INVALID响应。

有人能告诉我我的代码出了什么问题吗?我很感激你的帮助。提前谢谢。

<?php
$post_data = file_get_contents('php://input');
$post_array = explode('&', $post_data);
$dataFromPayPal = array();
foreach ($post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2)
        $dataFromPayPal[$keyval[0]] = urldecode($keyval[1]);
}
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($dataFromPayPal as $key => $value) {
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
    } else {
        $value = urlencode($value);
    }
    $req .= "&$key=$value";
}
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
if( !($res = curl_exec($ch)) ) {
    curl_close($ch);
    exit;
}
curl_close($ch);

$tokens = explode("'r'n'r'n", trim($res));
$res = trim(end($tokens));
if (strcmp ($res, "INVALID") == 0) {
    echo "INVALID";
}
else if (strcmp ($res, "VERIFIED") == 0) {

$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$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'];

    //STORE temporary info to table
    //================
    require 'config.php';
    $query = "INSERT INTO Payments (ItemName,ItemNumber,PayStatus,PayAmount,PayCurrency,PayTransID,PayEmail,RecEmail) 
            VALUES('$item_name', '$item_number', '$payment_status', '$payment_amount', '$payment_currency', '$txn_id', '$payer_email', '$receiver_email')";
    $result = mysqli_query($con, $query);
    mysqli_close($con);

}

?>

我终于找到了询问的答案。您可以使用此代码作为Sandbox或Live的最终IPN。考虑以下因素:

  1. 请务必将您的IPN侦听器放置到->我的销售工具->即时支付通知部分
  2. 不要在沙箱中使用IPN模拟器,它将始终返回INVALID
  3. 创建并使用一个实际的沙盒按钮,但不要把你的IPN监听器放在RETURN PAGE上,上面写着"当客户完成结账时,把他们带到这个URL"

仅此而已。我希望这会有所帮助。

这是工作代码:

<?php
$post_data = file_get_contents('php://input');
$post_array = explode('&', $post_data);
$dataFromPayPal = array();
foreach ($post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2)
        $dataFromPayPal[$keyval[0]] = urldecode($keyval[1]);
}
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($dataFromPayPal as $key => $value) {
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
    } else {
        $value = urlencode($value);
    }
    $req .= "&$key=$value";
}
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
//use https://www.sandbox.paypal.com/cgi-bin/webscr in case you are testing this on a PayPal Sanbox environment
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
if( !($res = curl_exec($ch)) ) {
    curl_close($ch);
    exit;
}
curl_close($ch);

if (strcmp ($res, "INVALID") == 0) {
        echo "INVALID";
}
else if (strcmp ($res, "VERIFIED") == 0) {
        echo "VALID";
}
?>