Use php to implement the quick money payment function _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags sha1 hash
Use php to implement the fast payment function. The following code copies modulesdefaultcontrollersIndexController. phpIndexController. php implemented by zendframework in this project :? PhpclassIndexControllerextendsZend_Con This project is implemented using the zend framework
Modules/default/controllers/IndexController. php
IndexController. php

The code is as follows:
Class IndexController extends Zend_Controller_Action
{
Public function init ()
{
/* Initialize action controller here */
}
Public function indexAction ()
{

/* Simulate an order
* $ MockOrder refers to the information obtained from the database, which contains information about the Request for a certain amount of money. Here I am writing dead.
* OrderId: the order number, which is the primary key (unique) of the database table ). // Required fields
* Usr_idtype: specifies the credential type.
* Usr_idcode ID, as required.
* Whether the etx_status is favorable depends on your needs.
* Time_create is used to verify whether the discount time is met.
* The ets_license package code is similar to the product category and can be customized as needed.
* Contact_type: contact type. The value is invariably set to 1 or 2. 1 email, 2 mobile phone number, according to your needs, the block money can be blank.
* Contact_text contact information, which can be filled in according to contact_type. The Block price can be blank as needed.
* The etsPrice package price and product price are based on your needs.
* The actual price of orderPrice depends on your needs.
* The actual payment amount of the orderAmount order, which requires a handling fee. // Required fields
* OrderTime order time. // Required fields
* Whether the payment for the paySuccess order is successful. // Required fields
* Whether the buySuccess account is successfully created depends on your needs.
* PayTime: the time when the order is successfully paid. // Required fields
* In short, all fields related to orders are required.
* OrderId, orderAmount, and orderTime are required fields for Request.
* PaySuccess and payTime are the fields required for Response.
*/
$ MockOrder = array ();
$ MockOrder ['orderid'] = '000000'; // the order number. -- Required
$ MockOrder ['usr _ idtype'] = '1'; // credential type, ID card
$ MockOrder ['usr _ idcode'] = '000000'; // ID card number
$ MockOrder ['etx _ status'] = '0'; // whether the offer is made, no
$ MockOrder ['time _ create'] = '000000'; // verify whether the discount is made
$ MockOrder ['ets _ license '] = '1'; // package code and product category
$ MockOrder ['contact _ type'] = '1'; // contact method type 1, email
$ MockOrder ['contact _ text'] = 'x @ 163.com '; // contact information, email
$ MockOrder ['etspric'] = '000000'; // package price and product price
$ MockOrder ['orderprice'] = '000000'; // actual price
$ MockOrder ['orderamount '] = '000000'; // the actual payment price of the order, plus the handling fee. -- Required
$ MockOrder ['ordertime'] = '000000'; // The order generation time. -- Required
$ MockOrder ['paysuccess'] = '0'; // whether the order is successfully paid. -- Required
$ MockOrder ['buckets'] = '0'; // whether the account is successfully generated
$ MockOrder ['paytime'] = '0'; // The order payment time. -- Required

// BillRequest is the parameter required by quickmoney.
$ This-> view-> BillRequest = new Application_Model_BillRequest ($ MockOrder );
Zend_Debug: dump ($ this-> view-> BillRequest); exit;
}

// BgUrl address points to here
Public function receiveAction ()
{
// Receive database design
/* Use the $ 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'] fast money transaction time;
* $ MockReceive ['payamount'] actual payment amount of the order;
* $ MockReceive ['signature'] fee;
* $ MockReceive ['errorcode'] error code;
*/


/* $ _ REQUEST is the data returned by quickmoney.
* The merchantAcctId account is the same as the block money account used to submit the order.
* Version: The Gateway version with a fixed value: v2.0, which is consistent with the Gateway version number when the order is submitted.
* The language web page displays the language type, which is displayed in Chinese and consistent with the language type displayed on the web page when the order is submitted.
* SignType: signType, 4 PKI signature. it must be consistent with the signature type when the order is submitted.
* PayType payment method, 00 all, consistent with the payment method when submitting the order
* BankId banking code
* OrderId: The Merchant's order number, which is consistent with the merchant's order number when the order is submitted
* OrderTime: the time when the merchant's order was submitted, consistent with the time when the merchant's order was submitted
* OrderAmount refers to the merchant order amount, which is consistent with the merchant order amount when the order is submitted.
* DealId: fast money transaction number
* BankDealId: bank transaction No.
* DealTime: fast money transaction time
* Actual payment amount of the payAmount order
* Renewal fee
* Ext1 extension field 1, which is consistent with the extension field 1 when the order is submitted
* Ext2 extension field 2, which is consistent with the extension field 2 when the order is submitted
* PayResult processing result 10: payment successful; 11: payment failed
* ErrCode error code, which can be empty
* SignMsg signature string
*/
$ BillResponse = new Application_Model_BillResponse ($ _ REQUEST );
// $ BillResponse-> checkSignMsg verifies that the signature string is correct and prevents bugs.
If ($ BillResponse-> checkSignMsg ){
// Determine whether the order payment is successful
If ($ BillResponse-> isSuccess ){
// Return to quickmoney. the quickmoney will jump to the new page according to the redirecturl address. this is the success page.
Return" 1 Http: // 99 bill/default/index/sucess "; Exit;
} Else {
// Return to quickmoney. the quickmoney will jump to the new page according to the redirecturl address. this is a failure page.
Return" 1 Http: // 99 bill/default/index/fail "; Exit;
}
}
// Return to quickmoney. the quickmoney will jump to the new page according to the redirecturl address. this is a failure page.
Return" 1 Http: // 99 bill/default/index/fail "; Exit;
}

// Redirecturl
// Succeeded
Public function success ()
{

}

// Failed
Public function fail ()
{

}
}


