<?php
//====== 手机支付 ======
require "common.php";
$o=Req("o");$c=Req("code");$w=["openid"=>""];$u=$cfg["Aw"].Req("r");
$r=Qra(Qry("Select Mp,Mt From ".$pre."pay Where Mc='".$o."'"));
if($r["Mt"]*1==1&&strpos($_SERVER['HTTP_USER_AGENT'],"MicroMessenger")===false)die('<h1>请在微信内打开此页面进行支付！</h1>');
if($r["Mt"]*1==0&&strpos($_SERVER['HTTP_USER_AGENT'],"AlipayClient")===false)die('<h1>请在支付宝内打开此页面进行支付！</h1>');
if($cfg["Ap"]["Fs"]==1){
	if($r["Mt"]*1==1&&$c=="")die("<script>window.location='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$cfg["Ag"]["Wa"]."&redirect_uri=".urlencode($cfg["Aw"]."mobile.php?o=".$o)."&response_type=code&scope=snsapi_base&state=".rand(100,999)."#wechat_redirect';</script>");
	if($r["Mt"]*1==1&&$c!=""){$w=Dec(Cul("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$cfg["Ag"]["Wa"]."&secret=".$cfg["Ag"]["Wk"]."&code=".$c."&grant_type=authorization_code"));if(!isset($w["openid"]))die("鉴权失败");}
	$e=Dec(Pay($o,$r["Mp"],$r["Mt"],$w["openid"]));
	if($e["err"]!==0)die($e["err"]);
	if($r["Mt"]==0){//支付宝
		echo "<script>window.location='".$e["qrcode"]."';</script>";
	}elseif($r["Mt"]==1){//微信
		$a=Rnd();$t=time();$k=strtoupper(md5("appId=".$cfg["Ag"]["Wa"]."&nonceStr=".$a."&package=prepay_id=".$e["qrcode"]."&signType=MD5&timeStamp=".$t."&key=".$cfg["Ag"]["Wk"]));
		echo '<!DOCTYPE HTML><html><head><meta charset="utf-8"><meta name="viewport" content="width=360, initial-scale=1, user-scalable=no"><title>微信支付</title><script type="text/javascript">function onBridgeReady(){WeixinJSBridge.invoke("getBrandWCPayRequest",{"appId":"'.$cfg["Ap"]["Wc"].'","timeStamp":"'.$t.'","nonceStr":"'.$a.'","package":"prepay_id='.$e["qrcode"].'","signType":"MD5","paySign":"'.$k.'"},function(res){if(res.err_msg=="get_brand_wcpay_request:ok"){dv.innerText="支付成功！";}else dv.innerText="支付失败！";});}function pay(){if(typeof WeixinJSBridge=="undefined"){if(document.addEventListener){document.addEventListener("WeixinJSBridgeReady",onBridgeReady,false);}else if(document.attachEvent){document.attachEvent("WeixinJSBridgeReady",onBridgeReady);document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);}}else{onBridgeReady();}}</script></head><body style="font-size:18px;" onLoad="pay();"><div id="dv" style="text-align:center;">正在调用微信支付接口，请稍候...</div></body></html>';
	}
}else{//免签
	$e=Rnd();
	$s="gw_mchid=".$cfg["Ap"]["Gi"]."&gw_notify=".$cfg["Aw"]."notify.php&gw_order=".$o."&gw_price=".$r["Mp"]."&gw_rand=".$e."&gw_type=".$r["Mt"];
	$k=strtolower(md5($s."&key=".$cfg["Ap"]["Gk"]));
	echo '<meta charset="utf-8"><form action="https://www.pays.top/app/pay.php?mode=html" method="post" name="pay"><input type="hidden" name="gw_mchid" value="'.$cfg["Ap"]["Gi"].'"><input type="hidden" name="gw_notify" value="'.$cfg["Aw"].'notify.php"><input type="hidden" name="gw_order" value="'.$o.'"><input type="hidden" name="gw_price" value="'.$r["Mp"].'"><input type="hidden" name="gw_rand" value="'.$e.'"><input type="hidden" name="gw_type" value="'.$r["Mt"].'"><input type="hidden" name="gw_sign" value="'.$k.'"><input type="hidden" name="gw_return" value="'.$u.'"></form><script type="text/javascript">document.pay.submit();</script>';
}
?>