This article mainly introduces dedecms's integration of relevant information on the payment interface of Tenpay. if you need it, you can refer to dedecms's travel website and ask the customer to pay for it, I searched for the Internet, either to buy or to ask for money. I only wrote it myself.
Code:
The code is as follows:
<? Php
If (! Defined ('deinc') exit ('request Error! ');
/**
* Caifu Tong interface
*/
Class tenpay
{
Var $ dsql;
Var $ mid;
Var $ reqURL_onLine = "http://www.tenpay.com ";
Var $ return_url = '/plus/carbuyaction. php? Dopost = return '; // return address
/**
* Constructor
*
* @ Access public
* @ Param
*
* @ Return void
*/
Function tenpay ()
{
Global $ dsql;
$ This-> dsql = $ dsql;
}
Function _ construct ()
{
$ This-> tenpay ();
}
/**
* Set the interface to send the address
*
* Example: $ this-> SetReturnUrl ($ cfg_basehost. "/tuangou/control/index. php? Ac = pay & orderid = ". $ p2_Order)
*
* @ Param string $ returnurl will send the address
* @ Return void
*/
Function SetReturnUrl ($ returnurl = '')
{
If (! Empty ($ returnurl ))
{
$ This-> return_url = $ returnurl;
}
}
/**
* Generate payment code
* @ Param array $ order information
* @ Param array $ payment method information
*/
Function GetCode ($ order, $ payment)
{
Global $ pai_basehost, $ pai_cmspath;
// Process the level-2 Directory
If (! Empty ($ pai_cmspath) $ pai_basehost = $ pai_basehost. '/'. $ pai_cmspath;
$ Partner = $ payment ['tenpay _ account'];
$ Out_trade_no = $ order ['out _ trade_no '];
$ Total_val = floatval ($ order ['price']) * 100;
$ Body = $ order ['out _ trade_no '];
$ Attach = '';
$ Bank_type = 'default ';
/* Transaction Type: 2. virtual transaction, 1. Physical transaction */
$ Trans_type = 1;
$ Trade_mode = empty ($ payment ['tenpay _ pay_method '])? '1': $ payment ['tenpay _ pay_method '];
$ Parameter = array (
'Partner '=> $ partner,
'Out _ trade_no '=> $ out_trade_no, // order number
'Total _ total' => $ total_amount, // total amount
'Your y _ url' => $ your _basehost. $ this-> return_url. "& code =". $ payment ['code'], // return address
'Return _ url' => $ response _basehost. $ this-> return_url. "& code =". $ payment ['code'], // reminder address
'Body' => $ body, // transaction description
'Bank _ type' => $ bank_type, // The transaction type is default.
// User ip address
'Spbill _ create_ip' => $ _ SERVER ['remote _ ADDR '], // transaction ip address
'Currency _ type' => '1', // Currency: 1 RMB
'Subobject' => $ body, // item name
// Optional system parameters
'Sign _ type' => 'md5', // encryption method
'Service _ version' => '1. 0', // Default API version 1.0
'Input _ charset' => 'utf-8', // System encoding 'gbk'
'Sign _ key_index '=> '1', // key number
// Optional business parameters
'Attach '=> $ attach, // if the attached data is left blank by default
'Product _ quota' => '', // item fee
'Transport _ region' => '0', // logistics fee
'Time _ start' => date ("YmdHis"), // date ("YmdHis ")
'Time _ expire '=> '', // order expiration time
'Buyer _ id' => '', // buyer Caifu account
'Goods _ tag' => '', // product tag
'Trade _ mode' => $ trade_mode, // transaction mode (1. real-time account arrival mode, 2. intermediary guarantee mode, 3. backend selection (the seller enters the payment center list ))
'Transport _ desc' => '', // logistics description
'Trans _ type' => $ trans_type, // transaction type
'Agentid' => '', // platform ID
'Agent _ type' => '', // proxy mode (0. no proxy, 1. card sale mode, 2. online shop mode)
'Seller _ id' => ''// seller's merchant id
);
Ksort ($ parameter );
Reset ($ parameter );
$ Param = '';
$ Sign = '';
Foreach ($ parameter AS $ key => $ val)
{
$ Param. = "$ key =". urlencode ($ val )."&";
If (""! = $ Val & "sign "! = $ Key ){
$ Sign. = "$ key = $ val &";
}
}
$ Param = substr ($ param, 0,-1 );
$ Sign. = "key =". $ payment ['tenpay _ key'];
$ Sign = strtolower (md5 ($ sign ));
$ Button ='
Https://gw.tenpay.com/gateway/pay.htm? '. $ Param.' & sign = '. $ sign.' ">Pay now
';
Return $ button;
/* Clear the shopping cart */
Require_once DEDEINC. '/shopcar. class. php ';
$ Cart = new MemberShops ();
$ Cart-> clearItem ();
$ Cart-> MakeOrders ();
Return $ button;
}
/**
* Response Operations
*/
Function respond ()
{
/* Introduce the configuration file */
$ Code = preg_replace ("# [^ 0-9a-z-] # I", "", $ _ GET ['code']);
Require_once DEDEDATA. '/payment/'. $ code. '. php ';
$ Attach = $ _ GET ['Attach '];
$ Trade_state = $ _ GET ['trade _ state'];
$ Total_detail = $ _ GET ['total _ region'];
$ Out_trade_no = trim ($ _ GET ['out _ trade_no ']);
If (preg_match ("/S-P [0-9] + RN [0-9]/", $ order_sn )){
// Check whether the payment amount is consistent
$ Row = $ this-> dsql-> GetOne ("SELECT * FROM # @__ shops_orders WHERE oid = '{$ order_sn }'");
If ($ row ['pricecount']! = $ _ GET ['total _ region'])
{
Return $ msg = "payment failed. the payment amount does not match the total price of the product! ";
}
$ This-> mid = $ row ['userid'];
/* Check whether the digital signature is correct */
Ksort ($ _ GET );
Reset ($ _ GET );
$ Sign = '';
Foreach ($ _ get as $ key => $ val)
{
If (""! = $ Val & "sign "! = $ Key & $ key! = 'Code '){
$ Sign. = "$ key = $ val &";
}
}
$ Sign. = "key =". $ payment ['tenpay _ key'];
If (strtolower (md5 ($ sign) = strtolower ($ _ GET ['sign'])
{
If ($ trade_state = 0)
{
/* Change order status */
If ($ this-> success_db ($ out_trade_no ))
Return $ msg = "payment successful!
Back to homepage member center ";
Else return $ msg = "payment failed!
Back to homepage member center ";
}
}
Else {
// Return $ msg = "payment failed!
Back to homepage member center ";
}
}
/* Process item transactions */
Function success_db ($ order_sn)
{
$ Time = time ();
$ Mid = $ this-> mid;
// Obtain order information and check order validity
$ Row = $ this-> dsql-> GetOne ("SELECT state, priceCount FROM #@__ shops_orders WHERE oid = '$ order_sn '");
If ($ row ['state']> 0)
{
Return TRUE;
}
/* Change order status _ payment successful */
$ SQL = "update' #@__ shops_orders 'set 'state' = '1' WHERE 'id' = '$ order_sn' AND 'userid' = '". $ this-> mid. "'";
If ($ this-> dsql-> ExecuteNoneQuery ($ SQL ))
{
$ This-> log_result ("verify_success, order number:". $ order_sn); // Save the verification result to a file
Return TRUE;
} Else {
$ This-> log_result ("verify_failed, order number:". $ order_sn); // Save the verification result to a file
Return FALSE;
}
}
Function log_result ($ word ){
Global $ cmd_cmspath;
$ Fp = fopen (dirname (_ FILE _). "/.../../data/payment/log.txt", "");
Flock ($ fp, LOCK_EX );
Fwrite ($ fp, $ word. ", execution date :". strftime ("% Y-% m-% d % H: % I: % S", time ()). "\ r \ n ");
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
}
}