//支付宝支付(pc端是一个二维码,移动端是唤起app进行支付)
function aliipay(order) {
let orderList = {orderId: order}; //订单号
waitOrderStatusChange(order,0); //判断订单状态
var url = "";
let url_qrcode = "/api/aliPay/aliPayH5Order"; //电脑h5支付
let url_h5 = "/api/aliPay/aliPayH5OrderInMobileBrowser"; //手机端支付
if (isMobileWxBrowser()) {
//在手机的微信浏览器
let userId = window.localStorage.getItem('loginUid');
let token = window.localStorage.getItem('loginToken');
goBackPage('apply.html?orderId=' + order + '&userId=' + userId + "&token=" + token);
return
}
if (isPcBrowser()) {
// 在电脑浏览器
url = url_qrcode;
}
if (isMobileBrowserOnly()) {
// 只在手机的浏览器
url = url_h5;
}
requestDataSimply(url, "POST", orderList, function (data) { //ajax请求接口
if (isEmpty(data.data)) {
lztToast("发起支付失败"); //弹窗
return;
}
if (isPcBrowser()) {
// 在电脑浏览器(请参考支付api)
let routerDta = window.location.href = "payOrder.html?htmlData=" + data.data;
const div = document.createElement('divform');
div.innerHTML = data.data; // data就是接口返回的form 表单字符串
document.body.appendChild(div);
document.forms[0].setAttribute('target', '_blank'); // 新开窗口跳转
document.forms[0].submit();
return;
}
if (isMobileBrowserOnly()) {
// 只在手机的浏览器
let routerDta = window.location.href = "payOrder.html?htmlData=" + data.data;
const div = document.createElement('divform');
div.innerHTML = data.data; // data就是接口返回的form 表单字符串
document.body.appendChild(div);
document.forms[0].setAttribute('target', '_blank'); // 新开窗口跳转
document.forms[0].submit();
return;
}
if (isMobileWxBrowser()) {
// "在手机的微信浏览器"
let routerDta = window.location.href = "payOrder.html?htmlData=" + data.data;
const div = document.createElement('divform');
div.innerHTML = data.data; // data就是接口返回的form 表单字符串
document.body.appendChild(div);
document.forms[0].setAttribute('target', '_blank') // 新开窗口跳转
document.forms[0].submit();
return;
}
});
}
// 微信支付
function wechatpay(order) {
let orderList = {orderId: order};
waitOrderStatusChange(order,0);
//微信内支付
if (isMobileWxBrowser()) {
requestDataSimply("/api/weixinPay/weixinPayH5OrderInWeChatBrowser", "POST", orderList, function (data) {
let wxPayData = data.data;
if (isEmpty(wxPayData)) {
lztToast("发起支付失败");
return;
}
// "在手机的微信浏览器"
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(data.data);
}
}, function (err) {
});
return;
}
//手机浏览器支付
if (isMobileBrowserOnly()) {
//手机浏览器H5支付未开通,暂时跳转到浏览器让用户扫描下载
// requestDataSimply("/api/weixinPay/weixinPayH5OrderInMobileBrowser", "POST", orderList, function (data) {
let params = getApiParams();
let userId = params.userId;
let token = params.token;
let url = 'weixinQrCodeTip.html?orderId=' + order + '&userId=' + userId + "&token=" + token;
window.open(url);
return
}
//Pc网站支付
if (isPcBrowser()) {
// 在电脑浏览器
requestDataSimply("/api/weixinPay/weixinPayH5Order", "POST", orderList, function (data) {
let url = data.data;
if (isEmpty(url)) {
lztToast("发起支付失败");
return;
}
let params = getApiParams();
let userId = params.userId;
let token = params.token;
let openUrl = 'weixinQrCodeTip.html?orderId=' + order + '&userId=' + userId + "&token=" + token + "&img=/" + url;
// openUrl = encodeURIComponent(openUrl);
// console.log(openUrl);
window.open(openUrl);
});
return;
}
lztToast("支付无效");
}
function goBackHomePage() {
goBackPage();
}
function onBridgeReady(data) {
//en [{"code":200,"data":{"nonce_str":"OjNWjRp0EVPwnFtV","code_url":"weixin://wxpay/bizpayurl?pr=O5J3IWQ","appid":"wx95691992c842c873","sign":"123390F3D9D9079E8402F9C161A11DB9341844CA0DE7B12720AD65EBBC1F6EBE","trade_type":"NATIVE","return_msg":"OK","result_code":"SUCCESS","mch_id":"1553043411","return_code":"SUCCESS","prepay_id":"wx111307478981372688b12d2c1724923800"},"message":"success","success":true}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId": data.appId, //公众号名称,由商户传入
"timeStamp": data.timeStamp, //时间戳,自1970年以来的秒数
"nonceStr": data.nonceStr, //随机串
"package": data.package,
"signType": data.signType, //微信签名方式:
"paySign": data.paySign, //微信签名
},
function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
// 使用以上方式判断前端返回,微信团队郑重提示:
//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
}
function waitOrderStatusChange(orderId, times) {
if (times >= 3) {
console.log("timeout");
return;
}
let params = {orderId : orderId};
$.ajax({
type: "get",
url: "/api/appPay/waitUnitOrderStatusChange",
data: getApiParams(params),
dataType: 'json',
success: function (data) {
if (data.code === 200) {
window.location.href = "myOrder.html";
} else {
waitOrderStatusChange(orderId, times + 1);
}
},
error: function (xhr, state, errorThrown) {
waitOrderStatusChange(orderId, times + 1);
}
});
}
// 判断浏览器的环境
var os = function () {
var ua = navigator.userAgent,
isWindowsPhone = /(?:Windows Phone)/.test(ua),
isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
isAndroid = /(?:Android)/.test(ua),
isFireFox = /(?:Firefox)/.test(ua),
isChrome = /(?:Chrome|CriOS)/.test(ua),
isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
isiPhone = /(?:iPhone)/.test(ua) && !isTablet,
isPc = !isiPhone && !isAndroid && !isSymbian;
return {
isTablet: isTablet,
isiPhone: isiPhone,
isAndroid: isAndroid,
isPc: isPc
};
}();
//平台、设备和操作系统
var system = {
win: false,
mac: false,
xll: false,
ipad: false
};
//检测平台
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") === 0);
system.ipad = (navigator.userAgent.match(/iPad/i) != null) ? true : false;
/**
* 判断是否在电脑浏览器
* @returns {boolean}
*/
function isPcBrowser() {
if (system.win || system.mac || system.xll) {
return true;
} else {
return false;
}
}
/**
* 判断是否只在电脑浏览器(排除电脑端微信浏览器)
* @returns {boolean}
*/
function isPcBrowserOnly() {
if (system.win || system.mac || system.xll) {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return false;
} else {
return true;
}
} else {
return false;
}
}
/**
* 判断是否在电脑中的微信浏览器
* @returns {boolean}
*/
function isPcWxBrowser() {
if (system.win || system.mac || system.xll) {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
}
}
return false;
}
/**
* 判断是否在手机浏览器
* @returns {boolean}
*/
function isMobileBrowser() {
if (system.win || system.mac || system.xll) {
return false;
} else {
return true;
}
}
/**
* 判断是否只在手机浏览器(排除手机端微信浏览器)
* @returns {boolean}
*/
function isMobileBrowserOnly() {
if (system.win || system.mac || system.xll) {
return false;
} else {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return false;
} else {
return true;
}
}
}
/**
* 判断是否在手机中的微信浏览器
* @returns {boolean}
*/
function isMobileWxBrowser() {
if (system.win || system.mac || system.xll) {
return false;
} else {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
}
}
/**
* 是否是安卓手机
* @returns {boolean}
*/
function isAndroid() {
return os.isAndroid;
}
/**
* 是否iOS平台
* @returns {boolean}
*/
function isiOS() {
if (isiPhone()) return true;
if (isiPad()) return true;
return false;
}
/**
* 是否是苹果手机
* @returns {boolean}
*/
function isiPhone() {
return os.isiPhone;
}
/**
* 是否是平板
* @returns {boolean}
*/
function isiPad() {
return os.isTablet;
}
希望此篇文章对同行有帮助。