<?php 

defined('G_IN_SYSTEM')or exit('Access Denied.');

ini_set("display_errors","ON");

class myunpay_url extends SystemAction {
    private $out_trade_no;
	public function __construct(){			
		$this->db=System::load_sys_class('model');	

	} 	

	public function qiantai(){	
	   include dirname(__FILE__)."/lib/yunpay/yun_md5.function.php";		
		$pay_type =$this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = 'myunpay' and `pay_start` = '1'");
		$pay_type_key = unserialize($pay_type['pay_key']);

		$key =  $pay_type_key['key']['val'];		//支付KEY

		$id =  $pay_type_key['id']['val'];		//支付商号ID	
		
		$user =  $pay_type_key['user']['val'];		//云支付账号


		$yunNotify = md5Verify($_REQUEST['i1'],$_REQUEST['i2'],$_REQUEST['i3'],$key,$id);
		
		if($yunNotify) {//验证成功
			//商户订单号
			$out_trade_no = $_REQUEST['i2'];
			//云支付交易号
			$trade_no = $_REQUEST['i4'];
			//价格
			$yunprice=$_REQUEST['i1'];
		
		
		//开始处理及时到账和担保交易订单
			$this->out_trade_no = $out_trade_no;
			$this->yeepay_chuli();
			$dingdaninfo = $this->db->GetOne("select * from `@#_member_addmoney_record` where `code` = '$out_trade_no'");
			if(!$dingdaninfo || $dingdaninfo['status'] == '未付款'){
				_messagemobile("支付失败");			
			}else{
				if(empty($dingdaninfo['scookies'])){
					_messagemobile("充值成功!",WEB_PATH."/mobile/home/userbalance");
				}else{
					if($dingdaninfo['scookies'] == '1'){
						_messagemobile("支付成功!",WEB_PATH."/mobile/cart/paysuccess");
					}else{
						_messagemobile("商品还未购买,请重新购买商品!",WEB_PATH."/mobile/");
					}
						
				}
			}	
			
			
			}else{
				echo "fail";exit;
			}
		
		}//开始处理订单结束
				

	//function end
	
	public function houtai(){
	
		
	  include dirname(__FILE__)."/lib/yunpay/yun_md5.function.php";		
		$pay_type =$this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = 'myunpay' and `pay_start` = '1'");
		$pay_type_key = unserialize($pay_type['pay_key']);

		$key =  $pay_type_key['key']['val'];		//支付KEY

		$id =  $pay_type_key['id']['val'];		//支付商号ID	
		
		$user =  $pay_type_key['user']['val'];		//云支付账号
//计算得出通知验证结果
		$yunNotify = md5Verify($_REQUEST['i1'],$_REQUEST['i2'],$_REQUEST['i3'],$key,$id);
		if($yunNotify) {//验证成功
			//商户订单号
			$out_trade_no = $_REQUEST['i2'];
			//云支付交易号
			$trade_no = $_REQUEST['i4'];
			//价格
			$yunprice=$_REQUEST['i1'];
			$this->out_trade_no = $out_trade_no;
			$this->yeepay_chuli();	
				
		
			
			}else{
				echo "fail";exit;
			}
		
		}//开始处理订单结束
				

	//function end
	
	
	private function yeepay_chuli(){
		$pay_type =$this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = 'myunpay' and `pay_start` = '1'");
		$out_trade_no = $this->out_trade_no;
		$this->db->Autocommit_start();
		$dingdaninfo = $this->db->GetOne("select * from `@#_member_addmoney_record` where `code` = '$out_trade_no' for update");
		if(!$dingdaninfo){ return false;}	//没有该订单,失败
		if($dingdaninfo['status'] == '已付款'){
			return '已付款';
		}
		$c_money = intval($dingdaninfo['money']);
		$uid = $dingdaninfo['uid'];
		$time = time();		
		
		$up_q1 = $this->db->Query("UPDATE `@#_member_addmoney_record` SET `pay_type` = '云支付', `status` = '已付款' where `id` = '$dingdaninfo[id]' and `code` = '$dingdaninfo[code]'");
		$up_q2 = $this->db->Query("UPDATE `@#_member` SET `money` = `money` + $c_money where (`uid` = '$uid')");			
		$up_q3 = $this->db->Query("INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('$uid', '1', '账户', '充值', '$c_money', '$time')");
		
		if($up_q1 && $up_q2 && $up_q3){			
			$this->db->Autocommit_commit();
		}else{
			$this->db->Autocommit_rollback();
			return '充值失败';
		}			
		if(empty($dingdaninfo['scookies'])){					
			return "充值完成";	//充值完成	
		}
		
		$scookies = unserialize($dingdaninfo['scookies']);			
		$pay = System::load_app_class('pay','pay');		
		$pay->scookie = $scookies;
		$ok = $pay->init($uid,$pay_type['pay_id'],'go_record');	//云购商品	
		if($ok != 'ok'){
			$_COOKIE['Cartlist'] = '';_setcookie('Cartlist',NULL);			
			return '商品购买失败';	//商品购买失败			
		}		

		$check = $pay->go_pay(1);
		if($check){
			$this->db->Query("UPDATE `@#_member_addmoney_record` SET `scookies` = '1' where `code` = '$out_trade_no' and `status` = '已付款'");
			$_COOKIE['Cartlist'] = '';_setcookie('Cartlist',NULL);
			return "商品购买成功";
		}else{
			return '商品购买失败';
		}			

	}
	
}//


?>