<?php 
/*********************************************
 *[tttuangou] (C)2005 - 2010 Cenwor Inc.
 *
 * 财付通支付接口文件
 *
 * @author 管乐<guanle19861986@yahoo.com.cn>
 * @package www.tttuangou.net
 **********************************************/

function payTo($payment,$returnurl,$pay){
		$payment=unserialize($pay['pay_config']);
		$cmd_no = '1';
        
        $sp_billno = $pay['orderid'];
        
        $today = date('Ymd');
        
        $bill_no = str_pad($pay['orderid'], 18, 0, STR_PAD_LEFT);
        $transaction_id = $payment['tenpay_account'].$bill_no;
        
        $bank_type = '0';
        
        $desc = $pay['name'];
        $attach = $pay['name'];
        
        $return_url = $returnurl;
        
        $total_fee = floatval($pay['price'])*100;
        
        $fee_type = '1';
        
        $attach = '';
        
        $sign_text = "cmdno=" . $cmd_no . "&date=" . $today . "&bargainor_id=" . $payment['tenpay_account'] .
          "&transaction_id=" . $transaction_id . "&sp_billno=" . $sp_billno .
          "&total_fee=" . $total_fee . "&fee_type=" . $fee_type . "&return_url=" . $return_url .
          "&attach=" . $attach . "&key=" . $payment['tenpay_key'];
        $sign = strtoupper(md5($sign_text));
        $parameter = array(
            'cmdno'             => $cmd_no,                                 'date'              => $today,                                  'bank_type'         => $bank_type,                              'desc'              => $desc,                                   'purchaser_id'      => '',                                      'bargainor_id'      => $payment['tenpay_account'],              'transaction_id'    => $transaction_id,                         'sp_billno'         => $sp_billno,                              'total_fee'         => $total_fee,                              'fee_type'          => $fee_type,                               'return_url'        => $return_url,                             'attach'            => $attach,                                 'sign'              => $sign,                               	'sys_id'            => '542554970',                 
            'sp_suggestuser'    => '1202822001'                         );
        $button  = '<form action="https:/'.'/www.tenpay.com/cgi-bin/v1.0/pay_gate.cgi" target="_blank" style="clear:right;" >';
        foreach ($parameter AS $key=>$val){
            	$button  .= "<input type='hidden' name='$key' value='$val' />";
        }
        $button  .= '<input type="submit" class="formbutton"  value="立即支付" /></form>';
		return $button;
}

function payRe(){
	 $cmd_no         = $_GET['cmdno'];
     $pay_result     = $_GET['pay_result'];
     $pay_info       = $_GET['pay_info'];
     $bill_date      = $_GET['date'];
     $bargainor_id   = $_GET['bargainor_id'];
     $transaction_id = $_GET['transaction_id'];
     $sp_billno      = $_GET['sp_billno'];     $total_fee      = $_GET['total_fee'];     $fee_type       = $_GET['fee_type'];
     $attach         = $_GET['attach'];
     $sign           = $_GET['sign'];
     
     if ($pay_result > 0){
       	return "支付失败!";
    };
    $error='';
    $DatabaseHandler = Obj::registry('DatabaseHandler');
    	$sql='select * from '.TABLE_PREFIX.'tttuangou_order where orderid  = '.addslashes($_GET['sp_billno']).' and userid ='.MEMBER_ID;
	$query = $DatabaseHandler->Query($sql);
	$order=$query->GetRow();
	if($order=='')return "无法继续付款，无法找到订单!";
		$sql='select * from '.TABLE_PREFIX.'tttuangou_payment where pay_id   = '.$order['paytype'];
	$query = $DatabaseHandler->Query($sql);
	$pay=$query->GetRow();
	$payment=unserialize($pay['pay_config']);
    
    $sign_text  = "cmdno=" . $cmd_no . "&pay_result=" . $pay_result .
     	"&date=" . $bill_date . "&transaction_id=" . $transaction_id .
        "&sp_billno=" . $sp_billno . "&total_fee=" . $total_fee .
        "&fee_type=" . $fee_type . "&attach=" . $attach .
        "&key=" . $payment['tenpay_key'];
    $sign_md5 = strtoupper(md5($sign_text));
    if ($sign_md5 != $sign){
       return "支付失败!";
    }else{
       	return $ary=array('price'=> $total_fee/100 ,'orderid'=>$_GET['sp_billno']);
    };
}

function addmymoney(){
	 $error='';
	 $cmd_no         = $_GET['cmdno'];
     $pay_result     = $_GET['pay_result'];
     $pay_info       = $_GET['pay_info'];
     $bill_date      = $_GET['date'];
     $bargainor_id   = $_GET['bargainor_id'];
     $transaction_id = $_GET['transaction_id'];
     $sp_billno      = $_GET['sp_billno'];     $total_fee      = $_GET['total_fee'];     $fee_type       = $_GET['fee_type'];
     $attach         = $_GET['attach'];
     $sign           = $_GET['sign'];
     
     if ($pay_result > 0){
       	return "支付失败!";
    };
		$DatabaseHandler = Obj::registry('DatabaseHandler');
	$sql='select count(*) from '.TABLE_PREFIX.'tttuangou_addmoney where id=\''.addslashes($_GET['sp_billno']).'\'';
	$query = $DatabaseHandler->Query($sql);
	$pay=$query->GetRow();
	if($pay['count(*)']!=0)return '支付失败';
	$sql='select * from '.TABLE_PREFIX.'tttuangou_payment where pay_code   =\''.addslashes($_GET['pay']).'\'';
	$query = $DatabaseHandler->Query($sql);
	$pay=$query->GetRow();
	$payment=unserialize($pay['pay_config']);
	
	 $sign_text  = "cmdno=" . $cmd_no . "&pay_result=" . $pay_result .
     	"&date=" . $bill_date . "&transaction_id=" . $transaction_id .
        "&sp_billno=" . $sp_billno . "&total_fee=" . $total_fee .
        "&fee_type=" . $fee_type . "&attach=" . $attach .
        "&key=" . $payment['tenpay_key'];
	 $sign_md5 = strtoupper(md5($sign_text));
    if (trim($sign_md5) != trim($sign)){
       return "验证失败!";
    }else{
       return $ary=array('price'=>  $total_fee/100 ,'orderid'=>$_GET['sp_billno']);
    };
}