- 新增 src/lib/constants.ts,集中管理 ORDER_STATUS / PAYMENT_TYPE / PAYMENT_PREFIX 等常量
- 后端 service/status/timeout/limits 全量替换魔法字符串为 ORDER_STATUS.*
- PaymentTypeMeta 新增 provider 字段,分离 sublabel(选择器展示)与 provider(提供商名称)
- getPaymentDisplayInfo() 返回 { channel, provider } 用于用户端/管理端展示
- 支持通过 PAYMENT_SUBLABEL_* 环境变量覆盖默认 sublabel
- PaymentQRCode: H5 支付自动跳转(含易支付微信 weixin:// scheme 兜底)
- 订单列表/详情页:显示可读的渠道名+提供商,不再暴露内部标识符
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { prisma } from '@/lib/db';
|
|
import { ORDER_STATUS } from '@/lib/constants';
|
|
import { cancelOrderCore } from './service';
|
|
|
|
const INTERVAL_MS = 30_000; // 30 seconds
|
|
let timer: ReturnType<typeof setInterval> | null = null;
|
|
|
|
export async function expireOrders(): Promise<number> {
|
|
const orders = await prisma.order.findMany({
|
|
where: {
|
|
status: ORDER_STATUS.PENDING,
|
|
expiresAt: { lt: new Date() },
|
|
},
|
|
select: {
|
|
id: true,
|
|
paymentTradeNo: true,
|
|
paymentType: true,
|
|
},
|
|
});
|
|
|
|
if (orders.length === 0) return 0;
|
|
|
|
let expiredCount = 0;
|
|
|
|
for (const order of orders) {
|
|
try {
|
|
const outcome = await cancelOrderCore({
|
|
orderId: order.id,
|
|
paymentTradeNo: order.paymentTradeNo,
|
|
paymentType: order.paymentType,
|
|
finalStatus: ORDER_STATUS.EXPIRED,
|
|
operator: 'timeout',
|
|
auditDetail: 'Order expired',
|
|
});
|
|
|
|
if (outcome === 'cancelled') expiredCount++;
|
|
} catch (err) {
|
|
console.error(`Error expiring order ${order.id}:`, err);
|
|
}
|
|
}
|
|
|
|
if (expiredCount > 0) {
|
|
console.log(`Expired ${expiredCount} orders`);
|
|
}
|
|
|
|
return expiredCount;
|
|
}
|
|
|
|
export function startTimeoutScheduler(): void {
|
|
if (timer) return;
|
|
|
|
// Run immediately on startup
|
|
expireOrders().catch(console.error);
|
|
|
|
// Then run every 30 seconds
|
|
timer = setInterval(() => {
|
|
expireOrders().catch(console.error);
|
|
}, INTERVAL_MS);
|
|
|
|
console.log('Order timeout scheduler started');
|
|
}
|
|
|
|
export function stopTimeoutScheduler(): void {
|
|
if (timer) {
|
|
clearInterval(timer);
|
|
timer = null;
|
|
console.log('Order timeout scheduler stopped');
|
|
}
|
|
}
|