php - 使用 HTTPS 的 Paypal IPN 验证回传

标签 php paypal paypal-ipn

根据新的安全要求(2016、2017 和 2018),在“IPN”期间,服务器和 Paypal 之间的交换似乎需要 HTTPS。 This question is linked to this subject还有this .

我们应该如何修改这个 PHP IPN 代码?

$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.paypal.com:80\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);

$req = 'cmd=_notify-validate';

...

fputs ($fp, $header . $req);

将两次出现的 www.paypal.com 替换为 https://www.paypal.com 就足够了吗?/p>

还有,我的店铺网站不是HTTPS的问题,这个连接会被拒绝吗?


以下是从 Paypal 收到的部分电子邮件:

enter image description here


编辑(2018/06/22),这是应用接受的答案代码后的实际 IPN 代码。奇怪的是,我仍然得到:“IPN 验证回发到 HTTPS。需要更新:是”。所以这意味着下面的代码仍然不是 100% 兼容 HTTPS。为什么?

<?php
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
  $value = trim(urlencode(stripslashes($value)));
  $req .= "&$key=$value";
}

$header .= "POST /cgi-bin/webscr HTTP/1.0\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\r\n";
$fp = fsockopen('tls://www.paypal.com', 443, $errno, $errstr, 30);

// variables
$item_name = $_POST['item_name'];
$business = $_POST['business'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
// and many more

if (!$fp)
{
  // HTTP ERROR
} else
{
  fputs ($fp, $header . $req);
  while (!feof($fp))
  {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0)
    {
        // send email to customer, etc.
    }
  }
  fclose ($fp);
}
?>

最佳答案

hostname

If OpenSSL support is installed, you may prefix the hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.

http://www.php.net/fsockopen

端口也需要更改为 443。所以:

$header .= "Host: www.paypal.com\r\n";
...
$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
...
fputs ($fp, $header . $req);

https:// 将不起作用,因为您正在打开一个 socket,这是一种低级传输。 HTTP 是其之上的应用程序级协议(protocol),套接字不知道或不关心它。在套接字级别,它是一个 TLS 连接。

Also, is the fact my shop website is not HTTPS a problem, will this connection be refused?

浏览器与您的服务器有什么样的连接是无关紧要的,没有人知道。您正在打开一个从 PHP 程序到 Paypal 的套接字,您也可以直接从命令行执行此操作,而根本不涉及任何“HTTP”连接。

关于php - 使用 HTTPS 的 Paypal IPN 验证回传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465835/

相关文章:

javascript - 根据用户账户状态弹出

paypal - 如何创建 Payflow 链接沙盒帐户

paypal - Paypal 沙箱 merchantpaymentweb : no style and JS errors 问题

asp.net - Paypal IPN 中缺少自定义值

paypal - 使用 PayPal 的延迟 IPN(在沙盒中和真实付款)

paypal - 待处理的 Paypal IPN

php - 尽可能容易地随机化 mysql 输出

php - 按时执行的自执行脚本

PHP max_input_vars

PHP创建多个目录