2016-02-05 66 views
1

第一次在這裏,我試圖解決問題後的幾天完全難倒。這是這個問題的背景...PayPal IPN聽衆回發失敗

我有一個貝寶IPN監聽器腳本。事實上,我有兩個版本,因爲第一個版本沒有工作,所以我嘗試了另一個版本,但是這也不起作用。無論我來自PayPal獲得的IPN監聽器的腳本...

paypal_notifyA.php

從... 開發商。貝寶。 COM /文檔/經典/ IPN/gs_IPN/

腳本將停止在...

的fputs($ FP,$頭$ REQ。);

<?php require_once("../lib/PHPMailer/PHPMailerAutoload.php"); ?> 

<?php 
// Paypal IPN 

// Send an empty HTTP 200 OK response to acknowledge receipt of the notification 
header('HTTP/1.1 200 OK'); 

// Assign payment notification values to local variables 
$invoiceID   = $_POST['invoice']; 
$paymentStatus  = $_POST['payment_status']; 
$productID   = $_POST['item_number']; 
$paymentAmount  = $_POST['mc_gross']; 
$paymentCurrency = $_POST['mc_currency']; 
$txnID    = $_POST['txn_id']; 
$receiverEmail  = $_POST['receiver_email']; 
$payerEmail   = $_POST['payer_email']; 
$response   = $_POST['response']; 

// Build the required acknowledgement message out of the notification just received 
$req = 'cmd=_notify-validate';     // Add 'cmd=_notify-validate' to beginning of the acknowledgement 
foreach ($_POST as $key => $value) {   // Loop through the notification NV pairs 
    $value = urlencode(stripslashes($value)); // Encode these values 
    $req .= "&$key=$value";     // Add the NV pairs to the acknowledgement 
} 

// Set up the acknowledgement request headers 
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";     // HTTP POST request 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

// The PayPal IPN Simulator fails on the line below... 
// Open a socket for the acknowledgement request 
$fp = fsockopen('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // paypal.com (LIVE) - sandbox.paypal.com (TEST) 

// Send the HTTP POST request back to PayPal for validation 
fputs($fp, $header . $req); 

while (!feof($fp)) {      // While not EOF 
    $res = fgets($fp, 1024);    // Get the acknowledgement response 
    if (strcmp ($res, "VERIFIED") == 0) { // Response contains VERIFIED - process notification 

     $testStage = "Got past Verified!"; 
     require_once("../emails/ipn_test.php"); 

     // DO MY OWN VERIFIED STUFF HERE 

// ***** INVALID RESPONSE ***** 
} else if (strcmp ($res, "INVALID") == 0) { // Response contains INVALID - reject notification 

    // DO MY OWN INVALID STUFF HERE 
} 

} 
fclose($fp); // Close the file 
?> 

paypal_notifyB.php

從... github上。 COM /paypal/ipn-code-samples/blob/master/paypal_ipn.php

腳本將停止在...

$水庫= curl_exec($ CH);

,並報告在錯誤日誌中的...

無法連接到PayPal,以驗證IPN消息:錯誤:14077410:SSL例程:SSL23_GET_SERVER_HELLO:SSLV3警報握手失敗

<?php require_once("../lib/PHPMailer/PHPMailerAutoload.php"); ?> 

<?php 
// Paypal Notify 

// Start session 
session_start(); 

// CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory. 
// Especially useful if you encounter network errors or other intermittent problems with IPN (validation). 
// Set this to 0 once you go live or don't require logging. 
define("DEBUG", 1); 

// Set to 0 once you're ready to go live 
define("USE_SANDBOX", 1); 

define("LOG_FILE", "./ipn.log"); 


// Read POST data 
// reading posted data directly from $_POST causes serialization 
// issues with array data in POST. Reading raw POST data from input stream instead. 
$raw_post_data = file_get_contents('php://input'); 
$raw_post_array = explode('&', $raw_post_data); 
$myPost = array(); 
foreach ($raw_post_array as $keyval) { 
    $keyval = explode ('=', $keyval); 
    if (count($keyval) == 2) 
     $myPost[$keyval[0]] = urldecode($keyval[1]); 
} 

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
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 IPN data back to PayPal to validate the IPN data is genuine 
// Without this step anyone can fake IPN data 
if(USE_SANDBOX == true) { 
    $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; 
    $livetestEmail = "[email protected]"; 
} else { 
    $paypal_url = "https://www.paypal.com/cgi-bin/webscr"; 
    $livetestEmail = "[email protected]"; 
} 

$ch = curl_init($paypal_url); 
if ($ch == FALSE) { 
    return FALSE; 
} 

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_VERIFYPEER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 

if(DEBUG == true) { 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLINFO_HEADER_OUT, 1); 
} 

