Overview
Support Unicorn uses a credit-based billing system:- 1 credit = 1 AI message (configurable)
- Organizations start with 50 free credits/month
- AI responses are blocked when credits run out (no negative balance)
- Monthly subscriptions with included credits + ability to purchase additional credits
- Only AI chat responses consume credits (embeddings are bundled)
Architecture
Database Schema
- Organizations: Extended with billing fields (
stripe_customer_id,credits_balance,subscription_status) - CreditTransactions: Audit log of all credit movements (usage, purchase, refund, renewal)
- StripeSubscriptions: Monthly subscription tracking
- StripeCharges: One-time credit purchases
- PaymentMethods: Stored payment cards
- Messages: Links to credit transactions
Services
- StripeService: HTTParty-based API client for Stripe
- BillingService: Core credit management logic
- CreditTrackingService: Tracks AI message usage
Background Jobs
- TrackAiMessageUsageJob: Async credit deduction after AI messages
- LowCreditsNotificationJob: Sends Slack alerts when credits exhausted
- MonthlySubscriptionRenewalJob: Handles monthly subscription renewals
Setup Instructions
Step 1: Create Stripe Account
- Go to stripe.com and sign up
- Complete account verification
- Switch to Test Mode (toggle in top right) for development
Step 2: Get API Keys
Development Keys (Test Mode)
- In Stripe Dashboard, ensure you’re in Test mode
- Navigate to Developers → API keys
- Copy:
- Publishable key:
pk_test_...(safe to expose in frontend) - Secret key:
sk_test_...(keep secret, server-side only)
- Publishable key:
Production Keys (Live Mode)
- Switch to Live mode in Stripe Dashboard
- Navigate to Developers → API keys
- Copy the live keys
Step 3: Create Subscription Products
Create Products and Prices in Stripe for subscription plans: Starter Plan:- Name: Support Unicorn - Starter
- Price: $29.00 USD/month
- Credits: 1,500 per month
- Copy the Price ID (starts with
price_)
- Name: Support Unicorn - Professional
- Price: $99.00 USD/month
- Credits: 10,000 per month
- Copy the Price ID
Step 4: Configure Environment Variables
Development
Add to.env.development:
Production
Set via Fly.io secrets:Step 5: Get Webhook Signing Secret
Development: Using Stripe CLI
whsec_... value and add to .env.development.
Production: Stripe Dashboard
- Go to dashboard.stripe.com/webhooks
- Click ”+ Add endpoint”
- Set Endpoint URL:
https://your-app.fly.dev/stripe/webhook - Select events:
checkout.session.completedinvoice.paidinvoice.payment_failedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedcharge.refunded
- Copy the signing secret (
whsec_...)
Testing
Test Stripe Integration
Use Stripe test cards:- Success:
4242 4242 4242 4242 - Decline:
4000 0000 0000 0002
Configuration Options
| Variable | Default | Description |
|---|---|---|
STRIPE_SECRET_KEY | required | Stripe API secret key |
STRIPE_PUBLISHABLE_KEY | required | Stripe publishable key |
STRIPE_WEBHOOK_SECRET | required | Webhook signing secret |
CREDITS_PER_MESSAGE | 1 | Credits charged per AI message |
PRICE_PER_CREDIT | 0.05 | Dollar amount per credit |
FREE_CREDITS_PER_MONTH | 50 | Monthly free tier allocation |
APP_URL | http://localhost:3000 | Base URL for Stripe redirects |
Troubleshooting
Webhook signature verification failed
- Ensure
STRIPE_WEBHOOK_SECRETmatches the secret fromstripe listen - Restart
stripe listenand copy the new secret
Credits not deducting
- Check Solid Queue is running:
bin/rails solid_queue:start - Check job dashboard:
http://localhost:3000/jobs - Review logs for errors in
TrackAiMessageUsageJob
AI still works with 0 credits
- Verify
organization.can_use_ai?returnsfalse - Check
organization.subscription_statusis'active' - Ensure credit checks are in place
Next Steps
File Processing Setup
Configure automated file processing