Paypal SandBox IPN始终返回INVALID

时间:2012-07-31 18:27:49

标签: php paypal paypal-ipn paypal-sandbox

正如下面答案中的一条评论所述,我尝试了this tutorial。所以现在我有以下内容:


ipn.php文件:

<?php

    $ipn_post_data = $_POST;

    $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';

    // Set up request to PayPal
    $request = curl_init();
    curl_setopt_array($request, array
    (
        CURLOPT_URL => $url,
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => http_build_query(array('cmd' => '_notify-validate') + $ipn_post_data),
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HEADER => FALSE,
        CURLOPT_SSL_VERIFYPEER => TRUE,
        CURLOPT_CAINFO => 'cacert.pem',
    ));

    // Execute request and get response and status code
    $response = curl_exec($request);
    $status   = curl_getinfo($request, CURLINFO_HTTP_CODE);

    // Close connection
    curl_close($request);

    if($status == 200 && $response == 'VERIFIED')
    {
        $subject = "valid";
        $message = "good";
    }
    else
    {
        $subject = "invalid";
        $message = "bad";
    }

    $to = "oshirowanen@mail.com";
    $from = "me@desktop.com";

    $header  = 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $header .= 'To: Oshirowanen <oshirowanen@mail.com>' . "\r\n";
    $header .= 'From: Me <me@desktop.com>' . "\r\n";

    mail($to,$subject,$message,$header);

?>

收到的电子邮件:

Subject "invalid"
Message "bad"

9 个答案:

答案 0 :(得分:14)

编辑:

既然我可以看到你输出的数组,请尝试替换它以摆脱PHP数组错误:

foreach ($_POST as $key => $value) {
    if (!is_array($value)) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    else if (is_array($value)) {
        $paymentArray = explode(' ', $value[0]);
        $paymentCurrency = urlencode(stripslashes($paymentArray[0]));
        $paymentGross = urlencode(stripslashes($paymentArray[1]));
        $req .= '&mc_currency=' . $paymentCurrency . '&mc_gross=' . $paymentGross;
    }
}

以下是完整编辑的代码:

// read the post from PayPal system and add 'cmd'
$req = 'cmd=' . urlencode('_notify-validate');

