【问题标题】:How to capture PaPal IPN data properly如何正确捕获 PaPal IPN 数据
【发布时间】:2015-01-21 13:29:25
【问题描述】:

我正在尝试捕获从 PayPal 发回的数据到 IPN 脚本。在我的按钮中,我确实正确设置了 notify_url 以及 rm 值 2(用于回发数据)。我也在按钮中使用 _xclick 命令 (cmd=_xclick)。

在我的以下代码中,我定期将数据或流写入错误日志以捕获正在发生的事情。但似乎 PayPal 正在向 IPN 推送,但没有捕获任何数据(或至少正确捕获)。

这是我目前的整个 IPN 脚本:

        // 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');


            error_log("Got " . $raw_post_data . " when processing IPN data");
            error_log("-------------------\n");

            $raw_post_array = explode('&', $raw_post_data);
            $myPost = array();
            foreach ($raw_post_array as $keyval) {
              $keyval = explode ('=', $keyval);
              if (count($keyval) == 2)
                 $myPost[$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 ($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";
            }


             error_log("Got response: " . $req . " when processing IPN data");
             error_log("-------------------\n");

            // STEP 2: POST IPN data back to PayPal to validate

            $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
            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'));
            $res = curl_exec($ch);

             error_log("Got response: " . $res . " when processing IPN VALIDATE");
             error_log("-------------------\n");



            if( !($res) ) {
                error_log("Got " . curl_error($ch) . " when processing IPN Validation");
                curl_close($ch);
                exit;
            }

            curl_close($ch);


             error_log("Got response: " . $res . " when processing IPN data");
             error_log("-------------------\n");

            // STEP 3: Inspect IPN validation result and act accordingly

            if (strcmp (trim($res), "VERIFIED") == 0) {
                // The IPN is verified, process it:
                // check whether the payment_status is Completed
                // check that txn_id has not been previously processed
                // check that receiver_email is your Primary PayPal email
                // check that payment_amount/payment_currency are correct
                // process the notification

                // assign posted variables to local variables
                $item_name = $_POST['item_name'];
                $item_number = $_POST['item_number'];
                $payment_status = $_POST['payment_status'];
                $payment_amount = $_POST['mc_gross'];
                $payment_currency = $_POST['mc_currency'];
                $txn_id = $_POST['txn_id'];
                $receiver_email = $_POST['receiver_email'];
                $payer_email = $_POST['payer_email'];

                // IPN message values depend upon the type of notification sent.
                // To loop through the &_POST array and print the NV pairs to the screen:

                foreach($_POST as $key => $value) {
                  //echo $key." = ". $value."<br>";
                  error_log($key." = ". $value."\n");
                }
            } else if (strcmp ($res, "INVALID") == 0) {
                // IPN invalid, log for manual investigation
                $t =  "The response from IPN was: <b>" .$res ."</b>";
                 error_log($t."\n");
            }

捕获数据的结果是 IPN 以 INVALID 响应。

我不确定我在这里缺少什么。似乎 file_get_contents('php://input') 根本没有捕获任何数据。有没有比使用 file_get_contents('php://input') 或 $_POST 变量更好的方法来捕获输入数据?

感谢任何其他见解。谢谢。

【问题讨论】:

    标签: php paypal paypal-ipn


    【解决方案1】:

    尝试通过以下代码更改第 1 步:

     // Prepare data that will sent to Paypal for verification
    $req = "cmd=_notify-validate";
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
        $content = "&$key=$value";
    }
    

    注意:你可以使用paypal的sandbox website for developer来测试你的ipn监听器 那么你应该换行

    $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
    

    通过

    $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
    

    希望这会有所帮助,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2017-12-04
      • 1970-01-01
      • 2016-01-08
      • 2014-11-17
      相关资源
      最近更新 更多