<?php  

//include("tenpay_util.php");

include("../../global.inc.php");//加载信息

class tenpay_online_payment extends CommonFrameWork
{
	var $seller;//卖家QQ帐号
	var $domain;//商城地址
	var $site_name;//商城名称
	var $encode_type;//编码类型
	/*	
	function tenpay_check_config ()//检查配置文件项目
	{
		
	}
	*/
	function set_config(){
		//取配置文件内容
		//$fp = @fopen(BasePath.'/payment/tenpay/tenpay_config.php', 'r');
		//$configfile = @fread($fp, @filesize(BasePath.'/payment/tenpay/tenpay_config.php'));
		//@fclose($fp);
		//替换相应内容
		$this->domain = $this->_configinfo['websit']['site_url'];//商户网站域名
		$this->site_name = $this->_configinfo['websit']['site_name'];//商户网站名称
		$this->encode_type = (strtoupper($this->_configinfo['websit']['ncharset'])=='GBK')?'1':'2';//编码类型
		/*
		$configfile = preg_replace("/[$]domain\s*\=\s*[\"'].*?[\"'].*?;/is", "\$domain = \"$domain\";", $configfile);//替换商户网站域名
		$configfile = preg_replace("/[$]site_name\s*\=\s*[\"'].*?[\"'].*?;/is", "\$site_name = \"$site_name\";", $configfile);//商户网站名称

		@chmod(BasePath.'/payment/tenpay/tenpay_config.php',0777);
		$fp = @fopen(BasePath.'/payment/tenpay/tenpay_config.php', 'w+');
		@fwrite($fp, trim($configfile));
		@fclose($fp);
		*/
	}




	//检查并组合支付参数串
	function AddParameter($buffer,$parameterName,$parameterValue)
	{
		//更新配置文件
		//$this->set_config();

		if ($parameterValue=="")
			return $buffer;

		if (empty($buffer))

		
			$buffer = $parameterName . "=". $parameterValue;

		else

			$buffer = $buffer . "&" . $parameterName . "=" .$parameterValue;


		return $buffer;
	}
	
	
	
