<?php
error_reporting(0); 
include "../../config/ht.php";
$code=base64_decode($_GET[code]);
if (strpos($code, "|ubo|") !== false) {
$p= explode("|ubo|",$code); 
$ddh=$p[0];
$money=$p[1]*100;
$topayid=$p[2];
$paylx=$p[3];
$blx=$p[4];
}
$type="where biaoshi='$paylx'";
$hzpay=queryall(hzpay,$type);
if($hzpay[zt]==1){
$type="WHERE id='$topayid'";
$hzpaylist=queryall(hzpaylist,$type);
if($hzpaylist){
if($hzpaylist[sz]==1){
$sub_mch_id=$hzpaylist[payid];//商户ID
$app_key=$hzpaylist[paykey];//商户秘钥
$appkey=$hzpaylist[payurl];//
}else{
echo '代理通道已被停止使用';
exit;
}
}else{
echo '代理通道未添加';
exit;
}
}else{
$type="WHERE biaoshi='$paylx' and  zt='1' and sz='1'";
$hzpayto=queryall(hzpayto,$type);
if($hzpayto){
$sub_mch_id=$hzpayto[payid];//商户ID
$app_key=$hzpayto[paykey];//商户秘钥
$appkey=$hzpayto[payurl];//
}else{
echo '通道未设置默认接口';
exit;
}
}
$openid=$_GET['openid'];
$notify_url="http://".$_SERVER["HTTP_HOST"]."/hzpay/".$paylx."/tzurl.php";
$backurl="http://".$_SERVER["HTTP_HOST"]."/check-".$ddh.".html";//这是同步跳转地址
$url='https://www.xuelea.com/api/v2/wechat/mp';
$pp=$ddh.$money.$sub_mch_id.$app_key.$appkey;
$sign=md5($pp);
$post= array(
'mch_id' =>$sub_mch_id,
'sub_mch_id' =>'',
 'openid' =>$openid,
'total_fee' =>$money,
'out_trade_no' =>$ddh,
'subject' =>'vip',
'notify_url' =>$notify_url,
'extra' =>$sign,
);
ksort($post);
$need = [];
foreach ($post as $key => $value) {
if (! $value || $key == 'sign') {
continue;
 }
$need[] = "{$key}={$value}";
}
$string = implode('&', $need).$app_key;
$sign=strtoupper(md5($string));
$post["sign"] = $sign;
//echo "<pre>";print_r($post);echo "<pre>";
$result = post_curls($url,$post);
$paysj=json_decode($result, true); 
//获取内付参数
$code=$paysj[code];
$appid=$paysj[data][jsapi_app_id];
$timestamp=$paysj[data][jsapi_timeStamp];
$nonceStr=$paysj[data][jsapi_nonceStr];
$playid=$paysj[data][jsapi_package];
$playsign=$paysj[data][jsapi_paySign];
function post_curls($url, $post){
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); 
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
curl_setopt($curl, CURLOPT_HEADER, 0); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$res = curl_exec($curl); 
if (curl_errno($curl)) {echo 'Errno'.curl_error($curl);}
curl_close($curl); 
return $res; 
}
?>
<!DOCTYPE html>
<html>
<head>
<title>微信安全支付</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<style type="text/css">
body {padding: 0;margin: 0;background-color: #eeeeee;font-family: '黑体';}
.pay-main {background-color: #4cb131;padding-top: 20px;padding-left: 20px;padding-bottom: 20px;}
.pay-main img {margin: 0 auto;display: block;}
.pay-main .lines {margin: 0 auto;text-align: center;color: #cae8c2;font-size: 12pt;margin-top: 10px;}
.tips .img {margin: 20px;}
.tips .img img {width: 20px;}
.tips span {vertical-align: top;color: #ababab;line-height: 18px;padding-left: 10px;padding-top: 0px;}
.action {background: #4cb131;padding: 10px 0;color: #ffffff;text-align: center;font-size: 14pt;border-radius: 10px 10px;margin: 15px;}
.action:focus {background: #4cb131;}
.action.disabled {background-color: #aeaeae;}
.footer {position: absolute;bottom: 0;left: 0;right: 0;text-align: center;padding-bottom: 20px;font-size: 10pt;color: #aeaeae;}
.footer .ct-if {margin-top: 6px;font-size: 8pt;}
</style>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
$(document).ready(function () {  
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('hideOptionMenu');
});	
});
</script>
<script>
$(document).ready(function () {  
$('body').on('touchmove', function (event) {
event.preventDefault();
});
});
</script>
</head>
<body>
<script type="text/javascript">
function jsApiCall(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId":"<?php echo $appid?>", 
"timeStamp":"<?php  echo $timestamp?>", 
"nonceStr":"<?php  echo $nonceStr?>", 
"package":"<?php  echo $playid?>",
"signType":"MD5",
"paySign":"<?php  echo $playsign?>"
},
function(res){
WeixinJSBridge.log(res.err_msg);
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
self.location= "<?php echo $backurl?>";
} 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();
}
}
</script>
<script type="text/javascript">
setTimeout("callpay()", 1000 );
</script>
</body>
</html>