fix: restore wechat payment oauth and jsapi flow

This commit is contained in:
IanShaw027
2026-04-20 23:34:57 +08:00
parent 6f00efa350
commit 7ef7fd19e7
16 changed files with 1563 additions and 87 deletions

View File

@@ -105,6 +105,50 @@ describe('decidePaymentLaunch', () => {
expect(decision.recovery.paymentMode).toBe('popup')
expect(decision.recovery.resumeToken).toBe('resume-2')
})
it('returns wechat oauth launch when backend requires in-app authorization', () => {
const decision = decidePaymentLaunch(createOrderResult({
result_type: 'oauth_required',
payment_type: 'wxpay',
oauth: {
authorize_url: '/api/v1/auth/oauth/wechat/payment/start?payment_type=wxpay',
appid: 'wx123',
scope: 'snsapi_base',
redirect_url: '/auth/wechat/payment/callback',
},
}), {
visibleMethod: 'wxpay',
orderType: 'balance',
isMobile: true,
})
expect(decision.kind).toBe('wechat_oauth')
expect(decision.oauth?.authorize_url).toContain('/api/v1/auth/oauth/wechat/payment/start')
expect(decision.paymentState.paymentType).toBe('wxpay')
})
it('returns wechat jsapi launch when backend has a jsapi payload ready', () => {
const decision = decidePaymentLaunch(createOrderResult({
result_type: 'jsapi_ready',
payment_type: 'wxpay',
jsapi: {
appId: 'wx123',
timeStamp: '1712345678',
nonceStr: 'nonce-123',
package: 'prepay_id=wx123',
signType: 'RSA',
paySign: 'signed-payload',
},
}), {
visibleMethod: 'wxpay',
orderType: 'subscription',
isMobile: true,
})
expect(decision.kind).toBe('wechat_jsapi')
expect(decision.jsapi?.appId).toBe('wx123')
expect(decision.paymentState.orderType).toBe('subscription')
})
})
describe('buildCreateOrderPayload', () => {

View File

@@ -1,4 +1,11 @@
import type { CreateOrderRequest, CreateOrderResult, MethodLimit, OrderType } from '@/types/payment'
import type {
CreateOrderRequest,
CreateOrderResult,
MethodLimit,
OrderType,
WechatJSAPIPayload,
WechatOAuthInfo,
} from '@/types/payment'
export const PAYMENT_RECOVERY_STORAGE_KEY = 'payment.recovery.current'
@@ -16,6 +23,8 @@ export type PaymentLaunchKind =
| 'redirect_waiting'
| 'stripe_popup'
| 'stripe_route'
| 'wechat_oauth'
| 'wechat_jsapi'
| 'unhandled'
export interface PaymentRecoverySnapshot {
@@ -47,6 +56,8 @@ export interface PaymentLaunchDecision {
paymentState: PaymentRecoverySnapshot
recovery: PaymentRecoverySnapshot
stripeMethod?: StripeVisibleMethod
oauth?: WechatOAuthInfo
jsapi?: WechatJSAPIPayload
}
export interface BuildCreateOrderPayloadInput {
@@ -139,6 +150,15 @@ export function decidePaymentLaunch(
return { kind, paymentState, recovery: paymentState, stripeMethod }
}
if (result.result_type === 'oauth_required' && result.oauth?.authorize_url) {
return { kind: 'wechat_oauth', paymentState: baseState, recovery: baseState, oauth: result.oauth }
}
const jsapiPayload = result.jsapi ?? result.jsapi_payload
if (result.result_type === 'jsapi_ready' && jsapiPayload) {
return { kind: 'wechat_jsapi', paymentState: baseState, recovery: baseState, jsapi: jsapiPayload }
}
if (baseState.qrCode) {
return { kind: 'qr_waiting', paymentState: baseState, recovery: baseState }
}