	//产生支付链接
	function tenpay_interface_pay ($mch_name,$mch_price,$transport_desc,$transport_fee,$mch_desc,$mch_vno)
	{
		//更新配置文件
		$this->set_config();
		include_once("tenpay_config.php");
		 if (empty($mch_price))
		 {
		 	$retcode = "09001";
			$retmsg  = "缺少mch_price";
			
		 }
		 
		 if ($transport_desc=="")
		 {
		 	$retcode = "06002";
			$retmsg  = "缺少transport_desc，将被默认设置不需要物流信息";
			$transport_desc = "无物流信息";
		 }
		 
		 if ($transport_fee=="")
		 {
		 	if ($retcode == "06002")
		 	{
				$transport_fee = "0";
			}
			else 
			{
			$retcode = "09002";
			$retmsg  = "缺少transport_fee，将被默认设置不需要物流信息";
			
			}
		 }
		 
		 
		 
		 if (empty($mch_desc))
		 {
		 	$retcode = "06003";
			$retmsg = "缺少交易描述信息，将被默认设置为网站地址加交易购物";
			$mch_desc = $this->domain . "交易购物";
		 }
		 
		 if (empty($mch_vno))
		 {
			$retcode = "09003";
			$retmsg = "缺少商家订单号";
		 }
		 
		if (empty($mch_name))
		 {
		 	$retcode = "06005";
			$retmsg  = "缺少商品名称，将被默认设置为网站地址加订单号";
			$mch_name = $this->site_name."订单：" . $mch_vno;
		 }
		 
		 	 
		 if (empty($mch_returl))
		 {
		 	$retcode = "06006";
			$retmsg  = "缺少回调url，将被默认设置为/tenpay/notify_handler.php";
			$mch_returl = $this->domain . $tenpay_dir ."/tenpay_notify.php";
		 }
		 
		 if (empty($show_url))
		 {
		 	$retcode = "06007";
			$retmsg  = "缺少回显url，将被默认设置为/tenpay/tenpay_show.php";
			$show_url = $this->domain . $tenpay_dir ."/tenpay_show.php";
		 }
		 
		 		  

		 
		//判断测试开关，如果开启测试，支付金额为1分 
		 if ($beta_switch == "1") 
			 {
			 echo "enter beta_switch";
			 	$mch_price = "1";
			 	$transport_fee = "0";
			 	

						$buffer = $this->AddParameter($buffer, "attach", 			$attach);
						$buffer = $this->AddParameter($buffer, "chnid", 			$chnid);
						$buffer = $this->AddParameter($buffer, "cmdno", 			"12");
						$buffer = $this->AddParameter($buffer, "encode_type", 		$this->encode_type);
						$buffer = $this->AddParameter($buffer, "mch_desc", 			$mch_desc);
						$buffer = $this->AddParameter($buffer, "mch_name", 			$mch_name);
						$buffer = $this->AddParameter($buffer, "mch_price", 		$mch_price);
						$buffer = $this->AddParameter($buffer, "mch_returl", 		$mch_returl);
						$buffer = $this->AddParameter($buffer, "mch_type", 			$mch_type);
						$buffer = $this->AddParameter($buffer, "mch_vno", 			$mch_vno);
						$buffer = $this->AddParameter($buffer, "need_buyerinfo", 	$need_buyerinfo);
						$buffer = $this->AddParameter($buffer, "seller", 			$this->seller);
						$buffer = $this->AddParameter($buffer, "show_url", 			$show_url);
						$buffer = $this->AddParameter($buffer, "transport_desc", 	$transport_desc);
						$buffer = $this->AddParameter($buffer, "transport_fee", 	$transport_fee);
						$buffer = $this->AddParameter($buffer, "version", 			$version);
	
			
				$md5_sign 	=strtoupper(md5($buffer."&key=".$sp_key));	
				$redurl = $pay_url . "?".$buffer . "&sign=" . $md5_sign;
				
				
				echo $retcode . "<br></br>".$retmsg."<br></br>";
				echo $redurl;
				
				
				return $redurl;
			 	}
		 else 
		 {   
		 
			 if ($retcode < "09000")
			 {
											
						$buffer = $this->AddParameter($buffer, "attach", 		$attach);
						$buffer = $this->AddParameter($buffer, "chnid", 			$chnid);
						$buffer = $this->AddParameter($buffer, "cmdno", 			"12");
						$buffer = $this->AddParameter($buffer, "encode_type", 	$this->encode_type);
						$buffer = $this->AddParameter($buffer, "mch_desc", 		$mch_desc);
						$buffer = $this->AddParameter($buffer, "mch_name", 		$mch_name);
						$buffer = $this->AddParameter($buffer, "mch_price", 		$mch_price);
						$buffer = $this->AddParameter($buffer, "mch_returl", 	$mch_returl);
						$buffer = $this->AddParameter($buffer, "mch_type", 		$mch_type);
						$buffer = $this->AddParameter($buffer, "mch_vno", 		$mch_vno);
						$buffer = $this->AddParameter($buffer, "need_buyerinfo", $need_buyerinfo);
						$buffer = $this->AddParameter($buffer, "seller", 		$this->seller);
						$buffer = $this->AddParameter($buffer, "show_url", 		$show_url);
						$buffer = $this->AddParameter($buffer, "transport_desc", $transport_desc);
						$buffer = $this->AddParameter($buffer, "transport_fee", 	$transport_fee);						
						$buffer = $this->AddParameter($buffer, "version", 		$version);
			
				$md5_sign 	= strtoupper(md5($buffer."&key=".$sp_key));	

				$redurl = $pay_url . "?".$buffer . "&sign=" . $md5_sign;

				return $redurl;
			 }
		}
	}

	//更改订单状态,$mch_vno 订单号,$status 订单状态,$cft_tid 财付通订单流水号
	function updateOrderState($mch_vno,$status,$cft_tid){
		if(!is_object($obj_product_order)){
			require_once("order.class.php");
			$obj_product_order = new ProductOrderClass();
		}
		if($mch_vno != '' && $status !== ''){
			$order_array = $obj_product_order->getOneOrderBySpcode ( $mch_vno );
			switch ($order_array['sell_type']){
				case '0'://拍卖
					require_once('order_process_auction.class.php');
					$obj_order_process = new OrderProcessAuction();
					break;
				case '1'://一口价
					require_once('order_process_fixprice.class.php');
					$obj_order_process = new OrderProcessFixprice();
					break;
				case '2'://团购
					require_once('order_process_group.class.php');
					$obj_order_process = new OrderProcessGroup();
					break;
				default:
					exit;
			}
			$obj_order_process->alipay_id = $cft_tid;
			$result = $obj_order_process->changeOrderState($mch_vno,$status);
			return $result;
		}else{
			return false;
		}
	}
}
?>
