fix(pay): 支付成功后轮询确认订单状态
- 微信/余额等支付完成后进入 paying 状态轮询后端 - 延长轮询次数,避免回调延迟误判失败 Co-authored-by: Cursor <cursoragent@cursor.com>
这个提交包含在:
5
.gitignore
vendored
普通文件
5
.gitignore
vendored
普通文件
@@ -0,0 +1,5 @@
|
||||
unpackage/
|
||||
node_modules/
|
||||
.hbuilderx/
|
||||
.DS_Store
|
||||
*.log
|
||||
291
pages/order/payment/result.vue
普通文件
291
pages/order/payment/result.vue
普通文件
@@ -0,0 +1,291 @@
|
||||
<!-- 支付结果页 -->
|
||||
<template>
|
||||
<view class="success-page">
|
||||
|
||||
<!-- 商品结果页 -->
|
||||
<template v-if="orderType==='goods'">
|
||||
<view class="success-box u-flex-col u-row-center u-col-center">
|
||||
<image class="pay-img" :src="payImg[payState]" mode=""></image>
|
||||
<text class="notice">{{ payText[payState] }}</text>
|
||||
<text class="pay-money"
|
||||
v-if="payState === 'success' && orderDetail.total_fee">¥{{ orderDetail.total_fee }}</text>
|
||||
<view class="btn-box u-flex u-row-between">
|
||||
<block
|
||||
v-if="payState === 'success' && orderDetail.activity_type === 'groupon' && orderDetail.ext_arr.buy_type === 'groupon'">
|
||||
<button class="u-reset-button base-btn" v-if="orderDetail.ext_arr.groupon_id > 0"
|
||||
@tap="$Router.replace({ path: '/pages/activity/groupon/detail', query: { id: orderDetail.ext_arr.groupon_id } })">
|
||||
拼团详情
|
||||
</button>
|
||||
<button class="u-reset-button base-btn" v-else
|
||||
@tap="$Router.push('/pages/activity/groupon/my-groupon')">我的拼团</button>
|
||||
</block>
|
||||
|
||||
<button class="u-reset-button base-btn" v-else
|
||||
@tap="$Router.pushTab('/pages/index/index')">返回首页</button>
|
||||
|
||||
|
||||
<button class="u-reset-button base-btn u-m-l-10 u-m-r-10"
|
||||
@tap="$Router.replace({ path: '/pages/order/detail', query: { id: orderDetail.id } })">查看订单</button>
|
||||
<button class="u-reset-button again-pay " v-show="payState === 'fail'" @tap="onPay">重新支付</button>
|
||||
</view>
|
||||
</view>
|
||||
<!-- modal -->
|
||||
<!-- #ifdef MP-WEIXIN -->
|
||||
<u-modal v-if="payState === 'success'" ref="uModal" v-model="showModal" :show-cancel-button="true"
|
||||
confirm-color="#4C7AF3" cancel-color="#666666" @confirm="onConfirm" confirm-text="订阅消息" cancel-text="取消"
|
||||
content="是否订阅消息,追踪订单信息?" title="提示"></u-modal>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<template v-if="orderType==='recharge'">
|
||||
<view class="success-box u-flex-col u-row-center u-col-center">
|
||||
<image class="pay-img" :src="payImg[payState]" mode=""></image>
|
||||
<text class="notice">{{ payText[payState] }}</text>
|
||||
<text class="pay-money"
|
||||
v-if="payState === 'success' && orderDetail.total_fee">¥{{ orderDetail.total_fee }}</text>
|
||||
<view class="btn-box u-flex u-row-between">
|
||||
|
||||
<button class="u-reset-button base-btn" @tap="$Router.pushTab('/pages/index/index')">返回首页</button>
|
||||
<button v-if="payState === 'fail'" class="u-reset-button again-pay" @tap="onPay">重新支付</button>
|
||||
<button v-else class="u-reset-button base-btn"
|
||||
@tap="$Router.pushTab('/pages/index/user')">个人中心</button>
|
||||
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Pay from '@/shopro/pay';
|
||||
import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
|
||||
let payTimer = null;
|
||||
const payCount = 15;
|
||||
export default {
|
||||
components: {},
|
||||
data() {
|
||||
return {
|
||||
showModal: false,
|
||||
messageType: '',
|
||||
templateIds: [],
|
||||
wxOpenTags: '',
|
||||
orderDetail: {}, //订单详情
|
||||
orderType: '', //订单类型
|
||||
payText: {
|
||||
fail: '支付失败',
|
||||
success: '支付成功',
|
||||
paying: '查询中...'
|
||||
},
|
||||
payImg: {
|
||||
fail: this.$IMG_URL + '/imgs/order/order_pay_fail.gif',
|
||||
success: this.$IMG_URL + '/imgs/order/order_pay_success.gif',
|
||||
paying: this.$IMG_URL + '/imgs/order/order_paying.gif'
|
||||
},
|
||||
payState: '' //支付状态
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
...mapGetters(['subscribeMessageIdsMap'])
|
||||
},
|
||||
onLoad() {
|
||||
this.payState = this.$Route.query.payState ? this.$Route.query.payState : 'paying';
|
||||
this.orderType = this.$Route.query.orderType
|
||||
this.$Route.query.orderId && this.getOrderDetail();
|
||||
switch (this.payState) {
|
||||
case 'success':
|
||||
this.getCartList();
|
||||
break;
|
||||
case 'fail':
|
||||
break;
|
||||
case 'paying':
|
||||
this.checkTimer();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
onHide() {
|
||||
clearInterval(payTimer);
|
||||
payTimer = null;
|
||||
},
|
||||
methods: {
|
||||
...mapActions(['getCartList']),
|
||||
|
||||
// 订阅消息
|
||||
onConfirm() {
|
||||
// #ifdef MP-WEIXIN
|
||||
this.templateIds.length && this.$store.commit('subscribeMessage', this.messageType);
|
||||
// #endif
|
||||
},
|
||||
|
||||
// 获取订单详情
|
||||
getOrderDetail() {
|
||||
switch (this.$Route.query.orderType) {
|
||||
case 'goods':
|
||||
this.getGoodsOrderDetail()
|
||||
break;
|
||||
case 'recharge':
|
||||
this.getRechargeOrderDetail()
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
// 支付订单信息
|
||||
getGoodsOrderDetail() {
|
||||
let that = this;
|
||||
that.$http('order.detail', {
|
||||
id: that.$Route.query.orderId
|
||||
}).then(async res => {
|
||||
if (res.code === 1) {
|
||||
that.orderDetail = res.data;
|
||||
if (res.data.status > 0) {
|
||||
this.messageType = this.orderDetail.activity_type == 'groupon' ? 'grouponResult' :
|
||||
'result';
|
||||
// #ifdef MP-WEIXIN
|
||||
this.templateIds = this.subscribeMessageIdsMap[this.messageType];
|
||||
this.showModal = this.templateIds.length;
|
||||
// #endif
|
||||
} else if (that.payState === 'success') {
|
||||
that.checkTimer();
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 充值订单信息
|
||||
getRechargeOrderDetail() {
|
||||
let that = this;
|
||||
that.$http('money.rechargeDetail', {
|
||||
id: that.$Route.query.orderId
|
||||
}).then(res => {
|
||||
if (res.code === 1) {
|
||||
that.orderDetail = res.data;
|
||||
if (res.data.status > 0) {
|
||||
this.messageType = 'result';
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 支付倒计时
|
||||
checkTimer() {
|
||||
let that = this;
|
||||
let count = 0;
|
||||
that.payState = 'paying';
|
||||
payTimer = setInterval(() => {
|
||||
count++;
|
||||
if (count < payCount) {
|
||||
that.checkPay();
|
||||
} else {
|
||||
clearInterval(payTimer);
|
||||
that.payState = 'fail';
|
||||
}
|
||||
}, 800);
|
||||
},
|
||||
|
||||
// 检测支付
|
||||
async checkPay() {
|
||||
let that = this;
|
||||
let paiMap = {
|
||||
'goods': 'order.detail',
|
||||
'recharge': 'money.rechargeDetail'
|
||||
}
|
||||
let res = await that.$http(
|
||||
paiMap[that.orderType], {
|
||||
id: that.$Route.query.orderId
|
||||
},
|
||||
false
|
||||
);
|
||||
if (res.code === 1 && res.data.status > 0) {
|
||||
that.payState = 'success';
|
||||
that.orderDetail = res.data;
|
||||
clearInterval(payTimer);
|
||||
that.getCartList();
|
||||
}
|
||||
},
|
||||
|
||||
// 重新支付
|
||||
onPay() {
|
||||
let that = this;
|
||||
let pay = new Pay(that.$Route.query.type, that.orderDetail, that.orderType);
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="scss" scopeds>
|
||||
.success-box {
|
||||
background: #fff;
|
||||
width: 750rpx;
|
||||
padding: 40rpx 0;
|
||||
|
||||
.pay-img {
|
||||
width: 130rpx;
|
||||
height: 130rpx;
|
||||
}
|
||||
|
||||
.notice {
|
||||
font-size: 30rpx;
|
||||
|
||||
font-weight: bold;
|
||||
color: rgba(51, 51, 51, 1);
|
||||
line-height: 30rpx;
|
||||
margin-top: 30rpx;
|
||||
}
|
||||
|
||||
.pay-money {
|
||||
font-size: 36rpx;
|
||||
color: #ff3000;
|
||||
font-weight: 600;
|
||||
margin-top: 20rpx;
|
||||
}
|
||||
|
||||
.btn-box {
|
||||
margin-top: 30rpx;
|
||||
width: 660rpx;
|
||||
|
||||
.base-btn {
|
||||
width: 320rpx;
|
||||
line-height: 70rpx;
|
||||
background: rgba(255, 255, 255, 1);
|
||||
border: 1rpx solid rgba(223, 223, 223, 0.5);
|
||||
border-radius: 35rpx;
|
||||
font-size: 28rpx;
|
||||
|
||||
font-weight: 400;
|
||||
color: rgba(153, 153, 153, 1);
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.again-pay {
|
||||
width: 320rpx;
|
||||
line-height: 70rpx;
|
||||
background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
|
||||
box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
|
||||
border-radius: 35rpx;
|
||||
font-size: 28rpx;
|
||||
|
||||
font-weight: 400;
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.hot-box {
|
||||
background: #fff;
|
||||
padding: 20rpx;
|
||||
margin-top: 20rpx;
|
||||
|
||||
.hot-title {
|
||||
font-size: 30rpx;
|
||||
|
||||
font-weight: 500;
|
||||
color: rgba(51, 51, 51, 1);
|
||||
height: 80rpx;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
282
shopro/pay/index.js
普通文件
282
shopro/pay/index.js
普通文件
@@ -0,0 +1,282 @@
|
||||
import $http from '@/shopro/request/index'
|
||||
// #ifdef H5
|
||||
import wxsdk from '@/shopro/wechat/sdk'
|
||||
// #endif
|
||||
import wechat from '@/shopro/wechat/wechat'
|
||||
import {
|
||||
router as $Router
|
||||
} from '@/shopro/router/index.js'
|
||||
import store from '@/shopro/store';
|
||||
import $platform from '@/shopro/platform';
|
||||
|
||||
/**
|
||||
* 支付
|
||||
*
|
||||
* @param {String} payment = ['wechat','alipay','wallet'] - 支付方式
|
||||
* @param {Object} order = {} - 订单详情
|
||||
* @param {String} orderType = ['goods','recharge'] - 订单类型
|
||||
*/
|
||||
|
||||
export default class ShoproPay {
|
||||
|
||||
|
||||
// wxOfficialAccount wxMiniProgram App H5
|
||||
// wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
|
||||
// alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
|
||||
// wallet v v v v
|
||||
|
||||
|
||||
constructor(payment, order, orderType) {
|
||||
this.payment = payment;
|
||||
this.order = order;
|
||||
this.orderType = orderType;
|
||||
this.platform = $platform.get();
|
||||
let payMehod = this.getPayMethod();
|
||||
console.log(this.payment, this.payment, this.platform);
|
||||
payMehod();
|
||||
}
|
||||
|
||||
getPayMethod() {
|
||||
var payMethod = {
|
||||
'wxOfficialAccount': {
|
||||
'wechat': () => {
|
||||
this.wxOfficialAccountPay()
|
||||
},
|
||||
'alipay': () => {
|
||||
this.copyPayLink()
|
||||
},
|
||||
'wallet': () => {
|
||||
this.walletPay()
|
||||
}
|
||||
},
|
||||
'wxMiniProgram': {
|
||||
'wechat': () => {
|
||||
this.wxMiniProgramPay()
|
||||
},
|
||||
'group': () => {
|
||||
this.wxMiniProgramPay()
|
||||
},
|
||||
'alipay': () => {
|
||||
this.copyPayLink()
|
||||
},
|
||||
'wallet': () => {
|
||||
this.walletPay()
|
||||
}
|
||||
},
|
||||
'App': {
|
||||
'wechat': () => {
|
||||
this.wechatPay()
|
||||
},
|
||||
'group': () => {
|
||||
this.wechatPay()
|
||||
},
|
||||
'alipay': () => {
|
||||
this.aliPay()
|
||||
},
|
||||
'wallet': () => {
|
||||
this.walletPay()
|
||||
},
|
||||
},
|
||||
'H5': {
|
||||
'wechat': () => {
|
||||
this.wechatWapPay()
|
||||
},
|
||||
'alipay': () => {
|
||||
this.goToPayLink()
|
||||
},
|
||||
'wallet': () => {
|
||||
this.walletPay()
|
||||
},
|
||||
},
|
||||
}
|
||||
return payMethod[this.platform][this.payment];
|
||||
}
|
||||
|
||||
// 预支付
|
||||
prepay() {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
let that = this;
|
||||
let params = {
|
||||
order_id: that.order.id,
|
||||
order_sn: that.order.order_sn,
|
||||
payment: that.payment
|
||||
}
|
||||
if (uni.getStorageSync('openid')) {
|
||||
params.openid = uni.getStorageSync('openid');
|
||||
}
|
||||
|
||||
$http('money.prepay', params, '支付中').then(res => {
|
||||
if (res.code === 1) {
|
||||
res.data === 'no_openid' ?
|
||||
uni.showModal({
|
||||
title: '微信支付',
|
||||
content: '请先绑定微信再使用微信支付',
|
||||
confirmText: '去绑定',
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
// 跳转到个人中心绑定信息
|
||||
uni.navigateTo({
|
||||
url: '/pages/user/info'
|
||||
});
|
||||
|
||||
// wechat.bind();
|
||||
}
|
||||
},
|
||||
}) :
|
||||
resolve(res);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// 微信H5支付
|
||||
async wxOfficialAccountPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
wxsdk.wxpay(result.data.pay_data, (res) => {
|
||||
res.errMsg == "chooseWXPay:ok" ? that.payResult('paying') : that.payResult('fail')
|
||||
});
|
||||
}
|
||||
|
||||
// 浏览器微信支付
|
||||
async wechatWapPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
if (result.code === 1) {
|
||||
var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
|
||||
let reg = new RegExp('&', 'g') //g代表全部
|
||||
let newUrl = url[1].replace(reg, '&');
|
||||
let domain = store.getters.initShop.domain; //域名需要https
|
||||
let params = encodeURIComponent(
|
||||
`${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
|
||||
)
|
||||
window.location.href = newUrl + '&redirect_url=' + params;
|
||||
}
|
||||
}
|
||||
|
||||
// 微信小程序支付
|
||||
async wxMiniProgramPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
...result.data.pay_data,
|
||||
success: res => {
|
||||
that.payResult('paying')
|
||||
},
|
||||
fail: err => {
|
||||
console.log('支付取消或者失败:', err);
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 余额支付
|
||||
async walletPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
result.code === 1 && that.payResult('paying')
|
||||
}
|
||||
|
||||
// 支付宝复制链接支付
|
||||
async copyPayLink() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
if (result.code === 1) {
|
||||
//引入showModal 点击确认 复制链接;
|
||||
uni.showModal({
|
||||
title: '支付宝支付',
|
||||
content: '复制链接到外部浏览器',
|
||||
confirmText: '复制链接',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
uni.setClipboardData({
|
||||
data: result.data.pay_data,
|
||||
success: function(data) {
|
||||
that.$u.toast('已复制到剪切板');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 支付链接
|
||||
async goToPayLink() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
if (result.code === 1) {
|
||||
window.location = result.data.pay_data;
|
||||
}
|
||||
}
|
||||
|
||||
// 支付宝支付
|
||||
async aliPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
|
||||
if (result.code === 1) {
|
||||
uni.requestPayment({
|
||||
provider: 'alipay',
|
||||
orderInfo: result.data.pay_data, // 支付宝订单数据
|
||||
success: res => {
|
||||
that.payResult('paying')
|
||||
},
|
||||
fail: err => {
|
||||
console.log('支付取消或者失败:', err);
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 微信app支付
|
||||
async wechatPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
|
||||
if (result.code === 1) {
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
orderInfo: JSON.parse(result.data.pay_data), // 微信订单数据(官方说是string。实测为object)
|
||||
// orderInfo: result.data.pay_data,
|
||||
success: res => {
|
||||
that.payResult('paying')
|
||||
},
|
||||
fail: err => {
|
||||
console.log('取消支付或支付失败:', err);
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult(
|
||||
'fail');
|
||||
return;
|
||||
uni.showModal({
|
||||
title: '温馨提示',
|
||||
content: `支付取消或者失败。errMsg:${err.errMsg}。errCode:${err.errCode}。code:${err.code}`,
|
||||
showCancel: false,
|
||||
success: ret => {
|
||||
console.log('取消支付或支付失败:', err);
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult(
|
||||
'fail')
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 支付结果跳转,success:成功,fail:失败
|
||||
payResult(resultType) {
|
||||
const that = this;
|
||||
$Router.replace({
|
||||
path: '/pages/order/payment/result',
|
||||
query: {
|
||||
orderId: that.order.id,
|
||||
type: that.payment, //重新支付的时候使用
|
||||
payState: resultType,
|
||||
orderType: that.orderType
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户