php - 为什么我的 paypal ipn 验证总是返回 INVALID?

标签 php codeigniter paypal

我使用的是根据 paypal 网站的示例代码创建的 PHP 类。我也在 CodeIgniter 中使用它。我使用 IPN 模拟器测试 IPN 监听器。我的电子邮件已发送,所以我知道它正在被访问。问题是我总是收到 INVALID 的响应,我不知道为什么。这是我第一次将 PayPal 集成到我的网站中。是什么导致了这个问题?

这是我的类(class):

<?php

class Paypal {

    public $sandbox = false;
    private $_url;
    public $verified = false;
    public $fields = array();
    public $post_fields = array();
    public $result;

    public function __construct($params = array()){

        $this->sandbox = (isset($params['sandbox'])) ? $params['sandbox'] : false;

        $this->_url = ($this->sandbox) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr';

    }

    public function run(){

        $this->verified = false;

        // STEP 1: read POST data
        // Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
        // Instead, read raw POST data from the input stream.
        $raw_post_data = file_get_contents('php://input');
        $raw_post_array = explode('&', $raw_post_data);

        foreach ($raw_post_array as $keyval) {

          $keyval = explode ('=', $keyval);

          if (count($keyval) == 2)

            $this->post_fields[$keyval[0]] = urldecode($keyval[1]);

        }

        // read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
        $req = 'cmd=_notify-validate';

        if (function_exists('get_magic_quotes_gpc')) {

          $get_magic_quotes_exists = true;

        }

        foreach ($this->post_fields 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";

        }

        // Step 2: POST IPN data back to PayPal to validate
        $ch = curl_init($this->_url);
        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_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
        // In wamp-like environments that do not come bundled with root authority certificates,
        // please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
        // the directory path of the certificate as shown below:
        curl_setopt($ch, CURLOPT_CAINFO, FCPATH.'cacert.pem');
        if ( !($res = curl_exec($ch)) ) {
          // error_log("Got " . curl_error($ch) . " when processing IPN data");
          curl_close($ch);
          die('curl did not work<br>'.FCPATH.'cacert.pem');
        }
        curl_close($ch);

        if (strcmp ($res, "VERIFIED") == 0) {

            $this->verified = true;            

        }

        $this->result = $res;

    }

这是我的 CI Controller :

function paypalipn(){

        $this->load->model('subscription');
        $this->load->library('paypal', array('sandbox' => true));;

        $this->paypal->run();

        $fields = $this->paypal->post_fields;

        if($this->paypal->verified){

            $data = array(
                'user_id' => $fields['buyer_id'],
                'txn_id' => $fields['txn_id'],
                'payment_gross' => $fields['mc_gross'],
                'currency_code' => $fields['mc_currency'],
                'payer_email' => $fields['payer_email'],
                'plan_id' => $fields['item_number'],
                'payment_status' => $fields['payment_status']
            );

            $this->subscription->create_payment($data);

        }

        $this->load->library('email');

        $this->email->to('*******@gmail.com');
        $this->email->from('**************');
        $this->email->subject('PayPal IPN');
        $this->email->message($this->paypal->result."\nAmount: ".$fields['mc_gross']."\nCurrency: ".$fields['mc_currency']."\nUser ID: ".$fields['buyer_id']);

        $this->email->send();

    }

电子邮件中的所有字段都是空白的,$this->paypal->result 总是返回“INVALID”。有人有什么想法吗?感谢您的宝贵时间。

最佳答案

在您的 CI Controller 中,如果您编写了如下代码:

function paypalipn(){

        $this->load->model('subscription');
        $this->load->library('paypal', array('sandbox' => true));;

        $this->paypal->run();

        $fields = $this->paypal->post_fields;

        if($this->paypal->verified){

            $data = array(
                'user_id' => $fields['buyer_id'],
                'txn_id' => $fields['txn_id'],
                'payment_gross' => $fields['mc_gross'],
                'currency_code' => $fields['mc_currency'],
                'payer_email' => $fields['payer_email'],
                'plan_id' => $fields['item_number'],
                'payment_status' => $fields['payment_status']
            );

            $this->subscription->create_payment($data);
        $this->load->library('email');
        $this->email->to('*******@gmail.com');
        $this->email->from('**************');
        $this->email->subject('PayPal IPN');
        $this->email->message($this->paypal->result."\nAmount: ".$fields['mc_gross']."\nCurrency: ".$fields['mc_currency']."\nUser ID: ".$fields['buyer_id']);
        $this->email->send();
        }
    } 

或者如果您已经停止脚本以防 if 语句失败

if($this->paypal->verified){

            $data = array(
                'user_id' => $fields['buyer_id'],
                'txn_id' => $fields['txn_id'],
                'payment_gross' => $fields['mc_gross'],
                'currency_code' => $fields['mc_currency'],
                'payer_email' => $fields['payer_email'],
                'plan_id' => $fields['item_number'],
                'payment_status' => $fields['payment_status']
            );

            $this->subscription->create_payment($data);
} else {
exit("We are sad to inform you that verification FAILED. Bye!");
}

您不会收到任何电子邮件。问题出在您的 paypal 类中的 public function run() 中,这很可能是失败的部分。

       if ( !($res = curl_exec($ch)) ) {
          curl_close($ch);
          die('curl did not work<br>'.FCPATH.'cacert.pem');
        }
        curl_close($ch);
       if (strcmp ($res, "VERIFIED") == 0) {   
            $this->verified = true;                
        }

试试这样重写

       $res=curl_exec($ch); // define $res
       if($res==null){  // check if $res is null
            exit('curl did not work<br>'.FCPATH.'cacert.pem');
        }
       curl_close($ch);
       if (strcmp ($res, "VERIFIED") === 0) {   
            $this->verified = true;                
       }

关于使用 strcmp() 的最后 3 行,并且基于用户 commentstrcmp() 文档中。

  • strcmp() will return NULL on failure.
  • This has the side effect of equating to a match when using an equals comparison (==).
  • Instead, you may wish to test matches using the identical comparison (===), which should not catch a NULL return.

以上是什么意思?在一个小例子中。

if(null==0) {
echo "1";
}
if(null===0) {
echo "2";
}

上面的例子只会输出=> 1在您的情况下,这意味着如果 strcmp() 失败,您会将 verified 设置为 true,这是不正确的。

关于php - 为什么我的 paypal ipn 验证总是返回 INVALID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36706669/

相关文章:

php - CodeIgniter推荐的教程?

paypal - 如何查看 Paypal 账单协议(protocol)状态?

ios - iOS 移动应用程序中的 Paypal 集成

Paypal:不同国家/地区的账单和送货地址?

c# - php lib memcached 奇怪的输入值

php - 更改密码 PHP/Ajax/JQuery

javascript - 选择并显示区域 jquery 日期选择器,但保存为转换后的公历日期

PHP DateTime操作-设置新时区并扣除24小时

php - 使用 jquery 和 php 动态重命名侧边栏菜单

php - 使用 Codeigniter 数据库 session 的永久 session