<?php

//---------------------------------------------------------
//财付通即时到帐支付应答（处理回调）示例，商户按照此文档进行开发即可
//---------------------------------------------------------

require_once ("./classes/PayResponseHandler.class.php");
$arr=include("../Conf/config.php");
 define("_UL_", $_SERVER['SERVER_NAME']);
/* 密钥 */
$key = $arr["KUAIQIAN_BUKEY"];

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

//判断签名
if($resHandler->isTenpaySign()) {
	
	//交易单号
	$transaction_id = $resHandler->getParameter("transaction_id");
	
	//金额,以分为单位
	$total_fee = $resHandler->getParameter("total_fee");
	
	//支付结果
	$pay_result = $resHandler->getParameter("pay_result");
	
	//商户交易号
	$sp_billno=$resHandler->getParameter("sp_billno");
	
	if( "0" == $pay_result ) {
	
		//------------------------------
		//处理业务开始
		//------------------------------
			@mysql_connect($arr["DB_HOST"].":".$arr["DB_PORT"],$arr["DB_USER"],$arr["DB_PWD"]);
			@mysql_select_db($arr["DB_NAME"]);
			$sql="select * from ".$arr["DB_PREFIX"]."m_pay where ordernum=".$sp_billno;
			$result=mysql_query($sql);
			//--------
			$rss=mysql_fetch_array($result);
			if($rss["paystate"]==0)
			{
			  $sql1="update ".$arr["DB_PREFIX"]."m_pay set trade_no='".$transaction_id."',payallmoney=paymoney+payallmoney,gettime='".date('Y-m-d H:i:s')."',paystate=1 where ordernum='".$sp_billno."'";
			  mysql_query($sql1);
			  //-----
			  //echo $sql1."<br>";
			  $sql2="select * from ".$arr["DB_PREFIX"]."m_member where username='".$rss["username"]."'";
			  $result2=mysql_query($sql2);
			  //-------
			  //echo $sql2."<br>";
			  $rss2=mysql_fetch_array($result2);
			  $sql3="update ".$arr["DB_PREFIX"]."m_member set money=money+".$rss["paymoney"]." where username='".$rss["username"]."'";
			  mysql_query($sql3);
		
		//------------------------------
		//处理业务完毕
		//------------------------------	
		
		//调用doShow, 打印meta值跟js代码,告诉财付通处理成功,并在用户浏览器显示$show页面.
		$show = "http://"._UL_."/index.php/paystate/suc/order_id/".$sp_billno;
		$resHandler->doShow($show);
	
	} else {
		//当做不成功处理
		echo "<script language='javascript'>location.href='http://"._UL_."/index.php/home/paystate/err'</script>";
	}
	
} else {
	echo "<script language='javascript'>location.href='http://"._UL_."/index.php/home/paystate/err'</script>";
}

//echo $resHandler->getDebugInfo();

?>