简体   繁体   English

即使付款发送后,PayPal IPN也会返回INVALID

[英]PayPal IPN returning INVALID even after payment is sent

I am developing a shopping cart application, and in that application, I decided to integrate PayPal IPN. 我正在开发一个购物车应用程序,在那个应用程序中,我决定整合PayPal IPN。 However, it keeps returning INVALID. 但是,它会一直返回INVALID。 It successfully transfers money and is deducted from my account and moved to the buyer's account. 它成功转账并从我的账户中扣除并转移到买方账户。

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

My inputs and outputs are exactly the same. 我的输入和输出完全相同。 However, this particular key is different: 但是,这个特殊的关键是不同的:

INPUT: [payment_date] => 2016-12-09T15:07:18Z OUTPUT: payment_date=2016-12-09T14%3A12%3A21Z (After the query is made). INPUT: [payment_date] => 2016-12-09T15:07:18Z输出: payment_date=2016-12-09T14%3A12%3A21Z (查询后)。

I encountered this same error yesterday. 我昨天遇到了同样的错误。 Try the following: 请尝试以下方法:

$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 variables are already encoded and there is no need to do it twice, replace with this code below PayPal IPN变量已经编码,无需执行两次,请使用下面的代码替换

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM