<?php
	vendor('Weixinpay.WxPayApiPay');
	vendor('Weixinpay.WxPayJsApiPay');
	vendor('Weixinpay.WxPayNativePay');
	vendor('Weixinpay.WxPayConfig');
	vendor('Weixinpay.sTopLogger');
class weixinpayAction extends FirstendAction {

	public function _initialize() {
        parent::_initialize();	
    }        
	
	public function index() {			
		$Out_trade_no = I('info');			
		$is = I('is');
		$tem = I('tem');
		$type = I('type');	
		if($tem){
		$p = I('p');	
		$money = C('ftx_qm_price');	
		$page = C('ftx_qm_page');
		$title = "在线起名";
        if($is){		
		$x = I('x');	
		$s = I('ss');	
		$n = I('n');	
		$z = I('z');
		$o = I('o');
		if($o){
		$item = D('qm_log')->where(array('trade_no'=>$o))->find();	
		if($item['b']){
		$p = $item['b'];	
		}
		}
		$in['a'] = $p+1;
		$in['b'] = $p+$page;
		$in['x'] = $x;
		$in['s'] = $s;
		$in['n'] = $n;
		$in['z'] = $z;				
		$attach = json_encode($in);
		$jumpurl = U('/qiming/'.$x.'/'.$s.'/'.$n.'/'.$z.'/'.$p,true,true,false,true);
		$ordersn = md5($jumpurl.time());	
		if($type=='wx'){
		try{
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();	
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);  
		$input->SetAttach($attach);
        $input->SetOut_trade_no($ordersn);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/payqm');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);        
		$jsApiParameters = $tools->GetJsApiParameters($order);				
		$this->assign('jsApiParameters',$jsApiParameters);
		} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);		
        }						
		$backurl = C('ftx_site_url').'?m=wxpay&a=chaxun&order='.$ordersn;		
		$this->assign('backurl',$backurl);	
		$this->assign('title', $title);	
		$this->assign('money', $money);	
		$this->display();	
		}
		if($type=='h5'){
		$arr['h5_info']['type'] = "Wap";
		$arr['h5_info']['wap_url'] = C('ftx_site_url');
		$arr['h5_info']['wap_name'] = C('ftx_site_name');
		$json = json_encode($arr);        
		$notify = new NativePay();        		
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);
        $input->SetAttach($attach);
        $input->SetOut_trade_no($ordersn);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/payqm');
        $input->SetTrade_type("MWEB");		
        $input->SetScene_info("$json");
        $result = $notify->GetH5PayUrl($input);		
		//file_put_contents(FTX_DATA_PATH."weixinpay_qm",serialize($result));					
        $url = $result["mweb_url"];  			
        $backurl = C('ftx_site_url').'?m=wxpay&a=chaxun&order='.$ordersn;	
		$url = $url.'&redirect_url='.urlencode($backurl);				
		$this->assign('url', $url);	
		$this->display('h5');	
		}			
		}else{			
		$tem = base64_decode($tem);
		$u = parse_url($tem);		
		parse_str($u['query'], $params);
		$x = $params['x'];	
		$s = $params['s'];
		$n = $params['n'];	
		$z = $params['z'];
		$p = $params['p'];
		$o = $params['o'];
		if($o){
		$item = D('qm_log')->where(array('trade_no'=>$o))->find();	
		if($item['b']){
		$p = $item['b'];	
		}
		}
		$in['a'] = $p+1;
		$in['b'] = $p+$page;
		$in['x'] = $x;
		$in['s'] = $s;
		$in['n'] = $n;
		$in['z'] = $z;		
		$attach = json_encode($in);
		$backurl = C('ftx_site_url').'?m=wxpay&a=chaxun&order='.$Out_trade_no;		
		try{
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();			
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);  
		$input->SetAttach($attach);
        $input->SetOut_trade_no($Out_trade_no);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/payqm');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);
		$jsApiParameters = $tools->GetJsApiParameters($order);	
		$this->assign('jsApiParameters',$jsApiParameters);
		} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);		
        }			
		$this->assign('money', $money);
		$this->assign('title', $title);
		$this->assign('backurl', $backurl);	
		$this->display();
		}
		//起名止
		}else{
		if($is){			
		$price = I('price');	
		$link = I('link');	
		$type = I('type');	
		$mod = I('mod');	
		if($mod == 'copypay'){			
		$backurl = $link;	
		$jumpurl = $backurl;
		$ip = get_client_ip();
		$order_copy = md5($backurl.$ip);
		$data['order'] = $order_copy;	
		$data['row'] = $link;
		$data['add_time'] = time();
		D('wpay_log')->add($data);	
		if($price){
		$money = $price;	
		}else{
		$money = C('ftx_copypay_money');	
		}			
		$title = "付费复制";
		if($type=='wx'){
		try{
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();	
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);  
		$input->SetAttach($order_copy);
        $input->SetOut_trade_no($order_copy);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/paycopy');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);        
		$jsApiParameters = $tools->GetJsApiParameters($order);				
		$this->assign('jsApiParameters',$jsApiParameters);
		} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);		
        }						
		$this->assign('backurl',$jumpurl);	
		$this->assign('title', $title);	
		$this->assign('money', $money);	
		$this->display();	
		}
		if($type=='h5'){
		$arr['h5_info']['type'] = "Wap";
		$arr['h5_info']['wap_url'] = C('ftx_site_url');
		$arr['h5_info']['wap_name'] = C('ftx_site_name');
		$json = json_encode($arr);        
		$notify = new NativePay();        		
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);
        $input->SetAttach($order_copy);
        $input->SetOut_trade_no($order_copy);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/paycopy');
        $input->SetTrade_type("MWEB");		
        $input->SetScene_info("$json");
        $result = $notify->GetH5PayUrl($input);		
		//file_put_contents(FTX_DATA_PATH."weixinpay_copy",serialize($result));					
        $url = $result["mweb_url"];  
		if($is=='ios'){
		$jumpurl = urlencode($jumpurl);
		$jumpback = urlencode(C('ftx_site_url').'to?link='.$jumpurl);
		$url = $url.'&redirect_url='.$jumpback;	
		}	
		$this->assign('url', $url);	
		$this->display('h5');	
		}		
		}elseif($mod == 'downpay'){			
		$backurl = $link;	
		$jumpurl = $backurl;
		$ip = get_client_ip();
		$order_copy = md5($backurl.$ip);
		$data['order'] = $order_copy;	
		$data['row'] = $link;
		$data['add_time'] = time();
		D('wpay_log')->add($data);	
		if($price){
		$money = $price;	
		}else{
		$money = C('ftx_copypay_money');	
		}	
		$title = "解锁文档下载";
		if($type=='wx'){
		try{
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();	
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);  
		$input->SetAttach($order_copy);
        $input->SetOut_trade_no($order_copy);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));        
        $input->SetNotify_url(C('ftx_site_url').'notify/paydown');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);        
		$jsApiParameters = $tools->GetJsApiParameters($order);				
		$this->assign('jsApiParameters',$jsApiParameters);
		} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);		
        }						
		$this->assign('backurl',$jumpurl);	
		$this->assign('title', $title);	
		$this->assign('money', $money);	
		$this->display();	
		}
		if($type=='h5'){
		$arr['h5_info']['type'] = "Wap";
		$arr['h5_info']['wap_url'] = C('ftx_site_url');
		$arr['h5_info']['wap_name'] = C('ftx_site_name');
		$json = json_encode($arr);        
		$notify = new NativePay();        		
        $input = new WxPayUnifiedOrder();
        $input->SetBody($title);
        $input->SetAttach($order_copy);
        $input->SetOut_trade_no($order_copy);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));        
        $input->SetNotify_url(C('ftx_site_url').'notify/paydown');
        $input->SetTrade_type("MWEB");		
        $input->SetScene_info("$json");
        $result = $notify->GetH5PayUrl($input);
        $url = $result["mweb_url"];  
		if($is=='ios'){
		$jumpurl = urlencode($jumpurl);
		$jumpback = urlencode(C('ftx_site_url').'to?link='.$jumpurl);
		$url = $url.'&redirect_url='.$jumpback;	
		}					
		//file_put_contents(FTX_DATA_PATH."weixinpay_down",$url);	
		$this->assign('url', $url);	
		$this->display('h5');	
		}		
		}
		}else{
		$bc = I('bc');	
		$bc = base64_decode($bc);
		$u = parse_url($bc);		
		parse_str($u['query'], $params);					
		$mod = $params['mod'];	
		if($mod == 'copypay'){			
		$data['order'] = $Out_trade_no;	
		$data['row'] = $params['link'];
		$data['add_time'] = time();	
		$money = $params['price'];
		D('wpay_log')->add($data);
		$backurl = $params['link'];
		try{
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();			
        $input = new WxPayUnifiedOrder();
        $input->SetBody('付费复制');  
		$input->SetAttach($Out_trade_no);
        $input->SetOut_trade_no($Out_trade_no);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/paycopy');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);
		$jsApiParameters = $tools->GetJsApiParameters($order);	
		$this->assign('jsApiParameters',$jsApiParameters);
		} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);		
        }	
		}elseif($mod == 'downpay'){
		$money = $params['price'];	
		$data['order'] = $Out_trade_no;	
		$data['row'] = $params['link'];
		$data['add_time'] = time();	
		D('wpay_log')->add($data);
		$backurl = $params['link'];
		try{
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();			
        $input = new WxPayUnifiedOrder();
        $input->SetBody('付费下载');  
		$input->SetAttach($Out_trade_no);
        $input->SetOut_trade_no($Out_trade_no);
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));        
        $input->SetNotify_url(C('ftx_site_url').'notify/paydown');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);
		$jsApiParameters = $tools->GetJsApiParameters($order);	
		$this->assign('jsApiParameters',$jsApiParameters);
		} catch(Exception $e) {
		$logHandler= new sTopLogger;
		$logHandler->conf["log_file"] = "data/weixinpay/top_biz_err/" . date("Y-m-d") . ".log";	
	    $logHandler->log($e);		
        }	
		}
		$this->assign('money', $money);
		$this->assign('title', $title);
		$this->assign('backurl', $backurl);	
		$this->display();
		}
		}
    }

    
}

