PayPal订阅支付和定期IPN处理

PayPal订阅支付和定期IPN处理,paypal,paypal-ipn,paypal-subscriptions,Paypal,Paypal Ipn,Paypal Subscriptions,我需要关于贝宝订阅支付IPN处理的建议。 我已经基于PayPal代码示例编写了一个IPN处理程序/侦听器。侦听器将IPN消息复制回PayPal,前面加上cmd=\u notify-validate。我可以毫无问题地设置订阅,也就是说,用户输入他们的详细信息,并将其与订单信息一起传递给PayPal,在PayPal中他们登录到自己的帐户并同意订阅。贝宝成功回复后,订单得到确认,我的数据库得到更新。 我遇到的问题是定期付款通知。我已经通过PayPal沙箱设置了每天的订阅,每次PayPal通知客户付款,

我需要关于贝宝订阅支付IPN处理的建议。 我已经基于PayPal代码示例编写了一个IPN处理程序/侦听器。侦听器将IPN消息复制回PayPal,前面加上cmd=\u notify-validate。我可以毫无问题地设置订阅,也就是说,用户输入他们的详细信息,并将其与订单信息一起传递给PayPal,在PayPal中他们登录到自己的帐户并同意订阅。贝宝成功回复后,订单得到确认,我的数据库得到更新。 我遇到的问题是定期付款通知。我已经通过PayPal沙箱设置了每天的订阅,每次PayPal通知客户付款,等待客户登录PayPal帐户并接受付款,这将导致另一个IPN确认付款完成。我正在发回验证请求之前的IPN消息,并从PayPal Sandbox收到一个空响应。我期待收到“验证”或“无效”根据贝宝文件?但是,PayPal对返回消息的响应是“”还是null? IPN验证代码如下所示,并使用“”作为URL:

  $url_parsed=parse_url($this->paypal_url);

  // generate the post string from the _POST vars and load the _POST vars into an array
  $post_string = "cmd=_notify-validate"; // start IPN response with validate command
  foreach ($_POST as $field=>$value) {
     $post_string .= '&';
     $this->ipn_data["$field"] = $value;
     $post_string .= $field.'='.urlencode(stripslashes($value));
  }

  // open the connection to PayPal
  $fp = fsockopen($url_parsed[host],443,$err_num,$err_str,30);

  if(!$fp) {

     // could not open the connection.  If logging is on, log the error message
     $this->last_error = "fsockopen error no. $errnum: $errstr";
     $this->log_ipn_results(false);
     return false;

  } else {

     // Post the data back to PayPal
     fputs($fp, "POST $url_parsed[path] HTTPS/1.1\r\n");
     fputs($fp, "Host: $url_parsed[host]\r\n");
     fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
     fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
     fputs($fp, "Connection: close\r\n\r\n");
     fputs($fp, $post_string . "\r\n\r\n");

     // loop through the response from the server and append to variable
     while(!feof($fp)) {
        $this->ipn_response .= fgets($fp, 1024);
     }

     fclose($fp); // close connection

  /* PayPal sends a single word back, which is VERIFIED if the message originated with PayPal
     or INVALID if there is any discrepancy with what was originally sent */
  if (strcmp ("INVALID", $this->ipn_response) != 0) {
  // The above is a work around to address null response! For now!
     // Valid IPN transaction.
     $this->log_ipn_results(true);
     return true;

  } else {

     // Invalid IPN transaction.  Check the log for details.
     $this->last_error = 'IPN Validation Failed.';
     $this->log_ipn_results(false);
     return false;
  }
我已经测试了超时,并且相信进程在30秒的时间限制内运行良好,并确认$post_字符串的结构在开始时用cmd复制了原始消息。我能想到的唯一的另一个问题是IPN VAR的返回发布是从SSL证书保护的页面发送的?不管怎样,除非我遗漏了一些东西,否则我不相信PayPal沙盒实际上在响应,因此结果为空?
任何建议或指导都将不胜感激,因为我依赖于多个每日订阅付款期通过沙盒进行测试。

当您发回用于验证交易的参数时,不要从值中删除斜杠。它们必须在收到时发回。

我实现了PHP Curl处理程序,如下所示:

<?php

   function validate_ipn() {

      // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
      // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
      // Set this to 0 once you go live or don't require logging.
      define("DEBUG", 1);
      define("LOG_FILE", "../log/ipn.log");

      // Set to 0 once you're ready to go live
      define("USE_SANDBOX", 1);

      // Read POST data
      // reading posted data directly from $_POST causes serialization
      // issues with array data in POST. Reading raw POST data from input stream instead.
      $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)
                      $myPost[$keyval[0]] = urldecode($keyval[1]);
      }
      // read the post from PayPal system and add 'cmd'
      $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";
      }

      // Post IPN data back to PayPal to validate the IPN data is genuine
      // Without this step anyone can fake IPN data
      if (USE_SANDBOX == true) {
         $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
      } else {
         $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
      }

      $ch = curl_init($paypal_url);
      if ($ch == FALSE) {
         return FALSE;
      }

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

      if (DEBUG == true) {
         curl_setopt($ch, CURLOPT_HEADER, 1);
         curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
      }

      // CONFIG: Optional proxy configuration
      //curl_setopt($ch, CURLOPT_PROXY, $proxy);
      //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

      // Set TCP timeout to 30 seconds
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

      // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
      // of the certificate as shown below. Ensure the file is readable by the webserver.
      // This is mandatory for some environments.

      //$cert = __DIR__ . "./cacert.pem";
      //curl_setopt($ch, CURLOPT_CAINFO, $cert);

      $res = curl_exec($ch);
      if (curl_errno($ch) != 0) { // cURL error
         if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
         }
         curl_close($ch);
         exit;

      } else {
         // Log the entire HTTP response if debug is switched on.
         if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);    
            error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);

            // Split response headers and payload
            list($headers, $res) = explode("\r\n\r\n", $res, 2);
         }
         curl_close($ch);
      }

      // Inspect IPN validation result and act accordingly
      if (strcmp ($res, "VERIFIED") == 0) {

         if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
         }
         return true;
      } else if (strcmp ($res, "INVALID") == 0) {
         // log for manual investigation
         // Add business logic here which deals with invalid IPN messages
         if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
         }
         return false;
      }

   }

?>

尝试过,但仍然返回空值?如果回答是错误的,PayPal会返回“无效”吗?应该是。我不知道“null return”在PHP中是什么意思。这可能是连接问题吗?非常感谢您的帮助和及时回复。在这种情况下,我不知道空响应是否意味着PayPal Sandbox“没有给出回复”,或者它实际上返回了一个空字符串/变量?请将标题更改为HTTP/1.1。HTTP和HTTPS都无法提供解决方案。我读过其他问题,通过HTTPS(虽然与PayPal无关)以这种方式重新发布数据。解决方案可能是使用PHP CURL版本的IPN处理程序/侦听器,因为它可以处理http和HTTPS?虽然仍然不明白为什么这个处理程序不工作?