我正在尝试捕获从PayPal回发到IPN脚本的数据。在我的按钮中,我确实设置了notify_url以及rm值为2(用于发回数据)。我也在按钮中使用_xclick命令(cmd = _xclick)。
在我的下面的代码中,我定期写入数据或流到错误日志以捕获正在发生的事情。但似乎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以INVALID响应。
我不确定我在这里缺少什么。似乎file_get_contents(&#39; php://输入&#39;)根本没有捕获任何数据。是否有更好的方法来捕获输入数据而不是使用file_get_contents(&#39; php:// input&#39;)或使用$ _POST变量?
感谢任何其他见解。谢谢。
答案 0 :(得分:0)
尝试通过以下代码更改步骤1:
// Prepare data that will sent to Paypal for verification
$req = "cmd=_notify-validate";
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
$content = "&$key=$value";
}
注意:您可以使用sandbox website for developer的paypal来测试您的ipn列表器 那你应该换行
$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
通过
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
希望这会是肝脏,