Modules/default/views/scripts/index. phtml
Https://www.99bill.com/gateway/recvMerchantInfoAction.htm

The code is as follows:
BillRequest;?>




Script
Document. getElementById ('qpa'). click ();
Script


Models/BillRequest. php
BillRequest. php

The code is as follows:
Class Application_Model_BillRequest
{
Public function _ construct ($ MockOrder ){
/*
* RMB Gateway account.
* Method 1: The account is an 11-digit RMB gateway merchant number + 01. this parameter is required. 01 corresponds to the Industrial and Commercial Bank of China.
* Method 2: The account is a 16-digit RMB gateway merchant.
*/
$ This-> merchantAcctId = "1001011111101 ";
// The backend address of the payment result received by the server. this parameter must be set. the absolute path // cannot be blank.
$ This-> bgUrl = "http: // 99 bill/default/index/receive ";
// The Merchant's order number. the following time is used to define the order number. the merchant can define this value according to the rule of his/her order number. // it cannot be blank.
$ This-> orderId = 'tolpc'. sprintf ("% 09d", $ MockOrder ['orderid']);
// Order amount, in the unit of "points", The Merchant test can be tested with 1 point. do not test with a large amount. this parameter is required // cannot be blank
$ This-> orderAmount = $ MockOrder ['orderamount '];
// Order submission time, in the format of yyyyMMddHHmmss, for example, 20071117020101 // cannot be blank.
$ This-> orderTime = date ("YmdHis", $ MockOrder ['ordertime']);
// Name of the payer, which can be blank.
$ This-> payerName = "";
// Contact type of the payer; 1 indicates the email method; 2 indicates the mobile phone contact method. It can be null.
$ This-> payerContactType = "";
// Contact information of the payer, which corresponds to the payerContactType setting. if payerContactType is set to 1, enter the email address. if payerContactType is set to 2, enter the mobile phone number. It can be null.
$ This-> payerContact = "";
// Item name, which can be blank.
$ This-> productName = "TOLPC ";
// Number of items, which can be empty.
$ This-> productNum = "1 ";
// Product code, which can be empty.
$ This-> productId = $ MockOrder ['ets _ license '];
// Product description, which can be empty.
$ This-> productDesc = "";
// Payment method. generally, 00 indicates all payment methods. If a bank directly connects to a merchant, the value is 10. this parameter is required. // The value cannot be blank.
$ This-> payType = "00 ";
// Encoding method, 1 represents the UTF-8; 2 represents GBK; 3 represents the default GB2312 is 1, this parameter is required // cannot be blank
$ This-> inputCharset = "1 ";
// Gateway version, fixed value: v2.0, this parameter is required // cannot be blank
$ This-> version = "v2.0 ";
// Language type. 1 indicates Chinese and 2 indicates English. The default value is 1. this parameter is required. // it cannot be blank.
$ This-> language = "1 ";
// Signature type. The value is 4, indicating the PKI encryption method. this parameter is required. // cannot be blank.
$ This-> signType = "4 ";
// The page address for receiving the payment result. this parameter is generally set to null.
$ This-> pageUrl = "";
// Extended field 1. the merchant can pass the required parameters. the original value is returned after the payment is completed and can be blank.
$ This-> ext1 = $ MockOrder ['orderid'];
// Extended from segment 2. the merchant can pass the required parameters. the original value is returned after the payment is completed, which can be blank.
$ This-> ext2 = $ MockOrder ['ordertime'];
// Bank code. if payType is 00, this value can be blank. if payType is 10, this value must be filled in. for details, see the bank list.
$ This-> bankId = "";
// You cannot submit a duplicate logo for the same order. enter 1 in the physical shopping cart and 0 in the virtual product. 1 indicates that only one request can be submitted. 0 indicates that the request can be submitted again if the payment fails. Can be empty.
$ This-> redoFlag = "";
// The account number of the quick money partner, that is, the merchant ID, which can be blank.
$ This-> pid = "";

// The request parameter provided by quickmoney.
$ KeyOrders = array ('inputcharset', 'pageurl', 'bgurl', 'version', 'language', 'signtype', 'merchantacctid', 'payername ', 'payercontacttype', 'payercontact ',
'Orderid', 'orderamount ', 'ordertime', 'productname', 'productnum', 'produtid', 'productdesc', 'ext1', 'ext2', 'paytype ', 'bankid', 'redoflag', 'pid ',);

// Determine whether the value of the request parameter provided by quick money is null, and rebuild 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-encode
// Urldecode () restores unencoded strings
// GetSignMsg () PKI encryption, or MD5 encryption
// MD5 encryption method strtoupper (md5 (urldecode (http_build_query ($ params); this is not commonly 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 quick money
// A random local KEY is generated. The KEY encrypted with this 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 the SHA1 hash encryption, followed by $ priv_key_id private key encryption. Data itself is not encrypted.
Openssl_sign ($ param, $ signMsg, $ priv_key_id, OPENSSL_ALGO_SHA1 );
// Release $ priv_key_id from the memory
Openssl_free_key ($ priv_key_id );
// Encode the data using base64
Return base64_encode ($ signMsg );
}
}


