Overview
Stripe handles payment processing in AI Studio for:- Project purchases (image editing projects)
- Subscription management (future feature)
- Invoicing and receipts
Configuration
Environment Variables
Add the following to your.env.local file:
Setup Steps
Create Stripe Account
- Sign up at stripe.com
- Complete account verification
- Activate your account for live payments
Get API Keys
- Go to API Keys
- For testing: Copy the Test mode secret key (starts with
sk_test_) - For production: Copy the Live mode secret key (starts with
sk_live_) - Add to
.env.localasSTRIPE_SECRET_KEY
Create Product and Price
Create a product for project purchases:Option 1: Via Stripe DashboardAdd the Price ID to your
- Go to Products
- Click Add Product
- Name: “AI Studio Project”
- Description: “AI-powered photo editing project”
- Set price: $99.00 USD (one-time payment)
- Save and copy the Price ID (starts with
price_)
.env.local:Configure Webhooks (Optional)
For production, set up webhooks to handle payment events:
- Go to Webhooks
- Click Add endpoint
- URL:
https://yourdomain.com/api/webhooks/stripe - Select events:
checkout.session.completedpayment_intent.succeededpayment_intent.payment_failed
- Copy the webhook signing secret
- Add to
.env.localasSTRIPE_WEBHOOK_SECRET
Stripe Client
The Stripe client is configured inlib/stripe.ts:
lib/stripe.ts
Creating Checkout Sessions
Basic Checkout Session
With Customer Information
API Route Example
Create a checkout session from an API route:app/api/create-checkout/route.ts
Webhook Handling
Handle Stripe webhook events:app/api/webhooks/stripe/route.ts
Pricing Configuration
The default pricing is defined inSTRIPE_CONFIG:
Multiple Currencies
To support multiple currencies, create separate Price IDs:Testing
Test Cards
Use Stripe’s test cards in test mode:- Success:
4242 4242 4242 4242 - Declined:
4000 0000 0000 0002 - 3D Secure:
4000 0027 6000 3184
Test Mode vs Live Mode
Stripe has separate API keys for testing and production:- Test mode: Use
sk_test_keys for development - Live mode: Use
sk_live_keys for production
Error Handling
Handle Stripe errors gracefully:Security Best Practices
- Never expose secret keys - Only use in server-side code
- Validate webhook signatures - Always verify webhook authenticity
- Use HTTPS - Required for live mode
- Store minimal data - Don’t store card details (Stripe handles this)
- Use metadata - Store IDs for correlating payments with your database