$.ajax({
url: '/wap/pOrder_wap/orderInfo/invokingPay',//调用支付接口
type: "POST",
data: JSON.stringify(data2),
dataType: "json",
async: true,
contentType: "application/json;utf-8",
success: function (result) {
if (result.status == 0) {
//微信获取链接,再次重定向
var resultMsg = result.data;
var redirectUrl = "&redirect_url=" + siteRootPath + "订单列表url";
if (!isWeixin) {
if (resultMsg.mweb_url) {
if (siteRootPath.indexOf("dev-zuma.com") > 0) {
window.location.href = resultMsg.mweb_url + "&redirect_url=订单列表url";
} else {
window.location.href = resultMsg.mweb_url + redirectUrl;
}
}
} else {
var prepayId = "prepay_id="+resultMsg.prepay_id;
var preid = resultMsg.prepay_id;
var nonceStr = resultMsg.nonce_str;
var appid = resultMsg.appid;
var timeStamp = resultMsg.timeStamp;
var paySign = resultMsg.paySign;
var signType= 'MD5';
var m5 = "appId=" + appid + "&nonceStr=" + nonceStr + "&prepayId=" + prepayId + "&signType=MD5&timeStamp=" + timeStamp + "&key=gfdsgdfsgre56t5465466367hgfdhy5e4yu";
wx.miniProgram.getEnv(function(res) {
if(res.miniprogram){ //小程序下的逻辑
var payParam = '?timestamp='+timeStamp+'&nonceStr='+nonceStr +'&package='+prepayId+"&preid=" + preid +'&paySign='+paySign;
//定义path 与小程序的支付页面的路径相对应
var path = '/pages/pay/pay' + payParam;
//通过JSSDK的api跳转到指定的小程序页面
wx.miniProgram.navigateTo({
url: path,
success: function(){
console.log('success')
},
fail: function(){
console.log('fail');
},
});
}else{//h5下的逻辑
// var m5 = "appId=" + appid + "&nonceStr=" + nonceStr + "&package=" + prepayId + "&signType=MD5&timeStamp=" + timeStamp + "&key=gfdsgdfsgre56t5465466367hgfdhy5e4yu";
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId": appid, //公众号名称,由商户传入
"timeStamp": timeStamp, //时间戳,自1970年以来的秒数
"nonceStr": nonceStr,// randomWord(true, 3, 32), //随机串
"package": prepayId,//"prepay_id=" + prepayId,
"signType": "MD5", //微信签名方式:
"paySign": paySign //hex_md5(m5).toUpperCase()
},
function (result) {
if (result.err_msg == "get_brand_wcpay_request:ok") {
// 使用以上方式判断前端返回,微信团队郑重提示:
var url = encodeURIComponent("http://www.baidu.com/wap/pOrder_wap/orderInfo/successToPay?outTradeNo="+resultMsg.out_trade_no);
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7565758e1ef2f419&redirect_uri=" + url + "&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1";
}
});
}
})
}
}
}
})
// pages/wxpay/wxpay.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
//页面加载调取微信支付(原则上应该对options的携带的参数进行校验)
that.requestPayment(options);
console.log(options)
},
//根据 obj 的参数请求wx 支付
requestPayment: function (obj) {
//获取options的订单Id
//var orderId = obj.orderId;
console.log(obj);
console.log('11111=' + obj.nonceStr);
console.log('222222=' + obj.package);
console.log('333333333=' + obj.preid);
//console.log('prepay_id=' + obj.timestamp);
//调起微信支付
wx.requestPayment({
//相关支付参数
'timeStamp': obj.timestamp,
'nonceStr': obj.nonceStr,
'package': 'prepay_id=' + obj.preid,
'signType': 'MD5',
'paySign': obj.paySign,
//小程序微信支付成功的回调通知
'success': function (res) {
console.log(444444);
//定义小程序页面集合
//小程序主动返回到上一个页面。即从wxpay page到index page。此时index page的webview已经重新加载了url 了
wx.navigateTo({
url: '../list/list'
})
},
//小程序支付失败的回调通知
'fail': function (res) {
console.log("支付失败");
console.log(res);
wx.navigateTo({
url: '../list/list'
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})