68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { createServerSupabaseClient } from '@/lib/supabase-server'
|
|
import { addCredit } from '@/lib/services/credit'
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const { amount } = await request.json()
|
|
|
|
// 验证金额
|
|
if (!amount || amount <= 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid amount. Amount must be greater than 0.' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (amount > 1000) {
|
|
return NextResponse.json(
|
|
{ error: 'Amount too large. Maximum top-up amount is $1000.' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const supabase = await createServerSupabaseClient()
|
|
|
|
// 获取当前用户
|
|
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
|
|
|
if (authError || !user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
// 检查是否是开发环境
|
|
const isDevelopment = process.env.NODE_ENV === 'development'
|
|
|
|
if (isDevelopment) {
|
|
// 开发环境:模拟充值成功
|
|
console.log(`🧪 Development mode: Simulating top-up of $${amount} for user ${user.id}`)
|
|
|
|
const creditTransaction = await addCredit(
|
|
user.id,
|
|
amount,
|
|
'user_purchase',
|
|
`Development top-up: $${amount}`,
|
|
)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `Successfully topped up $${amount}`,
|
|
transaction: creditTransaction
|
|
})
|
|
} else {
|
|
// 生产环境:集成真实的支付网关
|
|
// 这里可以集成 Stripe, PayPal 等支付服务
|
|
return NextResponse.json(
|
|
{ error: 'Payment integration not implemented yet. Please contact support.' },
|
|
{ status: 501 }
|
|
)
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Top-up failed:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to process top-up' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |