<?php
require '../conn/conn.php';
require '../conn/function.php';
$action=$_GET["action"];
$no=t($_GET["no"]);

if($action=="check"){
    $O=getrs("select * from sl_orders where O_no='$no'");
    if($O["O_state"]==1){
        die("{\"msg\":\"success\",\"url\":\"return.php?out_trade_no=$no\"}");
    }else{
        die("{\"msg\":\"error\"}");
    }
}

$O=getrs("select * from sl_orders where O_no='$no'");
$money=$O["O_price"]*$O["O_num"];
$title=$O["O_title"];

if(isWeixin()){
    $notify_url=gethttp().$domain."/shop/wxpay_notify.php";
    $timeStamp=time();
    $code=$_GET["code"];
    
    if($code==""){
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$C_wxpay_appid&redirect_uri=".urlencode(gethttp().$domain."/shop/wxpay.php?no=$no")."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        header("location:".$url);
    }else{
        $info=GetBody("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$C_wxpay_appid&secret=$C_wxpay_appsecret&code=".$code."&grant_type=authorization_code","","GET");
        $openid=json_decode($info)->openid;
        $fee=intval($money*100);
        $sign=strtoupper(md5("appid=".$C_wxpay_appid."&attach=".$no."&body=".$title."&mch_id=".$C_wxpay_mchid."&nonce_str=".$no."&notify_url=".$notify_url."&openid=".$openid."&out_trade_no=".$no."&spbill_create_ip=127.0.0.1&total_fee=".$fee."&trade_type=JSAPI&key=".$C_wxpay_key));
        $info=GetBody("https://api.mch.weixin.qq.com/pay/unifiedorder","<xml><appid>".$C_wxpay_appid."</appid><attach>".$no."</attach><body>".$title."</body><mch_id>".$C_wxpay_mchid."</mch_id><nonce_str>".$no."</nonce_str><notify_url>".$notify_url."</notify_url><openid>".$openid."</openid><out_trade_no>".$no."</out_trade_no><spbill_create_ip>127.0.0.1</spbill_create_ip><total_fee>".$fee."</total_fee><trade_type>JSAPI</trade_type><sign>".$sign."</sign></xml>");
        
        $postObj = simplexml_load_string($info);
        $prepay_id=$postObj->prepay_id;
        
        $paySign=strtoupper(md5("appId=".$C_wxpay_appid."&nonceStr=".$no."&package=prepay_id=".$prepay_id."&signType=MD5&timeStamp=".$timeStamp."&key=".$C_wxpay_key));
        die("<!DOCTYPE html>
    <html>
    <head>
        <meta charset=\"utf-8\"> 
        <title>收银台</title>
    </head>
    <body>
    <script>
    function jsApiCall()
        {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                {\"appId\":\"".$C_wxpay_appid."\",\"timeStamp\":\"".$timeStamp."\",\"nonceStr\":\"".$no."\",\"package\":\"prepay_id=".$prepay_id."\",\"signType\":\"MD5\",\"paySign\":\"".$paySign."\"},
                function(res){
                    if(res.err_msg.indexOf(\":ok\")>-1){
                        window.location.href=\"return.php?out_trade_no=$no\";
                    }
                    else{
                        alert(\"取消支付\");
                    }
                }
            );
        }

        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();
    </script>

    </body>
    </html>");
    }
}else{
	$url=urlencode(gethttp().$domain."/shop/wxpay.php?no=$no");
	die("
<!DOCTYPE html>
<html>
<head>
	<meta charset=\"utf-8\"> 
	<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
	<title>收银台</title>
	<link rel=\"stylesheet\" href=\"https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css\">  
	<script src=\"https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js\"></script>
	<script src=\"https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js\"></script>
	<style>
	    body{background:#f7f7f7}
	    .pop {width: 100%;height: 100%;position: fixed;top: 0;left: 0;background: rgba(0,0,0,.7);z-index: 999;}
	    .pop img{float: right;margin: 10px;max-width: calc(100% - 20px)}
	    .pop div{color: #ffffff;text-align: center;margin-top:300px}
	    .top{text-align:center;padding:10px;border-bottom:solid 1px #DDD;background:#fff}
	    #center_info{text-align: center;padding: 30px;position: fixed;top: calc(50% - 240px);left: calc(50% - 180px);background: #fff;border-radius: 15px;box-shadow: 0 5px 15px 0 #ddd;overflow: hidden;}
	</style>
	<script>
timename = setInterval(\"check()\", 3000);
function check() {
    $.ajax({
        url: '?action=check&no=".$no."',
        type: 'get',
        success: function(data) {
            data = JSON.parse(data);
            if (data.msg == \"success\") {
                top.location.href=data.url;
            } else {

            }
        }
    });
}
</script>
</head>
<body>
    <div class=\"top\">
        <img src=\"../member/images/wxpay.png\">
    </div>
    <div id=\"center_info\">
        <div>".$title."</div><div style=\"font-size:40px;\">￥".$money."</div><img src=\"../conn/qrcode.php?url=".$url."\" style=\"width:300px\"><div style=\"margin-top:10px;background: #222;color: #fff;margin: 20px -30px -30px -30px;padding: 20px;\">请使用微信扫码支付</div>    </div>
</body>
</html>");
}
?>