新增支付失败逻辑
This commit is contained in:
@@ -175,3 +175,71 @@ export function buildPaymentItemFromApp(appData) {
|
||||
const displayName = appData.goodName || appData.remark || appData.type || '缴费'
|
||||
return { ...appData, type: displayName, rechargeAmount: amount }
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付返回结果
|
||||
* 日照银行支付小程序完成支付后,通过 navigateBackMiniProgram 携带 extraData 返回,
|
||||
* 本小程序在 App.onShow 中通过 referrerInfo.extraData 接收(场景值 1038)。
|
||||
* 这里只负责捕获/读取/清除原始返回数据,成功/失败判定见 resolvePaymentResult。
|
||||
*/
|
||||
const PAYMENT_RESULT_KEY = 'payment_return_result'
|
||||
|
||||
/**
|
||||
* 捕获支付返回结果(在 App.onShow 中调用)
|
||||
* 注意:App 冷/热启动进入时 extraData 是支付参数(payAmount/billIds 等),
|
||||
* 并非支付返回,需跳过,避免覆盖真实结果。
|
||||
*/
|
||||
export function capturePaymentResult(onShowOptions) {
|
||||
if (!onShowOptions) return
|
||||
const extraData = onShowOptions?.referrerInfo?.extraData
|
||||
if (!extraData) return
|
||||
|
||||
// 含支付参数字段说明是 App 跳转进入(冷/热启动),不是支付返回,跳过
|
||||
if (typeof extraData === 'object' && (extraData.payAmount || extraData.billId || extraData.billIds || extraData.acct || extraData.custId)) {
|
||||
return
|
||||
}
|
||||
|
||||
let parsed = extraData
|
||||
if (typeof extraData === 'string') {
|
||||
try {
|
||||
parsed = JSON.parse(extraData)
|
||||
} catch (e) {
|
||||
parsed = parseQueryString(extraData)
|
||||
}
|
||||
}
|
||||
console.log('捕获到支付返回结果:', JSON.stringify(parsed))
|
||||
uni.setStorageSync(PAYMENT_RESULT_KEY, parsed)
|
||||
}
|
||||
|
||||
/** 读取支付返回结果(读取后建议调用 clearPaymentReturnResult 清除) */
|
||||
export function getPaymentReturnResult() {
|
||||
try { return uni.getStorageSync(PAYMENT_RESULT_KEY) || null } catch (e) { return null }
|
||||
}
|
||||
|
||||
/** 清除支付返回结果 */
|
||||
export function clearPaymentReturnResult() {
|
||||
uni.removeStorageSync(PAYMENT_RESULT_KEY)
|
||||
}
|
||||
|
||||
// 失败/成功信号值(大小写不敏感),字段名与取值以日照银行实际返回为准,如不一致只需调整下方映射
|
||||
const FAIL_SIGNALS = ['fail', 'failed', 'cancel', 'cancelled', 'error', 'closed', 'notpay', 'payerror', '2', 'false', 'no']
|
||||
const SUCCESS_SIGNALS = ['success', 'ok', 'paid', 'finish', 'finished', '1', '0000', '0', '200', 'true', 'yes']
|
||||
const RESULT_FIELDS = ['payResult', 'result', 'status', 'payStatus', 'tradeState', 'returnCode', 'code', 'errCode']
|
||||
|
||||
/**
|
||||
* 判定日照银行返回的支付结果:'success' | 'fail' | null(无法判定)
|
||||
*/
|
||||
export function resolvePaymentResult(data) {
|
||||
if (!data || typeof data !== 'object') return null
|
||||
// 布尔字段优先
|
||||
if (typeof data.success === 'boolean') return data.success ? 'success' : 'fail'
|
||||
if (typeof data.isSuccess === 'boolean') return data.isSuccess ? 'success' : 'fail'
|
||||
for (const key of RESULT_FIELDS) {
|
||||
const v = data[key]
|
||||
if (v === undefined || v === null || v === '') continue
|
||||
const s = String(v).trim().toLowerCase()
|
||||
if (FAIL_SIGNALS.includes(s)) return 'fail'
|
||||
if (SUCCESS_SIGNALS.includes(s)) return 'success'
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user