Using PHP to implement the Fast Money payment function (involving interfaces) _php tutorial

Source: Internet
Author: User
Tags md5 encryption sha1 hash zend framework
This project is implemented using the Zend Framework Framework
modules/default/controllers/indexcontroller.php
indexcontroller.php
Copy CodeThe code is as follows:
Class Indexcontroller extends Zend_controller_action
{
Public Function init ()
{
/* Initialize Action Controller Here */
}
Public Function indexaction ()
{

/* Mock orders
* $MockOrder is the information that is taken from the database, which contains some information about the request of the block money. Here I write dead.
*orderid The 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.
*etx_status whether the preferential, according to their own needs.
*time_create Verify that the offer is in accordance with the time required.
*ets_license Package code is like a product category, depending on your needs.
*contact_type contact type, fixed selection value 1, 2. 1 e-mail, 2 mobile phone number, according to their own needs, block money over there can be empty.
*contact_text contact information, according to Contact_type to fill, according to their own needs, block money over there can be empty.
*etsprice package prices and commodity prices, according to their own needs.
*orderprice actual price, according to your needs.
*orderamount the actual payment amount of the order, this will be added to the handling fee. Required Fields
*ordertime order time. Required Fields
*paysuccess whether the order was paid successfully. Required Fields
*buysuccess account success, according to their own needs
*paytime Order Payment success time. Required Fields
* In short, all orders related to the necessary fields
*orderid, OrderAmount, ordertime are required fields for request
*paysuccess, Paytime for response required fields
*/
$MockOrder = Array ();
$MockOrder [' orderId '] = ' 100000125 ';//order number. -Necessary
$MockOrder [' usr_idtype '] = ' 1 ';//document type, ID
$MockOrder [' usr_idcode '] = ' 371111199011111111 ';//ID number
$MockOrder [' etx_status '] = ' 0 ';//whether preferential, no
$MockOrder [' time_create '] = ' 1352338189 ';//Verify Discount time
$MockOrder [' ets_license '] = ' 1 ';//package Code and product category
$MockOrder [' contact_type '] = ' 1 ';//contact Type 1, mailbox
$MockOrder [' contact_text '] = ' x@163.com ';//contact information, email
$MockOrder [' etsprice '] = ' 30800 ';//package Price and commodity price
$MockOrder [' orderprice '] = ' 30800 ';//actual Price
$MockOrder [' orderamount '] = ' 31100 ';//order actual payment price, plus handling fee. -Necessary
$MockOrder [' ordertime '] = ' 1352338199 ';//order generation time. -Necessary
$MockOrder [' paysuccess '] = ' 0 ';//whether the order was paid successfully. -Necessary
$MockOrder [' buysuccess '] = ' 0 ';//The account is generated successfully
$MockOrder [' paytime '] = ' 0 ';//order payment time. -Necessary

Billrequest is the fast money. Some of the required parameters
$this->view->billrequest = new Application_model_billrequest ($MockOrder);
Zend_debug::d UMP ($this->view->billrequest); exit;
}

Bgurl Address point here
Public Function receiveaction ()
{
Receive Database design
/* Using $mockreceive array emulation
* $MockReceive = array ();
* $MockReceive [' ID '] primary key;
* $MockReceive [' orderId '] merchant order number;
* $MockReceive [' receivetime '] acceptance time;
* $MockReceive [' queryString ']http_build_encode ($_request);
* $MockReceive [' dealid '] fast money trading number;
* $MockReceive [' bankdealid '] bank transaction number;
* $MockReceive [' payresult '] processing result 10: Payment success; 11: Payment failure;
* $MockReceive [' dealtime '] fast money trading hours;
* $MockReceive [' payamount '] order actual payment amount;
* $MockReceive [' fee '] fees;
* $MockReceive [' errcode '] error code;
*/


/*$_request is the data returned from the fast money.
* Merchantacctid RMB account number, consistent with the block account when submitting the order.
* Version gateway, fixed value: v2.0, consistent with the gateway version number when the order is submitted.
* Language page display language type, 1 Chinese display, with the submission of the Order of the page display language type consistent
* Signtype signature Type, 4PKI signature, consistent with the type of signature when the order is submitted
* PayType Payment method, 00 all, consistent with the payment method when the order is submitted
* BankID Bank Code
* OrderID Merchant Order number, consistent with merchant order number when submitting 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 Trading Number
* Bankdealid Bank transaction number
* DealTime Fast Money trading hours
* Payamount Order Actual payment amount
* Fee Fee
* Ext1 extension Field 1, consistent with the extended field 1 when submitting an order
* ext2 extension field 2, consistent with the extended field 2 when submitting an order
* Payresult Processing result 10: Payment success; 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, prevent bug exploits, etc.
if ($BillResponse->checksignmsg) {
Determine if the order payment is successful
if ($BillResponse->issuccess) {
Return to fast money, fast money will follow the RedirectURL address to the new page, here is the success page
Return " 1 http://99bill/default/index/sucess "; exit;
}else{
Return to fast money, fast money will follow the RedirectURL address to the new page, this is the failure page
Return " 1 Http://99bill/default/index/fail "; exit;
}
}
Return to fast money, fast money will follow the RedirectURL address to the new page, this is the failure page
Return " 1 Http://99bill/default/index/fail "; exit;
}

RedirectURL Address
Success
Public Function Success ()
{

}

Failed
Public function fail ()
{

}
}

Modules/default/views/scripts/index/index.phtml
Https://www.99bill.com/gateway/recvMerchantInfoAction.htm
Copy CodeThe code is as follows:
billrequest;? >





models/billrequest.php
billrequest.php
Copy CodeThe code is as follows:
Class Application_model_billrequest
{
Public function __construct ($MockOrder) {
/*
* RMB gateway account.
* The first way: This account is 11-bit RMB gateway merchant number +01, this parameter is required. 01 corresponds to ICBC.
* Second way: This account is a 16-bit RMB gateway merchant
*/
$this->merchantacctid = "1001011111101";
The server receives the backend address of the payment result, this parameter must be filled in, absolute path//cannot be empty.
$this->bgurl = "http://99bill/default/index/receive";
Merchant Order number, the following time to define the order number, the merchant can be defined according to their own order number rules to define the value//cannot be empty.
$this->orderid = ' tolpc '. sprintf ("%09d", $MockOrder [' orderId ']);
The amount of the order, the amount of "cent" for the unit, the merchant test to 1 points test can, do not use a large amount of test, 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 ']);
The name of the payer can be empty.
$this->payername= "";
Payer contact type, 1 for e-mail, 2 for mobile contact. can be empty.
$this->payercontacttype = "";
Payer contact information, and payercontacttype settings corresponding to Payercontacttype 1, then fill in the email address, payercontacttype 2, then fill in the mobile phone number. can be empty.
$this->payercontact = "";
Product name, can be empty.
$this->productname= "TOLPC";
The number of items can be empty.
$this->productnum = "1";
Commodity code, can be empty.
$this->productid = $MockOrder [' Ets_license '];
Product description, can be empty.
$this->productdesc = "";
The payment method, generally 00, represents all payment methods. If the bank is directly connected to the merchant, the value is 10, required//cannot be empty
$this->paytype = "00";
Encoding method, 1 for UTF-8; 2 stands for GBK; 3 means GB2312 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 for Chinese display, 2 for English display. Default is 1, this parameter is required//cannot be empty
$this->language = "1";
The signature type, which is 4, represents the PKI encryption method, which is required//cannot be empty
$this->signtype = "4";
The address of the page that receives the payment result, which is generally set to empty.
$this->pageurl = "";
Extension Field 1, the merchant can pass their own required parameters, pay the fast money will return the original value, can be empty.
$this->ext1 = $MockOrder [' orderId '];
Extended from 2, merchants can pass their own required parameters, pay the fast money will return the original value, can be empty.
$this->ext2 = $MockOrder [' Ordertime '];
Bank code, if PayType is 00, the value can be empty, if PayType is 10, this value must be filled in, please refer to the Bank list for details.
$this->bankid = "";
The same order is forbidden to repeat the mark, the physical shopping cart filled 1, virtual products with 0. 1 delegates can only be submitted once, and 0 will be resubmitted in the event of unsuccessful payment. can be empty.
$this->redoflag = "";
Quick Money Partner's account number, which is the merchant number, can be empty.
$this->pid = "";

Quick Money provides the request parameter.
$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 if the value of the request parameter provided by the fast money is empty, and re-assemble the array with non-null parameters and values
foreach ($KeyOrders as $key) {
if ("= = $this->{$key}) {continue;}
$params [$key] = $this->{$key};
}
Http_build_query () Request string after generating Url-encode
UrlDecode () to restore an encoded string
GETSIGNMSG () PKI encryption, or using MD5 encryption
MD5 Encryption Mode Strtoupper (MD5 (UrlDecode (Http_build_query ($params))); This is not used.
Common 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 for fast Money
Local randomly generated a key, using 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.
Computes 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 memory
Openssl_free_key ($priv _key_id);
Encode data using Base64
Return Base64_encode ($SIGNMSG);
}
}

models/billresponse.php
billresponse.php
Copy CodeThe code is as follows:
Class Application_model_billresponse
{
/*
* __CONSTRUCT () constructor
* Generate 19 parameters and values, there may be a value of one parameter is empty, $this->errcode value 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 Signature string
* Quick Money Returns the signature string is $this->signmsg
* Use base64 to decode the preceding string
* Verify the Public key authentication using fast Money
* Fast money over there they have returned the parameter value is not empty using private key encryption to generate the $this->signmsg
* The fast money gives us the public key corresponding to the private key, which we use to verify the public key. 1 successes, 0 failures,-1 errors.
*/
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 Success 10, 11 failure
Return '->payresult; ' = = $this
}
Public Function Getorderid () {
Return Str_replace (' XXX ', ', $this->orderid);
}
}

Need a public key and a private key, this is not a pair of
It's all half.
99bill-rsa.cer
99bill-rsa.pem

http://www.bkjia.com/PHPjc/327972.html www.bkjia.com true http://www.bkjia.com/PHPjc/327972.html techarticle This project uses the Zend Framework framework to implement the modules/default/controllers/indexcontroller.php indexcontroller.php copy code code as follows:? PHP class Indexcontroller extends Zend_con ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.