Use PHP to implement quick money payment function (involving interfaces)_PHP tutorial

WBOY
Release: 2016-07-21 15:01:40
Original
660 people have browsed it

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

Copy code The code is as follows:

class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
Public function indexAction()
{

/*Mock Order
*$MockOrder is the information taken out from the database, it contains some dollar Request information. I wrote it to death here.
*orderId order number, the primary key (unique) of the database table. //Required fields
*usr_idtype document type, according to your needs.
*usr_idcode ID number, according to your needs.
*Whether etx_status is discounted or not depends on your needs.
*time_create Verify whether it meets the preferential time, according to your own needs.
*ets_license package code is like a product category, according to your needs.
*contact_type contact type, fixed selection value 1,2. 1 email, 2 mobile phone number, according to your own needs, the dollar side can be empty.
*contact_text contact information, fill in according to contact_type, according to your own needs, the dollar side can be empty.
*etsPrice package price and product price, according to your needs.
*orderPrice is the actual price, based on your needs.
*orderAmount is the actual payment amount of the order, which will be subject to a handling fee. //Required fields
*orderTime order time. ​​​​​//Required fields
​​​​​​​​​​​ // The necessary field <*> *Buysuccess account is successful, and the success time is successful according to your needs
*Paytime order payment. //Required fields
*In short, everything related to the order is a necessary field
*orderId, orderAmount, orderTime are required fields for Request
*paySuccess, payTime are required fields for Response
*/
$MockOrder = array();
$MockOrder['orderId'] = '100000125';//Order number. --Required
$MockOrder['usr_idtype'] = '1';//Document type, ID card
$MockOrder['usr_idcode'] = '371111199011111111';//ID card number
$MockOrder ['etx_status'] = '0';//Whether it is a discount, no
$MockOrder['time_create'] = '1352338189';//Verify whether the time is discounted
$MockOrder['ets_license'] = '1 ';//Package code and product category
$MockOrder['contact_type'] = '1';//Contact type 1, email
$MockOrder['contact_text'] = 'x@163.com' ;//Contact information, email
$MockOrder['etsPrice'] = '30800';//Package price and product price
$MockOrder['orderPrice'] = '30800';//Actual price
$MockOrder['orderAmount'] = '31100';//The actual price paid for the order, plus handling fee. --Required
$MockOrder['orderTime'] = '1352338199';//Order generation time. --Required
$MockOrder['paySuccess'] = '0';//Whether the order was paid successfully. --Required
$MockOrder['buySuccess'] = '0';//Whether the account is generated successfully
$MockOrder['payTime'] = '0';//Order payment time.--Necessary

//BillRequest is some parameters needed by Kuaiqian
$this->view->BillRequest = new Application_Model_BillRequest($MockOrder);
Zend_Debug::dump ($this->view->BillRequest);exit;
}

//bgUrl address points here
public function receiveAction()
{
//receive database Design
/*Use $MockReceive array to simulate
* $MockReceive = array();
* $MockReceive['id'] primary key;
* $MockReceive['orderId'] merchant order number;
* $MockReceive['receiveTime'] receiving time;
* $MockReceive['queryString']http_build_encode($_REQUEST);
* $MockReceive['dealId'] fast money transaction number;
* $MockReceive['bankDealId'] bank transaction number;
* $MockReceive['payResult'] processing result 10: payment successful; 11: payment failed;
* $MockReceive['dealTime'] quick money transaction time ;
* $MockReceive['payAmount'] actual payment amount of order;
* $MockReceive['fee'] fee;
* $MockReceive['errCode'] error code;
*/


/*$_REQUEST is the data returned by Kuaiqian
* merchantAcctId RMB account number, consistent with the dollar account number when submitting the order.
* version Gateway version, fixed value: v2.0, consistent with the gateway version number when submitting the order.
* language The language type displayed on the web page, 1 Chinese display, is consistent with the language type displayed on the web page when submitting the order
* signType signature type, 4PKI signature, consistent with the signature type when submitting the order
* payType Payment method, 00 all, consistent with the payment method when submitting the order
* bankId bank code
* orderId merchant order number, consistent with the merchant order number when submitting the order
* orderTime merchant order submission time , consistent with the merchant order submission time when the order is submitted
* orderAmount merchant order amount, consistent with the merchant order amount when the order is submitted.
* dealId fast money transaction number
Consistent with the extended field 1 when submitting the order
* ext2 extended field 2, consistent with the extended field 2 when submitting the order
* payResult processing result 10: payment successful; 11: payment failed
* errCode Error code, can be empty
* signMsg signature string
*/
$BillResponse = new Application_Model_BillResponse($_REQUEST);
//$BillResponse->checkSignMsg verifies whether the signature string is correct, Prevent bugs and loopholes
if($BillResponse->checkSignMsg){
//Judge whether the order payment is successful
if($BillResponse->isSuccess){
//Return to quick money, Quick Money will jump to a new page according to the redirecturl address. Here is the success page
return "1http://99bill/default/index/sucess"; exit;
}else{
//Return to Kuaiqian, Kuaiqian will jump to a new page according to the redirecturl address. This is the failure page
return "1";exit;
}
}
//Return to Kuaiqian, Kuaiqian will jump to the new page according to the redirecturl address, this It is a failure page
return "1http://99bill/default/index/fail";exit;
}

