// #ifdef APP-PLUS import { initDypay, canOpenDypay, openDypay, resetDypay } from '@/uni_modules/tb-douyin-pay'; // #endif import request from './request.js'; import { BASE_URL } from './config.js'; import { TOKEN_NAME, USER_DATA } from './auth.js'; import { toSdkPayInfo, queryUntilSettled } from './douyin-pay-adapter.mjs'; let active = false; let resuming = false; function accountKey() { if (!uni.getStorageSync(TOKEN_NAME)) return ''; const user = uni.getStorageSync(USER_DATA) || {}; const id = user.userId || user.id; return id ? `tb-douyin:${BASE_URL}:${id}` : ''; } async function query(orderNum) { return queryUntilSettled(async options => { const response = await request({ ...options, isShowLoading: false, timeout: 30000 }); if (response?.bizcode !== 100) throw new Error('查询支付结果失败'); return response.data; }, orderNum); } export function douyinPayTypes(types) { // #ifdef H5 return types.split(',').includes('6') ? types : `${types},6`; // #endif // #ifdef APP-PLUS return types.split(',').includes('6') ? types : `${types},6`; // #endif // #ifndef APP-PLUS return types; // #endif } export function filterDouyinPayWays(ways) { // #ifdef H5 return ways || []; // #endif // #ifdef APP-PLUS return ways || []; // #endif // #ifndef APP-PLUS return (ways || []).filter(item => item.type !== 'douyin'); // #endif } function showState(state, key, orderNum) { if (key !== accountKey()) return; if (state.state !== 'pending' && uni.getStorageSync(key) === orderNum) uni.removeStorageSync(key); uni.$emit('douyin-payment-result', { orderNum, state: state.state }); uni.showToast({ title: state.state === 'paid' ? '支付成功' : state.state === 'failed' ? '支付未成功,请查看订单' : state.state === 'closed' ? '原支付已关闭,可重新下单' : state.message || '支付结果待确认,请稍后查看订单', icon: 'none', duration: 2500 }); } // Called only by a user payment action, never by onShow. Renew the SAME transaction on the server. async function continuePendingPayment(key, orderNum) { const response = await request({ url: '/payment/douyin/resume', method: 'POST', data: { orderNum }, isShowLoading: false, timeout: 60000 }); if (key !== accountKey()) return { state: 'pending' }; if (response?.bizcode !== 100) throw new Error('暂时无法续付,请查看订单后重试'); const result = response.data; if (!result || result.orderNum !== orderNum) throw new Error('续付订单不匹配,请查看订单'); if (result.state === 'paid' || result.state === 'closed') { // The existing query endpoint also reconciles business orders when a webhook was missed. const state = result.state === 'paid' ? await query(orderNum) : { state: 'closed' }; if (key === accountKey() && state.state !== 'pending') resetDypay(); showState(state, key, orderNum); return state; } if (result.state !== 'unpaid') { const messages = { QUERY_FAILED: '抖音查单失败,暂不能重新支付,请稍后重试', CLOSE_UNCONFIRMED: '旧支付关单尚未确认,请稍后重试', RECONCILIATION_REQUIRED: '订单状态需核对,请联系客服,勿重复付款', CHANNEL_PENDING: '抖音支付处理中,请稍后查看订单' }; const state = { state: 'pending', message: messages[result.reason] }; showState(state, key, orderNum); return state; } if (result.payment?.orderNum !== orderNum) throw new Error('续付订单不匹配,请查看订单'); // Validate before the confirmation dialog. Never store the SDK signature locally. toSdkPayInfo(result.payment); resetDypay(); const confirmed = await new Promise(resolve => uni.showModal({ title: '继续上一笔支付', content: `上一笔抖音支付尚未完成,是否继续支付原订单?${result.payment.amount != null ? `金额:¥${result.payment.amount}。` : ''}不会创建新订单。`, confirmText: '继续支付', cancelText: '暂不支付', success: res => resolve(res.confirm === true), fail: () => resolve(false) })); if (!confirmed || key !== accountKey()) return { state: 'pending' }; return executePayment(result.payment, key); } async function executePayment(payment, key) { // #ifdef APP-PLUS if (key !== accountKey()) return { state: 'pending' }; const payInfo = toSdkPayInfo(payment); if (!canOpenDypay()) throw new Error('请先安装或升级抖音客户端'); if (!initDypay({ appId: payInfo.appid, callbackScheme: payment.douyin.callbackScheme || 'trustbridgeapp' })) { throw new Error('支付初始化失败,请稍后重试'); } // Persist before entering native code so a killed process remains recoverable. uni.setStorageSync(key, payment.orderNum); const nativeResult = await new Promise(resolve => openDypay({ payInfo, showLoading: true }, resolve)); const state = await query(payment.orderNum); if (state.state === 'pending' && nativeResult?.resultCode && !['0', '3'].includes(String(nativeResult.resultCode))) { // A native error is diagnostic only: never clear a potentially live payment on its authority. const descriptions = { '1': '支付已取消', '2': '支付 SDK 调用失败', '100': '请安装或升级抖音客户端', '103': '原生支付仍在处理中', '-1': '支付参数校验失败' }; state.message = `${descriptions[String(nativeResult.resultCode)] || '支付未完成'},请查询订单后重试`; } showState(state, key, payment.orderNum); return state; // #endif } // Runs BEFORE creating an order. A pending record offers explicit continuation, not a new order. export async function prepareDouyinPay(payway) { if (payway !== 'douyin') return true; try { // #ifndef APP-PLUS throw new Error('抖音支付仅支持 Android/iOS App'); // #endif // #ifdef APP-PLUS if (active || resuming) throw new Error('正在处理支付,请勿重复点击'); const key = accountKey(); if (!key) throw new Error('请先登录后再支付'); const previous = uni.getStorageSync(key); if (previous) { resuming = true; try { const result = await continuePendingPayment(key, previous); if (result.state !== 'closed' || key !== accountKey() || uni.getStorageSync(key)) return false; const confirmed = await new Promise(resolve => uni.showModal({ title: '原支付已关闭', content: '上一笔支付已确认关闭。是否按当前页面重新发起支付?原商品订单若已过期,请返回商品页重新下单。', confirmText: '重新支付', cancelText: '暂不支付', success: res => resolve(res.confirm === true), fail: () => resolve(false) })); if (!confirmed || key !== accountKey() || uni.getStorageSync(key)) return false; if (!canOpenDypay()) throw new Error('请先安装或升级抖音客户端'); return true; } finally { resuming = false; } } if (!canOpenDypay()) throw new Error('请先安装或升级抖音客户端'); return true; // #endif } catch (error) { uni.showToast({ title: error.message || '暂时无法支付,请稍后重试', icon: 'none' }); return false; } } // Takes the existing settlement/recharge response; does NOT create a second order. export async function appDypayFun(payment) { // #ifndef APP-PLUS uni.showToast({ title: '抖音支付仅支持 Android/iOS App', icon: 'none' }); return; // #endif // #ifdef APP-PLUS if (active || resuming) return; active = true; const key = accountKey(); try { if (!key || !payment.orderNum) throw new Error('登录身份或支付订单编号缺失'); const previous = uni.getStorageSync(key); if (previous) { // Even legacy callers must not open a different payment while the saved one is unresolved. return await continuePendingPayment(key, previous); } return await executePayment(payment, key); } catch (error) { if (key === accountKey()) uni.showToast({ title: error.message || '支付结果待确认,请查看订单', icon: 'none' }); return { state: 'error', message: error.message || '支付结果待确认,请查看订单' }; } finally { active = false; } // #endif } export async function resumeDouyinPay() { // #ifdef APP-PLUS if (active || resuming) return; const key = accountKey(); const pending = key && uni.getStorageSync(key); if (!pending) return; resuming = true; try { const state = await query(pending); if (key === accountKey() && state.state !== 'pending') resetDypay(); showState(state, key, pending); } catch (_) { /* Keep the order for a later onShow. Never infer a payment failure. */ } finally { resuming = false; } // #endif }