search
HomeBackend DevelopmentPHP TutorialHow to use php to implement quick money payment function

This article is a detailed analysis and introduction to the code that uses PHP to implement the quick money payment function. Friends who need it can refer to it

This project is implemented using the zend framework framework
modules/default/controllers/IndexController.php
IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }
    public function indexAction()
    { 
     /*模拟订单 
      *$MockOrder是从数据库取出来的信息,它包含一些块钱Request的信息。这里我写死了。
      *orderId订单号,数据库表的主键(唯一)。    //必要字段
      *usr_idtype证件类型,根据自己需要。
      *usr_idcode证件号,根据自己需要。
      *etx_status是否优惠,根据自己需要。
      *time_create验证是否符合优惠的时间,根据自己需要。
      *ets_license套餐代码如同商品类别,根据自己需要。
      *contact_type联系方式类型,固定选择值1,2。1电子邮件,2手机号,根据自己需要,块钱那边可以为空。
      *contact_text联系方式,根据contact_type来填写,根据自己需要,块钱那边可以为空。
      *etsPrice套餐价格及商品价格,根据自己需要。
      *orderPrice实际价格,根据自己需要。
      *orderAmount订单实际支付金额,这个要加手续费的。  //必要字段
      *orderTime订单时间。        //必要字段
      *paySuccess订单是否支付成功。      //必要字段
      *buySuccess账号是否生成功,根据自己需要
      *payTime订单支付成功时间。       //必要字段
      *总之凡是跟订单有关的都是必要字段
      *orderId、orderAmount、orderTime为 Request所需字段
      *paySuccess、payTime为Response所需字段
      */
  $MockOrder = array();
  $MockOrder[&#39;orderId&#39;] = &#39;100000125&#39;;//订单号。--必要
  $MockOrder[&#39;usr_idtype&#39;] = &#39;1&#39;;//证件类型,身份证
  $MockOrder[&#39;usr_idcode&#39;] = &#39;371111199011111111&#39;;//身份证号
  $MockOrder[&#39;etx_status&#39;] = &#39;0&#39;;//是否优惠,否
  $MockOrder[&#39;time_create&#39;] = &#39;1352338189&#39;;//验证是否优惠时间
  $MockOrder[&#39;ets_license&#39;] = &#39;1&#39;;//套餐代码及商品类别
  $MockOrder[&#39;contact_type&#39;] = &#39;1&#39;;//联系方式类型 1,邮箱
  $MockOrder[&#39;contact_text&#39;] = &#39;x@163.com&#39;;//联系方式,邮箱
  $MockOrder[&#39;etsPrice&#39;] = &#39;30800&#39;;//套餐价格及商品价格
  $MockOrder[&#39;orderPrice&#39;] = &#39;30800&#39;;//实际价格
  $MockOrder[&#39;orderAmount&#39;] = &#39;31100&#39;;//订单实际支付价格,加手续费的。--必要
  $MockOrder[&#39;orderTime&#39;] = &#39;1352338199&#39;;//订单生成时间。--必要
  $MockOrder[&#39;paySuccess&#39;] = &#39;0&#39;;//订单是否支付成功。--必要
  $MockOrder[&#39;buySuccess&#39;] = &#39;0&#39;;//账号是否生成成功
  $MockOrder[&#39;payTime&#39;] = &#39;0&#39;;//订单支付时间。--必要
  //BillRequest就是快钱那边需要的的一些参数
  $this->view->BillRequest = new Application_Model_BillRequest($MockOrder);
  Zend_Debug::dump($this->view->BillRequest);exit;  
    }
    //bgUrl地址指向这里
    public function receiveAction()
    {
     //receive数据库设计
     /*用$MockReceive数组模拟
      * $MockReceive = array();
      * $MockReceive[&#39;id&#39;]主键;
      * $MockReceive[&#39;orderId&#39;]商户订单号;
      * $MockReceive[&#39;receiveTime&#39;]接受时间;
      * $MockReceive[&#39;queryString&#39;]http_build_encode($_REQUEST);
      * $MockReceive[&#39;dealId&#39;]快钱交易号;
      * $MockReceive[&#39;bankDealId&#39;]银行交易号;
      * $MockReceive[&#39;payResult&#39;]处理结果10:支付成功;11:支付失败;
      * $MockReceive[&#39;dealTime&#39;]快钱交易时间;
      * $MockReceive[&#39;payAmount&#39;]订单实际支付金额;
      * $MockReceive[&#39;fee&#39;]费用;
      * $MockReceive[&#39;errCode&#39;]错误代码;
      */
     
     /*$_REQUEST是快钱那边返回来的数据
      * merchantAcctId人民币账号,与提交订单时的块钱账号保持一致。
      * version网关版本,固定值:v2.0,与提交订单时的网关版本号保持一致。
      * language网页显示语言种类,1中文显示,与提交订单时的网页显示语言种类保持一致
      * signType签名类型,4PKI签名,与提交订单时的签名类型保持一致
      * payType支付方式,00全部,与提交订单时的支付方式保持一致
      * bankId银行代码
      * orderId商户订单号,与提交订单时的商户订单号保持一致
      * orderTime商户订单提交时间,与提交订单时的商户订单提交时间保持一致
      * orderAmount商户订单金额,与提交订单时的商户订单金额保持一致。
      * dealId快钱交易号
      * bankDealId银行交易号
      * dealTime快钱交易时间
      * payAmount订单实际支付金额
      * fee费用
      * ext1扩展字段1,与提交订单时的扩展字段1保持一致
      * ext2扩展字段2,与提交订单时的扩展字段2保持一致
      * payResult处理结果 10:支付成功;11:支付失败
      * errCode错误代码,可为空
      * signMsg签名字符串
      */
  $BillResponse = new Application_Model_BillResponse($_REQUEST);
  //$BillResponse->checkSignMsg验证签名字符串是否正确,防止bug漏洞等
  if($BillResponse->checkSignMsg){
   //判断订单支付是否成功
   if($BillResponse->isSuccess){
    //返回给快钱,快钱会按照redirecturl地址跳到新页面,这里是成功页面
    return "<result>1</result><redirecturl>http://99bill/default/index/sucess</redirecturl>";exit;
   }else{
    //返回给快钱,快钱会按照redirecturl地址跳到新页面,这个是失败页面
    return "<result>1</result><redirecturl>http://99bill/default/index/fail</redirecturl>";exit;
   }
  }
  //返回给快钱,快钱会按照redirecturl地址跳到新页面,这个是失败页面
  return "<result>1</result><redirecturl>http://99bill/default/index/fail</redirecturl>";exit;
    }
    //redirecturl地址
    //成功
    public function success()
    {
    }
    //失败
    public function fail()
    {
    }
}

modules/default/views/scripts/index/index.phtml
https://www.99bill.com/ gateway/recvMerchantInfoAction.htm

<?php $BillRequest = (array)$this->BillRequest;?>
<p style="display:none;">
<form name="kqPay" action="https://www.99bill.com/gateway/recvMerchantInfoAction.htm" method="post">
<?php foreach($BillRequest as $key => $val):?>
 <input type="hidden" name="<?php echo $key;?>" value="<?php echo $val;?>"/>
<?php endforeach;?>
 <input type="submit" name="submit" value="提交到快钱" id="kqPay">
</form>
</p>
<script>
document.getElementById(&#39;kqPay&#39;).click();
</script>

models/BillRequest.php
BillRequest.php

<?php
class Application_Model_BillRequest
{
 public function __construct($MockOrder){
  /*
   * 人民币网关账号。
   *第一种方式:该账号为11位人民币网关商户编号+01,该参数必填。01对应工商银行。
   *第二种方式:该账号为16位人民币网关商户
   */
  $this->merchantAcctId = "1001011111101"; 
  //服务器接收支付结果的后台地址,该参数务必填写,绝对路径//不能为空。
  $this->bgUrl = "http://99bill/default/index/receive";
  //商户订单号,以下采用时间来定义订单号,商户可以根据自己订单号的定义规则来定义该值//不能为空。
  $this->orderId = &#39;TOLPC&#39;.sprintf("%09d", $MockOrder[&#39;orderId&#39;]);
  //订单金额,金额以“分”为单位,商户测试以1分测试即可,切勿以大金额测试,该参数必填//不能为空
  $this->orderAmount =$MockOrder[&#39;orderAmount&#39;];
  //订单提交时间,格式:yyyyMMddHHmmss,如:20071117020101//不能为空。
  $this->orderTime = date("YmdHis", $MockOrder[&#39;orderTime&#39;]);
  //支付人姓名,可以为空。
  $this->payerName= ""; 
  //支付人联系类型,1 代表电子邮件方式;2 代表手机联系方式。可以为空。
  $this->payerContactType =  "";
  //支付人联系方式,与payerContactType设置对应,payerContactType为1,则填写邮箱地址;payerContactType为2,则填写手机号码。可以为空。
  $this->payerContact =  "";
  //商品名称,可以为空。
  $this->productName= "TOLPC";
  //商品数量,可以为空。
  $this->productNum = "1";
  //商品代码,可以为空。
  $this->productId = $MockOrder[&#39;ets_license&#39;];
  //商品描述,可以为空。
  $this->productDesc = "";
  //支付方式,一般为00,代表所有的支付方式。如果是银行直连商户,该值为10,必填//不能为空
  $this->payType = "00";
  //编码方式,1代表 UTF-8; 2 代表 GBK; 3代表 GB2312 默认为1,该参数必填//不能为空
  $this->inputCharset = "1";
  //网关版本,固定值:v2.0,该参数必填//不能为空
  $this->version =  "v2.0";
  //语言种类,1代表中文显示,2代表英文显示。默认为1,该参数必填//不能为空
  $this->language =  "1";
  //签名类型,该值为4,代表PKI加密方式,该参数必填//不能为空
  $this->signType =  "4";
  //接收支付结果的页面地址,该参数一般置为空即可。
  $this->pageUrl = "";
  //扩展字段1,商户可以传递自己需要的参数,支付完快钱会原值返回,可以为空。
  $this->ext1 = $MockOrder[&#39;orderId&#39;];
  //扩展自段2,商户可以传递自己需要的参数,支付完快钱会原值返回,可以为空。
  $this->ext2 = $MockOrder[&#39;orderTime&#39;];
  //银行代码,如果payType为00,该值可以为空;如果payType为10,该值必须填写,具体请参考银行列表。
  $this->bankId = "";
  //同一订单禁止重复提交标志,实物购物车填1,虚拟产品用0。1代表只能提交一次,0代表在支付不成功情况下可以再提交。可为空。
  $this->redoFlag = "";
  //快钱合作伙伴的帐户号,即商户编号,可为空。
  $this->pid = "";

  //快钱提供的request参数。
  $KeyOrders = array(&#39;inputCharset&#39;,&#39;pageUrl&#39;,&#39;bgUrl&#39;,&#39;version&#39;,&#39;language&#39;,&#39;signType&#39;,&#39;merchantAcctId&#39;,&#39;payerName&#39;,&#39;payerContactType&#39;,&#39;payerContact&#39;,
   &#39;orderId&#39;,&#39;orderAmount&#39;,&#39;orderTime&#39;,&#39;productName&#39;,&#39;productNum&#39;,&#39;productId&#39;,&#39;productDesc&#39;,&#39;ext1&#39;,&#39;ext2&#39;,&#39;payType&#39;,&#39;bankId&#39;,&#39;redoFlag&#39;,&#39;pid&#39;,);

  //判断快钱提供的request参数的值是否为空,把非空的参数及值重新组建数组
  foreach($KeyOrders as $key){
   if(&#39;&#39;==$this->{$key}){continue;}
   $params[$key] = $this->{$key};
  }
  //http_build_query()生成URL-encode之后的请求字符串
  //urldecode()还原未编码的字符串
  //getSignMsg() PKI加密,也可使用MD5加密
  //MD5加密方式  strtoupper(md5(urldecode(http_build_query($params))));这种不常用了。
  //常用PKI加密
  $this->signMsg = $this->getSignMsg(urldecode(http_build_query($params)));
 }

 //PKI加密技术

 public function getSignMsg($param){
  //99bill-rsa.pem是快钱的一个CA证书
  //本地随机生成一个KEY,用此KEY加密数据 KEY为$priv_key_id
  $priv_key_id = openssl_get_privatekey(file_get_contents("99bill-rsa.pem", "r"));
  //用$priv_key_id给$param数据加密。
  //计算一个签名字符串$param通过使用SHA1哈希加密,随后$priv_key_id私钥加密。数据本身是不加密的。
  openssl_sign($param, $signMsg, $priv_key_id, OPENSSL_ALGO_SHA1);
  //从存储器上释放$priv_key_id
  openssl_free_key($priv_key_id);
  //使用base64对数据进行编码
  return base64_encode($signMsg);
 }
}

models/BillResponse.php
BillResponse.php

<?php
class Application_Model_BillResponse
{
 /*
  * __construct()构造函数
  * 生成19个参数及值,可能有一个参数的值为空,$this->errCode的值可能为空
  */
 public function __construct($response){
  $KeyOrders = array(&#39;merchantAcctId&#39;,&#39;version&#39;,&#39;language&#39;,&#39;signType&#39;,&#39;payType&#39;,&#39;bankId&#39;,&#39;orderId&#39;,&#39;orderTime&#39;,&#39;orderAmount&#39;,
   &#39;dealId&#39;,&#39;bankDealId&#39;,&#39;dealTime&#39;,&#39;payAmount&#39;,&#39;fee&#39;,&#39;ext1&#39;,&#39;ext2&#39;,&#39;payResult&#39;,&#39;errCode&#39;, &#39;signMsg&#39;);
  foreach($KeyOrders as $key){
   $this->{$key} = $response[$key];
  }
 }
 /*
  * 检查签名字符串
  * 快钱返回的签名字符串是$this->signMsg
  * 使用base64对前面字符串进行解码
  * 验证使用快钱给的公钥验证
  * 快钱那边他们把返回来的参数值不为空的使用私钥加密生成了$this->signMsg
  * 快钱给了我们私钥对应的公钥,我们使用这个公钥来验证。1成功,0失败,-1错误。
  */
 public function checkSignMsg(){
  $KeyOrders = array(&#39;merchantAcctId&#39;,&#39;version&#39;,&#39;language&#39;,&#39;signType&#39;,&#39;payType&#39;,&#39;bankId&#39;,&#39;orderId&#39;,&#39;orderTime&#39;,&#39;orderAmount&#39;,
   &#39;dealId&#39;,&#39;bankDealId&#39;,&#39;dealTime&#39;,&#39;payAmount&#39;,&#39;fee&#39;,&#39;ext1&#39;,&#39;ext2&#39;,&#39;payResult&#39;,&#39;errCode&#39;,);
  foreach($KeyOrders as $key){
   if(&#39;&#39;==$this->{$key}){continue;}
   $params[$key] = $this->{$key};
  }
  //$pub_key_id 公钥
  $pub_key_id = openssl_get_publickey(file_get_contents("99bill-rsa.cer", "r"));
  return openssl_verify(urldecode(http_build_query($params)), base64_decode($this->signMsg), $pub_key_id); 
 }
 public function isSuccess(){
  //$this->payResult成功时10,失败时11
  return &#39;10&#39;==$this->payResult;
 }
 public function getOrderId(){
  return str_replace(&#39;XXX&#39;, &#39;&#39;, $this->orderId);
 }
}

Requires a public key and a private key, this is not a pair
They are both half
99bill-rsa.cer
99bill-rsa.pem

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP uses SWOOLE extension to achieve scheduled synchronization        

zen_cart's method of generating orders before payment

The above is the detailed content of How to use php to implement quick money payment function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.