<?php
vendor('Weixinpay.WxPayApiPay');
vendor('Weixinpay.WxPayJsApiPay');
vendor('Weixinpay.WxPayConfig');
vendor('Weixinpay.sTopLogger');
vendor('Weixinpay.phpqrcode');
 class downpayAction extends FirstendAction{       
       public function _initialize() {
	    parent::_initialize();	    		
      }    
    public function index(){	    
		$link = I('link');		
		$ip = get_client_ip();
		$price = I('price');
		if(!$price){
		$price = C('ftx_downpay_money');	
		}
		$order = md5($link.$ip);				
		$back = C('ftx_site_url').'/?link='.urldecode($link).'&mod=downpay&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 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_downpay_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);		
		}
		
    }
    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 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;
   }
    
 }
 ?>