PayPal IPN:如何从此类中获取开机自检


Paypal IPN: how get the POSTs from this class?

我正在使用这个类

<?php
class paypalIPN {
    //sandbox:
    private $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    //live site:
    //private $paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
    private $data = null;
    public function __construct()
    {
        $this->data = new stdClass;
    }
    public function isa_dispute()
    {
        //is it some sort of dispute.
        return $this->data->txn_type == "new_case";
    }
    public function validate()
    {
        // parse the paypal URL
        $response = "";
        $url_parsed = parse_url($this->paypal_url); 
        // generate the post string from the _POST vars aswell as load the
        // _POST vars into an arry so we can play with them from the calling
        // script.
        $post_string    = '';    
        foreach ($_POST as $field=>$value) {        
            $this->data->$field = $value;
            $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 
        }
        $post_string.="cmd=_notify-validate"; // append ipn command
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->paypal_url);
        //curl_setopt($ch, CURLOPT_VERBOSE, 1);
        //keep the peer and server verification on, recommended 
        //(can switch off if getting errors, turn to false)
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
        $response = curl_exec($ch);
        if (curl_errno($ch))
        {
            die("Curl Error: " . curl_errno($ch) . ": " . curl_error($ch));
        } 
        curl_close($ch);
        return $response;
        if (preg_match("/VERIFIED/", $response))
        {  
            // Valid IPN transaction.
           return $this->data;
        }
        else
        {  
            return false;         
        }
    }
}

我记得在这种模式下:

 public function get_ipn()
{
    $ipn = new paypalIPN();
    $result = $ipn->validate();
    $logger = new Log('/error.log');
    $logger->write(print_r($result));
}

但我只获得"已验证"或"1"(使用print_r功能)。

我只是尝试直接返回原始卷曲响应

return $response;

return $this->response;

或者也

return $this->parse_string;

但每次我只收到"1"或"已验证"......

谢谢

PayPal IPN 通知作为发布的数据进入脚本。在该类中,您可以看到代码使用超全局$_POST来引用此传入数据。您可以直接使用已发布的数据,而不是使用该类。

危险在于它可能不是来自PayPal。

在这里展示的脚本正在做一个回传验证——也就是说,它把你认为PayPal发布给你的信息放回去;你问他们,"这是真的吗?"。PayPal正在验证,是的,这些信息来自他们。您在该代码中看到的$response变量仅包含来自PayPal的此确认。

在您的代码中,当您调用 $result = $ipn->validate(); 时,感兴趣的数据是由带有行return $this->data;的验证器返回的(相同的数据仍在$_POST中)。根据您的代码,它将在变量$result中。这就是你想要使用的,它有交易数据,它是IPN通知。同样,类内部的$response值只是PayPal让你知道你将要使用的数据是真实的一个无趣的点头。

附带说明一下,这个类有点混乱,不是很灵活。教程代码?几个建议:最好将数据注入验证方法,而不是直接读取$_POST

<?php
...
public function validate($data)
{
    ...
    foreach ($data as $field=>$value) { 
        ...
    }
    ...
}
//use
$result = $ipn->validate($_POST);
?>

此外,如果 curl 请求出现问题,您的验证器将调用 die。它可能应该返回 false,或者最好还是抛出一个您可以使用 try...catch 处理的异常。您不希望整个过程仅仅因为PayPal速度慢且请求超时而弹出带有神秘错误代码的白屏。处理错误,不要die.最后,与验证器一样,您应该将 url 注入构造函数中,而不是将其硬编码到类中。这样,您就可以从外部在实时和沙盒之间切换,而不是修改类文件。

我很困惑。 $_POST是一个超级全球性的。因此,您不需要此类来告诉您值,因为您已经拥有了它们。已验证响应是让您知道数据是真实的。