109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { authMiddleware } from '@/lib/auth-middleware'
|
|
import connectDB from '@/lib/mongodb'
|
|
import { User as UserModel } from '@/models/user'
|
|
import BillingService from '@/lib/billing-service'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// Check authentication
|
|
const user = await authMiddleware(request)
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
// Get billing ID from query parameters
|
|
const { searchParams } = new URL(request.url)
|
|
const billingId = searchParams.get('billing_id')
|
|
const amount = parseFloat(searchParams.get('amount') || '0')
|
|
|
|
if (!billingId) {
|
|
return NextResponse.json({ error: 'Billing ID is required' }, { status: 400 })
|
|
}
|
|
|
|
// Connect to MongoDB
|
|
await connectDB()
|
|
|
|
// Get user data for billing
|
|
const userData = await UserModel.findOne({ email: user.email })
|
|
if (!userData) {
|
|
return NextResponse.json({ error: 'User not found' }, { status: 404 })
|
|
}
|
|
|
|
// Mock hosting configuration (in production, this would come from database)
|
|
const hostingConfig = {
|
|
billing_id: billingId,
|
|
siliconId: user.id,
|
|
domain: `demo-${billingId}.siliconpin.com`,
|
|
cp_url: `https://cp-${billingId}.siliconpin.com:2083`,
|
|
panel: 'cPanel',
|
|
user_id: `user_${billingId}`,
|
|
password: `secure_password_${Date.now()}`,
|
|
server_ip: '192.168.1.100',
|
|
nameservers: ['ns1.siliconpin.com', 'ns2.siliconpin.com'],
|
|
ftp_settings: {
|
|
host: `ftp.demo-${billingId}.siliconpin.com`,
|
|
username: `user_${billingId}`,
|
|
port: 21,
|
|
},
|
|
database_settings: {
|
|
host: 'localhost',
|
|
prefix: `db_${billingId}_`,
|
|
},
|
|
ssl_certificate: {
|
|
enabled: true,
|
|
type: "Let's Encrypt",
|
|
auto_renew: true,
|
|
},
|
|
}
|
|
|
|
const configJson = JSON.stringify(hostingConfig, null, 2)
|
|
|
|
// Process billing for hosting configuration download
|
|
try {
|
|
await BillingService.processServiceDeployment({
|
|
user: {
|
|
id: user.id,
|
|
email: user.email,
|
|
siliconId: userData.siliconId,
|
|
},
|
|
service: {
|
|
name: `Hosting Configuration - ${hostingConfig.domain}`,
|
|
type: 'hosting',
|
|
id: billingId,
|
|
config: hostingConfig,
|
|
},
|
|
amount: amount,
|
|
currency: 'INR',
|
|
cycle: 'onetime',
|
|
deploymentSuccess: true,
|
|
deploymentResponse: { configDownloaded: true },
|
|
metadata: {
|
|
userAgent: request.headers.get('user-agent'),
|
|
ip: request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip'),
|
|
downloadType: 'hosting_config',
|
|
domain: hostingConfig.domain,
|
|
},
|
|
})
|
|
|
|
console.log('Hosting config billing processed for:', billingId)
|
|
} catch (billingError) {
|
|
console.error('Billing processing failed:', billingError)
|
|
// Continue with download even if billing fails
|
|
}
|
|
|
|
// Return the config as a downloadable JSON file
|
|
return new NextResponse(configJson, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Content-Disposition': `attachment; filename="sp_credential_${billingId}.json"`,
|
|
'Cache-Control': 'no-cache',
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('Download error:', error)
|
|
return NextResponse.json({ error: 'Download failed' }, { status: 500 })
|
|
}
|
|
}
|