mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 12:02:27 +08:00
fix wxpay pending order reconciliation
This commit is contained in:
@@ -114,6 +114,11 @@ const paidOrder = ref<PaymentOrder | null>(null)
|
||||
|
||||
let pollTimer: ReturnType<typeof setInterval> | null = null
|
||||
let countdownTimer: ReturnType<typeof setInterval> | null = null
|
||||
let verifyAttempts = 0
|
||||
let lastVerifyAt = 0
|
||||
|
||||
const VERIFY_RETRY_INTERVAL_MS = 15000
|
||||
const VERIFY_RETRY_MAX_ATTEMPTS = 6
|
||||
|
||||
const isAlipay = computed(() => props.paymentType.includes('alipay'))
|
||||
const isWxpay = computed(() => props.paymentType.includes('wxpay'))
|
||||
@@ -186,8 +191,9 @@ async function renderQR() {
|
||||
|
||||
async function pollStatus() {
|
||||
if (!props.orderId) return
|
||||
const order = await paymentStore.pollOrderStatus(props.orderId)
|
||||
let order = await paymentStore.pollOrderStatus(props.orderId)
|
||||
if (!order) return
|
||||
order = await tryRecoverPendingOrder(order)
|
||||
if (order.status === 'COMPLETED' || order.status === 'PAID') {
|
||||
cleanup()
|
||||
paidOrder.value = order
|
||||
@@ -199,6 +205,27 @@ async function pollStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
async function tryRecoverPendingOrder(order: PaymentOrder): Promise<PaymentOrder> {
|
||||
if (!isWxpay.value) return order
|
||||
const outTradeNo = String(order.out_trade_no || '').trim()
|
||||
if (!outTradeNo) return order
|
||||
const normalizedStatus = String(order.status || '').trim().toUpperCase()
|
||||
if (normalizedStatus !== 'PENDING') return order
|
||||
const now = Date.now()
|
||||
if (verifyAttempts >= VERIFY_RETRY_MAX_ATTEMPTS || now - lastVerifyAt < VERIFY_RETRY_INTERVAL_MS) {
|
||||
return order
|
||||
}
|
||||
|
||||
lastVerifyAt = now
|
||||
verifyAttempts += 1
|
||||
try {
|
||||
const result = await paymentAPI.verifyOrder(outTradeNo)
|
||||
return result.data ?? order
|
||||
} catch {
|
||||
return order
|
||||
}
|
||||
}
|
||||
|
||||
function startCountdown(seconds: number) {
|
||||
remainingSeconds.value = Math.max(0, seconds)
|
||||
if (remainingSeconds.value <= 0) {
|
||||
@@ -250,6 +277,8 @@ function init() {
|
||||
expired.value = false
|
||||
cancelling.value = false
|
||||
qrUrl.value = props.qrCode
|
||||
verifyAttempts = 0
|
||||
lastVerifyAt = 0
|
||||
|
||||
let seconds = 30 * 60
|
||||
if (props.expiresAt) {
|
||||
|
||||
@@ -175,6 +175,11 @@ const outcome = ref<PaymentOutcome | null>(null)
|
||||
|
||||
let pollTimer: ReturnType<typeof setInterval> | null = null
|
||||
let countdownTimer: ReturnType<typeof setInterval> | null = null
|
||||
let verifyAttempts = 0
|
||||
let lastVerifyAt = 0
|
||||
|
||||
const VERIFY_RETRY_INTERVAL_MS = 15000
|
||||
const VERIFY_RETRY_MAX_ATTEMPTS = 6
|
||||
|
||||
const isAlipay = computed(() => props.paymentType.includes('alipay'))
|
||||
const isWxpay = computed(() => props.paymentType.includes('wxpay'))
|
||||
@@ -241,10 +246,32 @@ async function renderQR() {
|
||||
})
|
||||
}
|
||||
|
||||
async function tryRecoverPendingOrder(order: PaymentOrder): Promise<PaymentOrder> {
|
||||
if (!isWxpay.value) return order
|
||||
const outTradeNo = String(order.out_trade_no || '').trim()
|
||||
if (!outTradeNo) return order
|
||||
const normalizedStatus = String(order.status || '').trim().toUpperCase()
|
||||
if (normalizedStatus !== 'PENDING') return order
|
||||
const now = Date.now()
|
||||
if (verifyAttempts >= VERIFY_RETRY_MAX_ATTEMPTS || now - lastVerifyAt < VERIFY_RETRY_INTERVAL_MS) {
|
||||
return order
|
||||
}
|
||||
|
||||
lastVerifyAt = now
|
||||
verifyAttempts += 1
|
||||
try {
|
||||
const result = await paymentAPI.verifyOrder(outTradeNo)
|
||||
return result.data ?? order
|
||||
} catch {
|
||||
return order
|
||||
}
|
||||
}
|
||||
|
||||
async function pollStatus() {
|
||||
if (!props.orderId || outcome.value) return
|
||||
const order = await paymentStore.pollOrderStatus(props.orderId)
|
||||
let order = await paymentStore.pollOrderStatus(props.orderId)
|
||||
if (!order) return
|
||||
order = await tryRecoverPendingOrder(order)
|
||||
if (isSuccessStatus(order.status)) {
|
||||
cleanup()
|
||||
paidOrder.value = order
|
||||
@@ -291,6 +318,8 @@ function cleanup() {
|
||||
|
||||
// Initialize on mount
|
||||
qrUrl.value = props.qrCode
|
||||
verifyAttempts = 0
|
||||
lastVerifyAt = 0
|
||||
let seconds = 30 * 60
|
||||
if (props.expiresAt) {
|
||||
seconds = Math.floor((new Date(props.expiresAt).getTime() - Date.now()) / 1000)
|
||||
|
||||
@@ -3,6 +3,7 @@ import { flushPromises, mount } from '@vue/test-utils'
|
||||
|
||||
const pollOrderStatus = vi.hoisted(() => vi.fn())
|
||||
const cancelOrder = vi.hoisted(() => vi.fn())
|
||||
const verifyOrder = vi.hoisted(() => vi.fn())
|
||||
const showError = vi.hoisted(() => vi.fn())
|
||||
const toCanvas = vi.hoisted(() => vi.fn())
|
||||
|
||||
@@ -31,6 +32,7 @@ vi.mock('@/stores', () => ({
|
||||
vi.mock('@/api/payment', () => ({
|
||||
paymentAPI: {
|
||||
cancelOrder,
|
||||
verifyOrder,
|
||||
},
|
||||
}))
|
||||
|
||||
@@ -62,6 +64,7 @@ describe('PaymentStatusPanel', () => {
|
||||
vi.useFakeTimers()
|
||||
pollOrderStatus.mockReset()
|
||||
cancelOrder.mockReset()
|
||||
verifyOrder.mockReset()
|
||||
showError.mockReset()
|
||||
toCanvas.mockReset().mockResolvedValue(undefined)
|
||||
})
|
||||
@@ -128,4 +131,35 @@ describe('PaymentStatusPanel', () => {
|
||||
|
||||
openSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('actively verifies a stuck pending order and settles it when upstream confirms payment', async () => {
|
||||
pollOrderStatus.mockResolvedValue(orderFactory('PENDING'))
|
||||
verifyOrder.mockResolvedValue({
|
||||
data: orderFactory('COMPLETED'),
|
||||
})
|
||||
|
||||
const wrapper = mount(PaymentStatusPanel, {
|
||||
props: {
|
||||
orderId: 42,
|
||||
qrCode: 'https://pay.example.com/qr/42',
|
||||
expiresAt: '2099-01-01T12:30:00Z',
|
||||
paymentType: 'wxpay',
|
||||
orderType: 'balance',
|
||||
},
|
||||
global: {
|
||||
stubs: {
|
||||
Icon: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
await vi.advanceTimersByTimeAsync(3000)
|
||||
await flushPromises()
|
||||
|
||||
expect(pollOrderStatus).toHaveBeenCalledWith(42)
|
||||
expect(verifyOrder).toHaveBeenCalledWith('sub2_20260420abcd1234')
|
||||
expect(wrapper.text()).toContain('payment.result.success')
|
||||
expect(wrapper.emitted('success')).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -267,10 +267,15 @@ async function resolveOrderFromResumeToken(resumeToken: string): Promise<Payment
|
||||
|
||||
async function resolveOrderFromOutTradeNo(outTradeNo: string): Promise<PaymentOrder | null> {
|
||||
try {
|
||||
const result = await paymentAPI.verifyOrderPublic(outTradeNo)
|
||||
const result = await paymentAPI.verifyOrder(outTradeNo)
|
||||
return result.data
|
||||
} catch (_err: unknown) {
|
||||
return null
|
||||
try {
|
||||
const result = await paymentAPI.verifyOrderPublic(outTradeNo)
|
||||
return result.data
|
||||
} catch (_innerErr: unknown) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ const routeState = vi.hoisted(() => ({
|
||||
|
||||
const routerPush = vi.hoisted(() => vi.fn())
|
||||
const pollOrderStatus = vi.hoisted(() => vi.fn())
|
||||
const verifyOrder = vi.hoisted(() => vi.fn())
|
||||
const verifyOrderPublic = vi.hoisted(() => vi.fn())
|
||||
const resolveOrderPublicByResumeToken = vi.hoisted(() => vi.fn())
|
||||
|
||||
@@ -37,6 +38,7 @@ vi.mock('@/stores/payment', () => ({
|
||||
|
||||
vi.mock('@/api/payment', () => ({
|
||||
paymentAPI: {
|
||||
verifyOrder,
|
||||
verifyOrderPublic,
|
||||
resolveOrderPublicByResumeToken,
|
||||
},
|
||||
@@ -86,6 +88,7 @@ describe('PaymentResultView', () => {
|
||||
routeState.query = {}
|
||||
routerPush.mockReset()
|
||||
pollOrderStatus.mockReset()
|
||||
verifyOrder.mockReset()
|
||||
verifyOrderPublic.mockReset()
|
||||
resolveOrderPublicByResumeToken.mockReset()
|
||||
window.localStorage.clear()
|
||||
@@ -329,6 +332,7 @@ describe('PaymentResultView', () => {
|
||||
out_trade_no: 'legacy-123',
|
||||
trade_status: 'TRADE_SUCCESS',
|
||||
}
|
||||
verifyOrder.mockRejectedValue(new Error('auth required'))
|
||||
verifyOrderPublic.mockResolvedValue({
|
||||
data: orderFactory('PAID'),
|
||||
})
|
||||
@@ -343,11 +347,36 @@ describe('PaymentResultView', () => {
|
||||
|
||||
await flushPromises()
|
||||
|
||||
expect(verifyOrder).toHaveBeenCalledWith('legacy-123')
|
||||
expect(verifyOrderPublic).toHaveBeenCalledWith('legacy-123')
|
||||
expect(pollOrderStatus).not.toHaveBeenCalled()
|
||||
expect(wrapper.text()).toContain('payment.result.success')
|
||||
})
|
||||
|
||||
it('prefers authenticated order verification before falling back to public lookup', async () => {
|
||||
routeState.query = {
|
||||
out_trade_no: 'auth-verify-123',
|
||||
trade_status: 'TRADE_SUCCESS',
|
||||
}
|
||||
verifyOrder.mockResolvedValue({
|
||||
data: orderFactory('COMPLETED'),
|
||||
})
|
||||
|
||||
const wrapper = mount(PaymentResultView, {
|
||||
global: {
|
||||
stubs: {
|
||||
OrderStatusBadge: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
|
||||
expect(verifyOrder).toHaveBeenCalledWith('auth-verify-123')
|
||||
expect(verifyOrderPublic).not.toHaveBeenCalled()
|
||||
expect(wrapper.text()).toContain('payment.result.success')
|
||||
})
|
||||
|
||||
it('does not use public out_trade_no verification for bare order numbers without legacy return markers', async () => {
|
||||
routeState.query = {
|
||||
out_trade_no: 'legacy-bare',
|
||||
|
||||
Reference in New Issue
Block a user