//redirecturl address
//Success
public function success()
{

}

//Failure
public function fail()
{

}
}


modules/default/views/scripts/index/index.phtml
https://www.99bill.com/gateway/recvMerchantInfoAction.htm
复制代码 代码如下:

BillRequest;?>


$val):?>
 

 


<script><br>document.getElementById('kqPay').click();<br></script>

models/BillRequest.php
BillRequest.php
复制代码 代码如下:

class Application_Model_BillRequest
{
public function __construct($MockOrder){
/*
* RMB gateway account.
*The first method: the account is the 11-digit RMB gateway merchant number + 01, this parameter is required. 01 corresponds to Industrial and Commercial Bank of China.
*Second method: The account is a 16-digit RMB gateway merchant
*/
$this->merchantAcctId = "1001011111101";
//The backend address where the server receives the payment result, the The parameters must be filled in, and the absolute path // cannot be empty.
$this->bgUrl = "http://99bill/default/index/receive";
//Merchant order number, the following uses time to define the order number, the merchant can define the order number according to its own rules To define the value // cannot be empty.
$this->orderId = 'TOLPC'.sprintf("%09d", $MockOrder['orderId']);
//Order amount, the amount is in "cents", the merchant test is in 1 Just test in batches. Do not test with a large amount. This parameter is required //cannot be empty
$this->orderAmount =$MockOrder['orderAmount'];
//Order submission time, format: yyyyMMddHHmmss, such as: 20071117020101//cannot be empty.
$this->orderTime = date("YmdHis", $MockOrder['orderTime']);
//Payer name, can be empty.
$this->payerName= "";
//Payer contact type, 1 represents email; 2 represents mobile phone contact. Can be empty.
$this->payerContactType = "";
//Payer contact information, corresponding to the payerContactType setting. If payerContactType is 1, fill in the email address; if payerContactType is 2, fill in the mobile phone number. Can be empty.
$this->payerContact = "";
// Product name, can be empty.
$this->productName= "TOLPC";
//Product quantity, can be empty.
$this->productNum = "1";
//Product code, can be empty.
$this->productId = $MockOrder['ets_license'];
// Product description, can be empty.
$this->productDesc = "";
//Payment method, usually 00, represents all payment methods. If it is a merchant directly connected to the bank, the value is 10, required // cannot be empty
$this->payType = "00";
//Encoding method, 1 represents UTF-8; 2 represents GBK ; 3 represents GB2312, the default is 1, this parameter is required // cannot be empty
$this->inputCharset = "1";
//Gateway version, fixed value: v2.0, this parameter is required //Cannot be empty
$this->version = "v2.0";
//Language type, 1 represents Chinese display, 2 represents English display. The default is 1, this parameter is required//cannot be empty
$this->language = "1";
//Signature type, the value is 4, representing the PKI encryption method, this parameter is required/ /Cannot be empty
$this->signType = "4";
//The page address for receiving payment results. This parameter is generally left empty.
$this->pageUrl = "";
//Extended field 1, merchants can pass the parameters they need. After payment, the original value will be returned, which can be empty.
$this->ext1 = $MockOrder['orderId'];
//Extended from paragraph 2, merchants can pass the parameters they need, and the original value will be returned after payment, which can be empty.
$this->ext2 = $MockOrder['orderTime'];
//Bank code, if payType is 00, the value can be empty; if payType is 10, the value must be filled in, please refer to the details Bank list.
$this->bankId = "";
//Repeated submission of the same order is prohibited, fill in 1 for the physical shopping cart, and 0 for virtual products. 1 means it can only be submitted once, 0 means the payment is unsuccessful. You can submit it again. Can be empty.
$this->redoFlag = "";
//The account number of the quick money partner, that is, the merchant number, can be empty.
$this->pid = "";

//The request parameters provided by Kuaiqian.
$KeyOrders = array('inputCharset','pageUrl','bgUrl','version','language','signType','merchantAcctId','payerName','payerContactType','payerContact',
'orderId','orderAmount','orderTime','productName','productNum','productId','productDesc','ext1','ext2','payType','bankId','redoFlag',' pid',);

//Determine whether the value of the request parameter provided by Kuaiqian is empty, and reorganize the non-empty parameters and values ​​into an array
foreach($KeyOrders as $key){
if(''==$this->{$key}){continue;}
$params[$key] = $this->{$key};
}
// http_build_query() generates the request string after URL-encoding
//urldecode() restores the unencoded string
//getSignMsg() PKI encryption, MD5 encryption can also be used
//MD5 encryption method strtoupper(md5(urldecode(http_build_query($params)))); This is not commonly used anymore.
//Commonly used PKI encryption
$this->signMsg = $this->getSignMsg(urldecode(http_build_query($params)));
}

//PKI encryption technology

public function getSignMsg($param){
//99bill-rsa.pem is a CA certificate of Kuaiqian
//Locally generate a KEY randomly, use this KEY to encrypt the data KEY is $ priv_key_id
$priv_key_id = openssl_get_privatekey(file_get_contents("99bill-rsa.pem", "r"));
//Use $priv_key_id to encrypt $param data.
//Calculate a signature string $param by using SHA1 hash encryption, followed by $priv_key_id private key encryption. The data itself is not encrypted.
openssl_sign($param, $signMsg, $priv_key_id, OPENSSL_ALGO_SHA1);
//Release $priv_key_id from storage
openssl_free_key($priv_key_id);
//Use base64 to encode the data
return base64_encode($signMsg);
}
}

