<?php 

defined('G_IN_SYSTEM')or exit('No permission resources.');

//ini_set("display_errors","OFF");

class malipay_url extends SystemAction {
	public function __construct(){			
		$this->db=System::DOWN_sys_class('model');		
	} 	
	

	
	/* 同步通知 */
	public function qiantai(){
	
		sleep(2);
		include_once YYS_MANAGE."class/pay/lib/malipay/alipay_notify.class.php";	
		//计算得出通知验证结果
		$payconfig = System::DOWN_App_class("malipay","pay");	
		$alipay_config = $payconfig->get_config();
		$alipayNotify = new AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyReturn();
		if(!$verify_result) {		
			_notemobile("支付验证失败");exit;
		}
				
		$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'] == '未付款'){
				_notemobile("支付失败");			
			}else{
			if(empty($dingdaninfo['scookies'])){
				_notemobile("充值成功!<a href=".LOCAL_PATH."/mobile/home/userbalance>查看账户明细</a>");
			}else{
				if($dingdaninfo['scookies'] == '1'){					 
					header("location: ".LOCAL_PATH."/mobile/cart/paysuccess");
				}else{
					_notemobile("商品还未购买,请!<a href=".LOCAL_PATH."/member/cart/cartlist>返回购物车</a>重新购买商品");
				}					
			}
		}			
	}/* function end */
	
	
	/* 异步通知 */
	public function houtai(){			
		
			include_once YYS_MANAGE."class/pay/lib/malipay/alipay_notify.class.php";
			//计算得出通知验证结果
			$payconfig = System::DOWN_App_class("malipay","pay");	
			$alipay_config = $payconfig->get_config();
			$alipayNotify = new AlipayNotify($alipay_config);
			$verify_result = $alipayNotify->verifyNotify();

			if($verify_result) {
				//解析notify_data
				//注意：该功能PHP5环境及以上支持，需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件
				$doc = new DOMDocument();	
				if ($alipay_config['sign_type'] == 'MD5') {
					$doc->loadXML($_POST['notify_data']);
				}
				
				if ($alipay_config['sign_type'] == '0001') {
					$doc->loadXML($alipayNotify->decrypt($_POST['notify_data']));
				}
				
				if( ! empty($doc->getElementsByTagName( "notify" )->item(0)->nodeValue) ) {
					//商户订单号
					$this->out_trade_no = $out_trade_no = $doc->getElementsByTagName( "out_trade_no" )->item(0)->nodeValue;
					//支付宝交易号
					$trade_no = $doc->getElementsByTagName( "trade_no" )->item(0)->nodeValue;
					//交易状态
					$trade_status = $doc->getElementsByTagName( "trade_status" )->item(0)->nodeValue;
					
					if($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
						//TRADE_SUCCESS 	高级即时到账，买家付款成功后出现
						//TRADE_FINISHED    普通和高级(退款)会出现
						
						$ret = $this->updata_order();						
						if($ret == '已付款' || $ret == '充值完成' || $ret == '商品购买成功'){
							echo 'success';exit;
						}
						if($ret == '充值失败' || $ret == '商品购买失败'){
							echo $ret;exit;
						}
						echo "success";		//请不要修改或删除
					}					
				}
			} else {
				echo "fail";
			}	
	}
	
	
	/*支付与充值处理*/
	private function updata_order(){
		$pay_type =$this->db->YOne("SELECT * from `@#_payment` where `pay_class` = 'malipay' and `pay_start` = '1'");
		$out_trade_no = $this->out_trade_no;
		$this->db->tijiao_start();
		$dingdaninfo = $this->db->YOne("select * from `@#_yonghu_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 `@#_yonghu_addmoney_record` SET `pay_type` = '手机支付宝', `status` = '已付款' where `id` = '$dingdaninfo[id]' and `code` = '$dingdaninfo[code]'");
		$up_q2 = $this->db->Query("UPDATE `@#_yonghu` SET `money` = `money` + $c_money where (`uid` = '$uid')");			
		$up_q3 = $this->db->Query("INSERT INTO `@#_yonghu_zhanghao` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('$uid', '1', '账户', '充值', '$c_money', '$time')");
		
		if($up_q1 && $up_q2 && $up_q3){			
			$this->db->tijiao_commit();
		}else{
			$this->db->tijiao_rollback();
			return '充值失败';
		}			
		if(empty($dingdaninfo['scookies'])){					
			return "充值完成";	//充值完成	
		}
		
		$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'){
			$_COOKIE['Cartlist'] = '';_setcookie('Cartlist',NULL);			
			return '商品购买失败';	//商品购买失败			
		}		

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