refactor: 移除 ENABLED_PAYMENT_TYPES,支付类型由 PAYMENT_PROVIDERS 自动推导
PAYMENT_PROVIDERS 配置提供商后,各 provider 的 supportedTypes 自动注册为可用支付类型, 无需再手动配置 ENABLED_PAYMENT_TYPES。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { getEnv } from '@/lib/config';
|
|
||||||
import { queryMethodLimits } from '@/lib/order/limits';
|
import { queryMethodLimits } from '@/lib/order/limits';
|
||||||
|
import { initPaymentProviders, paymentRegistry } from '@/lib/payment';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /api/limits
|
* GET /api/limits
|
||||||
@@ -17,8 +17,8 @@ import { queryMethodLimits } from '@/lib/order/limits';
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
const env = getEnv();
|
initPaymentProviders();
|
||||||
const types = env.ENABLED_PAYMENT_TYPES;
|
const types = paymentRegistry.getSupportedTypes();
|
||||||
|
|
||||||
const todayStart = new Date();
|
const todayStart = new Date();
|
||||||
todayStart.setUTCHours(0, 0, 0, 0);
|
todayStart.setUTCHours(0, 0, 0, 0);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { createOrder, OrderError } from '@/lib/order/service';
|
import { createOrder, OrderError } from '@/lib/order/service';
|
||||||
import { getEnv } from '@/lib/config';
|
import { getEnv } from '@/lib/config';
|
||||||
|
import { initPaymentProviders, paymentRegistry } from '@/lib/payment';
|
||||||
|
|
||||||
const createOrderSchema = z.object({
|
const createOrderSchema = z.object({
|
||||||
user_id: z.number().int().positive(),
|
user_id: z.number().int().positive(),
|
||||||
@@ -14,6 +15,7 @@ const createOrderSchema = z.object({
|
|||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const env = getEnv();
|
const env = getEnv();
|
||||||
|
initPaymentProviders();
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const parsed = createOrderSchema.safeParse(body);
|
const parsed = createOrderSchema.safeParse(body);
|
||||||
|
|
||||||
@@ -32,7 +34,7 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate payment type is enabled
|
// Validate payment type is enabled
|
||||||
if (!env.ENABLED_PAYMENT_TYPES.includes(payment_type)) {
|
if (!paymentRegistry.getSupportedTypes().includes(payment_type)) {
|
||||||
return NextResponse.json({ error: `不支持的支付方式: ${payment_type}` }, { status: 400 });
|
return NextResponse.json({ error: `不支持的支付方式: ${payment_type}` }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
|||||||
import { getUser } from '@/lib/sub2api/client';
|
import { getUser } from '@/lib/sub2api/client';
|
||||||
import { getEnv } from '@/lib/config';
|
import { getEnv } from '@/lib/config';
|
||||||
import { queryMethodLimits } from '@/lib/order/limits';
|
import { queryMethodLimits } from '@/lib/order/limits';
|
||||||
|
import { initPaymentProviders, paymentRegistry } from '@/lib/payment';
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const userId = Number(request.nextUrl.searchParams.get('user_id'));
|
const userId = Number(request.nextUrl.searchParams.get('user_id'));
|
||||||
@@ -11,7 +12,9 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const env = getEnv();
|
const env = getEnv();
|
||||||
const [user, methodLimits] = await Promise.all([getUser(userId), queryMethodLimits(env.ENABLED_PAYMENT_TYPES)]);
|
initPaymentProviders();
|
||||||
|
const enabledTypes = paymentRegistry.getSupportedTypes();
|
||||||
|
const [user, methodLimits] = await Promise.all([getUser(userId), queryMethodLimits(enabledTypes)]);
|
||||||
|
|
||||||
// 收集 sublabel 覆盖(仅包含用户实际配置的项)
|
// 收集 sublabel 覆盖(仅包含用户实际配置的项)
|
||||||
const sublabelOverrides: Record<string, string> = {};
|
const sublabelOverrides: Record<string, string> = {};
|
||||||
@@ -27,7 +30,7 @@ export async function GET(request: NextRequest) {
|
|||||||
status: user.status,
|
status: user.status,
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
enabledPaymentTypes: env.ENABLED_PAYMENT_TYPES,
|
enabledPaymentTypes: enabledTypes,
|
||||||
minAmount: env.MIN_RECHARGE_AMOUNT,
|
minAmount: env.MIN_RECHARGE_AMOUNT,
|
||||||
maxAmount: env.MAX_RECHARGE_AMOUNT,
|
maxAmount: env.MAX_RECHARGE_AMOUNT,
|
||||||
maxDailyAmount: env.MAX_DAILY_RECHARGE_AMOUNT,
|
maxDailyAmount: env.MAX_DAILY_RECHARGE_AMOUNT,
|
||||||
@@ -35,7 +38,7 @@ export async function GET(request: NextRequest) {
|
|||||||
helpImageUrl: env.PAY_HELP_IMAGE_URL ?? null,
|
helpImageUrl: env.PAY_HELP_IMAGE_URL ?? null,
|
||||||
helpText: env.PAY_HELP_TEXT ?? null,
|
helpText: env.PAY_HELP_TEXT ?? null,
|
||||||
stripePublishableKey:
|
stripePublishableKey:
|
||||||
env.ENABLED_PAYMENT_TYPES.includes('stripe') && env.STRIPE_PUBLISHABLE_KEY
|
enabledTypes.includes('stripe') && env.STRIPE_PUBLISHABLE_KEY
|
||||||
? env.STRIPE_PUBLISHABLE_KEY
|
? env.STRIPE_PUBLISHABLE_KEY
|
||||||
: null,
|
: null,
|
||||||
sublabelOverrides: Object.keys(sublabelOverrides).length > 0 ? sublabelOverrides : null,
|
sublabelOverrides: Object.keys(sublabelOverrides).length > 0 ? sublabelOverrides : null,
|
||||||
|
|||||||
@@ -47,13 +47,6 @@ const envSchema = z.object({
|
|||||||
STRIPE_PUBLISHABLE_KEY: optionalTrimmedString,
|
STRIPE_PUBLISHABLE_KEY: optionalTrimmedString,
|
||||||
STRIPE_WEBHOOK_SECRET: optionalTrimmedString,
|
STRIPE_WEBHOOK_SECRET: optionalTrimmedString,
|
||||||
|
|
||||||
// ── 启用的支付渠道(在已配置服务商支持的渠道中选择) ──
|
|
||||||
// 易支付支持: alipay, wxpay;Stripe 支持: stripe
|
|
||||||
ENABLED_PAYMENT_TYPES: z
|
|
||||||
.string()
|
|
||||||
.default('alipay,wxpay')
|
|
||||||
.transform((v) => v.split(',').map((s) => s.trim())),
|
|
||||||
|
|
||||||
ORDER_TIMEOUT_MINUTES: z.string().default('5').transform(Number).pipe(z.number().int().positive()),
|
ORDER_TIMEOUT_MINUTES: z.string().default('5').transform(Number).pipe(z.number().int().positive()),
|
||||||
MIN_RECHARGE_AMOUNT: z.string().default('1').transform(Number).pipe(z.number().positive()),
|
MIN_RECHARGE_AMOUNT: z.string().default('1').transform(Number).pipe(z.number().positive()),
|
||||||
MAX_RECHARGE_AMOUNT: z.string().default('1000').transform(Number).pipe(z.number().positive()),
|
MAX_RECHARGE_AMOUNT: z.string().default('1000').transform(Number).pipe(z.number().positive()),
|
||||||
|
|||||||
@@ -46,14 +46,5 @@ export function initPaymentProviders(): void {
|
|||||||
paymentRegistry.register(new StripeProvider());
|
paymentRegistry.register(new StripeProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 校验 ENABLED_PAYMENT_TYPES 的每个渠道都有对应 provider 已注册
|
|
||||||
const unsupported = env.ENABLED_PAYMENT_TYPES.filter((t) => !paymentRegistry.hasProvider(t as PaymentType));
|
|
||||||
if (unsupported.length > 0) {
|
|
||||||
throw new Error(
|
|
||||||
`ENABLED_PAYMENT_TYPES 含 [${unsupported.join(', ')}],但没有对应的 PAYMENT_PROVIDERS 注册。` +
|
|
||||||
`请检查 PAYMENT_PROVIDERS 配置`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user