0
我想捕獲從PayPal發回IPN腳本的數據。在我的按鈕中,我確實設置了notify_url以及正確的值爲2(用於發佈數據)。我正在使用_xclick命令(cmd = _xclick)以及按鈕。如何正確捕獲PaPal IPN數據
在我的下面的代碼中,我定期寫入數據或流到錯誤日誌來捕捉正在發生的事情。但似乎PayPal正在推動IPN,但沒有數據被捕獲(或至少是正確的)。
這裏是我的整個IPN腳本的時刻:
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$raw_post_data = file_get_contents('php://input');
error_log("Got " . $raw_post_data . " when processing IPN data");
error_log("-------------------\n");
$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 IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$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";
}
error_log("Got response: " . $req . " when processing IPN data");
error_log("-------------------\n");
// STEP 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
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);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
error_log("Got response: " . $res . " when processing IPN VALIDATE");
error_log("-------------------\n");
if(!($res)) {
error_log("Got " . curl_error($ch) . " when processing IPN Validation");
curl_close($ch);
exit;
}
curl_close($ch);
error_log("Got response: " . $res . " when processing IPN data");
error_log("-------------------\n");
// STEP 3: Inspect IPN validation result and act accordingly
if (strcmp (trim($res), "VERIFIED") == 0) {
// The IPN is verified, process it:
// check whether the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process the notification
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
// IPN message values depend upon the type of notification sent.
// To loop through the &_POST array and print the NV pairs to the screen:
foreach($_POST as $key => $value) {
//echo $key." = ". $value."<br>";
error_log($key." = ". $value."\n");
}
} else if (strcmp ($res, "INVALID") == 0) {
// IPN invalid, log for manual investigation
$t = "The response from IPN was: <b>" .$res ."</b>";
error_log($t."\n");
}
結果捕捉到的數據是,IPN無效響應。
我不知道我在這裏錯過了什麼。看起來像file_get_contents('php:// input')根本沒有捕獲任何數據。有沒有更好的方法來捕獲輸入數據,而不是使用file_get_contents('php:// input')或使用$ _POST變量?
感謝任何其他見解。謝謝。