<?php 

defined('G_IN_SYSTEM')or exit('No permission resources.');
ini_set("display_errors","OFF");
include dirname(__FILE__).'/lib/chinabank.class.php';
class chinabank_url extends SystemAction {
	private $out_trade_no;
	public function __construct(){			
		$this->db=System::DOWN_sys_class('model');		
	} 	
	
	
	public function qiantai(){	
		if(_is_mobile()){
			$message = '_notemobile';
		}else{
			$message = '_note';
		}
		
		$out_trade_no = $this->out_trade_no;
		$dingdaninfo = $this->db->YOne("select * from `@#_yonghu_addmoney_record` where `code` = '$out_trade_no'");
		if(!$dingdaninfo || $dingdaninfo['status'] == '未付款'){
		_note("支付成功,请进后台查看!",LOCAL_PATH."/member/home/userbalance");		
		}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");
				}					
			}
		}
	}
	
	
		/*
		private function qiantai(){	
		$out_trade_no = $this->out_trade_no;
			$dingdaninfo = $this->db->YOne("select * from `@#_yonghu_addmoney_record` where `code` = '$out_trade_no'");		
			$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
			$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i";

			if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap')){
			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>重新购买商品");
				}					
			}
		}
	  
	  }else{
	   	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(){

		$pay_type =$this->db->YOne("SELECT * from `@#_payment` where `pay_class` = 'chinabank' 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	
		
		$v_oid     =trim($_POST['v_oid']);       // 商户发送的v_oid定单编号   
		$v_pmode   =trim($_POST['v_pmode']);    // 支付方式（字符串）   
		$v_pstatus =trim($_POST['v_pstatus']);   //  支付状态 ：20（支付成功）；30（支付失败）
		$v_pstring =trim($_POST['v_pstring']);   // 支付结果信息 ： 支付完成（当v_pstatus=20时）；失败原因（当v_pstatus=30时,字符串）； 
		$v_amount  =trim($_POST['v_amount']);     // 订单实际支付金额
		$v_moneytype  =trim($_POST['v_moneytype']); //订单实际支付币种    
		$remark1   =trim($_POST['remark1' ]);      //备注字段1
		$remark2   =trim($_POST['remark2' ]);     //备注字段2
		$v_md5str  =trim($_POST['v_md5str' ]);   //拼凑后的MD5校验值  
        $md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$key));
		if ($v_md5str==$md5string)
		{
			if($v_pstatus=="20")
			{	
				if(!$v_oid){
					echo "返回参数错误";exit;	
				}
					$this->out_trade_no=$v_oid;
					$ret = $this->chinabank_chuli();					
					echo "ok";
					exit;
				
			}
						
		}else{
			 ;
		}
	
	 echo "error";

	}
	
	
	
	private function chinabank_chuli(){
		$pay_type =$this->db->YOne("SELECT * from `@#_payment` where `pay_class` = 'chinabank' and `pay_start` = '1'");
		$out_trade_no = $this->out_trade_no;
		$dingdaninfo = $this->db->YOne("select * from `@#_yonghu_addmoney_record` where `code` = '$out_trade_no'");
		if(!$dingdaninfo){ return false;}	//没有该订单,失败
		if($dingdaninfo['status'] == '已付款'){
			return '已付款';
		}
		$c_money = intval($dingdaninfo['money']);
		$uid = $dingdaninfo['uid'];
		$time = time();
		
		$this->db->tijiao_start();
		$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 '商品购买失败';
		}			

	}
	
}//

?>