IPN Paypal“官方”示例代码不起作用

2023-12-26

我有一个问题,我想他们还发现了很多其他问题。我正在尝试将 PayPal 支付系统集成到我的网站中,但 IPN 存在一些问题。我尝试了这段代码,在 github Paypal 上找到:

    <?php require('PaypalIPN.php');
use PaypalIPN;
$ipn = new PayPalIPN();
// Use the sandbox endpoint during testing.
$ipn->useSandbox();
$verified = $ipn->verifyIPN();
if ($verified) {

}
// Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
header("HTTP/1.1 200 OK");
?>

所需班级:

<?php
class PaypalIPN
{
    private $use_sandbox = false;
    private $use_local_certs = true;
    /*
     * PayPal IPN postback endpoints
     */
    const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr';
    const SANDBOX_VERIFY_URI = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr';
    /*
     * Possible responses from PayPal after the request is issued.
     */
    const VALID = 'VERIFIED';
    const INVALID = 'INVALID';
    /**
     * Sets the IPN verification to sandbox mode (for use when testing,
     * should not be enabled in production).
     * @return void
     */
    public function useSandbox()
    {
        $this->use_sandbox = true;
    }
    /**
     * Determine endpoint to post the verification data to.
     * @return string
     */
    public function getPaypalUri()
    {
        if ($this->use_sandbox) {
            return self::SANDBOX_VERIFY_URI;
        } else {
            return self::VERIFY_URI;
        }
    }
    /**
     * Verification Function
     * Sends the incoming post data back to paypal using the cURL library.
     *
     * @return bool
     * @throws Exception
     */
    public function verifyIPN()
    {
        if ( ! count($_POST)) {
            throw new Exception("Missing POST Data");
        }
        $raw_post_data = file_get_contents('php://input');
        $raw_post_array = explode('&', $raw_post_data);
        $myPost = [];
        foreach ($raw_post_array as $keyval) {
            $keyval = explode('=', $keyval);
            if (count($keyval) == 2) {
                // Since we do not want the plus in the datetime string to be encoded to a space, we manually encode it.
                if ($keyval[0] === 'payment_date') {
                    if (substr_count($keyval[1], '+') === 1) {
                        $keyval[1] = str_replace('+', '%2B', $keyval[1]);
                    }
                }
                $myPost[$keyval[0]] = urldecode($keyval[1]);
            }
        }
        // Build the body of the verification post request, adding the _notify-validate command.
        $req = 'cmd=_notify-validate';
        $get_magic_quotes_exists = false;
        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";
        }
        // Post the data back to paypal, using curl. Throw exceptions if errors occur.
        $ch = curl_init($this->getPaypalUri());
        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_SSLVERSION, 6);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        // This is often required if the server is missing a global cert bundle, or is using an outdated one.
        if ($this->use_local_certs) {
            curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/cert/cacert.pem");
        }
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Connection: Close']);
        $res = curl_exec($ch);
        $info = curl_getinfo($ch);
        $http_code = $info['http_code'];
        if ($http_code != 200) {
            throw new Exception("PayPal responded with http code $http_code");
        }
        if ( ! ($res)) {
            $errno = curl_errno($ch);
            $errstr = curl_error($ch);
            curl_close($ch);
            throw new Exception("cURL error: [$errno] $errstr");
        }
        curl_close($ch);
        // Check if paypal verfifes the IPN data, and if so, return true.
        if ($res == self::VALID) {
            return true;
        } else {
            return false;
        }
    }
}

当我使用 IPN Simulator 进行测试时,得到以下响应:IPN 未发送,握手未验证。请检查您的信息。有人能帮我吗?


从您的示例代码:

   <?php require('PaypalIPN.php');
^^^  /** This will cause your script to fail.**/

你应该有NOpaypal IPN 接受页面上 PHP 周围的空白。


如果您还没有安装他们的cacert.pem文件,那么您需要调整类设置,以便 paypal 类 cURL 不会尝试使用该 pem 文件:

 private $use_local_certs = false; // set to true when you have the 
                                   // file in your server filesystem

在IPN模拟器上你需要选择网络接受作为要执行的模拟类型。


是否required 文件存在吗?根据您的代码,该文件应与您的 IPN 侦听器文件位于同一文件夹中。是这样吗?如果找不到该文件,脚本将失败。


如果这些详细信息可以解决问题,或者您是否有更多详细信息需要添加,请告诉我们。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IPN Paypal“官方”示例代码不起作用 的相关文章

