【问题标题】:Can't understand the proper to use Paypal's IPN无法理解正确使用 Paypal 的 IPN
【发布时间】:2019-06-20 06:13:30
【问题描述】:

正如标题所说,我不明白如何使用 IPN。

这个页面 https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNImplementation/ 说我需要实现我的 IPN 侦听器,他们提供了示例。 这是 PHP 的示例:

IPN.php

<?php
class PaypalIPN
{
    /** @var bool Indicates if the sandbox endpoint is used. */
    private $use_sandbox = false;
    /** @var bool Indicates if the local certificates are used. */
    private $use_local_certs = true;
    /** Production Postback URL */
    const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr';
    /** Sandbox Postback URL */
    const SANDBOX_VERIFY_URI = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr';
    /** Response from PayPal indicating validation was successful */
    const VALID = 'VERIFIED';
    /** Response from PayPal indicating validation failed */
    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;
    }
    /**
     * Sets curl to use php curl's built in certs (may be required in some
     * environments).
     * @return void
     */
    public function usePHPCerts()
    {
        $this->use_local_certs = false;
    }
    /**
     * 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 = array();
        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, array(
            'User-Agent: PHP-IPN-Verification-Script',
            'Connection: Close',
        ));
        $res = curl_exec($ch);
        if ( ! ($res)) {
            $errno = curl_errno($ch);
            $errstr = curl_error($ch);
            curl_close($ch);
            throw new Exception("cURL error: [$errno] $errstr");
        }
        $info = curl_getinfo($ch);
        $http_code = $info['http_code'];
        if ($http_code != 200) {
            throw new Exception("PayPal responded with http code $http_code");
        }
        curl_close($ch);
        // Check if PayPal verifies the IPN data, and if so, return true.
        if ($res == self::VALID) {
            return true;
        } else {
            return false;
        }
    }
}
?>

他们还提供了一个例子:

例子.php

<?php 
namespace Listener;
require('PaypalIPN.php');
use PaypalIPN;
$ipn = new PaypalIPN();
// Use the sandbox endpoint during testing.
$ipn->useSandbox();
$verified = $ipn->verifyIPN();
if ($verified) {
    /*
     * Process IPN
     * A list of variables is available here:
     * https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNandPDTVariables/
     */
}
// Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
header("HTTP/1.1 200 OK");
?>

我想要做什么:

我将在我的页面上设置 Paypal 按钮,让用户为基于订阅的软件付费(1/3/6/12 个月)。 (他们不订阅,他们只支付 1/3/6/12 个月所需的钱)。 我需要在收到付款后自动更新数据库上的订阅到期日期。

我的问题:

  1. 我不知道我需要编辑什么,IPN.php 是否应该按照提供的方式上传到服务器上?
  2. 它是否安全?
  3. 我应该将通知 URL(在 Paypal 的 IPN 设置上)指向 IPN.php 或我编辑的 Example.php 版本?
  4. 除此之外,还有什么建议吗?

我想澄清一下,我已经阅读了 paypal 指南上的内容,但我迷失了太多信息。

【问题讨论】:

    标签: php paypal paypal-ipn


    【解决方案1】:

    1) 您显示的代码是定义一个类,因此您可以启动一个连接实例,在其中定义不同的变量。所以你不应该改变它。您应该将其上传到您的服务器并相应地使用它。

    2) 嗯...这是一个广泛的问题。比如... 连锁邮件安全吗?对于用剑攻击你的人来说是安全的:是的。对于向您投掷核武器的人来说是安全的:不。这还取决于您如何使用锁子甲。

    但是,如果我应该尝试回答我认为您要问的问题:

    脚本与 PayPal 建立了安全连接,是的。因此,您需要确保在您正在制定的解决方案中,脚本和您的 API 密钥不会被盗/滥用;这样人们就可以通过您的解决方案访问 PayPal,做您不希望他们做的事情。

    3) 根据documentation that you linked to,则:

    完成侦听器后,将其推送到您的站点并在您的帐户设置中指定侦听器 URL 或通知 URL。请参阅IPN Setup 了解更多信息。

    ...我必须承认我以前没有使用过这个。但看起来 here 是一种在您的网站和 Paypal 之间传递通知的方式。所以我假设这是一条路径,Paypal 检查以获取通知,将显示,嗯......某处(?)。很抱歉在这一点上的模糊答案。

    4) 我的建议是,看看是否有已经集成的解决方案(框架或其他东西)。如果是我,我想我会使用 Laravel 或 WordPress,但这只是因为那是我有信心使用的工具。但是两者都带有解决方案(Laravel 和WordPress),这些解决方案由(希望)知道自己在做什么的人维护。因此,如果 PayPal 推出了一个新的 API,而弃用了您现在使用的 API,那么您不必重新开始查看文档。但是了解一个框架需要相当多的时间(只是提醒一下)。

    但是,- 这一切都取决于你想要做什么。所以第四点的 TL;DR 版本:使用框架。

    【讨论】:

    • 感谢您的回答,对我帮助很大。关于第 2 点,您是对的,它太宽泛了。我的意思是如果我需要验证数据或其他什么。但我认为答案很明显:D。感谢您的帮助
    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2014-05-30
    • 2012-01-23
    • 2015-06-28
    • 2013-06-19
    • 2017-02-09
    • 2012-09-21
    • 2023-03-11
    相关资源
    最近更新 更多