471 lines
15 KiB
PHP
471 lines
15 KiB
PHP
<?php
|
||
|
||
require_once dirname(__FILE__) . '/phpqrcode/merged/phpqrcode.php';
|
||
require_once dirname(__FILE__) . '/common.php';
|
||
|
||
define('DEFAULT_MARKET_KEY', 'test'); /// 默认的门店
|
||
define('DEFAULT_SIGN_KEY', 'test'); /// 默认的签名key
|
||
|
||
|
||
/**
|
||
* @note 提取请求参数
|
||
* @param array $Parameters 参数列表
|
||
* @param string $Name 参数名
|
||
* @param string $Default 当没有取到时,使用的默认值
|
||
* @return string
|
||
*/
|
||
function GetRequestParameter($Parameters, $Name, $Default = '') {
|
||
if (isset($Parameters[$Name]) && !empty($Parameters[$Name]))
|
||
return strval($Parameters[$Name]);
|
||
else
|
||
return isset($_REQUEST[$Name]) ? $_REQUEST[$Name] : $Default;
|
||
}
|
||
|
||
|
||
/// 是否二维码
|
||
$is_qrcode = isset($_REQUEST['is_qrcode']) ? $_REQUEST['is_qrcode'] : '';
|
||
if (strcasecmp('y', $is_qrcode) == 0 || 0 != intval($is_qrcode)) {
|
||
/// 需要二维码则以本页面地址生成二维码
|
||
//$url = GetLocaleUrl();
|
||
$url = '';
|
||
foreach ($_REQUEST as $k => $v) {
|
||
if (strcasecmp('is_qrcode', $k) == 0) continue;
|
||
|
||
$url .= "&{$k}={$v}";
|
||
}
|
||
|
||
if (empty($url))
|
||
$url = GetLocaleUrl();
|
||
else {
|
||
$sublen = mb_strlen('&', USEDCHARSET);
|
||
$url = GetLocaleUrl() . '?' . mb_substr($url, $sublen, mb_strlen($url, USEDCHARSET) - $sublen, USEDCHARSET);
|
||
}
|
||
|
||
$local_path = dirname(__FILE__);
|
||
$short_name = '/qrcodeimg/' . date('YmdHis') . '.png';
|
||
$logo_name = $local_path . '/img/qr_logo.png';
|
||
$long_name = $local_path . $short_name;
|
||
QRcode::png($url, $long_name, QR_ECLEVEL_L, 8, 2);
|
||
|
||
/// 合并二维码和logo图片
|
||
$img = imagecreatefromstring(file_get_contents($long_name));
|
||
$logo = imagecreatefromstring(file_get_contents($logo_name));
|
||
|
||
if ($img && $logo) {
|
||
$img_width = imagesx($img); /// 二维码图片宽度
|
||
$img_height = imagesy($img); /// 二维码图片高度
|
||
$logo_width = imagesx($logo); /// logo图片宽度
|
||
$logo_height = imagesy($logo); /// logo图片高度
|
||
$logo_qr_width = $img_width / 6;
|
||
$scale = $logo_width / $logo_qr_width;
|
||
$logo_qr_height = $logo_height / $scale;
|
||
$from_width = ($img_width - $logo_qr_width) / 2;
|
||
|
||
/// 重新组合图片并调整大小
|
||
imagecopyresampled($img, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
|
||
|
||
/// 输出图片
|
||
header('Content-type: image/png');
|
||
imagepng($img);
|
||
|
||
imagedestroy($logo);
|
||
imagedestroy($img);
|
||
}
|
||
|
||
///$img_link = dirname(GetLocaleUrl()) . $short_name;
|
||
///echo "<center><a href=\"{$img_link}\"><img src=\"{$img_link}\"></a><br /><font size=\"20\">在线支付</font></center>";
|
||
unlink($long_name);
|
||
exit;
|
||
}
|
||
|
||
?>
|
||
|
||
|
||
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
|
||
<link href="css/style.css" rel="stylesheet">
|
||
<!-- <link href="css/bootstrap.min.css" rel="stylesheet">-->
|
||
|
||
<script src="js/jquery-2.1.4.min.js"></script>
|
||
<script src="js/common.js"></script>
|
||
</head>
|
||
<body class="body_bg" id="mainbody">
|
||
|
||
<header class="yl_logo">
|
||
<!--<img src="img/yl.png" class="img-responsive">-->
|
||
</header>
|
||
|
||
<script type="text/javascript">
|
||
function id(x) {
|
||
if (typeof x == "string")
|
||
return document.getElementById(x);
|
||
else
|
||
return x;
|
||
}
|
||
</script>
|
||
|
||
|
||
<?php
|
||
|
||
/// 所有参数
|
||
$attach = (array)json_decode(rawurldecode(GetRequestParameter(null, 'attach')));
|
||
//if (!empty($attach)) var_dump($attach);
|
||
|
||
/// sid
|
||
$sid = GetRequestParameter($attach, 'sid');
|
||
/// scode
|
||
$scode = GetRequestParameter($attach, 'scode');
|
||
/// app_id
|
||
$app_id = GetRequestParameter($attach, 'app_id');
|
||
/// 开发者key
|
||
$dev_key = GetRequestParameter($attach, 'dev_key');
|
||
/// 门店key
|
||
$market_key = GetRequestParameter($attach, 'market_key');
|
||
/// 支付金额(分)
|
||
$fee = GetRequestParameter($attach, 'fee');
|
||
/// 支付标题
|
||
$title = GetRequestParameter($attach, 'title');
|
||
/// 订单号
|
||
$order_id = GetRequestParameter($attach, 'order_id', md5(date('YmdHis') . rand(1000, 9999)));
|
||
/// 回调地址(异步)
|
||
$notice_url = GetRequestParameter($attach, 'notice_url');
|
||
/// 回调地址(同步)
|
||
$return_url = GetRequestParameter($attach, 'return_url', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : GetLocaleUrl());
|
||
/// 是否静默授权
|
||
$silence = intval(GetRequestParameter($attach, 'silence', '0'));
|
||
/// 签名用的key(目前支付不做校验,退款才会校验签名。)
|
||
/// 具体的签名算法见详细代码。
|
||
$sign_key = GetRequestParameter($attach, 'sign_key', DEFAULT_SIGN_KEY);
|
||
|
||
if (empty($app_id)) {
|
||
echo '没有指定app_id参数!';
|
||
} elseif (empty($dev_key)) {
|
||
echo '没有指定dev_key参数!';
|
||
} elseif (empty($market_key)) {
|
||
echo '没有指定market_key参数!';
|
||
} elseif (empty($notice_url)) {
|
||
echo '没有指定notice_url参数!';
|
||
} elseif (empty($sid) || empty($scode)) { /// 判断是否有sid和scode, 什么数据都没有则先跳转到登录页面授权
|
||
$attach = json_encode(GetAttachParameters(array('attach', 'is_qrcode',)));
|
||
$redirect_uri = rawurlencode(GetLocaleUrl());
|
||
$url = "https://api.tscce.cn/source/login/login.php?app_id={$app_id}&dev_key={$dev_key}&market_key={$market_key}&silence={$silence}&redirect_uri={$redirect_uri}&attach={$attach}";
|
||
header("Location: {$url}");
|
||
exit;
|
||
} else {
|
||
if (empty($fee)) { /// 有sid和scode 但是 没有fee 则要求页面输入并提交fee参数
|
||
$param = '';
|
||
foreach ($attach as $k => $v) {
|
||
$param .= " \" <input name='{$k}' type='text' value='{$v}' />\" + " . PHP_EOL;
|
||
}
|
||
|
||
$attach = GetAttachParameters(array('attach', 'is_qrcode',));
|
||
foreach ($attach as $k => $v) {
|
||
$param .= " \" <input name='{$k}' type='text' value='{$v}' />\" + " . PHP_EOL;
|
||
}
|
||
$html = <<<EOL
|
||
<script>
|
||
function submit() {
|
||
var fee = document.getElementById('fee').value;
|
||
|
||
if ('' == fee || undefined == fee) {
|
||
alert('请输入要支付的金额(单位为分)');
|
||
return;
|
||
}
|
||
|
||
var script =
|
||
"<form style='display:none;' id='frm' name='frm' method='post' action=''>" +
|
||
" <input name='fee' type='text' value='" + fee + "' />" + /// 支付金额(单位分)
|
||
{$param}"</form>";
|
||
|
||
$('#mainbody').append(script);
|
||
$('#frm').submit();
|
||
}
|
||
</script>
|
||
|
||
<section>
|
||
<div class="neirong">
|
||
<ul>
|
||
<li><span class="name">总金额:</span><input id="fee" type="text" autofocus placeholder="单位:分" class="input"> <sapn class="red">*</sapn></li>
|
||
</ul>
|
||
<footer class="text-center"><a href="javascript:;" onclick="submit();" class="btn btn-blue foot_btn">确定</a></footer>
|
||
</div>
|
||
</section>
|
||
|
||
EOL;
|
||
} else { /// 有fee参数表示已提交页面中输入的支付金额
|
||
/// 获取附加参数
|
||
$ignore = array('sid','scode','app_id','dev_key','market_key','fee','title','order_id','notice_url','return_url','sign_key','is_qrcode','attach',);
|
||
$attach1 = GetAttachParameters($ignore, $attach);
|
||
$attach2 = GetAttachParameters($ignore);
|
||
$attach = $attach1 + $attach2;
|
||
|
||
/// 通过接口获取门店所支援的支付方式
|
||
$data = array(
|
||
'appid' => $app_id, /// appid
|
||
'devkey' => $dev_key, /// 开发者key
|
||
'market_key' => $market_key, /// 门店key
|
||
'level' => 1, /// 支付类型(1: 线上, 2: 扫码, 3: app)
|
||
'version' => 1,/// 版本号
|
||
);
|
||
$data['sign'] = SignParameter($data, $sign_key); /// 生成签名
|
||
|
||
$result = new ResultObject(rawurldecode(SendPost('https://api.tscce.cn/api/newpay/querylist', $data)));
|
||
if (0 == $result->error && count($result->data) > 0) { /// 成功
|
||
/// 获取详细的支付接口信息,这里区分是微信还是支付宝,每种方式只取第一种
|
||
$wechat = null; /// 微信限定
|
||
$alipay = null; /// 支付宝限定
|
||
$browser = null; /// 浏览器限定
|
||
foreach ($result->data as $item) {
|
||
if (mb_strstr($item->third_flag, 'wechat', false, USEDCHARSET)) { /// 包含wechat表示都是各种类型的微信支付
|
||
if (empty($wechat)) $wechat = $item;
|
||
} elseif (mb_strstr($item->third_flag, 'alipay', false, USEDCHARSET)) { /// 包含alipay表示都是各种类型的支付宝支付
|
||
if (empty($alipay)) $alipay = $item;
|
||
} else {
|
||
if (empty($browser)) $browser = $item;
|
||
}
|
||
}
|
||
|
||
if (empty($wechat) && empty($alipay) && empty($browser)) {
|
||
$html = <<<EOL
|
||
<script>
|
||
alert('该门店不支援任何的支付方式!');
|
||
</script>
|
||
EOL;
|
||
} else {
|
||
$wechatform = '';
|
||
$alipayform = '';
|
||
$browserform = '';
|
||
|
||
$wechathtml = '';
|
||
$alipayhtml = '';
|
||
$browserhtml = '';
|
||
|
||
if (!empty($wechat)) {
|
||
/// 支付要用的参数
|
||
$data = $attach + array(
|
||
'appid' => $app_id, /// appid
|
||
'devkey' => $dev_key, /// 开发者key
|
||
'sid' => $sid, /// sid
|
||
'scode' => $scode, /// scode
|
||
'orderid' => $order_id, /// 订单号
|
||
'fee' => $fee, /// 支付金额(单位分)
|
||
'title' => $title, /// 支付标题
|
||
'notice_url' => $notice_url, /// 回调页面地址(异步)
|
||
'return_url' => $return_url, /// 回调页面地址(同步)
|
||
'paytype' => $wechat->type_id, /// 支付类型(可通过接口获取所有支持的类型列表)
|
||
'version' => 1, /// 接口版本号, 整型, 目前只支持1
|
||
);
|
||
$data['sign'] = SignParameter($data, $sign_key); /// 生成签名
|
||
|
||
/// ==== ↓生成form表单↓ ====
|
||
$wechatform = '<form style="display:none;" id="wechatform" name="wechatform" method="post" action="https://api.tscce.cn/api/newpay/pay/online/">' . PHP_EOL;
|
||
foreach ($data as $k => $v) {
|
||
$wechatform .= sprintf(' <input name="%s" type="text" value=\'%s\' />', $k, $v) . PHP_EOL;
|
||
}
|
||
$wechatform .= '</form>';
|
||
/// ==== ↑生成form表单↑ ====
|
||
|
||
$wechathtml = <<<EOL
|
||
<div class="dr_btn">
|
||
<button id='btnWechat' onclick='wechatform.submit();' class="btn btn-green wx" style="display:none;">微信支付</button>
|
||
</div>
|
||
EOL;
|
||
}
|
||
|
||
if (!empty($alipay)) {
|
||
/// 支付要用的参数
|
||
$data = $attach + array(
|
||
'appid' => $app_id, /// appid
|
||
'devkey' => $dev_key, /// 开发者key
|
||
'sid' => $sid, /// sid
|
||
'scode' => $scode, /// scode
|
||
'orderid' => $order_id, /// 订单号
|
||
'fee' => $fee, /// 支付金额(单位分)
|
||
'title' => $title, /// 支付标题
|
||
'notice_url' => $notice_url, /// 回调页面地址(异步)
|
||
'return_url' => $return_url, /// 回调页面地址(同步)
|
||
'paytype' => $alipay->type_id, /// 支付类型(可通过接口获取所有支持的类型列表)
|
||
'version' => 1, /// 接口版本号, 整型, 目前只支持1
|
||
);
|
||
$data['sign'] = SignParameter($data, $sign_key); /// 生成签名
|
||
|
||
$alipayform = '<form style="display:none;" id="alipayform" name="alipayform" method="post" action="https://api.tscce.cn/api/newpay/pay/online/">' . PHP_EOL;
|
||
foreach ($data as $k => $v) {
|
||
$alipayform .= sprintf(' <input name="%s" type="text" value=\'%s\' />', $k, $v) . PHP_EOL;
|
||
}
|
||
$alipayform .= '</form>';
|
||
|
||
$alipayhtml = <<<EOL
|
||
<div class="dr_btn">
|
||
<button id='btnAlipay' onclick="alipayform.submit();" class="btn btn-blue">支付宝支付</button>
|
||
</div>
|
||
EOL;
|
||
}
|
||
|
||
if (!empty($browser)) {
|
||
/// 支付要用的参数
|
||
$data = $attach + array(
|
||
'appid' => $app_id, /// appid
|
||
'devkey' => $dev_key, /// 开发者key
|
||
'sid' => $sid, /// sid
|
||
'scode' => $scode, /// scode
|
||
'orderid' => $order_id, /// 订单号
|
||
'fee' => $fee, /// 支付金额(单位分)
|
||
'title' => $title, /// 支付标题
|
||
'notice_url' => $notice_url, /// 回调页面地址(异步)
|
||
'return_url' => $return_url, /// 回调页面地址(同步)
|
||
'paytype' => $browser->type_id, /// 支付类型(可通过接口获取所有支持的类型列表)
|
||
'version' => 1, /// 接口版本号, 整型, 目前只支持1
|
||
);
|
||
$data['sign'] = SignParameter($data, $sign_key); /// 生成签名
|
||
|
||
$browserform = '<form style="display:none;" id="browserform" name="browserform" method="post" action="https://api.tscce.cn/api/newpay/pay/online/">' . PHP_EOL;
|
||
foreach ($data as $k => $v) {
|
||
$browserform .= sprintf(' <input name="%s" type="text" value=\'%s\' />', $k, $v) . PHP_EOL;
|
||
}
|
||
$browserform .= '</form>';
|
||
|
||
$browserhtml = <<<EOL
|
||
<div class="dr_btn">
|
||
<button id='btnBrowser' onclick="browserform.submit();" class="btn btn-green">立即支付</button>
|
||
</div>
|
||
EOL;
|
||
}
|
||
/*
|
||
if (empty($wechat))
|
||
$html = <<<EOL
|
||
{$alipayform}
|
||
<script>
|
||
alipayform.submit();
|
||
</script>
|
||
EOL;
|
||
elseif (empty($alipay))
|
||
$html = <<<EOL
|
||
{$wechatform}
|
||
<script>
|
||
wechatform.submit();
|
||
</script>
|
||
EOL;
|
||
else
|
||
*/
|
||
$html = <<<EOL
|
||
{$wechatform}
|
||
{$alipayform}
|
||
{$browserform}
|
||
|
||
<div class="container-fluid">
|
||
{$wechathtml}
|
||
{$alipayhtml}
|
||
{$browserhtml}
|
||
</div>
|
||
|
||
|
||
<script>
|
||
/// 是否微信打开
|
||
function is_wechat() {
|
||
return 'undefined' != typeof WeixinJSBridge;
|
||
}
|
||
|
||
///
|
||
function callAlipay() {
|
||
if (is_wechat())
|
||
return;
|
||
|
||
var btnAlipay = id('btnAlipay');
|
||
if (btnAlipay)
|
||
btnAlipay.onclick();
|
||
else
|
||
alert('该门店不支援任何的支付方式!');
|
||
}
|
||
|
||
$(function() {
|
||
var btnWechat = id('btnWechat');
|
||
var btnAlipay = id('btnAlipay');
|
||
var btnBrowser = id('btnBrowser');
|
||
|
||
if (!btnWechat && !btnAlipay && !btnBrowser) {
|
||
alert('该门店不支援任何的支付方式!');
|
||
return;
|
||
}
|
||
|
||
/// 判断是否包含WeixinJSBridge对象,包含则表示在微信环境中.
|
||
if ('undefined' == typeof WeixinJSBridge) {
|
||
/// WeixinJSBridgeReady事件处理.
|
||
$(document).on('WeixinJSBridgeReady', function() {
|
||
if (btnWechat) {
|
||
//btnWechat.style.display = 'initial';
|
||
btnWechat.onclick();
|
||
} else {
|
||
alert('该门店不支援任何的支付方式!');
|
||
return;
|
||
}
|
||
|
||
if (btnAlipay) {
|
||
btnAlipay.style.display = 'none';
|
||
}
|
||
|
||
if (btnBrowser) {
|
||
btnBrowser.style.display = 'none';
|
||
}
|
||
});
|
||
/*
|
||
/// 这里要判断其他环境.
|
||
/// 这里暂时用user-agent标志来判断是否处于微信环境中(包含micromessenger标识则表示为微信环境.).
|
||
/// 如果包含微信的标识则不去处理支付宝了.
|
||
var ua = navigator.userAgent.toLowerCase();
|
||
if (ua.match(/MicroMessenger/i) == "micromessenger") {
|
||
///return true;
|
||
/// 包含micromessenger标识则表示为微信环境
|
||
} else {
|
||
///return false;
|
||
/// 不包含micromessenger标识则表示为其他环境
|
||
setTimeout('callAlipay();', 1000); /// 延迟1秒
|
||
}
|
||
*/
|
||
} else {
|
||
if (btnWechat) {
|
||
//btnWechat.style.display = 'initial';
|
||
btnWechat.onclick();
|
||
} else {
|
||
alert('该门店不支援任何的支付方式!');
|
||
return;
|
||
}
|
||
|
||
if (btnAlipay) {
|
||
btnAlipay.style.display = 'none';
|
||
}
|
||
|
||
if (btnBrowser) {
|
||
btnBrowser.style.display = 'none';
|
||
}
|
||
}
|
||
});
|
||
</script>
|
||
EOL;
|
||
}
|
||
} elseif (0 != $result->error) { /// 有错误发生
|
||
$html = <<<EOL
|
||
<script>
|
||
alert('{$result->msg}');
|
||
</script>
|
||
EOL;
|
||
} else {
|
||
$html = <<<EOL
|
||
<script>
|
||
alert('该门店不支援任何的支付方式!');
|
||
</script>
|
||
EOL;
|
||
}
|
||
}
|
||
|
||
echo $html;
|
||
}
|
||
?>
|
||
|
||
</body>
|
||
</html>
|
||
|