2016-12-09 88 views
1

我正在開發購物車應用程序,並且在該應用程序中,我決定整合PayPal IPN。但是,它一直返回INVALID。它成功轉移資金並從我的賬戶中扣除並轉移到買方的賬戶。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 OUTPUT:(查詢作出後)payment_date=2016-12-09T14%3A12%3A21Z

+0

你需要看數據的代碼替換您的IPN發送歷史和POST數據被髮送到貝寶。然後你將能夠確定什麼是錯誤的或在這裏分享。 –

+0

我確實觀察過。我可以在這裏分享。一秒。 –

+0

@DavidNguyen立即查看。更新。 –

回答

0

昨天我遇到了同樣的錯誤。請嘗試以下操作:

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

貝寶IPN變量已經被編碼,也沒有必要做兩次,下面

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

它一直說無效。 –

+0

你可以給我你的完整源代碼嗎? –

+0

查看我的更新回答Hassan – Akintunde007