Php IPN验证回发到HTTPS
我的PHP代码目前使用以下内容回发到PayPal。如何更新此内容以满足即将生效的新支付安全标准Php IPN验证回发到HTTPS,php,paypal,Php,Paypal,我的PHP代码目前使用以下内容回发到PayPal。如何更新此内容以满足即将生效的新支付安全标准 $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; } // post back to PayPal system to validate $header = "
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
if (!$fp)
{
//an error occurred...
}
else
{
fputs ($fp, $header . $req);
while (!feof($fp))
{
$res = fgets ($fp, 1024);
if(strcmp ($res, "VERIFIED") == 0)
{
//all is well...
}
}
}
要启用IPN验证回发到HTTPS,请按如下所示更改代码:
$fp = fsockopen( 'tls://ipnpb.paypal.com', 443, $errno, $errstr, 30);
PayPal建议在生产环境中使用ipnpb.PayPal.com端点
此外,您需要将HTTP/1.0更改为HTTP/1.1,以符合PayPal升级的要求
您需要提交的标题
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen( $req ) . "\r\n";
$header .= "Connection: close\r\n\r\n";
对于沙箱更改
$header .= "Host: www.paypal.com\r\n";
到
有几件事需要做:
- 您仍然使用HTTP/1.0;您需要切换到HTTP/1.1
- 您需要切换到使用TLS
$curl = curl_init( 'https://ipnpb.paypal.com/cgi-bin/webscr' );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $req );
$response = curl_exec( $curl );
if( 'VERIFIED' == trim( $response ) ) {
// IPN verified by PayPal
}
上面建议的新代码现在有机会进行测试 这不起作用:
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen( 'tls://ipnpb.paypal.com', 443, $errno, $errstr, 30);
这就是工作原理:
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
如何使用HTTP/1.0并发布到www.paypal.com,而不是使用HTTP/1.1并发布到tls://ipnpb.paypal.com?我对Paypal IPN验证和使用此代码也有类似的问题 //发回贝宝系统进行验证
//发回PayPal系统进行验证
$header.=“POST/cgi-bin/webscr-HTTP/1.0\r\n”;
$header.=“内容类型:application/x-www-form-urlencoded\r\n”;
$header.=“内容长度:”。斯特伦($req)。“\r\n\r\n”;
$fp=fsockopen('www.paypal.com',80,$errno,$errstr,30)代码>我发现这种方法有几个问题:1)它不能解决他仍然使用HTTP/1.0的事实(请参阅);2)PHP文档明确表示“ssl://将根据远程主机的功能和首选项尝试协商ssl V2或ssl V3连接。”()它没有提到任何关于TLS的内容,因此我不信任协商TLS连接<代码>tls://
是一个更安全的选择。是的,我同意马特的观点。谢谢您可以将其用作$fp=fsockopen('tls://ipnpb.paypal.com“,443,$errno,$errstr,30);请参阅我忘记添加我的原始代码的其余部分。我现在编辑了这个问题,将其包括在内。因此,我仍然需要将$fp赋值更改为:$fp=fsockopen($fp)tls://ipnpb.paypal.com“,443,$errno,$errstr,30);,对的这是比使用旋度更好的解决方案,正如第一次建议的那样?正确。此外,还需要更改$header=“POST/cgi-bin/webscr-HTTP/1.0\r\n”;至$header=“POST/cgi-bin/webscr-HTTP/1.1\r\n”;如前所述,遵守PayPal升级。请参阅我在上面的新帖子:“上面建议的新代码现在有机会进行测试。”如何使用HTTP/1.0并发布到www.PayPal.com,而不是使用HTTP/1.1并发布到www.PayPal.comtls://ipnpb.paypal.com? 来自Paypal Github depository。谢谢@Martin。您需要添加$header.=“主机:ipnpb.Paypal.com\r\n”
和$header.=“连接:关闭\r\n”
在您的$header=“POST/cgi-bin/webscr HTTP/1.1\r\n”之后代码>行。已进行建议的更改。我现在得到:HTTP/1.1 400坏请求您是否有一个pastebin链接,我可以在其中看到触发“坏请求”响应的完整代码?取出第11行的额外\r\n
。额外的\r\n已被删除,因此它现在是:$header.=“Content Length:”。斯特伦($req)。“\r\n”;
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);