PHP Integrated Tenpay Payment Interface

Source: Internet
Author: User
Tags flock


<?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 to processing address
/**
* Constructor function
*
* @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 will send the address
* @return void
*/
function Setreturnurl ($returnurl = ")
{
if (!empty ($returnurl))
{
$this->return_url = $returnurl;
}
}
/**
* Generate payment Codes
* @param array $order order information
* @param Array $payment payment method information
*/
function GetCode ($order, $payment)
{
Global $cfg _basehost, $cfg _cmspath;
Handling of Level two directories
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 trading, 1, physical trading * *
$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,//Trading description
' Bank_type ' = $bank _type,//transaction type default Tenpay
User IP
' Spbill_create_ip ' = $_server[' remote_addr ',//trading IP
' Fee_type ' + ' 1 ',//currency CNY 1
' Subject ' + $body,//Product name
System Optional Parameters
' Sign_type ' = ' MD5 ',//encryption method
' 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 returned as-is default to NULL
' Product_fee ' = ', '//commodity fees
' Transport_fee ' = ' 0 ',//logistics costs
' Time_start ' = = Date ("Ymdhis"),//Order generation Time Date ("Ymdhis")
' Time_expire ' = ', '//order expiry time
' buyer_id ' and ' = ',//buyer Tenpay Account
' Goods_tag ' + ',//commodity mark
' 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 ' and ' + ',//proxy mode (0. no agent, 1. means the card is easy to sell mode, 2. Indicates the store mode)
' seller_id ' = ' + '//seller 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 to pay </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 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 _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 the payment amount matches
$row = $this->dsql->getone ("select * from #@__shops_orders WHERE oid = ' {$order _sn} '");
if ($row [' pricecount ']! = $_get[' Total_fee '])
{
return $msg = "Payment failed, the amount of payment does not match the price of the goods!";
}
$this->mid = $row [' userid '];
/* Check that 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!<br> <a href= '/' > return home </a> <a href= '/member ' > Member Center </a>";
else return $msg = "Payment failed!<br> <a href= '/' > return home </a> <a href= '/member ' > Member Center </a>";
}
}
else{
return $msg = "Payment failed!<br> <a href= '/' > return home </a> <a href= '/member ' > Member Center </a>";
}
}
/* Deal with item trading */
function success_db ($order _sn)
{
$time =time ();
$mid = $this->mid;
Get order information and check the validity of your order
$row = $this->dsql->getone ("Select State,pricecount from #@__shops_orders WHERE oid= ' $order _sn '");
if ($row [' state '] > 0)
{
return TRUE;
}
/* Change Order Status _ Pay 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); Depositing validation results to a 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. ", Execution Date:". Strftime ("%y-%m-%d%h:%i:%s", Time ()). " \ r \ n ");
Flock ($FP, lock_un);
Fclose ($FP);
}
}

PHP Integrated Tenpay Payment Interface

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.