最新的2016 Paypal IPN代码始终返回INVALID响应

时间:2016-08-04 07:15:03

标签: php paypal-ipn paypal-sandbox

我知道有关此调查的问题很多,但我无法找到正确的答案来评估我的需求。这是我目前的项目代码。我总是使用Paypal Sandbox IPN模拟器从PayPal收到无效回复。

有人能指出我的代码有什么问题,感谢您的帮助。先感谢您。

<?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);


}


?>

1 个答案:

答案 0 :(得分:0)

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

  1. 请务必将您的IPN听众置于 - &gt;我的销售工具 - &gt;即时付款通知科。
  2. 不要在沙盒中使用IPN Simulator,它将始终返回INVALID。
  3. 创建并使用实际的沙箱按钮,但不要将您的IPN监听器放在返回页面上,表示&#34;在完成结帐时将客户带到此URL&#34;。
  4. 这就是全部。我希望这会有所帮助。

    这是工作代码:

    <?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";
    }
    
    ?>