models/BillResponse.php
BillResponse.php
Copy code The code is as follows:

class Application_Model_BillResponse
{
/*
* __construct() construction Function
* Generates 19 parameters and values. One parameter may have an empty value, and the value of $this->errCode may be empty
*/
public function __construct($response){
$KeyOrders = array('merchantAcctId','version','language','signType','payType','bankId','orderId','orderTime','orderAmount',
'dealId',' bankDealId','dealTime','payAmount','fee','ext1','ext2','payResult','errCode', 'signMsg');
foreach($KeyOrders as $key){
$this->{$key} = $response[$key];
}
}
/*
* Check the signature string
* The signature string returned by Kuaiqian It’s $this->signMsg
* Use base64 to decode the previous string
* Verify using the public key given by Kuaiqian
* Kuaiqian they ensure that the returned parameter value is not empty Using private key encryption to generate $this->signMsg
* Kuaiqian gave us the public key corresponding to the private key, and we used this public key to verify. 1 for success, 0 for failure, -1 for error.
*/
public function checkSignMsg(){
$KeyOrders = array('merchantAcctId','version','language','signType','payType','bankId','orderId', 'orderTime','orderAmount',
'dealId','bankDealId','dealTime','payAmount','fee','ext1','ext2','payResult','errCode',);
foreach($KeyOrders as $key){
if(''==$this->{$key}){continue;}
$params[$key] = $this->{ $key};
}
//$pub_key_id public key
$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 on success, 10 on failure 11
return '10'==$this->payResult;
}
public function getOrderId(){
return str_replace('XXX', '', $this->orderId) ;
}
}

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327972.htmlTechArticleThis project uses the zend framework to implement modules/default/controllers/IndexController.php IndexController.php Copy the code as follows : ?php class IndexController extends Zend_Con...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!