PHP-Paypal IPN验证失败

时间:2018-07-31 21:55:26

标签: php html iis paypal paypal-ipn

我想从这个话题开始,说我发现的所有可能的解决方案都没有为我解决。所以我希望有人可以帮助我。

这是我用于PayPal IPN(类文件)的代码。

<?php
require('../inc/db.php');

define('LOG_FILE', 'ipn_results.log');
define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
define('SSL_SAND_URL','https://www.sandbox.paypal.com/cgi-bin/webscr');

class paypal_class {

   var $last_error;                 // holds the last error encountered

   var $ipn_log;                    // bool: log IPN results to text file?

   var $ipn_log_file;               // filename of the IPN log
   var $ipn_response;               // holds the IPN response from paypal   
   var $ipn_data = array();         // array contains the POST values for IPN

   var $fields = array();           // array holds the fields to submit to paypal


   function paypal_class() {

      // initialization constructor.  Called when class is created.

      $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';

      $this->last_error = '';

      $this->ipn_log_file = '/ipn_results.log';
      $this->ipn_log = true; 
      $this->ipn_response = '';

      // populate $fields array with a few default values.  See the paypal
      // documentation for a list of fields and their data types. These defaul
      // values can be overwritten by the calling script.

      $this->add_field('rm','2');           // Return method = POST
      $this->add_field('cmd','_xclick'); 

   }

   function add_field($field, $value) {



      $this->fields["$field"] = $value;
   }

   function submit_paypal_post() {


     echo "<html>\n";
     echo "<head><title>Processing Payment...</title>";
     echo "<body onLoad=\"document.forms['paypal_form'].submit();\">\n";
     echo "<center><h2>Please wait, your order is being processed and you";
     echo " will be redirected to the paypal website.</h2></center>\n";
     echo "<form method=\"post\" name=\"paypal_form\" ";
     echo "action=\"".$this->paypal_url."\">\n";

     foreach ($this->fields as $name => $value) 
     {
         echo "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>\n";
      }
     echo "<center><br/><br/>If you are not automatically redirected to ";
     echo "paypal within 5 seconds...<br/><br/>\n";
     echo "<input type=\"submit\" value=\"Click Here\"></center>\n";
     echo "</body></html>\n";

   }

   function validate_ipn() {
    mysqli_query($db, "UPDATE matches SET status = 3");
      // parse the paypal URL
      $url_parsed=parse_url($this->paypal_url);        


      // read post data from PayPal 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";
    }

      // open the connection to paypal
      $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); 
      if(!$fp) {

         // could not open the connection.  If loggin is on, the error message
         // will be in the log.
         $this->last_error = "fsockopen error no. $errnum: $errstr";
         $this->log_ipn_results(false);       
         mysqli_query($db, "UPDATE matches SET status = 5");
         return false;

      } else { 

         // Post the data back to paypal
         fputs($fp, "POST $url_parsed[path] HTTP/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
        mysqli_query($db, "UPDATE matches SET status = 8");
      }

      if (eregi("VERIFIED",$this->ipn_response)) {

         // Valid IPN transaction.
         $this->log_ipn_results(true);
         mysqli_query($db, "UPDATE matches SET status = 9");
         return true;       

      } else {

         // Invalid IPN transaction.  Check the log for details.
         $this->last_error = 'IPN Validation Failed.';
         $this->log_ipn_results(false);   
         mysqli_query($db, "UPDATE matches SET status = 6");
         return false;

      }

   }

   function log_ipn_results($success) {

      if (!$this->ipn_log) return;  // is logging turned off?

      // Timestamp
      $text = '['.date('m/d/Y g:i A').'] - '; 

      // Success or failure being logged?
      if ($success) $text .= "SUCCESS!\n";
      else $text .= 'FAIL: '.$this->last_error."\n";

      // Log the POST variables
      $text .= "IPN POST Vars from Paypal:\n";
      foreach ($this->ipn_data as $key=>$value) {
         $text .= "$key=$value, ";
      }

      // Log the response from the paypal server
      $text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response;

      // Write to log
      $fp=fopen($this->ipn_log_file,'a');
      fwrite($fp, $text . "\n\n"); 

      fclose($fp);  // close file
   }

   function dump_fields() {



      echo "<h3>paypal_class->dump_fields() Output:</h3>";
      echo "<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
            <tr>
               <td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
               <td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
            </tr>"; 

      ksort($this->fields);
      foreach ($this->fields as $key => $value) {
         echo "<tr><td>$key</td><td>".urldecode($value)."&nbsp;</td></tr>";
      }

      echo "</table><br>"; 
   }
}         

