<?php
include_once("tenpay_config.php");
require_once("md5_request.php");
$tenpay = new tenpay_online_payment;
  

  /*取返回参数*/  
  import_request_variables("gpc", "frm_");
  $cmdno			= $frm_cmdno;
  $version			= $frm_version;
  $retcode			= $frm_retcode;
  $status			= $frm_status;
  $seller			= $frm_seller;
  $total_fee		= $frm_total_fee;
  $trade_price		= $frm_trade_price;
  $transport_fee	= $frm_transport_fee;
  $buyer_id			= $frm_buyer_id;
  $chnid 			= $frm_chnid;
  $cft_tid			= $frm_cft_tid;
  $mch_vno			= $frm_mch_vno;
  $attach			= $frm_attach;
  $sign				= $frm_sign;


  /*返回值定义*/
  $iRetOK       = 0;		// 成功					
  $iInvalidSpid = 1;		// 商户号错误
  $iInvalidSign = 2;		// 签名错误
  $iTenpayErr	  = 3;		// 财付通返回支付失败

  /*验签*/
  
$buffer = $tenpay->AddParameter($buffer, "attach", 				$attach);
$buffer = $tenpay->AddParameter($buffer, "buyer_id", 			$buyer_id);
$buffer = $tenpay->AddParameter($buffer, "cft_tid", 			$cft_tid);
$buffer = $tenpay->AddParameter($buffer, "chnid", 				$chnid);
$buffer = $tenpay->AddParameter($buffer, "cmdno", 				$cmdno);
$buffer = $tenpay->AddParameter($buffer, "mch_vno", 			$mch_vno);
$buffer = $tenpay->AddParameter($buffer, "retcode", 			$retcode);
$buffer = $tenpay->AddParameter($buffer, "seller", 				$seller);
$buffer = $tenpay->AddParameter($buffer, "status", 				$status);
$buffer = $tenpay->AddParameter($buffer, "total_fee", 			$total_fee);
$buffer = $tenpay->AddParameter($buffer, "trade_price", 		$trade_price);
$buffer = $tenpay->AddParameter($buffer, "transport_fee", 		$transport_fee);
$buffer = $tenpay->AddParameter($buffer, "version", 		$version);


$strLocalSign = strtoupper(md5($buffer."&key=".$sp_key));  

if ($strLocalSign  == $sign )
{
	//验证MD5签名成功 
	if ($retcode == "0")
	{    
	//支付成功，在这里处理业务逻辑注意判断订单是否重复的逻辑，注意订单金额为分
	echo "<meta name=\"TENCENT_ONLINE_PAYMENT\" content=\"China TENCENT\">";
	switch(($status)) {
		case 1: 
			log_result('交易创建'.$cft_tid);
			//交易创建
			$tenpay->updateOrderState($mch_vno,'0',$cft_tid);
			$url = $this->_config['websit']['site_url'].'/member/own_main.php';
			@header("Location: $url");
			exit;
			break;
		case 2:
			//收获地址填写完毕
			break;
		case 3:
			log_result('买家支付成功'.$cft_tid);
			//买家付款成功，注意判断订单是否重复的逻辑
			$tenpay->updateOrderState($mch_vno,'1',$cft_tid);
			$url = $this->_config['websit']['site_url'].'/member/own_main.php';
			@header("Location: $url");
			exit;
			break;
		case 4:
			//卖家发货成功
			log_result('卖家发货成功'.$cft_tid);
			$tenpay->updateOrderState($mch_vno,'2',$cft_tid);
			$url = $this->_config['websit']['site_url'].'/member/own_main.php';
			@header("Location: $url");
			exit;
			break;
		case 5:
			//买家收货确认，交易成功
			log_result('买家确认收货'.$cft_tid);
			$tenpay->updateOrderState($mch_vno,'3',$cft_tid);
			//跳转到买家评价页面
			//如果是状态3的话，则跳转到评价页面
			$url = $this->_config['websit']['site_url'].'/member/own_score.php?action=add&orderid='.$mch_vno.'&type=bought';
			@header("Location: $url");
			exit;
			break;
		case 6:
			//交易关闭，未完成超时关闭
			log_result('交易关闭'.$cft_tid);
			$tenpay->updateOrderState($mch_vno,'6',$cft_tid);
			break;
		case 7:
			//修改交易价格成功
			break;
		case 8:
			//买家发起退款
			break;
		case 9:
			//退款成功
			log_result('退款成功'.$cft_tid);
			$tenpay->updateOrderState($mch_vno,'6',$cft_tid);
			break;
		case 10:
			//退款关闭
			break;
		default:
			//error
			
		}
	}
	else
	{
	//支付失败，请根据retcode进行错误逻辑处理
	echo "retcode";
	}
}
else
{
echo "qianming";
//签名失败，请进行非法操作的逻辑处理
}


function  log_result($word) {
	$fp = fopen("log.txt","a");	
	flock($fp, LOCK_EX) ;
	fwrite($fp,$word."：执行日期：".strftime("%Y%m%d%H%I%S",time())."\t\n");
	flock($fp, LOCK_UN); 
	fclose($fp);
}
?>
