var that = this;
var wxChannel = null; // 微信支付
var aliChannel = null; // 支付宝支付
var channel = null; //支付通道
mui.init()
mui.plusReady(function() {
plus.navigator.setStatusBarStyle('light');
});
//点击支付
mui('.hotel_footer').on('tap', '.footer_right_true', function() {
if(that.name != null && that.idcard != null) {
//调用隐藏/显示弹出层
mui("#popover").popover('toggle', document.getElementById("div"));
} else {
alert('请补全您的信息')
}
})
//点击钱包支付
mui('.mui-table-view').on('tap', '#kzf_money', function() {
//余额支付
const client = '/BalancePay' //余额支付
var that = this
api.AjaxReponse(client, {
'access_token': localStorage.getItem('access_token'),
'order_number': localStorage.getItem('order_number')
}, function(data) {
if(data.code == 0) {
localStorage.setItem('order_uuid', data.data)
alert('支付成功!')
mui.openWindow({
url: './payment_details.html',
})
} else {
alert(data.msg)
}
})
})
mui.plusReady(function() {
// 获取支付通道
plus.payment.getChannels(function(channels) {
for(var i in channels) {
if(channels[i].id == "wxpay") {
wxChannel = channels[i];
// console.log(JSON.stringify(wxChannel))
} else {
aliChannel = channels[i];
}
}
}, function(e) {
alert("获取支付通道失败:" + e.message);
});
})
//点击微信支付
mui('.mui-table-view').on('tap', '.my_recommend', function() {
pay('wxpay');
})
//点击支付宝支付
mui('.mui-table-view').on('tap', '.integral', function() {
pay('alipay');
})
var pame = 'access_token=' + localStorage.getItem('access_token') + '&order_number=' + localStorage.getItem('order_number')
var ALIPAYSERVER = 'http://demo.dcloud.net.cn/helloh5/payment/alipay.php?total=';
// var WXPAYSERVER = 'http://demo.dcloud.net.cn/payment/?payid=wxpay&appid=HBuilder&total=1';
// 2. 发起支付请求
function pay(id) {
// 从服务器请求支付订单
var PAYSERVER = '';
if(id == 'alipay') {
PAYSERVER = ALIPAYSERVER;
channel = aliChannel;
} else if(id == 'wxpay') {
PAYSERVER = WXPAYSERVER;
channel = wxChannel;
} else {
plus.nativeUI.alert("不支持此支付通道!", null, "酒店支付");
return;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
switch(xhr.readyState) {
case 4:
if(xhr.status == 200) {
// alert(JSON.stringify(JSON.parse(xhr.responseText).data))
// alert(JSON.stringify(xhr.responseText.data))
// console.log(JSON.stringify(xhr.responseText))
var xhrdata = JSON.parse(xhr.responseText).data
// var paydata = {}
//
// paydata.appid = xhrdata.appid;
// paydata.nonce_str = xhrdata.nonce_str;
// paydata["package"] = "Sign=WXPay";
// paydata.partnerid = xhrdata.mch_id;
// paydata.prepayid = xhrdata.prepay_id;
// paydata.sign = xhrdata.sign;
// paydata.timestamp = Date.parse(new Date());
let paydata = { //后台返回的支付参数最好全部都是小写(论坛看到的提醒)
"appid": xhrdata.appid,
"noncestr": xhrdata.nonce_str,
"package": "Sign=WXPay",//固定
"partnerid": xhrdata.mch_id,
"prepayid": xhrdata.prepay_id,
"timestamp": xhrdata.timestamp,
"sign": xhrdata.sign
};
// alert(JSON.stringify(xhr.responseText))
plus.payment.request(channel, paydata, function(result) {
plus.nativeUI.alert("支付成功!", function() {
back();
});
}, function(error) {
console.log(JSON.stringify(error))
plus.nativeUI.alert("支付失败:" + error.code);
});
} else {
alert("获取订单信息失败!");
}
break;
default:
break;
}
}
xhr.open('POST', PAYSERVER);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(pame);
}
注:mui 微信支付其实是走的app支付,并非web支付…