【问题标题】:PayPal IPN returning INVALID even after payment is sent即使付款已发送,PayPal IPN 仍返回 INVALID
【发布时间】:2017-04-24 21:29:07
【问题描述】:

我正在开发一个购物车应用程序,我决定在该应用程序中集成 PayPal IPN。但是,它一直返回 INVALID。转账成功,从我的账户中扣款,转入买家账户。

if ( ! count($_POST)) {
    throw new \Exception("Missing POST Data");
}
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = [];
foreach ($raw_post_array as $keyval) {
    $keyval = explode('=', $keyval);
    if (count($keyval) == 2) {
        // Since we do not want the plus in the datetime string to be encoded to a space, we manually encode it.
        if ($keyval[0] === 'payment_date') {
            if (substr_count($keyval[1], '+') === 1) {
                $keyval[1] = str_replace('+', '%2B', $keyval[1]);
            }
        }
        $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
}
// Build the body of the verification post request, adding the _notify-validate command.
$req = 'cmd=_notify-validate';
$get_magic_quotes_exists = false;
if (function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($myPost 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";
}
// Post the data back to PayPal, using curl. Throw exceptions if errors occur.
$ch = curl_init(self::VERIFY_URI);
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_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// This is often required if the server is missing a global cert bundle, or is using an outdated one.

    curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/cert/cacert.pem");

curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Connection: Close']);
$res = curl_exec($ch);
$info = curl_getinfo($ch);
$http_code = $info['http_code'];
if ($http_code != 200) {
    throw new \Exception("PayPal responded with http code $http_code");
}
if ( ! ($res)) {
    $errno = curl_errno($ch);
    $errstr = curl_error($ch);
    curl_close($ch);
    throw new \Exception("cURL error: [$errno] $errstr");
}
curl_close($ch);
// Check if PayPal verifies the IPN data, and if so, return true.
if ($res == "VERIFIED") {
    return 'success';
} else {
    return print_r($res, true);
    }

我的输入和输出完全一样。但是,这个特殊的键是不同的:

输入:[payment_date] => 2016-12-09T15:07:18Z 输出:payment_date=2016-12-09T14%3A12%3A21Z(查询完成后)。

【问题讨论】:

  • 您需要查看在您的 IPN 历史记录中发送的数据以及发送到 Paypal 的 POST 数据。然后您将能够找出问题所在或在此处分享。
  • 我确实观察到了。我可以在这里分享。一秒钟。
  • @DavidNguyen 立即查看。已更新。
  • 看起来不错,但为什么付款日期变了?
  • @DavidNguyen 由于此脚本中的编码/解码过程。我认为这就是使它无效的原因?我该如何解决这个问题?

标签: php paypal paypal-ipn


【解决方案1】:

我昨天遇到了同样的错误。请尝试以下操作:

$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));

   if (strcmp($res, "VERIFIED") == 0) {
         return "Success";
    }else if (strcmp($res, "INVALID") == 0) {
       //deal with invalid IPN
       //mail admin or alert client
}

PayPal IPN 变量已经编码,无需重复,替换为下面这段代码

if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()){  
    $varvalue = urlencode(stripslashes($varvalue)); 
}
else { 
    $value = urlencode($value); 
} 

【讨论】:

  • 它一直说无效。
  • 你能给我你的完整来源吗?
  • 你用过我介绍给你的课程吗?
  • 好的,我明白了。你必须使用上面的代码吗?
  • 你要我删除那部分吗?
猜你喜欢
  • 2014-08-31
  • 2016-06-28
  • 2011-11-20
  • 2016-03-12
  • 2016-10-10
  • 2016-10-15
  • 2014-10-06
  • 2012-01-04
  • 2014-01-23
相关资源
最近更新 更多