foreach ($_POST as $key => $value) {
    if (!is_array($value)) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    else if (is_array($value)) {
        $paymentArray = explode(' ', $value[0]);
        $paymentCurrency = urlencode(stripslashes($paymentArray[0]);
        $paymentGross = urlencode(stripslashes($paymentArray[1]);
        $req .= '&mc_currency=' . $paymentCurrency . '&mc_gross=' . $paymentGross;
    }
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);


// assign posted variables to local variables
$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'];


if (strcmp ($res, "VERIFIED") == 0) {
    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
}
else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}

Check this out

修改:查看PayPal问题排查提示:

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNTesting

答案 1 :(得分:6)

问题在于您没有检查HTTP响应代码,因此您将“无效主机标头”作为PayPal响应进行解释,而它是Web服务器响应(对于状态代码400)。
如果查看PayPal documentation,有一个PHP示例与您的代码非常相似,因为它使用“fsockopen”,“fputs”和“fgets”函数与PayPal服务器进行通信。
但是,如果您在“fsockopen”调用之后仔细查看该注释,则可以阅读:

// Process validation from PayPal 
// TODO: This sample does not test the HTTP response code. All 
// HTTP response codes must be handled or you should use an HTTP 
// library, such as cUrl

这就是你的问题:在解析响应体之前,你没有检查HTTP响应代码是否为200(OK)。
此外,使用“strtolower”功能是不正确的,因为来自PayPal服务器的实际响应始终为大写,如上面引用的示例所示。
即使PayPal示例使用“fsockopen”方法,我认为使用PHP cURL库来实现您的IPN监听器应该更好。
还要看看以下答案:

但是,如果你真的想使用“fsockopen”函数,你应该总是在POST请求中指定"Host" header field,如下面的代码片段所示(取自PHP manual) :

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

<强>更新

这是递归stripslashes / urlencoding的简单函数:

<html>
<body>
<pre>
<?

$post = Array (
  "transaction" => Array("USD 20.00"),
  "payment_request_date" => "Sun Aug '05 08:49:20 PDT 2012",
  "return_url" => "http://000.000.000.000/success.php"
);

echo "before myUrlencode...\n";
print_r($post);

function myUrlencode($post) {
  foreach ($post as $key => $val) {
    if (is_array($val)) {
      $post[$key] = myUrlencode($val);
    } else {
      $post[$key] = urlencode(stripslashes($val));
    }
  }
  return($post);
}

echo "\nafter myUrlencode...\n";
print_r(myUrlencode($post));

?>
</pre>
</body>
</html>

答案 2 :(得分:2)

  1. 使用basic sample code 4b,

  2. 工作
  3. 从基本示例代码中清除$ipnNotificationUrl = "";,因为我在那里添加了一个我自己添加的值,

  4. 在沙盒中创建了卖家帐户,而不是业务专业帐户,

  5. 设置卖家帐户以启用ipn网址

  6. 使用以下PHP 5.2 sample code作为ipn侦听器

  7. 在监听器中添加了2行,如here所述,可以在下面看到2行:

  8. cacert.pem证书从here下载到我的服务器并将其放在与ipn listener相同的目录中:

  9. 第6点提到的2行:

    CURLOPT_SSL_VERIFYPEER => TRUE,
    CURLOPT_CAINFO => 'cacert.pem',
    

    我不知道为什么沙盒业务专业版帐户不允许我设置ipn网址,但卖家帐户会这样做。

答案 3 :(得分:1)

答案 4 :(得分:1)

我现在不确定你的代码究竟出现了什么问题,但是我在前面遇到了同样的问题,我的修正是在标题中添加HOST而主机必须是www.paypal.com。我使用fsockopen方法,现在工作正常。

在Curl中,我之前遇到过ssl问题。解决方案就是放下这些内容:

curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt");

当然文件cookies.txt必须存在。 而且我必须运行一个连接到页面以获取会话数据,然后发送帖子数据。

以下是使用fsockopen方法

对我工作正常的标题
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

答案 5 :(得分:1)

这是+字符的问题,它经常被错误地取出,所以我做了解决方法,它对我有用。

  

payment_data = 2016年6月4日星期六15:11:16 GMT + 0200(CEST)

foreach ($_POST as $key => $value) {
if($key !== "payment_date"){
    $req .= '&' . $key . '=' . rawurlencode(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
}else{
    $req .= '&' . $key . '=' . rawurlencode(str_replace(array('GMT '),array('GMT+'),$value));
}}

答案 6 :(得分:0)

以下是如何避免这些错误...

foreach ($_POST as $key => $value) {
     if ($key=='transaction')
          foreach ($value as $key2=>$value2) {
               $value['transaction'][$key2] = urlencode(stripslashes($value2));
     }
     else {
          $value = urlencode(stripslashes($value));
     }
     $req .= "&$key=$value";
 }

答案 7 :(得分:0)

在我看到Izudin的回答之前,头发拉了几个小时。他是对的..日期中的+没有被转移。为了测试,我将其从模拟器中预先填充的字段中删除,最后得到Verified

答案 8 :(得分:0)

我终于找到了此查询的更新(2016年8月5日)工作答案。您可以将此代码用作Sandbox或Live的最终IPN。考虑以下因素:

  1. 请务必将您的IPN听众置于 - &gt;我的销售工具 - &gt;即时付款通知科。
  2. 不要在沙盒中使用IPN Simulator,它将始终返回INVALID。
  3. 创建并使用实际的沙盒按钮,但不要将您的IPN侦听器放到RETURN PAGE上,该页面上写着“当客户完成结帐时将其带到此URL”。
  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";
    }
    
    ?>