PayPal IPN 模拟器总是在 PHP 中给出无效响应
Posted
技术标签:
【中文标题】PayPal IPN 模拟器总是在 PHP 中给出无效响应【英文标题】:PayPal IPN simulator always giving INVALID response in PHP 【发布时间】:2016-02-04 18:47:14 【问题描述】:我正在模拟沙盒购买,但我不断收到无效响应。
我在这里直接从 PayPal 获取了 IPN php 代码:https://github.com/paypal/ipn-code-samples
我已将 CURLOPT_SSL_VERIFYPEER 和 CURLOPT_SSL_VERIFYHOST 更改为 0,因为我没有运行 SSL 测试服务器,但即使像默认值一样将它们保留在 1 和 2 上,也不会改变任何内容:
<?php
define("DEBUG", 1);
define("USE_SANDBOX", 1);
define("LOG_FILE", "./ipn.log");
$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]);
$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";
if(USE_SANDBOX == true)
$paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
else
$paypal_url = "https://www.paypal.com/cgi-bin/webscr";
$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, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(DEBUG == true)
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$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
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);
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp ($res, "VERIFIED") == 0)
if(DEBUG == true)
error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
else if (strcmp ($res, "INVALID") == 0)
if(DEBUG == true)
error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
?>
日志文件中的结果:
INVALID
[2015-11-04 02:05 America/Los_Angeles] Invalid IPN: cmd=_notify-validate&payment_type=instant&payment_date=Wed+Nov+04+2015+10%3A42%3A20+GMT%2B0100+%28CET%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123+any+street&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=seller%40paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross1=12.34&txn_type=web_accept&txn_id=818471070¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCps-s-rl31A0KpwJ1YTxR%2FfcX8%2BmJdV9DGt4mO
【问题讨论】:
【参考方案1】:试试这个方法:
私有函数 PPHttpPost( $methodName_, $nvpStr_ ) $api_username = 'yourpaypal@email.com'; $api_password = 'QWEQWEWQEQWEQEQWE';$api_signature = 'WQEQWEQWEQWEWQEQWEQWEQWEQWEQWE.cT';$api_endpoint = "https://api-3t.paypal.com/nvp";$version = '124.0' ; $ch = curl_init();curl_setopt($ch, CURLOPT_URL, $api_endpoint);curl_setopt($ch, CURLOPT_VERBOSE, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$api_password&USER=$api_username&SIGNATURE=$api_signature&$nvpStr_"; curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);$httpResponse = curl_exec($ch);if(!$httpResponse) exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');// 提取响应详细信息。 $httpResponseAr = explode("&", $httpResponse); $httpParsedResponseAr = array();foreach ($httpResponseAr as $i => $value) $tmpAr = explode("=", $value); if(sizeof($ tmpAr) > 1) $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1]; if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) exit("对 $api_endpoint 的 POST 请求 ($nvpreq) 的 HTTP 响应无效。"); 返回 $httpParsedResponseAr;【讨论】:
感谢您的建议!这么晚才回复很抱歉!我已经尝试过了,但它不起作用,我没有用这种方法得到任何响应。在我的一切工作除了“无效”部分以上是关于PayPal IPN 模拟器总是在 PHP 中给出无效响应的主要内容,如果未能解决你的问题,请参考以下文章
Paypal SandBox IPN 总是返回 INVALID
无法接收 PayPal IPN - PayPal 总是收到 403 错误