2026-03-01 03:04:24 +08:00
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
|
import { z } from 'zod';
|
2026-03-07 04:15:54 +08:00
|
|
|
|
import { createOrder } from '@/lib/order/service';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
import { getEnv } from '@/lib/config';
|
2026-03-07 04:15:54 +08:00
|
|
|
|
import { paymentRegistry } from '@/lib/payment';
|
2026-03-06 23:05:12 +08:00
|
|
|
|
import { getCurrentUserByToken } from '@/lib/sub2api/client';
|
2026-03-07 04:15:54 +08:00
|
|
|
|
import { handleApiError } from '@/lib/utils/api';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
|
|
|
|
|
|
const createOrderSchema = z.object({
|
2026-03-06 23:05:12 +08:00
|
|
|
|
token: z.string().min(1),
|
2026-03-13 23:40:23 +08:00
|
|
|
|
amount: z.number().positive().max(99999999.99),
|
2026-03-06 15:33:22 +08:00
|
|
|
|
payment_type: z.string().min(1),
|
2026-03-03 01:56:22 +08:00
|
|
|
|
src_host: z.string().max(253).optional(),
|
|
|
|
|
|
src_url: z.string().max(2048).optional(),
|
2026-03-06 18:04:11 +08:00
|
|
|
|
is_mobile: z.boolean().optional(),
|
2026-03-13 19:06:25 +08:00
|
|
|
|
order_type: z.enum(['balance', 'subscription']).optional(),
|
|
|
|
|
|
plan_id: z.string().optional(),
|
2026-03-01 03:04:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const env = getEnv();
|
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
|
const parsed = createOrderSchema.safeParse(body);
|
|
|
|
|
|
|
|
|
|
|
|
if (!parsed.success) {
|
2026-03-01 17:58:08 +08:00
|
|
|
|
return NextResponse.json({ error: '参数错误', details: parsed.error.flatten().fieldErrors }, { status: 400 });
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-13 19:06:25 +08:00
|
|
|
|
const { token, amount, payment_type, src_host, src_url, is_mobile, order_type, plan_id } = parsed.data;
|
2026-03-06 23:05:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 通过 token 解析用户身份
|
|
|
|
|
|
let userId: number;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const user = await getCurrentUserByToken(token);
|
|
|
|
|
|
userId = user.id;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return NextResponse.json({ error: '无效的 token,请重新登录', code: 'INVALID_TOKEN' }, { status: 401 });
|
|
|
|
|
|
}
|
2026-03-01 03:04:24 +08:00
|
|
|
|
|
2026-03-13 19:06:25 +08:00
|
|
|
|
// 订阅订单跳过金额范围校验(价格由服务端套餐决定)
|
|
|
|
|
|
if (order_type !== 'subscription') {
|
|
|
|
|
|
if (amount < env.MIN_RECHARGE_AMOUNT || amount > env.MAX_RECHARGE_AMOUNT) {
|
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
|
{ error: `充值金额需在 ${env.MIN_RECHARGE_AMOUNT} - ${env.MAX_RECHARGE_AMOUNT} 之间` },
|
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Validate payment type is enabled
|
2026-03-06 17:53:47 +08:00
|
|
|
|
if (!paymentRegistry.getSupportedTypes().includes(payment_type)) {
|
2026-03-01 17:58:08 +08:00
|
|
|
|
return NextResponse.json({ error: `不支持的支付方式: ${payment_type}` }, { status: 400 });
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-01 17:58:08 +08:00
|
|
|
|
const clientIp =
|
|
|
|
|
|
request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() || request.headers.get('x-real-ip') || '127.0.0.1';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
|
|
|
|
|
|
const result = await createOrder({
|
2026-03-06 23:05:12 +08:00
|
|
|
|
userId,
|
2026-03-01 03:04:24 +08:00
|
|
|
|
amount,
|
|
|
|
|
|
paymentType: payment_type,
|
|
|
|
|
|
clientIp,
|
2026-03-06 14:04:51 +08:00
|
|
|
|
isMobile: is_mobile,
|
2026-03-02 20:40:16 +08:00
|
|
|
|
srcHost: src_host,
|
|
|
|
|
|
srcUrl: src_url,
|
2026-03-13 19:06:25 +08:00
|
|
|
|
orderType: order_type,
|
|
|
|
|
|
planId: plan_id,
|
2026-03-01 03:04:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-01 19:25:14 +08:00
|
|
|
|
// 不向客户端暴露 userName / userBalance 等隐私字段
|
|
|
|
|
|
const { userName: _u, userBalance: _b, ...safeResult } = result;
|
|
|
|
|
|
return NextResponse.json(safeResult);
|
2026-03-01 03:04:24 +08:00
|
|
|
|
} catch (error) {
|
2026-03-07 04:15:54 +08:00
|
|
|
|
return handleApiError(error, '创建订单失败,请稍后重试');
|
2026-03-01 03:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|