Php Can';我不了解使用贝宝的正确方法;s IPN

Php Can';我不了解使用贝宝的正确方法;s IPN,php,paypal,paypal-ipn,Php,Paypal,Paypal Ipn,正如标题所说,我不明白如何使用IPN 本页 说我需要实现我的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 use

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

本页 说我需要实现我的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;
        }
    }
}
?>
1)您显示的代码正在定义一个类,因此您可以启动一个连接实例,在其中定义不同的变量。所以你不应该改变这一点。你应该把它上传到你的服务器上,并相应地使用它

2)Ehm。。。这是一个广泛的问题。喜欢连锁邮件安全吗?对用剑攻击你的人来说是安全的:是的。对向你投掷核弹的人来说是安全的:不。这还取决于你如何使用连锁邮件

但如果我试着回答我认为你在问的问题:

脚本可以安全连接到PayPal,是的。因此,您需要确保,在您正在制作的解决方案中,脚本和API密钥没有被盗/滥用;所以人们可以通过你的解决方案访问PayPal,做你不想让他们做的事情

3)根据,然后:

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

。。。我必须承认我以前没用过这个。但这似乎是一种在您的网站和Paypal之间传递通知的方式。所以我假设这是一条路径,Paypal检查以获取通知,然后显示,嗯。。。某处(?)。很抱歉在这一点上的回答含糊不清

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

然而,这一切都取决于你想做什么。因此,TL;DR版本第四点:使用框架

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

2)Ehm。。。这是一个广泛的问题。喜欢连锁邮件安全吗?对用剑攻击你的人来说是安全的:是的。对向你投掷核弹的人来说是安全的:不。这还取决于你如何使用连锁邮件

但如果我试着回答我认为你在问的问题:

脚本可以安全连接到PayPal,是的。因此,您需要确保,在您正在制作的解决方案中,脚本和API密钥没有被盗/滥用;所以人们可以通过你的解决方案访问PayPal,做你不想让他们做的事情

3)根据,然后:

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

。。。我必须承认我以前没用过这个。但这似乎是一种在您的网站和Paypal之间传递通知的方式。所以我假设这是一条路径,Paypal检查以获取通知,然后显示,嗯。。。某处(?)。很抱歉在这一点上的回答含糊不清

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


然而,这一切都取决于你想做什么。因此,TL;DR版本第四点:使用框架

谢谢你的回答,这对我帮助很大。关于第二点你是对的,它太宽了。我的意思是如果我需要验证数据或其他什么。但是我认为答案很明显:D.谢谢你的帮助谢谢你的回答,它帮了我很多。关于第二点你是对的,它太宽了。我的意思是如果我需要验证数据或其他什么。但我认为答案很明显:D.谢谢你的帮助
<?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");
?>