支付宝兼容

This commit is contained in:
2026-03-09 09:07:58 +08:00
parent 069677957e
commit b3836b8bf2
31 changed files with 2382 additions and 307 deletions
+178 -93
View File
@@ -176,9 +176,6 @@ import {
createCouponPayment,
cancelCouponPayment
} from '@/config/api/coupon.js'
// import {
// cancelMemberCardPayment
// } from '@/config/api/member.js'
import {
createMemberCardPayment,
getMemberCardsByPosition,
@@ -353,7 +350,48 @@ const selectProduct = (product) => {
const orderNo = ref('')
// 处理购买
// 获取当前支付平台(前端维度)
const getPaymentPlatform = () => {
// 小程序环境
// #ifdef MP-WEIXIN
return 'WECHAT'
// #endif
// #ifdef MP-ALIPAY
return 'ALIPAY'
// #endif
// H5 默认使用 ANTOM
// #ifdef H5
return 'ANTOM'
// #endif
return 'WECHAT'
}
// 支付宝 tradePay 兼容:优先 tradeNO,其次 orderStr
const alipayTradePay = ({ tradeNo, orderStr, onSuccess, onFail }) => {
// #ifdef MP-ALIPAY
const tradeNO = tradeNo
if (tradeNO) {
my.tradePay({
tradeNO,
success: onSuccess,
fail: onFail
})
return
}
if (orderStr) {
my.tradePay({
orderStr,
success: onSuccess,
fail: onFail
})
return
}
// #endif
onFail && onFail(new Error('未获取到支付宝支付参数'))
}
// 处理购买(会员卡 / 优惠券),内部使用商品多支付平台方案下的统一思路
const handleBuy = async () => {
if (!selectedProduct.value) {
uni.showToast({
@@ -363,63 +401,89 @@ const handleBuy = async () => {
return
}
// 会员卡购买
const paymentPlatform = getPaymentPlatform()
// 会员卡购买(按接口文档:POST /app/member/pay
if (currentTab.value === 'card') {
try {
uni.showLoading({
title: '正在创建订单...'
})
const res = await createMemberCardPayment(selectedProduct.value.id)
const res = await createMemberCardPayment(selectedProduct.value.id, paymentPlatform)
uni.hideLoading()
if (res.code === 200 && res.data) {
orderNo.value = res.data.OutOrderNo;
// 调起微信支付
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.packageValue || res.data.package,
signType: res.data.signType || 'MD5',
paySign: res.data.paySign,
success: (payRes) => {
uni.showToast({
title: '支付成功',
icon: 'success'
})
// 支付成功后,跳转到我的会员卡页面
setTimeout(() => {
uni.navigateTo({
url: '/subPackages/business/my-card'
})
}, 1500)
},
fail: (err) => {
console.error('支付失败:', err)
console.log('支付失败详细信息:', err.errMsg.includes('cancel'));
if (err.errMsg && err.errMsg.includes('cancel')) {
if (orderNo.value) {
cancelMemberCardPayment(orderNo.value)
.then(cancelRes => {
console.log('取消支付订单成功:', cancelRes);
})
.catch(cancelErr => {
console.error('取消支付订单失败:', cancelErr);
});
// 不同平台分别发起支付(字段按文档)
// 微信小程序
// #ifdef MP-WEIXIN
if (paymentPlatform === 'WECHAT') {
// 会员卡订单号:OutOrderNo(文档)
orderNo.value = res.data.OutOrderNo
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.package,
signType: res.data.signType || 'MD5',
paySign: res.data.paySign,
success: () => {
uni.showToast({ title: '支付成功', icon: 'success' })
setTimeout(() => {
uni.navigateTo({ url: '/subPackages/business/my-card' })
}, 1500)
},
fail: (err) => {
console.error('支付失败:', err)
if (err.errMsg && err.errMsg.includes('cancel')) {
// 取消支付(本项目取消接口走 device 侧 cancel
orderNo.value && cancelMemberCardPayment(orderNo.value).catch(() => {})
uni.showToast({ title: '已取消支付', icon: 'none' })
} else {
uni.showToast({ title: '支付失败', icon: 'none' })
}
uni.showToast({
title: '已取消支付',
icon: 'none'
})
} else {
uni.showToast({
title: '支付失败',
icon: 'none'
})
}
})
return
}
// #endif
// 支付宝小程序
// #ifdef MP-ALIPAY
if (paymentPlatform === 'ALIPAY') {
// 文档返回:tradeNo / outTradeNo;也兼容 orderStr
const tradeNo = res.data.tradeNo || res.data.tradeNO
const payForm = res.data.payForm || res.data.orderStr
alipayTradePay({
tradeNo,
orderStr: payForm,
onSuccess: (payRes) => {
if (payRes.resultCode === '9000') {
uni.showToast({ title: '支付成功', icon: 'success' })
setTimeout(() => {
uni.navigateTo({ url: '/subPackages/business/my-card' })
}, 1500)
} else {
uni.showToast({ title: '支付失败', icon: 'none' })
}
},
onFail: () => {
uni.showToast({ title: '支付失败', icon: 'none' })
}
})
return
}
// #endif
// H5 + Antom(文档里 Antom 预留,当前后端可能返回 cashierUrl / h5Url 之一)
// #ifdef H5
if (paymentPlatform === 'ANTOM') {
const cashierUrl = res.data.cashierUrl || res.data.h5Url
if (cashierUrl) {
window.location.href = cashierUrl
return
}
})
}
// #endif
} else {
uni.showToast({
title: res.msg || '创建订单失败',
@@ -437,62 +501,83 @@ const handleBuy = async () => {
return
}
// 优惠券购买
// 优惠券购买(按接口文档:POST /app/coupon/pay
if (currentTab.value === 'coupon') {
try {
uni.showLoading({
title: '正在创建订单...'
})
const res = await createCouponPayment(selectedProduct.value.couponId)
const res = await createCouponPayment(selectedProduct.value.couponId, paymentPlatform)
uni.hideLoading()
if (res.code === 200 && res.data) {
// 调起微信支付
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.packageValue || res.data.package,
signType: res.data.signType || 'MD5',
paySign: res.data.paySign,
success: (payRes) => {
uni.showToast({
title: '支付成功',
icon: 'success'
})
// 支付成功后,跳转到我的优惠券页面
setTimeout(() => {
uni.navigateTo({
url: '/subPackages/business/my-coupon'
})
}, 1500)
},
fail: (err) => {
console.error('支付失败:', err)
if (err.errMsg && err.errMsg.includes('cancel')) {
// 用户取消支付,调用取消接口
const orderNo = res.data.OutOrderNo;
if (orderNo) {
cancelCouponPayment(orderNo)
.then(cancelRes => {
console.log('取消支付订单成功:', cancelRes);
})
.catch(cancelErr => {
console.error('取消支付订单失败:', cancelErr);
});
// 微信小程序
// #ifdef MP-WEIXIN
if (paymentPlatform === 'WECHAT') {
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.package,
signType: res.data.signType || 'MD5',
paySign: res.data.paySign,
success: () => {
uni.showToast({ title: '支付成功', icon: 'success' })
setTimeout(() => {
uni.navigateTo({ url: '/subPackages/business/my-coupon' })
}, 1500)
},
fail: (err) => {
console.error('支付失败:', err)
if (err.errMsg && err.errMsg.includes('cancel')) {
const outOrderNo = res.data.OutOrderNo || res.data.outOrderNo
outOrderNo && cancelCouponPayment(outOrderNo).catch(() => {})
uni.showToast({ title: '已取消支付', icon: 'none' })
} else {
uni.showToast({ title: '支付失败', icon: 'none' })
}
uni.showToast({
title: '已取消支付',
icon: 'none'
})
} else {
uni.showToast({
title: '支付失败',
icon: 'none'
})
}
})
return
}
// #endif
// 支付宝小程序
// #ifdef MP-ALIPAY
if (paymentPlatform === 'ALIPAY') {
const tradeNo = res.data.tradeNo || res.data.tradeNO
const payForm = res.data.payForm || res.data.orderStr
alipayTradePay({
tradeNo,
orderStr: payForm,
onSuccess: (payRes) => {
if (payRes.resultCode === '9000') {
uni.showToast({ title: '支付成功', icon: 'success' })
setTimeout(() => {
uni.navigateTo({ url: '/subPackages/business/my-coupon' })
}, 1500)
} else {
uni.showToast({ title: '支付失败', icon: 'none' })
}
},
onFail: () => {
uni.showToast({ title: '支付失败', icon: 'none' })
}
})
return
}
// #endif
// H5 + Antom
// #ifdef H5
if (paymentPlatform === 'ANTOM') {
const cashierUrl = res.data.cashierUrl || res.data.h5Url
if (cashierUrl) {
window.location.href = cashierUrl
return
}
})
}
// #endif
} else {
uni.showToast({
title: res.msg || '创建订单失败',