Files
biblical-guide.com/app/api/stripe/checkout/route.ts
Andrei a01377b21a feat: implement AI chat with vector search and random loading messages
Major Features:
-  AI chat with Azure OpenAI GPT-4o integration
-  Vector search across Bible versions (ASV English, RVA 1909 Spanish)
-  Multi-language support with automatic English fallback
-  Bible version citations in responses [ASV] [RVA 1909]
-  Random Bible-themed loading messages (5 variants)
-  Safe build script with memory guardrails
-  8GB swap memory for build safety
-  Stripe donation integration (multiple payment methods)

AI Chat Improvements:
- Implement vector search with 1536-dim embeddings (Azure text-embedding-ada-002)
- Search all Bible versions in user's language, fallback to English
- Cite Bible versions properly in AI responses
- Add 5 random loading messages: "Searching the Scriptures...", etc.
- Fix Ollama conflict (disabled to use Azure OpenAI exclusively)
- Optimize hybrid search queries for actual table schema

Build & Infrastructure:
- Create safe-build.sh script with memory monitoring (prevents server crashes)
- Add 8GB swap memory for emergency relief
- Document build process in BUILD_GUIDE.md
- Set Node.js memory limits (4GB max during builds)

Database:
- Clean up 115 old vector tables with wrong dimensions
- Keep only 2 tables with correct 1536-dim embeddings
- Add Stripe schema for donations and subscriptions

Documentation:
- AI_CHAT_FINAL_STATUS.md - Complete implementation status
- AI_CHAT_IMPLEMENTATION_COMPLETE.md - Technical details
- BUILD_GUIDE.md - Safe building guide with guardrails
- CHAT_LOADING_MESSAGES.md - Loading messages implementation
- STRIPE_IMPLEMENTATION_COMPLETE.md - Stripe integration docs
- STRIPE_SETUP_GUIDE.md - Stripe configuration guide

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-12 19:37:24 +00:00

100 lines
3.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { stripe } from '@/lib/stripe-server'
import { dollarsToCents } from '@/lib/stripe'
import { prisma } from '@/lib/db'
export async function POST(req: NextRequest) {
try {
const body = await req.json()
const { amount, email, name, message, isAnonymous, isRecurring, recurringInterval, locale } = body
// Validate required fields
if (!amount || !email) {
return NextResponse.json(
{ error: 'Amount and email are required' },
{ status: 400 }
)
}
// Convert amount to cents
const amountInCents = dollarsToCents(parseFloat(amount))
// Validate amount (minimum $1)
if (amountInCents < 100) {
return NextResponse.json(
{ error: 'Minimum donation amount is $1' },
{ status: 400 }
)
}
// Get the base URL for redirects
const baseUrl = process.env.NEXTAUTH_URL || 'http://localhost:3010'
const userLocale = locale || 'en'
// Create checkout session parameters
const sessionParams: any = {
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'Donation to Biblical Guide',
description: 'Support Biblical Guide - Every Scripture. Every Language. Forever Free.',
images: [`${baseUrl}/icon.png`],
},
unit_amount: amountInCents,
},
quantity: 1,
},
],
mode: isRecurring ? 'subscription' : 'payment',
success_url: `${baseUrl}/${userLocale}/donate/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${baseUrl}/${userLocale}/donate?canceled=true`,
customer_email: email,
metadata: {
donorName: name || 'Anonymous',
donorMessage: message || '',
isAnonymous: isAnonymous ? 'true' : 'false',
},
}
// Add recurring interval if applicable
if (isRecurring && recurringInterval) {
sessionParams.line_items[0].price_data.recurring = {
interval: recurringInterval,
}
}
// Create Stripe checkout session
const session = await stripe.checkout.sessions.create(sessionParams)
// Create donation record in database with PENDING status
await prisma.donation.create({
data: {
stripeSessionId: session.id,
email,
name: name || null,
amount: amountInCents,
currency: 'usd',
status: 'PENDING',
message: message || null,
isAnonymous: isAnonymous || false,
isRecurring: isRecurring || false,
recurringInterval: recurringInterval || null,
metadata: {
sessionUrl: session.url,
},
},
})
return NextResponse.json({ sessionId: session.id, url: session.url })
} catch (error) {
console.error('Error creating checkout session:', error)
return NextResponse.json(
{ error: 'Failed to create checkout session' },
{ status: 500 }
)
}
}