where("code='weixin' and  type = 'payment' ")->find(); // 找到微信支付插件的配置
        //$config_value = unserialize($paymentPlugin['config_value']); // 配置反序列化
        $config_value=M("wx_user")->where('store_id',getMobileStoId())->find();
        $this->appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
        $this->mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
        $this->key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
        $this->appsecret = $config_value['appsecret']; // 公众帐号secert(仅JSAPI支付的时候需要配置),
        mlog( $this->appid.$this->mchid.$this->key.$this->appsecret,'recharge');
    }    
    /**
     * 生成支付代码
     * @param   array   $orderno      订单信
     * @param   array   $config_value    支付方式信息
     */
    function get_code($orderno, $config_value)
    {       
            $notify_url = SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin'; // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            //$notify_url = C('site_url').U('Home/Payment/notifyUrl',array('pay_code'=>'weixin')); // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            //$notify_url = C('site_url')."/index.php?m=Home&c=Payment&a=notifyUrl&pay_code=weixin";
            $order_amount=M("order")->where("parent_sn",$orderno)->sum("order_amount");
            $input = new WxPayUnifiedOrder();
            $input->SetBody("TPshop商品"); // 商品描述
            $input->SetAttach("weixin"); // 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
            $input->SetOut_trade_no($orderno); // 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
            //$input->SetTotal_fee($order_amount*100); // 订单总金额,单位为分,详见支付金额
            $input->SetTotal_fee(0.01*100); // 订单总金额,单位为分,详见支付金额
            $input->SetNotify_url($notify_url); // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input->SetTrade_type("NATIVE"); // 交易类型   取值如下:JSAPI,NATIVE,APP,详细说明见参数规定    NATIVE--原生扫码支付
            $input->SetProduct_id("123456789"); // 商品ID trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
            $notify = new NativePay();
            $result = $notify->GetPayUrl($input); // 获取生成二维码的地址
            $url2 = $result["code_url"];
            return '.') ';        
    }    
    /**
     * 服务器点对点响应操作给支付接口方调用
     * 
     */
    function response()
    {
        try {
            mlog("开始","backorder");
            require_once("example/notify.php");
            $notify = new PayNotifyCallBack();
            $notify->Handle(false);
        }catch(Exception $e){
            mlog($e->getMessage(),"backorder");
        }
    }
    
    /**
     * 页面跳转响应操作给支付接口方调用
     */
    function respond2()
    {
        // 微信扫码支付这里没有页面返回
    }
    //余额充值
    function getJSAPI_VOP($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>0))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/index', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("余额充值:" . $orderno);
        $input->SetAttach("weixin_vop");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //积分充值
    function getJSAPI_VOI($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>1))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/index', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("余额充值:" . $orderno);
        $input->SetAttach("weixin_voi");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //购买服务卡
    function getJSAPI_SER($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>2))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/shopping_card', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("购买服务卡:" . $orderno);
        $input->SetAttach("weixin_ser");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //等级卡 3:购买  4:续费
    function getJSAPI_VLC($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>array('in','3,4')))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/userinfo', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("购买等级卡:" . $orderno);
        $input->SetAttach("weixin_vlc");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //购买礼包
    function getJSAPI_LB($orderno)
    {
        $ord = M('libao_formvip')
            ->alias("a")
            ->join("libao_form b", "a.lbid=b.id", "left")
            ->where("a.number='" . $orderno . "'")
            ->field("a.*,b.lbprice")
            ->order("a.id desc")
            ->find();
        $go_url = U('Mobile/payment/payment_success', array('stoid' => $ord['store_id'], 'ptype' => 2));
        $back_url = U('Mobile/Libao/index', array('stoid' => $ord['store_id']));
        $order_amount = $ord['lbprice'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("购买礼包:" . $orderno);
        $input->SetAttach("weixin_lb");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //普通订单
    function getJSAPI($orderno)
    {
        $ord = M('Order')->where("parent_sn", $orderno)->select();
        $go_url = U('Mobile/payment/payment_success', array('stoid' => $ord[0]['store_id'],"orderno"=>$ord[0]['order_sn']));
        //$back_url = U('/Mobile/User/order_detail',array('id'=>$ord[0]['order_id'],'stoid'=>$ord[0]['store_id']));
        $back_url = U('/Mobile/payment/pay_error', array('ordno' => $ord[0]['order_sn'], 'stoid' => $ord[0]['store_id'], '支付失败'));
        $order_amount = M('Order')->where("parent_sn", $orderno)->sum('order_amount');
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:" . $orderno);
        $input->SetAttach("weixin");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        mlog("支付订单:" . $orderno, "getJSAPI/" . $ord['store_id']);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $getreturn_msg="支付参数错误";
            if ($order2['return_code']=="FAIL")
            {
                $getreturn_msg=$order2['return_msg'];
            }
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord[0]['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => $getreturn_msg];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //送礼神器使用
    function getJSAPI2($orderno){
        $go_url="";
        $ord=M('giftuser_order')->where("parent_sn", $orderno)->select();
        if($ord[0]['state']==1){
            //开始赠送
            $go_url = U('Mobile/Giftuser/to_send',array('stoid'=>$ord[0]['store_id'],'ordno'=>$orderno));
        }else{
            //购物车购买要进入已购列表
            $go_url = U('Mobile/payment/payment_success2',array('stoid'=>$ord[0]['store_id']));
        }
        $back_url = U('/Mobile/payment/pay_error2',array('ordno'=>$ord[0]['order_sn'],'stoid'=>$ord[0]['store_id'],'支付失败'));
        $order_amount = M('giftuser_order')->where("parent_sn", $orderno)->sum('order_amount');
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid,$this->key,$this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:".$orderno);
        $input->SetAttach("weixin");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL().'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            //dump($order2); exit();
            //echo '统一下单支付单信息
';        
    }    
    /**
     * 服务器点对点响应操作给支付接口方调用
     * 
     */
    function response()
    {
        try {
            mlog("开始","backorder");
            require_once("example/notify.php");
            $notify = new PayNotifyCallBack();
            $notify->Handle(false);
        }catch(Exception $e){
            mlog($e->getMessage(),"backorder");
        }
    }
    
    /**
     * 页面跳转响应操作给支付接口方调用
     */
    function respond2()
    {
        // 微信扫码支付这里没有页面返回
    }
    //余额充值
    function getJSAPI_VOP($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>0))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/index', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("余额充值:" . $orderno);
        $input->SetAttach("weixin_vop");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //积分充值
    function getJSAPI_VOI($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>1))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/index', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("余额充值:" . $orderno);
        $input->SetAttach("weixin_voi");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //购买服务卡
    function getJSAPI_SER($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>2))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/shopping_card', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("购买服务卡:" . $orderno);
        $input->SetAttach("weixin_ser");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //等级卡 3:购买  4:续费
    function getJSAPI_VLC($orderno)
    {
        $ord = M('recharge')->where(array('order_sn'=>$orderno,'recharge_type'=>array('in','3,4')))->find();
        $go_url = U('Mobile/payment/recharge_success', array('stoid' => $ord['store_id'], 'order_id' => $ord['order_id']));
        $back_url = U('Mobile/User/userinfo', array('stoid' => $ord['store_id']));
        $order_amount = $ord['account'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("购买等级卡:" . $orderno);
        $input->SetAttach("weixin_vlc");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //购买礼包
    function getJSAPI_LB($orderno)
    {
        $ord = M('libao_formvip')
            ->alias("a")
            ->join("libao_form b", "a.lbid=b.id", "left")
            ->where("a.number='" . $orderno . "'")
            ->field("a.*,b.lbprice")
            ->order("a.id desc")
            ->find();
        $go_url = U('Mobile/payment/payment_success', array('stoid' => $ord['store_id'], 'ptype' => 2));
        $back_url = U('Mobile/Libao/index', array('stoid' => $ord['store_id']));
        $order_amount = $ord['lbprice'];
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("购买礼包:" . $orderno);
        $input->SetAttach("weixin_lb");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => '支付参数错误'];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //普通订单
    function getJSAPI($orderno)
    {
        $ord = M('Order')->where("parent_sn", $orderno)->select();
        $go_url = U('Mobile/payment/payment_success', array('stoid' => $ord[0]['store_id'],"orderno"=>$ord[0]['order_sn']));
        //$back_url = U('/Mobile/User/order_detail',array('id'=>$ord[0]['order_id'],'stoid'=>$ord[0]['store_id']));
        $back_url = U('/Mobile/payment/pay_error', array('ordno' => $ord[0]['order_sn'], 'stoid' => $ord[0]['store_id'], '支付失败'));
        $order_amount = M('Order')->where("parent_sn", $orderno)->sum('order_amount');
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid, $this->key, $this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:" . $orderno);
        $input->SetAttach("weixin");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL() . '/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        mlog("支付订单:" . $orderno, "getJSAPI/" . $ord['store_id']);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            $getreturn_msg="支付参数错误";
            if ($order2['return_code']=="FAIL")
            {
                $getreturn_msg=$order2['return_msg'];
            }
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            mlog(json_encode($jsApiParameters), "getJSAPI/" . $ord[0]['store_id']);
            if ($jsApiParameters == -999) {
                return ['code' => -1, 'msg' => $getreturn_msg];
            }
        } catch (Exception $e) {
            mlog('error:' . $e->getMessage(), "getJSAPI/" . $ord['store_id']);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //送礼神器使用
    function getJSAPI2($orderno){
        $go_url="";
        $ord=M('giftuser_order')->where("parent_sn", $orderno)->select();
        if($ord[0]['state']==1){
            //开始赠送
            $go_url = U('Mobile/Giftuser/to_send',array('stoid'=>$ord[0]['store_id'],'ordno'=>$orderno));
        }else{
            //购物车购买要进入已购列表
            $go_url = U('Mobile/payment/payment_success2',array('stoid'=>$ord[0]['store_id']));
        }
        $back_url = U('/Mobile/payment/pay_error2',array('ordno'=>$ord[0]['order_sn'],'stoid'=>$ord[0]['store_id'],'支付失败'));
        $order_amount = M('giftuser_order')->where("parent_sn", $orderno)->sum('order_amount');
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid,$this->key,$this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:".$orderno);
        $input->SetAttach("weixin");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL().'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            //dump($order2); exit();
            //echo '统一下单支付单信息
';
            //printf_info($order);exit;
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            if ($jsApiParameters == -999) {
                return ['code'=>-1,'msg'=>'支付参数错误'];
            }
        }catch(Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //天天拼单使用
    function getJSAPI3($orderno){
        $go_url="";
        $ord=M('order')->where("order_sn", $orderno)->select();
        //支付成功
        $go_url = U('Mobile/team/team_success',
            array('stoid'=>$ord[0]['store_id'],'orderno'=>$ord[0]['order_sn'],'wxok'=>1));
         //支付失败
        $back_url = U('/Mobile/team/team_error',array('orderno'=>$ord[0]['order_sn'],'stoid'=>$ord[0]['store_id']));
        $order_amount = M('order')->where("order_sn", $orderno)->sum('order_amount');
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid,$this->key,$this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:".$orderno);
        $input->SetAttach("weixin");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL().'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            //dump($order2); exit();
            //echo '统一下单支付单信息
';
            //printf_info($order);exit;
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            if ($jsApiParameters == -999) {
                return ['code'=>-1,'msg'=>'支付参数错误'];
            }
        }catch(Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    //天天拼单使用
    function getJSAPI_pt_wk($orderno){
        $go_url="";
        $ord=M('order')->where("order_sn", $orderno)->find();
        //支付成功
        $go_url = U('Mobile/team/team_success',array('stoid'=>$ord['store_id'],'orderno'=>$ord['order_sn']));
        //支付失败
        $back_url = U('/Mobile/team/team_error',array('orderno'=>$ord['order_sn'],'stoid'=>$ord['store_id'],'payfail'=>1));
        $order_amount = M('order')->where("order_sn", $orderno)->sum('pt_tail_money');
        mlog('333-'.$order_amount,'getCode_pt/'.$ord['store_id']);
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid,$this->key,$this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //$orderno.='_wk';
        $orderno.='_'.date("s").rand(10, 99);
        mlog("1:".$orderno,"getJSAPI_pt_wk/".$ord['store_id']);
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:".$orderno);
        $input->SetAttach("weixin");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL().'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            //dump($order2); exit();
            //echo '统一下单支付单信息
';
            //printf_info($order);exit;
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            if ($jsApiParameters == -999) {
                return ['code'=>-1,'msg'=>'支付参数错误'];
            }
        }catch(Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
    /**
     * 微信退款,传入微信parent_sn,$total_fee总额,$refund_fee退款金额,$path路径
     */
    function refund($ordno,$total_fee,$refund_fee,$path,$appid="",$mchid="",$key=""){
            $out_trade_no = $ordno;
            $input = new WxPayRefund();
            $input->SetOut_trade_no($out_trade_no);
            $input->SetTotal_fee($total_fee*100);
            $input->SetRefund_fee($refund_fee*100);
            $input->SetOut_refund_no(date("YmdHis"));
            if(empty($appid)) $appid=$this->appid;
            if(empty($mchid)) $mchid=$this->mchid;
            if(empty($key)) $key=$this->key;
            $input->SetOp_user_id($mchid);
            $rs=WxPayApi::refund($input,6,$path,$appid,$mchid,$key);
            return $rs;
    }
    //设置
    function setCong($stoid){
        $config_value=M("wx_user")->where('store_id',$stoid)->find();
        $this->appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
        $this->mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
        $this->key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
        $this->appsecret = $config_value['appsecret']; // 公众帐号secert(仅JSAPI支付的时候需要配置),
    }
    //查询订单
    public function query_odr($odrno,$stoid){
            $config_value=M("wx_user")->where('store_id',$stoid)->field('appid,mchid,mchkey')->find();
            $this->appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
            $this->mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
            $this->key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
            $input = new WxPayOrderQuery();
            $input->SetOut_trade_no($odrno);
            $result = WxPayApi::orderQuery($input,$this->appid,$this->mchid,$this->key);
            mlog("pro查询订单".json_encode($result),'query_odr/'.$stoid);
            if(array_key_exists("return_code", $result)
                && array_key_exists("result_code", $result)
                && $result["return_code"] == "SUCCESS"
                && $result["result_code"] == "SUCCESS")
            {
                return true;
            }
            return false;
    }
    //查询退款订单
    public function query_refund_odr($odrno,$stoid,$rf_no=null){
        $config_value=M("wx_user")->where('store_id',$stoid)->field('appid,mchid,mchkey')->find();
        $this->appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
        $this->mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
        $this->key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
        $input = new WxPayRefundQuery();
        $input->SetOut_trade_no($odrno);
        if($rf_no)$input->SetOut_refund_no($rf_no);
        $result = WxPayApi::refundQuery($input,12, $this->appid, $this->mchid,$this->key);
        mlog("pro查询订单".json_encode($result),'query_odr/'.$stoid);
        return $result;
    }
    /**
     * 微信退款,传入微信parent_sn,$total_fee总额,$refund_fee退款金额,$path路径
     */
    function refund2($ordno,$total_fee,$refund_fee,$path,$stoid,$rf_no="",$order_source_type=0){
        $config_value=M("wx_user")->where('store_id',$stoid)->field('appid,mchid,mchkey')->find();
        //---如果订单的来源是小程序的时候---
        if($order_source_type){
            $path_wxapp= BASE_PATH . 'public/cert/' . $stoid . '/weapp/';
            $conf_app_value = M("weapp")->where('store_id', $stoid)->field('appid,mchid,mchkey')->find();
            if($config_value['mchid']!=$conf_app_value['mchid']){
                $config_value=$conf_app_value;
                $path=$path_wxapp;
            }
        }
        $appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
        $mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
        $key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
        $out_trade_no = $ordno;
        $input = new WxPayRefund();
        $input->SetOut_trade_no($out_trade_no);
        $input->SetTotal_fee($total_fee*100);
        $input->SetRefund_fee($refund_fee*100);
        if(empty($rf_no)) $rf_no='t'.date("YmdHis").get_total_millisecond().rand(1000, 9999).$stoid;
        $input->SetOut_refund_no($rf_no);
        $input->SetOp_user_id($mchid);
        $rs=WxPayApi::refund($input,12,$path,$appid,$mchid,$key);
        mlog('退款结果:'.json_encode($rs),'refund/'.$stoid);
        return $rs;
    }
    function refund_wk($pay_sn,$total_fee,$refund_fee,$path,$stoid,$rf_no="",$order_source_type=0){
        $config_value=M("wx_user")->where('store_id',$stoid)->field('appid,mchid,mchkey')->find();
        //---如果订单的来源是小程序的时候---
        if($order_source_type){
            $path_wxapp= BASE_PATH . 'public/cert/' . $stoid . '/weapp/';
            $conf_app_value = M("weapp")->where('store_id', $stoid)->field('appid,mchid,mchkey')->find();
            if($config_value['mchid']!=$conf_app_value['mchid']){
                $config_value=$conf_app_value;
                $path=$path_wxapp;
            }
        }
        $appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
        $mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
        $key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
        //$out_trade_no = $ordno;
        $input = new WxPayRefund();
        $input->SetTransaction_id($pay_sn);
        $input->SetTotal_fee($total_fee*100);
        $input->SetRefund_fee($refund_fee*100);
        if(empty($rf_no)) $rf_no='t'.date("YmdHis").get_total_millisecond().rand(1000, 9999).$stoid;
        $input->SetOut_refund_no($rf_no);
        $input->SetOp_user_id($mchid);
        $rs=WxPayApi::refund($input,12,$path,$appid,$mchid,$key);
        mlog('退款结果:'.json_encode($rs),'refund/'.$stoid);
        return $rs;
    }
    /*------购买礼包等参数的入参------*/
    function getJSAPI_new_lb($ord){
        $go_url="";
        //支付成功
        $go_url = U('Mobile/Payment/pay_lb_success',array('stoid'=>$ord['store_id'],'orderno'=>$ord['order_sn']));
        //支付失败
        $back_url = U('/Mobile/Payment/pay_lb_error',array('orderno'=>$ord['order_sn'],'stoid'=>$ord['store_id']));
        $order_amount =$ord['pay_money'];
        mlog('333-'.$order_amount,'getJSAPI_new_lb/'.$ord['store_id']);
        //①、获取用户openid
        $tools = new JsApiPay();
        $tools->setConf($this->appid,$this->key,$this->appsecret);
        //$openId = $tools->GetOpenid();
        $openId = $_SESSION['openid'];
        //$orderno.='_wk';
        $orderno=$ord['order_sn'];
        mlog("1:".$orderno,"getJSAPI_new_lb/".$ord['store_id']);
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("支付订单:".$orderno);
        $input->SetAttach("weixin_new_lb");
        $input->SetOut_trade_no($orderno);
        $input->SetTotal_fee($order_amount*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("tp_wx_pay");
        //$input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetNotify_url(curHostURL().'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        try {
            $order2 = WxPayApi::unifiedOrder($input, $this->appid, $this->mchid, $this->key);
            //dump($order2); exit();
            //echo '统一下单支付单信息
';
            //printf_info($order);exit;
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            if ($jsApiParameters == -999) {
                return ['code'=>-1,'msg'=>'支付参数错误'];
            }
        }catch(Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        $html = <<
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',$jsApiParameters,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				 if(res.err_msg == "get_brand_wcpay_request:ok") {
				    location.href='$go_url';
				 }else{
                    //alert(res.err_code+res.err_desc+res.err_msg);
				    location.href='$back_url';
				 }
			}
		);
	}
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	callpay();
	
EOF;
        return $html;
    }
}