您找到的查询实际上不是代码的一部分。我使用它们进行故障排除,因此我可以看到问题出在哪里,但是由于某种原因它们没有被执行。它也不会记录日志,它应该登录文件 ipn_results.log ,但是它不是,我不知道路径是否有问题,或者我应该给我文件夹某些权限。但是。

此代码正在与另一个文件一起使用,我将在此处添加代码。

    <?php

    include_once('../inc/db.php');
    require ('../inc/steamauth.php');

    function filter($var)
        {
            return stripslashes(htmlspecialchars($var));
        }

    require_once('paypal.class.php');  // include the class file
    $p = new paypal_class; 
    $p->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';

    $this_script = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];


    if (empty($_GET['action'])) $_GET['action'] = 'process';  

    switch ($_GET['action']) {

       case 'process':

       $teamid = mysqli_real_escape_string($db, $_POST['teamid']);
       $type = mysqli_real_escape_string($db, $_POST['type']);


        switch($type) {

        case 'me':
            $cost = '0.01';
        break;

        case 'team':
            $cost = '0.01';
        break;

        }


          $p->add_field('business', 'MY EMAIL');
          $p->add_field('return', 'https://'.$_SERVER['HTTP_HOST']); //The success URL
          $p->add_field('custom', $teamid);
          $p->add_field('cancel_return', 'http://'.$_SERVER['HTTP_HOST']); // The "canceled" URL
          $p->add_field('notify_url', $this_script.'?action=ipn'); //The IPN URL, the URL pointing to THIS page.
          $p->add_field('item_number', filter($_POST['type']));
          $p->add_field('item_name', $_POST['type'] . '');
          $p->add_field('amount', $cost); // How ever much the VIP cost.

          $p->submit_paypal_post();

          break;


          case 'ipn':

          $db = mysqli_connect("localhost", "root", "*****", "***");

$problem =  mysqli_query($db, "UPDATE matches SET status = '1'");

          if ($p->validate_ipn()) {

              $complete = mysqli_query($db, "UPDATE matches SET status = '2'");

        }
          break;
     }     

    ?>

如您所见,如果IPN已验证,它将运行查询 $ complete ,但没有,它将运行查询 $ problem 表示它已连接到正确的PayPal,并且IPN网址正确。

我以前一直在使用此确切的代码,但是无论如何它都无法正常工作。 我正在使用 Windows 2016 VPS IIS 10

上运行

防火墙已禁用。 我的网站正在运行 SSL证书 我正在使用 PHP 5.3.8 ,但是我尝试使用 PHP 7.2.7

我不知道我提供的任何信息是否会导致代码出现问题。我还要提及的是,贝宝​​(PayPal)帐户是新帐户,并且是个人帐户。除了我需要更改的IPN设置,我不知道是否还有其他设置。

代码可以工作多长时间? 该代码运行良好,首先我要运行以html格式连接到我的paypal文件的表单(在本例中为paypal.php)。它会检查我选择哪种产品($ type),然后以正确的价格将我重定向到PayPal,并将付款转到正确的帐户。付款完成后,它会将我重定向回网站,但由于我要查询$ complete 才能运行,因此需要验证IPN。不会发生。

我认为这就是代码执行的内容。

 // Invalid IPN transaction.  Check the log for details.
         $this->last_error = 'IPN Validation Failed.';
         $this->log_ipn_results(false);   
         mysqli_query($db, "UPDATE matches SET status = 6");
         return false;

有人知道可能是什么原因吗?如前所述,我以前使用过此代码,并且可以正常工作,但现在却不行。

0 个答案:

没有答案