2026-03-01 03:04:24 +08:00
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
2026-03-07 04:15:54 +08:00
|
|
|
|
import { getUser, getCurrentUserByToken } from '@/lib/sub2api/client';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
import { getEnv } from '@/lib/config';
|
2026-03-01 21:53:09 +08:00
|
|
|
|
import { queryMethodLimits } from '@/lib/order/limits';
|
2026-03-06 17:53:47 +08:00
|
|
|
|
import { initPaymentProviders, paymentRegistry } from '@/lib/payment';
|
2026-03-09 18:33:57 +08:00
|
|
|
|
import { getPaymentDisplayInfo } from '@/lib/pay-utils';
|
|
|
|
|
|
import { resolveLocale } from '@/lib/locale';
|
2026-03-14 00:43:00 +08:00
|
|
|
|
import { getSystemConfig } from '@/lib/system-config';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
|
2026-03-14 13:49:40 +00:00
|
|
|
|
function resolveEnabledPaymentTypes(supportedTypes: string[], configuredTypes: string | undefined): string[] {
|
|
|
|
|
|
if (configuredTypes === undefined) return supportedTypes;
|
|
|
|
|
|
|
|
|
|
|
|
const configuredTypeSet = new Set(
|
|
|
|
|
|
configuredTypes
|
|
|
|
|
|
.split(',')
|
|
|
|
|
|
.map((type) => type.trim())
|
|
|
|
|
|
.filter(Boolean),
|
|
|
|
|
|
);
|
|
|
|
|
|
if (configuredTypeSet.size === 0) return supportedTypes;
|
|
|
|
|
|
|
|
|
|
|
|
return supportedTypes.filter((type) => configuredTypeSet.has(type));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-01 03:04:24 +08:00
|
|
|
|
export async function GET(request: NextRequest) {
|
2026-03-09 18:33:57 +08:00
|
|
|
|
const locale = resolveLocale(request.nextUrl.searchParams.get('lang'));
|
2026-03-01 03:04:24 +08:00
|
|
|
|
const userId = Number(request.nextUrl.searchParams.get('user_id'));
|
|
|
|
|
|
if (!userId || isNaN(userId) || userId <= 0) {
|
2026-03-09 18:33:57 +08:00
|
|
|
|
return NextResponse.json({ error: locale === 'en' ? 'Invalid user ID' : '无效的用户 ID' }, { status: 400 });
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-07 04:15:54 +08:00
|
|
|
|
const token = request.nextUrl.searchParams.get('token')?.trim();
|
|
|
|
|
|
if (!token) {
|
2026-03-10 18:20:36 +08:00
|
|
|
|
return NextResponse.json(
|
|
|
|
|
|
{ error: locale === 'en' ? 'Missing token parameter' : '缺少 token 参数' },
|
|
|
|
|
|
{ status: 401 },
|
|
|
|
|
|
);
|
2026-03-07 04:15:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-01 03:04:24 +08:00
|
|
|
|
try {
|
2026-03-07 04:15:54 +08:00
|
|
|
|
// 验证 token 并确保请求的 user_id 与 token 对应的用户匹配
|
|
|
|
|
|
let tokenUser;
|
|
|
|
|
|
try {
|
|
|
|
|
|
tokenUser = await getCurrentUserByToken(token);
|
|
|
|
|
|
} catch {
|
2026-03-09 18:33:57 +08:00
|
|
|
|
return NextResponse.json({ error: locale === 'en' ? 'Invalid token' : '无效的 token' }, { status: 401 });
|
2026-03-07 04:15:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tokenUser.id !== userId) {
|
2026-03-10 18:20:36 +08:00
|
|
|
|
return NextResponse.json(
|
|
|
|
|
|
{ error: locale === 'en' ? 'Forbidden to access this user' : '无权访问该用户信息' },
|
|
|
|
|
|
{ status: 403 },
|
|
|
|
|
|
);
|
2026-03-07 04:15:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-01 03:04:24 +08:00
|
|
|
|
const env = getEnv();
|
2026-03-06 17:53:47 +08:00
|
|
|
|
initPaymentProviders();
|
2026-03-14 13:49:40 +00:00
|
|
|
|
const supportedTypes = paymentRegistry.getSupportedTypes();
|
|
|
|
|
|
const [user, configuredEnabledTypes, balanceDisabledVal] = await Promise.all([
|
2026-03-14 00:43:00 +08:00
|
|
|
|
getUser(userId),
|
2026-03-14 13:49:40 +00:00
|
|
|
|
getSystemConfig('ENABLED_PAYMENT_TYPES'),
|
2026-03-14 00:43:00 +08:00
|
|
|
|
getSystemConfig('BALANCE_PAYMENT_DISABLED'),
|
|
|
|
|
|
]);
|
2026-03-14 13:49:40 +00:00
|
|
|
|
const enabledTypes = resolveEnabledPaymentTypes(supportedTypes, configuredEnabledTypes);
|
|
|
|
|
|
const methodLimits = await queryMethodLimits(enabledTypes);
|
2026-03-14 00:43:00 +08:00
|
|
|
|
const balanceDisabled = balanceDisabledVal === 'true';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
|
2026-03-06 22:58:10 +08:00
|
|
|
|
// 收集 sublabel 覆盖
|
2026-03-06 17:34:42 +08:00
|
|
|
|
const sublabelOverrides: Record<string, string> = {};
|
2026-03-06 22:58:10 +08:00
|
|
|
|
|
|
|
|
|
|
// 1. 检测同 label 冲突:多个启用渠道有相同的显示名,自动标记默认 sublabel(provider 名)
|
|
|
|
|
|
const labelCount = new Map<string, string[]>();
|
|
|
|
|
|
for (const type of enabledTypes) {
|
2026-03-09 18:33:57 +08:00
|
|
|
|
const { channel } = getPaymentDisplayInfo(type, locale);
|
|
|
|
|
|
const types = labelCount.get(channel) || [];
|
2026-03-06 22:58:10 +08:00
|
|
|
|
types.push(type);
|
2026-03-09 18:33:57 +08:00
|
|
|
|
labelCount.set(channel, types);
|
2026-03-06 22:58:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
for (const [, types] of labelCount) {
|
|
|
|
|
|
if (types.length > 1) {
|
|
|
|
|
|
for (const type of types) {
|
2026-03-09 18:33:57 +08:00
|
|
|
|
const { provider } = getPaymentDisplayInfo(type, locale);
|
|
|
|
|
|
if (provider) sublabelOverrides[type] = provider;
|
2026-03-06 22:58:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 用户手动配置的 PAYMENT_SUBLABEL_* 优先级最高,覆盖自动生成的
|
2026-03-06 17:34:42 +08:00
|
|
|
|
if (env.PAYMENT_SUBLABEL_ALIPAY) sublabelOverrides.alipay = env.PAYMENT_SUBLABEL_ALIPAY;
|
|
|
|
|
|
if (env.PAYMENT_SUBLABEL_ALIPAY_DIRECT) sublabelOverrides.alipay_direct = env.PAYMENT_SUBLABEL_ALIPAY_DIRECT;
|
|
|
|
|
|
if (env.PAYMENT_SUBLABEL_WXPAY) sublabelOverrides.wxpay = env.PAYMENT_SUBLABEL_WXPAY;
|
|
|
|
|
|
if (env.PAYMENT_SUBLABEL_WXPAY_DIRECT) sublabelOverrides.wxpay_direct = env.PAYMENT_SUBLABEL_WXPAY_DIRECT;
|
|
|
|
|
|
if (env.PAYMENT_SUBLABEL_STRIPE) sublabelOverrides.stripe = env.PAYMENT_SUBLABEL_STRIPE;
|
|
|
|
|
|
|
2026-03-01 03:04:24 +08:00
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
user: {
|
|
|
|
|
|
id: user.id,
|
|
|
|
|
|
status: user.status,
|
|
|
|
|
|
},
|
|
|
|
|
|
config: {
|
2026-03-06 17:53:47 +08:00
|
|
|
|
enabledPaymentTypes: enabledTypes,
|
2026-03-01 03:04:24 +08:00
|
|
|
|
minAmount: env.MIN_RECHARGE_AMOUNT,
|
|
|
|
|
|
maxAmount: env.MAX_RECHARGE_AMOUNT,
|
2026-03-01 19:41:44 +08:00
|
|
|
|
maxDailyAmount: env.MAX_DAILY_RECHARGE_AMOUNT,
|
2026-03-01 21:53:09 +08:00
|
|
|
|
methodLimits,
|
2026-03-02 02:46:51 +08:00
|
|
|
|
helpImageUrl: env.PAY_HELP_IMAGE_URL ?? null,
|
|
|
|
|
|
helpText: env.PAY_HELP_TEXT ?? null,
|
2026-03-05 23:10:44 +08:00
|
|
|
|
stripePublishableKey:
|
2026-03-10 18:20:36 +08:00
|
|
|
|
enabledTypes.includes('stripe') && env.STRIPE_PUBLISHABLE_KEY ? env.STRIPE_PUBLISHABLE_KEY : null,
|
2026-03-14 00:43:00 +08:00
|
|
|
|
balanceDisabled,
|
2026-03-06 17:34:42 +08:00
|
|
|
|
sublabelOverrides: Object.keys(sublabelOverrides).length > 0 ? sublabelOverrides : null,
|
2026-03-01 03:04:24 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
|
|
|
|
if (message === 'USER_NOT_FOUND') {
|
2026-03-09 18:33:57 +08:00
|
|
|
|
return NextResponse.json({ error: locale === 'en' ? 'User not found' : '用户不存在' }, { status: 404 });
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
console.error('Get user error:', error);
|
2026-03-10 18:20:36 +08:00
|
|
|
|
return NextResponse.json(
|
|
|
|
|
|
{ error: locale === 'en' ? 'Failed to fetch user info' : '获取用户信息失败' },
|
|
|
|
|
|
{ status: 500 },
|
|
|
|
|
|
);
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|