154 lines
4.0 KiB
TypeScript
154 lines
4.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth/next'
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
|
|
import { db } from '@/lib/db'
|
|
import crypto from 'crypto'
|
|
|
|
// 产品配置
|
|
const PRODUCTS = {
|
|
standard: {
|
|
name: 'FLNXI标准版',
|
|
price: 599,
|
|
},
|
|
deployment: {
|
|
name: '远程部署服务',
|
|
price: 299,
|
|
},
|
|
custom: {
|
|
name: '定制版咨询',
|
|
price: 0, // 需要沟通报价
|
|
},
|
|
}
|
|
|
|
// 生成订单号
|
|
function generateOrderNo(): string {
|
|
const timestamp = Date.now()
|
|
const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0')
|
|
return `FLNXI${timestamp}${random}`
|
|
}
|
|
|
|
// 生成易支付签名
|
|
function generateSign(params: Record<string, any>, key: string): string {
|
|
// 移除空值和sign参数
|
|
const filtered: Record<string, any> = {}
|
|
Object.keys(params)
|
|
.sort()
|
|
.forEach((k) => {
|
|
if (params[k] !== '' && params[k] !== null && params[k] !== undefined && k !== 'sign') {
|
|
filtered[k] = params[k]
|
|
}
|
|
})
|
|
|
|
// 拼接字符串
|
|
const signStr = Object.keys(filtered)
|
|
.map((k) => `${k}=${filtered[k]}`)
|
|
.join('&')
|
|
const signStrWithKey = `${signStr}&key=${key}`
|
|
|
|
// MD5加密并转大写
|
|
return crypto.createHash('md5').update(signStrWithKey).digest('hex').toUpperCase()
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
// 验证登录状态
|
|
const session = await getServerSession(authOptions)
|
|
if (!session || !session.user?.id) {
|
|
return NextResponse.json(
|
|
{ message: '请先登录' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { productType, paymentMethod = 'alipay' } = body
|
|
|
|
// 验证产品类型
|
|
if (!productType || !PRODUCTS[productType as keyof typeof PRODUCTS]) {
|
|
return NextResponse.json(
|
|
{ message: '无效的产品类型' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const product = PRODUCTS[productType as keyof typeof PRODUCTS]
|
|
|
|
// 定制版需要特殊处理
|
|
if (productType === 'custom') {
|
|
return NextResponse.json(
|
|
{ message: '定制版请联系客服咨询报价', redirect: '#contact' },
|
|
{ status: 200 }
|
|
)
|
|
}
|
|
|
|
// 检查易支付配置
|
|
const epayPid = process.env.EPAY_PID
|
|
const epayKey = process.env.EPAY_KEY
|
|
const epayApiUrl = process.env.EPAY_API_URL || 'https://your-epay-domain.com/submit.php'
|
|
|
|
if (!epayPid || !epayKey || !epayApiUrl) {
|
|
return NextResponse.json(
|
|
{ message: '支付系统未配置,请联系管理员' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
// 生成订单号
|
|
const orderNo = generateOrderNo()
|
|
const userId = parseInt(session.user.id)
|
|
|
|
// 创建订单
|
|
const order = await db.createOrder(
|
|
orderNo,
|
|
userId,
|
|
productType,
|
|
product.name,
|
|
product.price
|
|
)
|
|
|
|
// 获取回调地址
|
|
const baseUrl = process.env.NEXTAUTH_URL || request.headers.get('origin') || 'http://localhost:23000'
|
|
const notifyUrl = `${baseUrl}/api/payment/callback`
|
|
const returnUrl = `${baseUrl}/payment/success?order_no=${orderNo}`
|
|
|
|
// 构建易支付参数
|
|
const epayParams = {
|
|
pid: epayPid,
|
|
type: paymentMethod, // alipay, wxpay, qqpay等
|
|
out_trade_no: orderNo,
|
|
notify_url: notifyUrl,
|
|
return_url: returnUrl,
|
|
name: product.name,
|
|
money: product.price.toFixed(2),
|
|
}
|
|
|
|
// 生成签名
|
|
const sign = generateSign(epayParams, epayKey)
|
|
epayParams.sign = sign
|
|
epayParams.sign_type = 'MD5'
|
|
|
|
console.log('✅ 创建支付订单:', {
|
|
orderNo,
|
|
userId,
|
|
product: product.name,
|
|
amount: product.price,
|
|
})
|
|
|
|
// 返回支付参数,前端提交表单到易支付
|
|
return NextResponse.json({
|
|
success: true,
|
|
orderNo,
|
|
orderId: order.id,
|
|
paymentUrl: epayApiUrl,
|
|
paymentParams: epayParams,
|
|
})
|
|
} catch (error) {
|
|
console.error('创建支付订单错误:', error)
|
|
return NextResponse.json(
|
|
{ message: '创建订单失败,请稍后重试' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|