Using php to realize the fast money payment function of involves the interface
- 2020-07-21 07:02:52
- OfStack
This project is implemented by zend framework framework
modules/default/controllers/IndexController.php
IndexController.php
modules/default/views/scripts/index/index.phtml
https://www.99bill.com/gateway/recvMerchantInfoAction.htm
models/BillRequest.php
BillRequest.php
models/BillResponse.php
BillResponse.php
You need a public key and a private key. This is not a 1 pair
Is 1 and a half
99bill-rsa.cer
99bill-rsa.pem
modules/default/controllers/IndexController.php
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
/* Simulation of the order
*$MockOrder The information taken from the database that it contains 1 Some dollars Request The information. I wrote it dead here.
*orderId Order number, the primary key of the database table (only 1 ). // The necessary fields
*usr_idtype Certificate type, according to oneself need.
*usr_idcode Id number, according to their needs.
*etx_status Whether discount, according to their own needs.
*time_create Verify whether the preferential time according to their own needs.
*ets_license Package code is like the category of goods, according to their own needs.
*contact_type Contact type, fixed selection value 1,2 . 1 E-mail, 2 Cell phone number, according to their needs, dollars can be empty there.
*contact_text Contact information, according to contact_type To fill in, according to their own needs, the yuan side can be blank.
*etsPrice Package prices and commodity prices, according to their own needs.
*orderPrice Actual price, according to their own needs.
*orderAmount The actual amount to be paid for the order is subject to service charge. // The necessary fields
*orderTime Order time. // The necessary fields
*paySuccess Whether the order has been paid successfully. // The necessary fields
*buySuccess Whether the account is successful, according to their own needs
*payTime Successful order payment time. // The necessary fields
* Anything related to an order is a necessary field
*orderId , orderAmount , orderTime for Request The required fields
*paySuccess , payTime for Response The required fields
*/
$MockOrder = array();
$MockOrder['orderId'] = '100000125';// The order number. -- necessary
$MockOrder['usr_idtype'] = '1';// Certificate type, ID card
$MockOrder['usr_idcode'] = '371111199011111111';// Id number
$MockOrder['etx_status'] = '0';// Is it a discount? No
$MockOrder['time_create'] = '1352338189';// Verify the preferential time
$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 address
$MockOrder['etsPrice'] = '30800';// Package price and commodity price
$MockOrder['orderPrice'] = '30800';// The actual price
$MockOrder['orderAmount'] = '31100';// The actual price of the order is paid, plus service charge. -- necessary
$MockOrder['orderTime'] = '1352338199';// Order generation time. -- necessary
$MockOrder['paySuccess'] = '0';// Whether the order has been paid successfully. -- necessary
$MockOrder['buySuccess'] = '0';// Is the account generated successfully
$MockOrder['payTime'] = '0';// Order payment time. -- necessary
//BillRequest That's what you need on the fast money side 1 Some parameters
$this->view->BillRequest = new Application_Model_BillRequest($MockOrder);
Zend_Debug::dump($this->view->BillRequest);exit;
}
//bgUrl The address goes here
public function receiveAction()
{
//receive Database design
/* with $MockReceive An array of simulated
* $MockReceive = array();
* $MockReceive['id'] A primary key ;
* $MockReceive['orderId'] Merchant Order Number ;
* $MockReceive['receiveTime'] Accept time ;
* $MockReceive['queryString']http_build_encode($_REQUEST);
* $MockReceive['dealId'] Quick money trading number ;
* $MockReceive['bankDealId'] Bank transaction number ;
* $MockReceive['payResult'] The processing results 10 : Successful payment; 11 : Payment failure ;
* $MockReceive['dealTime'] Quick money trading hours ;
* $MockReceive['payAmount'] Actual amount paid for the order ;
* $MockReceive['fee'] cost ;
* $MockReceive['errCode'] The error code ;
*/
/*$_REQUEST It's the data that comes back from the fast money side
* merchantAcctId The RMB account is maintained with the RMB account at the time of order submission 1 Cause.
* version Gateway version, fixed value: v2.0 , with the gateway version number at the time the order was submitted 1 Cause.
* language Type of web page display language, 1 The Chinese language display is maintained with the web page display language when the order is submitted 1 to
* signType Signature type, 4PKI Signature, maintained with the signature type at the time the order was submitted 1 to
* payType Payment method, 00 All, with the payment method when the order is submitted 1 to
* bankId The bank code
* orderId The Merchant order number shall be maintained with the Merchant order number at the time the order was submitted 1 to
* orderTime The time for merchant order submission shall be maintained with the time for merchant order submission at the time of order submission 1 to
* orderAmount The merchant order amount shall be maintained with the merchant order amount when the order is submitted 1 Cause.
* dealId Quick money trading number
* bankDealId Bank transaction number
* dealTime Quick money trading hours
* payAmount Actual amount paid for the order
* fee cost
* ext1 Extension field 1 , and the extended field when the order is submitted 1 keep 1 to
* ext2 Extension field 2 , and the extended field when the order is submitted 2 keep 1 to
* payResult The processing results 10 : Successful payment; 11 : Payment failure
* errCode Error code, can be empty
* signMsg Signature string
*/
$BillResponse = new Application_Model_BillResponse($_REQUEST);
//$BillResponse->checkSignMsg Verify that the signature string is correct bug Holes etc.
if($BillResponse->checkSignMsg){
// Determine if the order payment was successful
if($BillResponse->isSuccess){
// Return to fast money, fast money will follow redirecturl Go to the new page. Here is the success page
return "<result>1</result><redirecturl>http://99bill/default/index/sucess</redirecturl>";exit;
}else{
// Return to fast money, fast money will follow redirecturl The address jumps to the new page. This is the failed page
return "<result>1</result><redirecturl>http://99bill/default/index/fail</redirecturl>";exit;
}
}
// Return to fast money, fast money will follow redirecturl The address jumps to the new page. This is the failed page
return "<result>1</result><redirecturl>http://99bill/default/index/fail</redirecturl>";exit;
}
//redirecturl address
// successful
public function success()
{
}
// failure
public function fail()
{
}
}
modules/default/views/scripts/index/index.phtml
https://www.99bill.com/gateway/recvMerchantInfoAction.htm
<?php $BillRequest = (array)$this->BillRequest;?>
<div 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=" Submit to express money " id="kqPay">
</form>
</div>
<script>
document.getElementById('kqPay').click();
</script>
models/BillRequest.php
BillRequest.php
<?php
class Application_Model_BillRequest
{
public function __construct($MockOrder){
/*
* RMB gateway account.
* The first 1 Way: The account is 11 RMB Gateway Merchant No +01, This parameter is required. 01 Corresponding to industrial and Commercial Bank.
* The first 2 Way: The account is 16 RMB gateway merchant
*/
$this->merchantAcctId = "1001011111101";
// Server to receive payment results background address, this parameter must be filled in, absolute path // Can't be empty.
$this->bgUrl = "http://99bill/default/index/receive";
// Merchant order number. The following time is used to define the order number. Merchant can define the value according to the definition rules of its order number // Can't be empty.
$this->orderId = 'TOLPC'.sprintf("%09d", $MockOrder['orderId']);
// Order amount, the amount of "points" as the unit, merchant test to 1 Do not test for large sums of money. This parameter must be filled in // Can't be empty
$this->orderAmount =$MockOrder['orderAmount'];
// Order submission time, format: yyyyMMddHHmmss , such as: 20071117020101// Can't be empty.
$this->orderTime = date("YmdHis", $MockOrder['orderTime']);
// Name of payer , It could be empty.
$this->payerName= "";
// Contact type of payer, 1 Means of E-mail; 2 S for mobile phone contact information. It could be empty.
$this->payerContactType = "";
// The payer's contact information, and payerContactType Set the corresponding, payerContactType for 1 , then fill in the email address; payerContactType for 2 , then fill in the mobile phone number. It could be empty.
$this->payerContact = "";
// Item name, can be empty.
$this->productName= "TOLPC";
// The quantity of goods can be empty.
$this->productNum = "1";
// Item code, can be empty.
$this->productId = $MockOrder['ets_license'];
// Product description, can be empty.
$this->productDesc = "";
// Payment method, 1 As for the 00 , which represents all modes of payment. If the bank is directly connected to the merchant, the value is 10 Will fill // Can't be empty
$this->payType = "00";
// Encoding mode, 1 On behalf of UTF-8; 2 On behalf of GBK; 3 On behalf of GB2312 The default is 1, This parameter is required // Can't be empty
$this->inputCharset = "1";
// Gateway version, fixed value: v2.0, This parameter is required // Can't be empty
$this->version = "v2.0";
// The language types, 1 Stands for Chinese display, 2 Represents the English display. The default is 1, This parameter is required // Can't be empty
$this->language = "1";
// Signature type , The value is 4 On behalf of PKI encryption , This parameter is required // Can't be empty
$this->signType = "4";
// The address of the page that receives the payment result, this parameter 1 Generally empty can be.
$this->pageUrl = "";
// Extension field 1 , the merchant can pass the parameters he needs, and the original value will be returned after the payment, which can be empty.
$this->ext1 = $MockOrder['orderId'];
// Extended since the period of 2 , the merchant can pass the parameters he needs, and the original value will be returned after the payment, which can be empty.
$this->ext2 = $MockOrder['orderTime'];
// Bank code, if payType for 00 , the value can be empty; if payType for 10 , the value must be filled in, please refer to the bank list for details.
$this->bankId = "";
// with 1 Orders do not duplicate the submission mark, filling the physical shopping cart 1 , for virtual products 0 . 1 The representative can only submit 1 Time, 0 The representative may resubmit if the payment is unsuccessful. Can be null.
$this->redoFlag = "";
// The account number of the fast money partner, namely the merchant number, can be blank.
$this->pid = "";
// Quick money request Parameters.
$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',);
// Judge what fast money offers request If the value of the parameter is null, the non-null parameters and values are rebuilt into an array
foreach($KeyOrders as $key){
if(''==$this->{$key}){continue;}
$params[$key] = $this->{$key};
}
//http_build_query() generate URL-encode The request string that follows
//urldecode() Restores an unencoded string
//getSignMsg() PKI Encryption can also be used MD5 encryption
//MD5 encryption strtoupper(md5(urldecode(http_build_query($params)))); This one is not in common use.
// The commonly used PKI encryption
$this->signMsg = $this->getSignMsg(urldecode(http_build_query($params)));
}
//PKI Encryption technology
public function getSignMsg($param){
//99bill-rsa.pem Is the fast 1 a CA certificate
// Local random generation 1 a KEY, With this KEY Encrypt the data KEY for $priv_key_id
$priv_key_id = openssl_get_privatekey(file_get_contents("99bill-rsa.pem", "r"));
// with $priv_key_id to $param Data encryption.
// To calculate 1 Signature string $param Through the use of SHA1 Hash encryption, and then $priv_key_id Private key encryption. The data itself is not encrypted.
openssl_sign($param, $signMsg, $priv_key_id, OPENSSL_ALGO_SHA1);
// To release from memory $priv_key_id
openssl_free_key($priv_key_id);
// use base64 Encode the data
return base64_encode($signMsg);
}
}
models/BillResponse.php
BillResponse.php
<?php
class Application_Model_BillResponse
{
/*
* __construct() The constructor
* generate 19 Number of parameters and values. There may be 1 The values of the parameters are null, $this->errCode The value of 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 that fast money returns is $this->signMsg
* use base64 Decode the preceding string
* Verify using the public key verification given by the express money
* The fast money side they return the parameter value is not null using private key encryption generated $this->signMsg
* The quick money gives us the public key corresponding to the private key, which we use to verify. 1 Success, 0 Failure, -1 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 The 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 success 10 , failure 11
return '10'==$this->payResult;
}
public function getOrderId(){
return str_replace('XXX', '', $this->orderId);
}
}
You need a public key and a private key. This is not a 1 pair
Is 1 and a half
99bill-rsa.cer
99bill-rsa.pem