<?php

//---------------------------------------------------------
//财付通即时到帐支付页面回调示例，商户按照此文档进行开发即可
//---------------------------------------------------------
require_once ("./classes/ResponseHandler.class.php");
require_once ("./classes/function.php");
require_once ("./tenpay_config.php");
include "../../include/conn.php";
log_result("进入前台回调页面");


/* 创建支付应答对象 */
$resHandler = new ResponseHandler();
$resHandler->setKey($key);

//判断签名
if($resHandler->isTenpaySign()) {
	
	//通知id
	$notify_id = $resHandler->getParameter("notify_id");
	//商户订单号
	$out_trade_no = $resHandler->getParameter("out_trade_no");
	//财付通订单号
	$transaction_id = $resHandler->getParameter("transaction_id");
	//金额,以分为单位
	$total_fee = $resHandler->getParameter("total_fee");
	//如果有使用折扣券，discount有值，total_fee+discount=原请求的total_fee
	$discount = $resHandler->getParameter("discount");
	//支付结果
	$trade_state = $resHandler->getParameter("trade_state");
	//交易模式,1即时到账
	$trade_mode = $resHandler->getParameter("trade_mode");
	
	
	if("1" == $trade_mode ) {
		if( "0" == $trade_state){ 
	               global $db;
	               //检测订单，存在就加点
	               $row=$db->getrow("select * from s_pay where S_Payid='".$out_trade_no."' and S_Vid=0");
	               if($row){	
	                  $S_Payrmb=$row["S_Payrmb"];
				$db->query("update s_users set S_RMB=S_RMB+$S_Payrmb where S_Name='".$row["S_Payuser"]."'");
				$db->query("update s_pay set S_Vid=1 where S_Payid='".$out_trade_no."'");
				//产生消息
				$s_xid=0;
				$s_cid=0;
				$s_namea=$payuser;
				$s_nameb="系统消息";
				$s_content="恭喜您，成功充值了 ".$S_Payrmb." 元，若出现问题请尽快联系客服。";
				$s_addtime=date('Y-m-d H:i:s');
				$sqlx="insert into s_xiaoxi (S_Xid,S_Nameb,S_Namea,S_Content,S_Addtime) values (".$s_xid.",'".$s_nameb."','".$s_namea."','".$s_content."','".$s_addtime."')";
				if($db->query($sqlx));
	                        echo "恭喜您，成功充值了 ".$S_Payrmb." 元，若出现问题请尽快联系客服。</br>";
	                        echo "请牢记您的定单号：".$out_trade_no;
	               }else{
	                        echo "验证失败,非法订单。";
	               }
	
		} else {
			//当做不成功处理
			echo "<br/>" . "即时到帐支付失败" . "<br/>";
		}
	}elseif( "2" == $trade_mode  ) {
		if( "0" == $trade_state) {
		
			//------------------------------
			//处理业务开始
			//------------------------------
			
			//注意交易单不要重复处理
			//注意判断返回金额
			
			//------------------------------
			//处理业务完毕
			//------------------------------	
			
			echo "<br/>" . "中介担保支付成功" . "<br/>";
		
		} else {
			//当做不成功处理
			echo "<br/>" . "中介担保支付失败" . "<br/>";
		}
	}
	
} else {
	echo "<br/>" . "认证签名失败" . "<br/>";
	echo $resHandler->getDebugInfo() . "<br>";
}

?>