微信小程序支付
小于 1 分钟
/** @format */
import Message from "@/utils/Message";
import {payParam} from "@/api/pay";
export default {
data() {
return {
provider: '',
cb: '',
orderId: '',
};
},
methods: {
async pay(params) {
uni.showLoading({ title: '支付中', mask: true });
payParam({
order_id: params.order_id, // 订单id
order_type: params.order_type // 订单类型
}).then(res=>{
if (res.code === 200){
console.log("支付订单")
console.log(res)
const provider = this.getProvider();
uni.requestPayment({
provider, // 固定参数
service: 5, // 固定值:5(拉起小程序收银台)
...res.data, // 支付信息
success: (res) => {
console.log(res)
// 用户在手机端支付成功后的页面回调 = 用户真实付了钱
},
fail: (err) => {
console.log(err)
// 用户唤起了支付,但是没有支付前直接返回
// this.orderFail();
},
});
}else{
Message.error(res.msg);
}
});
// const orderInfo = await this.$api.getOrderInfo({
// order_id: this.orderId,
// });
// this.requestPayMent(orderInfo.data);
},
async requestPayMent(orderInfo) {
const provider = await this.getProvider();
uni.requestPayment({
provider,
service: 5,
...orderInfo,
timeStamp: orderInfo.timestamp,
success: (res) => {
console.log(res);
this.orderSuccess();
},
fail: (err) => {
console.log(err);
this.orderFail();
},
});
},
getProvider() {
return new Promise((resolve, reject) => {
uni.getProvider({
service: 'payment',
success: (res) => {
console.log(res);
resolve(res.provider);
},
fail: (err) => {
console.log(err);
reject(err);
},
});
});
},
orderSuccess() {
uni.hideLoading();
uni.showToast({ title: '支付成功', mask: true });
if (this.cb) {
this.cb();
}
this.message();
setTimeout(() => {
this.$redTo('/pages/order/views/success/index', {
id: this.orderId,
});
}, 1500);
},
orderFail() {
uni.hideLoading();
uni.showToast({ title: '支付失败', icon: 'none', mask: true });
setTimeout(() => {
this.$redTo('/pages/order/views/detail/index', {
id: this.orderId,
});
}, 500);
}
},
};