With weaving dream made a travel website, customer request Tenpay pay, the internet looked for the next is not to buy is to ask for money, only to write their own.
Code:
Copy Code code as follows:
<?php
if (!defined (' Dedeinc ')) exit (' Request error! ');
/**
* Tenpay Interface class
*/
Class Tenpay
{
var $dsql;
var $mid;
var $reqURL _online = "http://www.tenpay.com";
var $return _url= '/plus/carbuyaction.php?dopost=return '; Return processing Address
/**
* Constructor
*
* @access Public
* @param
*
* @return void
*/
function Tenpay ()
{
Global $dsql;
$this->dsql = $dsql;
}
function __construct ()
{
$this->tenpay ();
}
/**
* Set interface will send address
*
* For example: $this->setreturnurl ($cfg _basehost.) /tuangou/control/index.php?ac=pay&orderid= ". $p 2_order)
*
* @param string $returnurl send Address
* @return void
*/
function Setreturnurl ($returnurl = ')
{
if (!empty ($returnurl))
{
$this->return_url = $returnurl;
}
}
/**
* Generate payment Code
* @param array $order order information
* @param Array $payment payment method information
*/
function GetCode ($order, $payment)
{
Global $cfg _basehost, $cfg _cmspath;
For processing of level two catalogs
if (!empty ($cfg _cmspath)) $cfg _basehost = $cfg _basehost. ' /'. $cfg _cmspath;
$partner = $payment [' Tenpay_account '];
$out _trade_no = $order [' Out_trade_no '];
$total _fee = floatval ($order [' Price ']) * 100;
$body = $order [' Out_trade_no '];
$attach = ';
$bank _type = ' DEFAULT ';
/* Transaction type: 2, virtual transaction, 1, the kind of 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_fee ' => $total _fee,//Total amount
' Notify_url ' => $cfg _basehost. $this->return_url. " &code= ". $payment [' Code '],//return address
' Return_url ' => $cfg _basehost. $this->return_url. " &code= ". $payment [' Code '],//reminder Address
' Body ' => $body,//Transaction description
' Bank_type ' => $bank _type,//transaction type default Tenpay
User IP
' Spbill_create_ip ' => $_server[' remote_addr ',//trading IP
' Fee_type ' => ' 1 ',//currency 1 RMB
' Subject ' => $body,//Product name
System Optional Parameters
' Sign_type ' => ' MD5 ',//encryption mode
' Service_version ' => ' 1.0 ',//interface version number default 1.0
' Input_charset ' => ' UTF-8 ',//system code ' GBK '
' Sign_key_index ' => ' 1 ',//Key serial number
Business Optional Parameters
' Attach ' => $attach,//Append data is returned as-is null default
' Product_fee ' => ',//commodity costs
' Transport_fee ' => ' 0 ',//logistics costs
' Time_start ' => date ("Ymdhis"),//Order generation Time Date ("Ymdhis")
' Time_expire ' => ',//order Expiration Time
' buyer_id ' => ',//buyer Tenpay Account
' Goods_tag ' => ',//mark of goods
' Trade_mode ' => $trade _mode,//Trading mode (1. Instant to account mode, 2 intermediary guarantee mode, 3. Background selection (seller enters payment Center list selection))
' Transport_desc ' => ',//logistics instructions
' Trans_type ' => $trans _type,//transaction type
' Agentid ' => ',//platform ID
' Agent_type ' => ',//Agent mode (0, no agent, 1) means card easy to sell mode, 2. Express Shop mode)
' seller_id ' => '/sellers merchant number
);
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 = ' <div style= ' text-align:center ' ><a href= ' https://gw.tenpay.com/gateway/pay.htm '. $param. ' &sign= '. $sign. ' " ><button> immediately use tenpay payment </button></a></div> ';
return $button;
* * Empty Shopping cart/
Require_once Dedeinc. ' /shopcar.class.php ';
$cart = new Membershops ();
$cart->clearitem ();
$cart->makeorders ();
return $button;
}
/**
* Response Action
*/
function respond ()
{
/* Introduce 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 _fee = $_get[' Total_fee '];
$out _trade_no = Trim ($_get[' out_trade_no '));
if (Preg_match ("/s-p[0-9]+rn[0-9]/", $order _sn)) {
Check if payment amount matches
$row = $this->dsql->getone ("select * from #@__shops_orders WHERE oid = ' {$order _sn} '");
if ($row [' Pricecount ']!= $_get[' Total_fee '])
{
return $msg = "Payment failure, the amount of payment and the total price of goods does not match!";
}
$this->mid = $row [' userid '];
/* Check the digital signature is correct * *
Ksort ($_get);
Reset ($_get);
$sign = ';
foreach ($_get as $key => $val)
{
if ("" "!= $val &&" sign "!= && $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 success!<br> <a href= '/' > Back home </a> <a href= '/member ' > Member Center </a>";
else return $msg = "Payment failed!<br> <a href= '/' > Back home </a> <a href= '/member ' > Member Center </a>";
}
}
else{
return $msg = "Payment failed!<br> <a href= '/' > Back home </a> <a href= '/member ' > Member Center </a>";
}
}
* * Deal with goods transaction
function success_db ($order _sn)
{
$time =time ();
$mid = $this->mid;
Get order information, check the validity of order
$row = $this->dsql->getone ("Select State,pricecount from #@__shops_orders WHERE oid= ' $order _sn '");
if ($row [' state '] > 0)
{
return TRUE;
}
/* Change Order Status _ payment Success * *
$sql = "UPDATE ' #@__shops_orders ' SET ' state ' = ' 1 ' WHERE ' oid ' = ' $order _sn ' and ' userid ' = '". $this->mid. "'";
if ($this->dsql->executenonequery ($sql))
{
$this->log_result ("verify_success, order Number:". $order _sn); Save validation Results to file
return TRUE;
} else {
$this->log_result ("verify_failed, order Number:". $order _sn);//save validation results to file
return FALSE;
}
}
function Log_result ($word) {
Global $cfg _cmspath;
$fp = fopen (dirname (__file__). " /.. /.. /data/payment/log.txt "," a ");
Flock ($FP, LOCK_EX);
Fwrite ($FP, $word. ", Date of execution:". Strftime ("%y-%m-%d%h:%i:%s", Time ()). " \ r \ n ");
Flock ($FP, lock_un);
Fclose ($FP);
}
}