// Set TCP timeout to 30 seconds 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); 

// CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below. Ensure the file is readable by the webserver. 
// This is mandatory for some environments. 

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

// PayPal IPN Simulator fails on the line below and reports following error... 
// Can't connect to PayPal to validate IPN message: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure 
$res = curl_exec($ch); 

if (curl_errno($ch) != 0) { // cURL error 

    if(DEBUG == true) { 
     error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE); 
    } 
    curl_close($ch); 
    exit; 
} else { 
     // Log the entire HTTP response if debug is switched on. 
     if(DEBUG == true) { 
      error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE); 
      error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE); 
     } 
     curl_close($ch); 
} 

// Inspect IPN validation result and act accordingly 
// Split response headers and payload, a better way for strcmp 

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

if (strcmp ($res, "VERIFIED") == 0) { // Response is verified 

    $testStage = "Got past Verified!"; 
    require_once("../emails/ipn_test.php"); 

    // DO MY OWN VERIFIED STUFF HERE 

// ***** INVALID RESPONSE ***** 
} else if (strcmp ($res, "INVALID") == 0) { // Response contains INVALID - reject notification 

    // DO MY OWN INVALID STUFF HERE 

    if(DEBUG == true) { 
     error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE); 
    } 

} 
?> 

當我在PayPal IPN模擬器中嘗試使用這兩個腳本時,兩者都報告...

IPN已發送且握手已得到驗證。

但是,這兩個腳本都不會與PayPal進行通信,並且永遠不會到達「If VERIFIED」條件。因此,在這種情況之後還沒有後端代碼是無關緊要的 - 只是想讓聽衆現在能夠工作!

我已經和我的虛擬主機談過了,他們表示他們禁用了SSLv3,希望導致這個問題。它沒有任何區別,兩個腳本在返回PayPal的地方仍然失敗。我的主機聲稱這不是「他們」的錯,而且這是PayPal的問題。儘管將主持人指向相關SO線程的方向(我不能發佈,因爲沒有聲望)。

stackoverflow。 com/questions/35030756/paypal-ipn-openssl-error14077410ssl-routinesssl23-get-server-hellosslv3-aler

stackoverflow。 COM /問題/ 34955346 /貝寶IPN-握手問題

而來自PayPal此頁......

貝寶知識。 com/infocenter/index?頁面=內容& ID = FAQ1766 &擴大=真&區域= EN_US

我希望有人能幫助我解決這個,至少可以確認這是「我的」腳本在故障或服務器有過錯,因爲我一無所獲!!

P.S.我對PHP很陌生。

回答

1

面對同樣的問題,但在這裏使用Java servlet來捕獲IPN通知。收到大量參數後,回覆狀態200 OK。然後,形成一個新的HTTP請求,保持第一步收到的所有參數,並添加一個額外的命令'cmd' - 事情看起來非常類似於您的php代碼。

正如我所看到的,fsockopen試圖連接到'tls://www.sandbox.paypal.com',而我正在使用'https://www.sandbox.paypal.com/cgi-bin/webscr'。你可以按照這個教程,這是很有幫助http://www.geekality.net/2011/05/28/php-tutorial-paypal-instant-payment-notification-ipn

VERIFIED最終收到。有一件事要注意,由於https,有一個證書過期警告,我必須禁用Apache HTTP客戶端中的忽略cookie。

希望這篇文章能夠減輕你的一些痛苦。