<?php

//---------------------------------------------------------
//财付通即时到帐支付页面回调示例，商户按照此文档进行开发即可
//---------------------------------------------------------
require_once ("./classes/ResponseHandler.class.php");
require_once ("./classes/function.php");
require_once ("./tenpay_config.php");
include_once("../../../lib/cs_conn.php");
include_once("../../../lib/cs_common.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 ".Getdbname('pay')." where cs_dingdan='".$out_trade_no."' and cs_pid=0");
	               if($row){
	               $CS_RMB=$row["CS_RMB"];
                           $rowu=$db->getrow("select * from ".Getdbname('user')." where cs_name='".$row["CS_User"]."'");
                           if($row["CS_Type"]=='vip'){   //升级VIP
				if($CS_RMB==30){
                                    $VipDs=CS_VipDs1;
                                }elseif($CS_RMB==180){
                                    $VipDs=CS_VipDs2;
                                }elseif($CS_RMB==365){
                                    $VipDs=CS_VipDs3;
                                }
				$time1=$rowu["CS_VipTime"];
				$time2=date('Y-m-d');
				$diff=strtotime($time1)-strtotime($time2);
				if($diff<0){
					$mydate=mktime(0,0,0,date("m"),date("d")+$CS_RMB,date("Y"));
					$NewUseDate= date("Y-m-d H:i:s",$mydate);
					$sqlv="update ".Getdbname('user')." set cs_viptime='".date('Y-m-d H:i:s')."',cs_endtime='".$NewUseDate."' where CS_Name='".$row["CS_User"]."'";
					$db->query($sqlv);
				}else{
					$strtimes = explode(" ",$rowu["CS_EndTime"]);
					$timearray = explode("-",$strtimes[0]);
					$year = $timearray[0];
					$month = $timearray[1]+$CS_RMB;
					$day = $timearray[2];
					$mydate2=mktime(0,0,0,$month,$day,$year);
					$NewUseDate=date("Y-m-d H:i:s",$mydate2);
					$sqlv="update ".Getdbname('user')." set cs_endtime='".$NewUseDate."' where CS_Name='".$row["CS_User"]."'";
					$db->query($sqlv);
				}
				//产生消息
				$content="恭喜您，成功升级了VIP会员".$CS_RMB."天，VIP到期时间:".$NewUseDate."，若出现问题请尽快联系客服。";
				$s_addtime=date('Y-m-d H:i:s');
				$sqlx="insert into ".Getdbname('xiaoxi')." (cs_usera,cs_userb,cs_name,cs_neir,cs_addtime) values ('".$row["CS_User"]."','系统消息','升级VIP - 系统消息','".$content."','".date('Y-m-d H:i:s')."')";
				$db->query($sqlx);
	                        echo "恭喜您，成功升级了VIP会员".$CS_RMB."天，VIP到期时间:".$NewUseDate."，若出现问题请尽快联系客服。</br>";
	                        echo "请牢记您的定单号：".$out_trade_no;

                           }else{   //充值金币

				$db->query("update ".Getdbname('user')." set CS_Cion=CS_Cion+$CS_RMB where CS_Name='".$row["CS_User"]."'");
				//产生消息
				$content="恭喜您，成功充值了 ".$CS_RMB." 元，若出现问题请尽快联系客服。";
				$s_addtime=date('Y-m-d H:i:s');
				$sqlx="insert into ".Getdbname('xiaoxi')." (cs_usera,cs_userb,cs_name,cs_neir,cs_addtime) values ('".$row["CS_User"]."','系统消息','充值成功 - 系统消息','".$content."','".date('Y-m-d H:i:s')."')";
				$db->query($sqlx);
	                        echo "恭喜您，成功充值了 ".$CS_RMB." 元，若出现问题请尽快联系客服。</br>";
	                        echo "请牢记您的定单号：".$out_trade_no;
                           }
			   $db->query("update ".Getdbname('pay')." set CS_Pid=1 where CS_Dingdan='".$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>";
}

?>
