<?php
class PayAction extends Action{
	// 元宝充值
	public function index(){
		// 已经登录
		if($_SESSION["ID"]){
			// 查询游戏
			$Data = M("YxRz");
			$yxList = $Data->field("yx_id id,yx_name name")->join("yeyou_yx ON yx_id = rz_yx")->where("rz_zh = ".$_SESSION["ID"])->group("yx_name")->select();
			$this->assign("yxList",$yxList);
		}
		// 支付方式
		$fsList = F("fsList");
		if(!$fsList){
			import("@.iGuoji.Webgame");
			$Webgame = new Webgame();
			$fsList = $Webgame->payfs();
			F("fsList",$fsList);
		}
		$this->assign("fsList",$fsList);
		// 显示页面
		$this->display();
	}
	// 提交充值
	public function post(){
		// 获取参数
		$yx = INT("yx");
		$qf = INT("qf");
		$fs = INT("fs");
		$money = INT("money");
		$zh = ABC("zh");
		$rezh = ABC("rezh");
		// 判断参数
		if(!$yx || !$qf || !$fs || !$money || !$zh || !$rezh){
			$this->error("每一项都必须选择!");
			exit;
		}
		// 查询区服
		$Data = M("Qf");
		$qfObj = $Data->where("qf_id = $qf")->find();
		if(!$qfObj){
			$this->error("区服不存在!");
			exit;
		}
		// 对比游戏
		if($qfObj["qf_yx"] != $yx){
			$this->error("游戏和区服不对应!");
			exit;
		}
		// 金额错误
		if($money < 1){
			$this->error("金额不能少于5元!");
			exit;
		}
		// 帐号不对
		if($zh != $rezh){
			$this->error("确认帐号不正确!");
			exit;
		}
		// 查询帐号
		$Data = M("Zh");
		$zhObj = $Data->where("zh_name = '$zh'")->find();
		if(!$zhObj){
			$this->error("帐号不存在!");
			exit;
		}
		// 添加订单
		$Data = M("Dd");
		$ddData["dd_id"] = DD();
		$ddData["dd_step"] = 1;
		$ddData["dd_zh"] = $zhObj["zh_id"];
		$ddData["dd_yx"] = $yx;
		$ddData["dd_qf"] = $qf;
		$ddData["dd_fs"] = $fs;
		$ddData["dd_money"] = $money;
		$ddData["dd_yb"] = 0;
		$ddData["dd_date"] = time();
		$rel = $Data->add($ddData);
		if($rel === false){
			$this->error("订单添加失败!");
			exit;
		}
		
		// 前往充值
		import("@.iGuoji.Webgame");
		$Webgame = new Webgame();
		$path = $Webgame->pay($qfObj["qf_kf"],$zhObj["zh_name"],$ddData["dd_id"],$fs,$money);
		redirect($path);
		exit;
	}
	
	// 充值返回
	public function back(){
		// 获取参数
		$point = INT("point");
		$order = INT("order");
		$sign = ABC("sign");
		if(!$order || !$sign){
			echo("Args Error");
			exit;
		}
		// 验证签名
		if($sign != md5(SH.WZ.KEY.$point.$order)){
			echo("Sign Error");
			exit;
		}
		// 查询订单
		$Data = M("Dd");
		$ddObj = $Data->where("dd_id = '$order'")->find();
		if($ddObj["dd_step"] == 2){
			echo("Repeat Error");
			exit;
		}
		// 修改订单
		$Data = M("Dd");
		$rel = $Data->where("dd_id = '$order'")->save(array("dd_step"=>2,"dd_yb"=>$point));
		if($rel === false){
			echo("Order Error");
			exit;
		}
		// 操作成功
		echo("Success");
		exit;
	}
}
?>