随机推荐

  • Facebook API 错误子代码 33

    我有一个应用程序可以获取leads当潜在客户填写表单时 在 webhook 请求后来自 facebook 某些页面抛出此错误 error message Unsupported get request Object with ID 2333
  • 通过react获取cookie

    我需要知道我的用户是否已连接 为此 我想读取我在服务器端使用express session设置的cookie app use session secret crypted key resave false saveUninitialized
  • 为什么用分号连接多个 MySQL 查询不能与 Perl DBI 一起使用?

    我想使用将值插入到两个单独的 MySQL 表中DBI https metacpan org pod DBI 我尝试将两个工作结合起来INSERT通过插入一个来查询 它们之间 dbh gt do q INSERT INTO testA tes
  • 在 docker 中运行 npm update,而不使用该特定更新的缓存

    背景 我正在编写代码node js using npm and docker 我试图让我的 docker 文件在构建时使用缓存 这样就不会花费太长时间 我们有一个 通用 存储库 用于保存在各种存储库中使用的逻辑 并且通过 npm 包进行传播
  • 使用 OpenMP 并行化 while 循环

    我有一个非常大的数据文件 这个数据文件中的每条记录有4行 我编写了一个非常简单的 C 程序来分析这种类型的文件并打印出一些有用的信息 该程序的基本思想是这样的 int main char buffer BUFFER SIZE while f
  • JPA onetoMany/ManytoOne 持续存在 - 违反完整性约束 - 找不到父键

    我的映射文件 相关数据 Parent Entity Table name ATTRIBUTE NAME uniqueConstraints UniqueConstraint columnNames NAME TEXT SequenceGen
  • 使用 Laravel Passport oauth/token 发送更多数据

    所以 我正在使用 Laravel Passport 到目前为止一切正常 但是 我想对护照代码进行一些小的更改 好吧 我希望不在供应商文件夹中 一旦我会要求用户更改其密码 以防他正在进行第一次登录 所以 我需要的是两件事 我相信 1 如何在
  • mysql 查询优化

    我的 x 表中有大约 总共 1 049 906 个 查询花费了 0 0005 秒 如果我只是检索尝试检索特定字段记录 花了不到6分钟 这是我的查询 SELECT CUSTOMER CODE FROM X TBL 客户代码 gt 唯一 上述查
  • 如何配置 Express 响应对象以自动向 JSON 添加属性?

    我有一个对象 var obj stuff stuff 在 Express 中 我将其发送给客户端 如下所示 res json obj 有没有办法配置响应对象自动将属性添加到它生成的 json 中 例如 输出 status ok data s
  • PHP 无需 cURL 即可获取 http 标头响应代码

    我编写了一个类来检测 cURL 是否可用 如果可用 则使用 cURL 执行 GET POST DELETE 在我使用的 cURL 版本中curl getinfo curl CURLINFO HTTP CODE 获取 HTTP 代码 如果 c
  • 监控传出互联网流量

    有没有办法以编程方式监控互联网流量 我想记录用户在互联网上访问的页面 这可以通过 NET 代码实现吗 是否有可用于检索数据的第 3 方 NET 组件 有关互联网流量的信息必须存储到数据库中 因此我无法使用 IE 的插件或其他东西 我们还希望
  • Python 3:从元组列表中删除空元组

    我有一个元组列表 内容如下 gt gt gt myList c e ca ea d do dog ear eat cat car dogs cars done eats cats ears don 我希望它是这样读的 gt gt gt my
  • 标准输出未正确传递?

    特殊的问题 由于某种原因 标准输出中的值无法被正确识别 我想做的是 grep 正在侦听的端口的值并尝试匹配 如果定义的端口存在 即 被监听产生一条消息 如果不存在 则产生另一条消息 name check prometheus status
  • 搜索并替换为 ruby​​ 正则表达式

    我的 MySQL 列中有一个包含 HTML 的文本 blob 字段 我必须更改一些标记 所以我想我会在 ruby 脚本中完成它 Ruby 在这里无关紧要 但很高兴看到它的答案 标记如下所示 h5 foo h5 table tbody tbo
  • 在 C++ 中忽略 std::cin 上的 EOF

    我有一个实现交互式 shell 的应用程序 类似于 Python 控制台 irb 的工作方式 现在的问题是 如果用户不小心点击了 DEOF 已发出 我的getline 调用返回一个空字符串 我将其视为 无输入 并再次显示提示 这会导致打印提
  • 从 Google Scholar 搜索结果中抓取和解析引文信息

    我有大约 20000 篇文章标题的列表 我想scrape他们来自谷歌学术的引用计数 我是 BeautifulSoup 库的新手 我有这个代码 import requests from bs4 import BeautifulSoup que
  • 接口中的只读和只写自动属性

    我读过自动实现的属性不能只读或只写 它们只能读写 然而 在学习界面时我遇到了foll 代码 它创建只读 只写和读写类型的自动属性 这是可以接受的吗 public interface IPointy A read write property
  • img = Image.open(fp) AttributeError: 类 Image 没有属性 'open'

    我想把图片做成PDF文件 我的代码如下 import sys import xlrd from PIL import Image import ImageEnhance from reportlab platypus import from
  • 如何确定 VS Code 使用的是哪个格式化程序?

    如果您安装了多个扩展 如何确定文档上正在运行哪个格式化程序 例如 我有几个可以格式化 HTML 的 HTML 扩展 但我不确定哪个扩展实际上正在格式化 或者是否有多个扩展 我什至不确定哪些扩展可能有助于诚实地格式化 最近 HTML 和 CS
  • IPN Paypal“官方”示例代码不起作用

    我有一个问题 我想他们还发现了很多其他问题 我正在尝试将 PayPal 支付系统集成到我的网站中 但 IPN 存在一些问题 我尝试了这段代码 在 github Paypal 上找到