//1
<view @click="gopayFun()">支付费用</view>
//2.引入jweixin-module包,导入jweixin-module
<script>
var wx = require('jweixin-module')
export default {
}
</script>
//具体安装:https://www.npmjs.com/package/jweixin-module
//3.去支付(uniapp开发h5-调用支付)
gopayFun() {
var that = this;
var params = {
userid: that.userid,
total_fee: that.payLowest //支付金额
}
this.$api.appPlateForm('GET', 'user/UnifiedOrder', params, function(res) {
console.log('打印支付res', res)
if (res.return_code == 'SUCCESS') {
that.out_trade_no = res.out_trade_no //订单号
wx.config({
debug: false,
appId: res.appId, // 必填,公众号的唯一标识
timestamp: res.timeStamp, // 必填,生成签名的时间戳
nonceStr: res.nonceStr, // 必填,生成签名的随机串
signature: res.paySign,
jsApiList: ['chooseWXPay']
});
wx.ready(function() {
wx.chooseWXPay({
timestamp: res.timeStamp,
nonceStr: res.nonceStr, // 支付签名随机串,不长于 32 位
package: res.package,
signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: res.paySign, // 支付签名
success(res) {
console.log('success:' + JSON.stringify(res));
//用定时器查询是否为已完成状态(限制执行次数为5次)
var repeat = 5;
that.timer = setInterval(function() {
if (repeat <= 0) {
clearInterval(timer);
} else {
repeat--;
that.QueryPayResult() //获取到支付状态
}
}, 1000);
},
fail(res) {
console.log('失败res',res)
uni.showToast({
icon:'none',
title:'未完成支付!'
})
}
});
});
} else {
uni.showToast({
icon: 'none',
title: res.msg,
duration: 1500
})
}
});
},
//微信支付-查询订单支付状态接口
QueryPayResult() {
var that = this;
var params = {
out_trade_no: that.out_trade_no //订单号
}
this.$api.appPlateForm('GET', 'user/QueryPayResult', params, function(res) {
if (res.trade_state == 'SUCCESS') {
uni.showToast({
icon:'none',
title:'支付成功!'
})
//清空定时器
if(that.timer) {
clearTimeout(that.timer);
that.timer = null;
}
//进入下一页
that.steps_active = 2
}else{
uni.showToast({
icon:'none',
title:res
})
}
}, function(err) {
uni.showToast({
icon: 'none',
title: err.msg
})
});
},
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/96051.html