php - 即使付款已发送,PayPal IPN 仍返回 INVALID

标签 php paypal paypal-ipn

我正在开发一个购物车应用程序,在该应用程序中,我决定集成 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(查询完成后)

最佳答案

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

$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); 
} 

关于php - 即使付款已发送,PayPal IPN 仍返回 INVALID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41059792/

相关文章:

wordpress - 在 Paypal 测试期间,我无法访问 sandbox.paypal 页面

ruby-on-rails - 将 text_field 自定义参数传递给 link_to paypal IPN

php - 按降序对一组软件版本进行排序

PHP Access 对象包含在一个类中。

windows-phone-7 - Windows Phone应用程序内支付的ApplicationPolicy

angular - Angular 2 组件中的 PayPal 快速结账 - 加载问题?

javascript - 在 Web 应用程序中实现灰盒

PHP - 在变量中查找某些字符串

php - 验证 PayPal IPN 的证书链

paypal - 即时付款通知 (IPN)