3

我正在尝试集成 Paypal,但我没有把事情做好。当网站被重定向到 Paypal 时,它会显示网页(我在这篇文章中添加的图像)。请任何人帮助我摆脱这个混乱,因为我是第一次这样做。请任何人告诉我我可能错的地方。替代文字 http://img341.imageshack.us/img341/6920/captureqt.jpg

我已经使用这个类将 Paypal 集成到我的网站。但不知道为什么我没有得到想要的东西。

/************************************************* ****************************** * PHP Paypal IPN 集成类


  • 作者:米卡·卡里克
  • 电子邮件:email@micahcarrick.com
  • 网站: http: //www.micahcarrick.com *
  • 文件:paypal.class.php
  • 版本:1.3.0
  • 版权所有:(c) 2005 - Micah Carrick
  • 您可以自由使用、分发和修改此软件
  • 根据 GNU 通用公共许可证的条款。见
  • 包括 license.txt 文件。

  • 版本历史:

  • v1.3.0 [10.10.2005] - 修复它以便处理单引号
  • 正确的方法,而不是简单地剥离它们。这
  • 需要,因为用户仍然可以输入
  • 引号。
    • v1.2.1 [06.05.2005] - 修复了先前修复的错字:) *
  • v1.2.0 [05.31.2005] - 添加了删除所有引号的可选功能
  • 从贝宝的帖子。IPN 会回来的
  • 有时在 certian 中使用引号时无效
  • 字段。*
  • v1.1.0 [05.15.2005] - 修改了 submit_paypal_post 中的表单输出
  • 允许不支持javascript的浏览器的方法
  • 提供一种手动提交表单的方法。*
  • v1.0.0 [04.16.2005] - 初始版本 *


  • 描述: *

  • 注意:有关此类的最新版本,请参见 www.micahcarrick.com
  • 以及任何适用的示例文件和其他文档。*
  • 该文件提供了一种简洁的方法来与 paypal 和
  • 贝宝即时支付通知 (IPN) 界面。这个文件是
  • 不打算使贝宝集成“即插即用”。它仍然
  • 要求开发人员(应该是你)了解贝宝
  • 处理并知道您想要/需要传递给贝宝的变量
  • 达到你想要的。
    *
  • 此类处理向贝宝提交订单以及
  • 处理即时付款通知。
    • 此代码基于 paypal 的 php-toolkit 的代码。我已经采取
  • 基本原则,并把它放在一个类中,这样它就有点
  • 更容易——至少对我来说——使用。php-toolkit 可以从
  • http://sourceforge.net/projects/paypal
    • 要向贝宝提交订单,请将您的订单表格 POST 到包含以下内容的文件中:*
  • $p = 新的 paypal_class;
  • $p->add_field('business', 'somebody@domain.com');
  • $p->add_field('first_name', $_POST['first_name']);
  • ...(在同一个庄园中添加您的所有领域)
  • $p->submit_paypal_post(); *
  • 要处理 IPN,请让您的 IPN 处理文件包含: *
  • $p = 新的 paypal_class;
  • if ($p->validate_ipn()) {
  • ...(IPN 已验证。详细信息在 ipn_data() 数组中)
  • } * *
  • 如果您是 paypal 的新手,这里有一些信息可以帮助您:*
    1. 下载并阅读商户用户手册和集成指南
  • http://www.paypal.com/en_US/pdf/integration_guide.pdf。这给
  • 您需要的所有信息,包括您可以传递给的字段
  • 贝宝(在此类中使用 add_field() )以及所有字段
  • 在 IPN 帖子中返回的数据(存储在 ipn_data() 数组中
  • 这节课)。它还描绘了整个交易过程。*
    1. 为买家和卖家创建一个“沙盒”帐户。这只是
  • 一个测试帐户,允许您从两个
  • 卖方和买方的观点。这方面的说明是可用的
  • https://developer.paypal.com/以及一个很棒的论坛
  • 可以询问您所有的贝宝集成问题。确保你遵循
  • 设置沙盒测试环境的所有说明,包括
  • 增加假银行账户和信用卡。

*/

类贝宝类{

变量 $last_error; // 保存最后遇到的错误

变量 $ipn_log; // bool: 将 IPN 结果记录到文本文件?

变量 $ipn_log_file; // IPN 日志的文件名 var $ipn_response; // 保存来自 paypal 的 IPN 响应
var $ipn_data = array(); // 数组包含 IPN 的 POST 值

var $fields = 数组();// 数组保存要提交到贝宝的字段

功能贝宝类(){

  // initialization constructor.  Called when class is created.

  $this->paypal_url = 'https://www.sandbox.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'); 

}

函数 add_field($field, $value) {

  // adds a key=>value pair to the fields array, which is what will be 
  // sent to paypal as POST variables.  If the value is already in the 
  // array, it will be overwritten.

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

}

功能 submit_paypal_post() {

  // this function actually generates an entire HTML page consisting of
  // a form with hidden elements which is submitted to paypal via the 
  // BODY element's onLoad attribute.  We do this so that you can validate
  // any POST vars from you custom form before submitting to paypal.  So 
  // basically, you'll have your own form which is submitted to your script
  // to validate the data, which in turn calls this function to create
  // another hidden form and submit to paypal.

  // The user will briefly see a message on the screen that reads:
  // "Please wait, your order is being processed..." and then immediately
  // is redirected to paypal.

  echo "<html>\n";
  echo "<head><title>Processing Payment...</title></head>\n";
  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 "</form>\n";
  echo "</body></html>\n";      

} //---------------------------------------- // 设置状态 /*function设置状态(){

   $db = new Connection();
   $con = $db->getConnection();
   $query="insert into paypal_Result(`mc_gross`,`protection_eligibility`) values ('1','2')";
   $rs=mysql_query($query,$con);
   if($rs != "")
   {
      return true; 
   }
   else
   {
       return false;
   }

}*/

//----------------------------------------

函数 validate_ipn() {

  // parse the paypal URL
  $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->ipn_data["$field"] = $value;
     $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 
  }
  $post_string.="cmd=_notify-validate"; // append ipn command

  // open the connection to paypal
  $fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,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);       
     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

  }

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

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

  }

}

函数 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

}

功能转储字段(){

  // Used for debugging, this function will output all the field/value pairs
  // that are currently defined in the instance of the class using the
  // add_field() function.

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

} }

4

1 回答 1

2

不确定您的代码有什么问题,但是使用内置类来避免代码中出现任何问题总是一个好主意,所以我建议您使用这个paypal 类,这将提高您的工作量边。

于 2010-02-06T18:53:28.970 回答