
本文旨在解决在使用 PHP 验证 PayPal IPN (Instant Payment Notification) 时遇到的 “Access Denied” 错误。通过修改 cURL 请求头中的 User-Agent 字段,可以有效避免此问题,确保 IPN 验证流程顺利进行。
在使用 PHP 处理 PayPal IPN 时,有时会遇到 “Access Denied” 错误,导致无法成功验证 IPN 数据。这通常是由于 PayPal 服务器对请求头中的 User-Agent 字段有要求,而默认情况下 PHP 的 cURL 请求可能没有设置此字段,或者使用了不被接受的 User-Agent。
以下是如何解决此问题的步骤:
修改 cURL 请求头
立即学习“PHP免费学习笔记(深入)”;
在你的 PHP IPN 监听脚本中,找到使用 cURL 发送请求的部分。你需要修改 CURLOPT_HTTPHEADER 选项,添加或修改 User-Agent 字段。
原始代码:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
登录后复制
修改后的代码:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: your-company-name'));
登录后复制
将 your-company-name 替换为你公司的名称或其他合适的标识符。这可以帮助 PayPal 识别你的请求,并避免 “Access Denied” 错误。
完整示例代码
下面是一个包含此修改的完整示例代码片段:
$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";
}
// Step 2: POST IPN data back to PayPal to validate
//Operacion: https://ipnpb.paypal.com/cgi-bin/webscr
//Sandbox: https://ipnpb.sandbox.paypal.com/cgi-bin/webscr
$ch = curl_init('https://ipnpb.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', 'User-Agent: your-company-name')); // 添加 User-Agent
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "https://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);
// inspect IPN validation result and act accordingly
if (strcmp ($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'];
$header = "From: <a class="__cf_email__" data-cfemail="137a7d757c537e7c7d76616a3d707c7e" href="/cdn-cgi/l/email-protection">[email protected]</a>/nReply-To:".$payer_email."/n";
$mensaje = "Se recibio " . $item_name . ", status fue: " . $payment_status . ", el recibidor fue: " . $receiver_email . ", el pagador fue: " . $payer_email . ". Se cobro $" . $payment_amount;
mail('<a class="__cf_email__" data-cfemail="e18c8e8f849398d3d1a1868c80888dcf828e8c" href="/cdn-cgi/l/email-protection">[email protected]</a>',$item_name, $mensaje, $header);
// 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>";
}
} else{
// IPN invalid, log for manual investigation
$payer = '<a class="__cf_email__" data-cfemail="e78a888982959ed5d7a7808a868e8bc984888a" href="/cdn-cgi/l/email-protection">[email protected]</a>';
$item_name = 'Ha fallado';
echo "The response from IPN was: <b>" .$res ."</b>";
$header = "From: <a class="__cf_email__" data-cfemail="e0898e868fa08d8f8e859299ce838f8d" href="/cdn-cgi/l/email-protection">[email protected]</a>/nReply-To:".$payer."/n";
$mensaje = "Fue invalido, recibiste " . $res;
mail('<a class="__cf_email__" data-cfemail="cba6a4a5aeb9b2f9fb8baca6aaa2a7e5a8a4a6" href="/cdn-cgi/l/email-protection">[email protected]</a>',$item_name, $mensaje, $header);
}
登录后复制
注意事项
- User-Agent 的选择: 选择一个能够代表你的应用程序或公司的 User-Agent。避免使用通用的 User-Agent 字符串,因为它们可能会被 PayPal 屏蔽。
- Sandbox 环境: 在开发和测试阶段,使用 PayPal 的 Sandbox 环境进行测试,以避免对真实交易产生影响。
- SSL 证书验证: 确保你的服务器配置了正确的 SSL 证书,以便与 PayPal 服务器建立安全的连接。 CURLOPT_SSL_VERIFYPEER 和 CURLOPT_SSL_VERIFYHOST 选项应该根据你的环境进行适当的设置。如果遇到 SSL 证书问题,可以尝试下载 cacert.pem 文件并设置 CURLOPT_CAINFO 选项。
- 错误日志: 在代码中添加适当的错误日志记录,以便在出现问题时能够快速定位和解决。
总结
通过修改 cURL 请求头中的 User-Agent 字段,可以有效地解决 PayPal IPN 验证时遇到的 “Access Denied” 错误。确保你的代码遵循 PayPal 的最佳实践,并进行充分的测试,以确保 IPN 处理流程的稳定性和可靠性。
以上就是解决 PayPal IPN “Access Denied” 问题的 PHP 教程的详细内容,更多请关注php中文网其它相关文章!


