在 PHP 中使用 IPN 的 Paypal 无法在新的 Beta 开发人员沙箱中运行
Posted
技术标签:
【中文标题】在 PHP 中使用 IPN 的 Paypal 无法在新的 Beta 开发人员沙箱中运行【英文标题】:Paypal with IPN in PHP not working in new Beta developer sandbox 【发布时间】:2013-03-08 03:43:39 【问题描述】:我已经在 php 中将 PayPal 与 IPN 集成,在新的 Paypal 开发之前它重定向到沙盒并使用沙盒成功登录,它允许测试支付,现在它重定向到 beta 开发者网站并且不测试支付,
以下是我使用的 PHP Paypal 类,
class paypal_class
var $error; // holds the error encountered
var $ipn_log; // log IPN results
var $ipn_log_file; // filename of the IPN log
var $ipn_response; // holds the IPN response from PayPal
var $ipn_data = array(); // contains the POST values for IPN
var $fields = array(); // holds the fields to submit to PayPal
function paypal_class()
// constructor.
$this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
$this->error = '';
$this->ipn_log_file = '.ipn_results.log';
$this->ipn_log = true;
$this->ipn_response = '';
$this->add_field('rm','2'); // Return method = POST
$this->add_field('cmd','_xclick');
function add_field($field, $value)
// adds a key=>value pair to the fields array
$this->fields["$field"] = $value;
function submit_paypal_post()
// generates an html page consisting of
// a form with hidden elements which is submitted to PayPal
echo "<html>\n";
echo "<head><title>Processing...</title></head>\n";
echo "<body onLoad=\"document.forms['paypal_form'].submit();\">\n";
echo "<center><h2>Please wait, your order is being processed and you";
echo " will be redirected to the paypal website.....</h2></center>\n";
echo "<form method=\"post\" name=\"paypal_form\" ";
echo "action=\"".$this->paypal_url."\">\n";
foreach ($this->fields as $name => $value)
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>\n";
echo "<center><br/><br/>If you are not automatically redirected to ";
echo "paypal within 5 seconds...<br/><br/>\n";
echo "<input type=\"submit\" value=\"Click Here\"></center>\n";
echo "</form>\n";
echo "</body></html>\n";
function validate_ipn()
// parse the paypal URL
$url_parsed=parse_url($this->paypal_url);
// generate the post string from the _POST vars
$post_string = '';
foreach ($_POST as $field=>$value)
$this->ipn_data["$field"] = $value;
$post_string .= $field.'='.urlencode (stripslashes ($value)).'&';
$post_string .= "cmd=_notify-validate";
// open the connection to paypal
$fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,30);
if(!$fp)
// Print the error if not able to open the connection.
$this->error = "Error no. $errnum: $errstr";
$this->log_ipn_results(false);
return false;
else
// Post data back to paypal
fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
fputs($fp, "Host: $url_parsed[host]\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $post_string . "\r\n\r\n");
// loop through the response from the server and append to variable
while(!feof($fp))
$this->ipn_response .= fgets($fp, 1024);
fclose($fp); // close connection
if (eregi("VERIFIED",$this->ipn_response))
// Valid IPN.
$this->log_ipn_results(true);
return true;
else
// Invalid IPN.
$this->error = 'IPN Validation Failed.';
$this->log_ipn_results(false);
return false;
function log_ipn_results($success)
if (!$this->ipn_log) return;
// Timestamp
$text = '['.date('m/d/Y g:i A').'] - ';
// Success or failure
if ($success) $text .= "SUCCESS!\n";
else $text .= 'FAIL: '.$this->error."\n";
// Log the POST variables
$text .= "IPN POST Values from Paypal:\n";
foreach ($this->ipn_data as $key=>$value)
$text .= "$key=$value, ";
// response from the paypal server
$text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response;
// Write to log
$fp=fopen($this->ipn_log_file,'a');
fwrite($fp, $text . "\n\n");
fclose($fp); // close file
提前致谢,
【问题讨论】:
在打开浏览器并访问 developer.paypal.com 之前尝试清除 cookie 和缓存 【参考方案1】:Paypal 进行了一些更改。现在在https://developer.paypal.com/ 上创建一个帐户,我使用与沙盒相同的电子邮件 ID。注册过程后登录https://developer.paypal.com/ 单击应用程序 单击沙盒帐户 要导入您以前的沙箱数据,请单击“导入数据”链接或单击“创建帐户”按钮创建一个新的测试帐户
Paypal 在注册时创建一个默认商家帐户。 要查看任何沙盒帐户,只需单击它。会有新的选择。浏览这些功能。
在这里您可以再次使用您的沙盒详细信息
【讨论】:
以上是关于在 PHP 中使用 IPN 的 Paypal 无法在新的 Beta 开发人员沙箱中运行的主要内容,如果未能解决你的问题,请参考以下文章
无法接收 PayPal IPN - PayPal 总是收到 403 错误
PayPal IPN HTTP 错误 400 - PHP - 使用 PayPal GIT 代码