[英]Problems with PayPal IPN variables
我使用PayPal作為主要付款系統,因此我決定將其添加到自定義的內置腳本中,但是IPN變量存在問題,因為例如當我嘗試回顯任何這些變量時,
$item_number
$txn_id
我沒有從上述變量中得到任何東西。 一切都正確地傳遞給PayPal並已收到付款,但是當用戶重定向到我的“謝謝”頁面時,我需要獲取該$ item_number項編號才能更新數據庫,但不傳遞任何內容。 我目前正在使用沙盒進行測試。
這是重定向到我的網站時的url值:
http://example.com/thankyou.php?tx=8LT93279034418017&st=Completed&amt=20%2e00&cc=USD&cm=&item_number=29
這是我的IPN代碼:
<?php
// STEP 1: Read POST data
// reading posted data from 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";
}
// STEP 2: Post IPN data back to paypal to validate
$ch = curl_init('https://www.sandbox.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'));
// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);
// STEP 3: Inspect IPN validation result and act accordingly
if (strcmp ($res, "VERIFIED") == 0) {
// 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 payment
// assign posted variables to local variables
$custom_value = $_POST['custom'];
$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'];
} else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
?>
非常感謝您的幫助!
您會混淆PDT和IPN。 PDT會將數據發送回您的返回URL。 IPN與您的結帳流程完全獨立,不會在屏幕上看到任何人,因此任何人都不會看到回顯數據。
IPN確實是處理數據庫更新,電子郵件通知等的推薦方法,因為即使啟用了自動返回功能,也無法保證用戶將其返回到ReturnURL(使用Payments Standard時),在這種情況下,代碼將永遠不會運行對於那些訂單。
看來您剛剛從PayPal提取了IPN示例,因此您需要對其進行自定義以適合您的需求,並且再次提醒您,屏幕上不會顯示任何內容。 您需要檢查PayPal的IPN歷史記錄和您的Web服務器日志,以查看是否受到攻擊以及是否可能發生任何錯誤。
當然,您還需要確保在PayPal個人資料中也配置了IPN,或者在付款代碼中設置了notify_url。
因此,您的“謝謝”頁面實際上僅此而已,然后IPN會處理更新數據庫並通知用戶他們需要知道的任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.