<?php
class cls_tab_pay_type extends cls_base_table
{
	private $obj_db;
	private $perms=array();
	//构造函数
	function __construct()
	{
		parent::__construct();
		global $obj_db;
		$this->obj_db=$obj_db;
		//$this->perms["type_class"]=array("alipay"=>"支付宝","tenpay"=>"财付通","99bill"=>"快钱","yifubao"=>"易宝付","chinabank"=>"网银");
		$this->perms["type_class"]=array("alipay"=>"支付宝","tenpay"=>"财付通","99bill"=>"快钱","chinabank"=>"网银");
		$this->perms["type_method"]=array("0"=>"在线支付","1"=>"银行转账");
		$this->perms["api_dir"]=SYS_ROOT."/tool/api_pay";
		$this->perms["pay_ok_url"]=WEB_URL."/index.php?app=pay_ok";
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function __set($name,$value){
		if(isset($this->perms["$name"])){
			$this->perms["$name"]=$value;
		}else{
		}
	}
	/*添加
	arr_fields:关健字数组
	*/
	function _insert($arr_fields)
	{
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["type_method"])) return "支付方式不能为空！";
		$arr_fields["type_method"]=intval($arr_fields["type_method"]);
		if(!isset($this->perms["type_method"][$arr_fields["type_method"]])){
			$arr_return["err"]="支付方式不存在！";
			return $arr_return;
		}
		if($arr_fields["type_method"]==0){
			if(!isset($arr_fields["type_class"])||$arr_fields["type_class"]==""){
				$arr_return["err"]="支付接口不能为空！";
				return $arr_return;
			}
			if(!isset($this->perms["type_class"][$arr_fields["type_class"]])){
				$arr_return["err"]="支付接口不存在！";
				return $arr_return;
			}
			$str_type_class=$arr_fields["type_class"];
			require_once($this->perms["api_dir"]."/".$arr_fields["type_class"]."/ctl.pay.php");
			$obj_tool_pay=new cls_tool_api_pay;
			$str_info=$obj_tool_pay->set_fields_value();
			$arr_fields["type_fields"]=$str_info;
		}
		$arr_return=parent::on_insert(DB_PRE."pay_type",$arr_fields);
		return $arr_return;
	}

	function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["type_method"])){
			$arr_return["err"]="支付方式不能为空！";
			return $arr_return;
		}
		$arr_fields["type_method"]=intval($arr_fields["type_method"]);
		if(!isset($this->perms["type_method"][$arr_fields["type_method"]])){
			$arr_return["err"]="支付方式不存在！";
			return $arr_return;
		}
		if($arr_fields["type_method"]==0){
			if(!isset($arr_fields["type_class"])||$arr_fields["type_class"]==""){
				$arr_return["err"]="支付接口不能为空！";
				return $arr_return;
			}
			if(!isset($this->perms["type_class"][$arr_fields["type_class"]])){
				$arr_return["err"]="支付接口不存在！";
				return $arr_return;
			}
			require_once($this->perms["api_dir"]."/".$arr_fields["type_class"]."/ctl.pay.php");
			$obj_tool_pay=new cls_tool_api_pay;
			$str_info=$obj_tool_pay->set_fields_value();
			$arr_fields["type_fields"]=$str_info;
		}
		$arr_return=parent::on_update(DB_PRE."pay_type",$arr_fields,$msg_where);
		return $arr_return;
	}
	function on_save($arr_fields=array())
	{
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["type_class"])){
			$arr_return["err"]="保存参数错误！";
			return $arr_return;
		}
		if($arr_fields["type_class"]!=""&&!isset($this->perms["type_class"][$arr_fields["type_class"]])){
			$arr_return["err"]="保存参数错误！";
			return $arr_return;
		}
		if($arr_fields["type_class"]==""){
			$arr_fields["type_method"]=1;
		}else{
			$arr_fields["type_method"]=0;
		}
		$arr_fields["type_updatetime"]=TIME;
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."pay_type where type_class='".$arr_fields["type_class"]."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$str_where="type_id='".$obj_rs["type_id"]."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$obj_rs["type_id"];
		}else{
			$arr_fields["type_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}
		return $arr_return;
	}

	function on_pay($arr_fields=array())
	{
		$arr_return=array("url"=>"","err"=>"");
		if(!isset($arr_fields["type_class"])){
			$arr_return["err"]="支付接口不能为空！";
			return $arr_return;
		}
		$type_class=$arr_fields["type_class"];
		if(!isset($this->perms["type_class"]["$type_class"])){
			$arr_return["err"]="支付接口不存在！";
			return $arr_return;
		}
		require_once($this->perms["api_dir"]."/".$arr_fields["type_class"]."/ctl.pay.php");
		$obj_tool_pay=new cls_tool_api_pay;
		$arr_cfg_fields=self::get_pay_fields($type_class,$arr_fields);
		$arr_return=$obj_tool_pay->on_pay($arr_cfg_fields);
		return $arr_return;
	}

	function on_pay_ok($arr_fields){
		$str_where="";
		if(isset($arr_fields["pay_id"])){
			$str_where=" where order_pay_id='".$arr_fields["pay_id"]."'";
		}else if(isset($arr_fields["number"])){
			$str_where=" where order_number='".$arr_fields["number"]."'";
		}
		if($str_where==""){
			set_log("pay_save","no where",$arr_fields);
			return;
		}
		$arr_val=array("order_state=1","order_pay_time='".date("Y-m-d H:i:s")."'");
		if(isset($arr_fields["pay_money"])) $arr_val[]="order_pay_money=".$arr_fields["pay_money"];

		$str_update=implode(",",$arr_val);
		$str_sql="update ".DB_PRE."pay_order set ".$str_update.$str_where;
		global $obj_db;
		$str_msg=$obj_db->execute($str_sql);
		if($str_msg!=""){
			set_log("pay_save",$str_msg.chr(10).$str_sql,$arr_fields);
		}else if($obj_db->affected_rows()<1){
			set_log("pay_save","没有要查询的订单".chr(10).$str_sql,$arr_fields);
		}else{
			//处理支付成功
			$obj_result=$obj_db->query("select order_id,order_user_id,order_id,order_pay_money from ".DB_PRE."pay_order".$str_where);
			if($obj_rs=$obj_db->fetch_array($obj_result)){
				include SYS_ROOT."/cls_tab/tab.sys.account.php";
				$obj_tab_account=new cls_tab_sys_account;
				$arr_msg=$obj_tab_account->on_pay($obj_rs["order_user_id"],$obj_rs["order_pay_money"],$obj_rs["order_id"]);
				if($arr_msg["err"]!="") set_log("pay_save",$arr_msg["err"]);
			}
		}
	}

	function get_type_fields($msgclass){
		global $obj_db;
		$arr_return=array();
		$obj_result=$obj_db->query("select type_fields from ".DB_PRE."pay_type where type_class='".$msgclass."'");
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_return=self::get_pay_fields_arr($obj_rs["type_fields"]);
		}
		return $arr_return;
	}
	function get_pay_fields($msgclass,$arr_f=array()){
		global $obj_db;
		$arr_return=array();
		$obj_result=$obj_db->query("select * from ".DB_PRE."pay_type where type_class='".$msgclass."'");
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_val_fields=self::get_pay_fields_arr($obj_rs["type_fields"]);
		}else{
			return $arr_return;
		}
		switch($msgclass){
			case "alipay":
				$arr_return["partner"]=$arr_val_fields["partner"];
				$arr_return["key"]=$arr_val_fields["key"];
				$arr_return["email"]=$arr_val_fields["email"];
				$arr_return["return_url"]=WEB_URL."/pay/alipay_return_url.php";
				$arr_return["notify_url"]=WEB_URL."/pay/alipay_notify_url.php";
				$arr_return["show_url"]="";
				$arr_return["subject"]=$arr_f["subject"];
				$arr_return["orderid"]=$arr_f["orderid"];
				$arr_return["price"]=$arr_f["price"];
				break;
			case "chinabank":
				$arr_return["parterid"]=$arr_val_fields["parterid"];
				$arr_return["key"]=$arr_val_fields["key"];
				$arr_return["orderid"]=$arr_f["orderid"];
				$arr_return["price"]=$arr_f["price"];
				$arr_return["return_url"]=WEB_URL."/pay/chinabank_return_url.php";
				break;
			case "tenpay":
				$arr_return["parterid"]=$arr_val_fields["parterid"];
				$arr_return["key"]=$arr_val_fields["key"];
				$arr_return["return_url"]=WEB_URL."/pay/tenpay_return_url.php";
				$arr_return["subject"]=$arr_f["subject"];
				$arr_return["orders_number"]=$arr_f["orderid"];
				$arr_return["goods_price"]=$arr_f["price"];
				$arr_return["detail"]=$arr_f["detail"];
				break;
			case "99bill":
				$arr_return["parterid"]=$arr_val_fields["parterid"];
				$arr_return["key"]=$arr_val_fields["key"];
				$arr_return["return_url"]=WEB_URL."/pay/99bill_return_url.php";
				$arr_return["subject"]=$arr_f["subject"];
				$arr_return["orders_number"]=$arr_f["orderid"];
				$arr_return["goods_price"]=$arr_f["price"];
				break;
		}
		return $arr_return;
	}
	function on_pay_return(){
	}
	function get_pay_fields_html($msgtype,$msgval="")
	{
		if(!isset($this->perms["type_class"][$msgtype])) return $msgval;
		$arr_fields=array();
		if($msgval!="")
		{
			$arr_fields=self::get_pay_fields_arr($msgval);
		}
		require_once($this->perms["api_dir"]."/".$msgtype."/ctl.pay.php");
		$obj_tool_pay=new cls_tool_api_pay;
		$str_info=$obj_tool_pay->get_fields_html($arr_fields);
		return $str_info;
	}
	function get_pay_fields_arr($msgval)
	{
		$arr_fields=array();
		if($msgval=="") return $arr_fields;
		$arr_x=explode("|",$msgval);
		$arr_y=array();
		foreach($arr_x as $item)
		{
			$arr_y=explode("=",$item);
			$arr_fields[$arr_y[0]]=$arr_y[1];
		}
		return $arr_fields;
	}
}
?>
