//在支付之前调用一下这个接口
payMoney() {
var that = this;
var url = _url + '/order/createOrder'
var data = {
userToken: localStorage.userToken,
orderId: that.orderId
}
that.$http.get(url, data)
.then(function (res) {
if (res.data.code == '100') {
res = res.data.data;
that.payNow(res.payOrderNo, res.orderPay, res.openid, res.attach);//获得返回所需要的一些参数
}
});
},
//立即付款
payNow: function (payOrderNo, orderPay, openid, attach) {//
var that = this;
var url = _url + '/pay/createOrder';//去创建一个订单,
var data = {
body: "会搜商学院", //这个是具体详情
outTradeNo: payOrderNo, //订单号
totalFee: orderPay, //总的金额(单位分,一定是整数,不能有小数点)
openid: openid, //用户的openId
attach: attach,
};
that.$http.jsonp(url, data)
.then(function (success) {
console.log(success);
if (success.data.code == '100') {//返回一些微信想要的参数
var res = success.data.data
appId = res.appId;
paySign = res.paySign;
timeStamp = res.timeStamp;
nonceStr = res.nonceStr;
packageStr = res.packageValue;
signType = res.signType;
callpay();
} else {
$.alert("支付失败");
}
})
function callpay() {
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();
}
}
function onBridgeReady() {
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId": appId, //公众号名称,由商户传入
"paySign": paySign, //微信签名
"timeStamp": timeStamp, //时间戳,自1970年以来的秒数
"nonceStr": nonceStr, //随机串
"package": packageStr, //预支付交易会话标识
"signType": signType //微信签名方式
},
function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
//window.location.replace("index.html");
location.href = "./my_order.html"
// alert('支付成功');
} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
alert('支付取消');
location.href = "./my_order.html"
} else if (res.err_msg == "get_brand_wcpay_request:fail") {
alert('支付失败');
location.href = "./my_order.html"
} //使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}
)
}
},