Models/BillResponse. php
BillResponse. php

The code is as follows:
Class Application_Model_BillResponse
{
/*
* _ Construct () constructor
* 19 parameters and values are generated. The value of one parameter may be null. The value of $ this-> errCode may be null.
*/
Public function _ construct ($ response ){
$ KeyOrders = array ('chantacctid', 'version', 'language', 'signtype', 'paytype', 'bankid', 'orderid', 'ordertime ', 'orderamount ',
'Dealid', 'bankdealid', 'dealtime', 'payamount', 'weight', 'text1', 'ext2', 'payresult', 'errorcode ', 'signmsg ');
Foreach ($ KeyOrders as $ key ){
$ This-> {$ key} = $ response [$ key];
}
}
/*
* Check the signature string
* The signature string returned by quick money is $ this-> signMsg
* Use base64 to decode the preceding string.
* Verification using the public key provided by quickmoney
* For quickmoney, they used private key encryption to generate $ this-> signMsg if the returned parameter value is not empty.
* Quickmoney gives us the public key corresponding to the private key. We use this public key for verification. 1 successful, 0 failed,-1 error.
*/
Public function checkSignMsg (){
$ KeyOrders = array ('chantacctid', 'version', 'language', 'signtype', 'paytype', 'bankid', 'orderid', 'ordertime ', 'orderamount ',
'Dealid', 'bankdealid', 'dealtime', 'payamount', 'weight', 'ext1', 'ext2', 'payresult', 'errorcode ',);
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-> 10 when the payResult is successful and 11 when the payment fails
Return '10' = $ this-> payResult;
}
Public function getOrderId (){
Return str_replace ('XXX', '', $ this-> orderId );
}
}


A public key and a private key are required. this is not a pair.
Half
99bill-rsa.cer
99bill-rsa.pem

The code of modules/default/controllers/IndexController. php IndexController. php implemented by the framework of the runtime framework is as follows :? Php class IndexController extends Zend_Con...

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.