<?php 

defined('G_IN_SYSTEM')or exit('No permission resources.');
ini_set("display_errors","OFF");
class alipay_url1 extends SystemAction {
	public function __construct(){
		$this->db=System::load_sys_class('model');
	}

	
	public function qiantai(){	
		sleep(2);
		$out_trade_no = $_GET['out_trade_no'];	//商户订单号
		$dingdaninfo = $this->db->YOne("select * from `@#_yonghu_addmoney_record` where `code` = '$out_trade_no'");
		if(!$dingdaninfo || $dingdaninfo['status'] == '未付款'){
			_note("支付失败");			
		}else{
			if(empty($dingdaninfo['scookies'])){
				_note("充值成功!",LOCAL_PATH."/member/home/userbalance");
			}else{
				if($dingdaninfo['scookies'] == '1'){
					_note("支付成功!",LOCAL_PATH."/member/cart/paysuccess");
				}else{
					_note("商品还未购买,请重新购买商品!",LOCAL_PATH."/member/cart/cartlist");
				}
					
			}
		}
	}
	
	public function houtai(){
		$sign        =trim(htmlspecialchars($_POST['key']));//回调签名
        $zongji_fee   =trim(htmlspecialchars($_POST['money']));//收款金额
		$out_trade_no =trim(htmlspecialchars($_POST['ddh']));// $_POST['out_trade_no'];	//商户订单号 名称
		$trade_no =trim(htmlspecialchars($_POST['ddh'])); //$_POST['trade_no'];			//支付宝交易号
		$trade_status='WAIT_SELLER_SEND_GOODS';
		if($sign!='336699qqww'){exit();}
	
		//开始处理及时到账和担保交易订单
		if($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS' || $trade_status == 'WAIT_SELLER_SEND_GOODS') {
			
			$dingdaninfo = $this->db->GetOne("select * from `@#_member_addmoney_record` where `code` = '$trade_no' and `status` = '未付款'");
			
			if(!$dingdaninfo){	echo "fail";exit;}	//没有该订单,失败

			$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){
			
			}else{
				echo "fail";exit;
			}			
			if(empty($dingdaninfo['scookies'])){					
					echo "okokok";exit;	//充值完成			
			}			
			$scookies = unserialize($dingdaninfo['scookies']);			
			$pay = System::DOWN_App_class('pay','pay');			
			$pay->scookie = $scookies;	

			$ok = $pay->init($uid,$pay_type['pay_id'],'go_record');	//云购商品	
			if($ok != 'ok'){
				_setcookie('Cartlist',NULL);
				echo "fail";exit;	//商品购买失败			
			}			
			$check = $pay->start_pay(1);
			if($check){
				$this->db->Query("UPDATE `@#_member_addmoney_record` SET `scookies` = '1' where `code` = '$out_trade_no' and `status` = '已付款'");
				_setcookie('Cartlist',NULL);
				echo "okokok";exit;			
			}else{
				echo "fail";exit;
			}
		
		}//开始处理订单结束
				

	}//function end
	
}//

?>