<?php
vendor('Weixinpay.WxPayApiPay');
vendor('Weixinpay.WxPayJsApiPay');
vendor('Weixinpay.WxPayConfig');
vendor('Weixinpay.sTopLogger');
vendor('Weixinpay.phpqrcode');
 class wxpayAction extends FirstendAction{       
       public function _initialize() {
	    parent::_initialize();	    		
      }    
    public function index(){	    
		$link = I('link');		
		$price = I('price');
		if(!$price){
		$price = C('ftx_copypay_money');	
		}
		$ip = get_client_ip();
		$order = md5($link.$ip);				
		$back = C('ftx_site_url').'/?link='.urldecode($link).'&mod=copypay&price='.$price.'&ip='.$ip;				
		$url = C('ftx_site_url')."weixinpay/index/info/".$order."?bc=".base64_encode($back);	
		$data['image'] = C('ftx_site_url')."?m=qcode&data=".urlencode($url);
		$data['order'] = $order;
		$data['price'] = $price;	
		header("Access-Control-Allow-Origin: *");
		$this->ajaxReturn(1,'',$data);
    }
	public function qm(){	    
		$x = I('x');	
		$s = I('ss');	
		$n = I('n');	
		$z = I('z');
		$p = I('p');	
		$o = I('o');	
		$jumpurl = U('/qiming/'.$x.'/'.$s.'/'.$n.'/'.$z.'/'.$p,true,true,false,true);
		$ordersn = md5($jumpurl.time());							
		$back = C('ftx_site_url').'/?x='.$x.'&s='.$s.'&n='.$n.'&z='.$z.'&o='.$o.'&p='.$p;				
		$url = C('ftx_site_url')."weixinpay/index/info/".$ordersn."?tem=".base64_encode($back);	
		$image = C('ftx_site_url')."?m=qcode&data=".urlencode($url);			
		header("Access-Control-Allow-Origin: *");
		$this->ajaxReturn(1,$ordersn,$image);
    }
	public function cha(){		    
	    header("Access-Control-Allow-Origin: *");
		$link = I('link');	
		$link = str_replace('?order=1','',$link);
		$ip = get_client_ip();		
		$order = md5($link.$ip);
		$item = D('ipay_log')->where(array('out_trade_no'=>$order,'pass'=>1))->find();	
		if($item){		
		$this->ajaxReturn(1);	
		}else{		
		$data['xianjia'] = C('ftx_copypay_money');	
		$data['yuanjia'] = $data['xianjia']*3;	
		$data['kefu'] = C('ftx_qq');
		$data['gzh'] = C('ftx_gzh');
		$data['ecode'] = C('ftx_site_url').C('ftx_wx_pic');
		$this->ajaxReturn(0,'',$data,$ip);		
		}			
    }
	public function yz(){		    
	    header("Access-Control-Allow-Origin: *");
		$code = I('code');	
		if($code == C('ftx_yzcode')){		
		$this->ajaxReturn(1);	
		}else{		
		$this->ajaxReturn(0);		
		}	
    }
	public function chaxun(){
		$order = I('order');
		$map['out_trade_no|trade_no'] = $order;
		$item = D('qm_log')->where($map)->find();
		if($item){
		$info = json_decode($item['info'], true);	
		$url = U('/order/'.$item['trade_no'].'/'.$info['a'],true,true,false,true);	
		if(IS_POST && IS_AJAX){
		$this->ajaxReturn(1,'',$url);	
		}else{
		header('Content-Type:text/plain; charset=utf-8');
		header("Location:" .$url);		
		exit();	
		}						
		}else{
		if(IS_POST && IS_AJAX){	
		$this->ajaxReturn(0,'未查询到支付订单，可能网络延时，请稍后再试');		
		}else{
	    $html = '<html>';	   
	    $html .= '<head>';
	    $html .= '<title>支付查询中...</title>';
	    $html .= '<meta http-equiv="x-dns-prefetch-control" content="on"/>';
	    $html .= '<meta name="apple-mobile-web-app-capable" content="yes"/>';
	    $html .= '<meta content="telephone=no" name="format-detection"/>';
	    $html .= '<META name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1, minimum-scale=1, user-scalable=no">';
		$html .= '<div style="width:100%;padding-top:100px;text-align:center;">订单延时提醒</div>';
	    $html .= '<div style="width:100%;padding-top:20px;text-align:center;">订单支付状态查询中，<span id="countdown">5</span>秒后将为您跳转</div>';
	    $html .= '<script type="text/javascript">';
	    $html .= 'var countdownElement = document.getElementById("countdown");';
	    $html .= 'var countdown = 5;';
	    $html .= 'function updateCountdown() {';
	    $html .= 'countdown--;';
	    $html .= 'countdownElement.innerHTML = countdown;';
	    $html .= 'if (countdown === 0) {';
	    $html .= 'clearInterval(interval);';
	    $html .= 'location.reload();';
	    $html .= '}';
	    $html .= '}';
	    $html .= 'var interval = setInterval(updateCountdown, 1000);';
		$html .= '</script>';
	    $html .= '</body>';
	    $html .= '</html>';
		echo $html;
		exit();
		}
		}
	}
	public function cx(){		
	    $out_trade_no = I('order');		
		if(isset($out_trade_no) && $out_trade_no != ""){
	try{
		$tools = new JsApiPay();		
		$input = new WxPayOrderQuery();
		$input->SetOut_trade_no($out_trade_no);
		$config = new WxPayConfig();
		$order = WxPayApi::orderQuery($config, $input);		
		$data = $this->printf_info($order);
		header("Access-Control-Allow-Origin: *");
		if($data['trade_state_desc']=='支付成功' && $data['trade_state']=='SUCCESS'){
		$this->ajaxReturn(1);	
		}else{
		$this->ajaxReturn(0);		
		}
		
	} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);
	}
	exit();
    }	 	
	}
	public function printf_info($data){
    foreach($data as $key=>$value){
        $item[$key]=$value;
    }
	return $item;
   }
    
 }
 ?>