Complete subscription payment system for AISBF with support for fiat (Stripe, PayPal) and cryptocurrency (BTC, ETH, USDT, USDC) payments. The system handles monthly/yearly subscriptions, tier upgrades/downgrades, payment retries, crypto wallet management, and quota enforcement.
## Requirements Summary
### Payment Types
**Fiat Payments (Stripe, PayPal):**
- User authenticates with payment gateway when adding payment method
- Authorization hold (not charge) for card verification, immediately released
- Automatic charging on subscription creation and renewals
- Managed by external gateway (Stripe/PayPal handles subscription logic)
**Crypto Payments (BTC, ETH, USDT, USDC):**
- HD wallet derivation (BIP32/BIP44) - one master seed per crypto type
- Unique address generated for each user per crypto type
- User manually sends crypto to their address
- System monitors blockchain for incoming transactions
- Converts crypto to fiat equivalent using multi-exchange pricing
- Credits user's wallet balance in fiat
- Subscription charges deduct from wallet balance
- Automatic consolidation to admin address when threshold exceeded
### Subscription Lifecycle
**Tier Changes:**
-**Upgrade:** Prorated charge (new tier price - unused portion of old tier)
-**Downgrade:** No refund, takes effect at period end
-**Cancellation:** No refund, access until period end
**Billing Cycle:**
- Immediate start (Option A) - billing cycle starts on upgrade date
- Monthly: 30 days, Yearly: 365 days
**Payment Failures:**
-**Retry Logic (Smart Retry - Option C):**
- Crypto: Retry immediately when wallet topped up
- Fiat: Retry daily (fixed schedule)
-**Max 3 attempts** over 3 days
- After 3 failures: Downgrade to free tier
- Email notifications at each stage
### Quota Enforcement
**Tier Limits:**
- Max providers, rotations, autoselections
- Max models per rotation/autoselection
- Max requests per day/month
**Enforcement Strategy (Creation Order - Option A):**
- Use oldest N configs when quota exceeded
- Never delete user configs
- Filter at runtime based on current tier
- Configs beyond quota are ignored but preserved
### Technical Requirements
**Architecture:**
- Fully async/non-blocking operations
- All external API calls use `asyncio` and `httpx`
- Background tasks with distributed locking
- Database-backed state for horizontal scaling
- Multiple instances can run concurrently
**Crypto Monitoring:**
-**API Polling (Option B):** Query blockchain APIs every 60 seconds
-**Webhooks (Option C):** Real-time notifications from BlockCypher/Alchemy
-**Admin Configurable:** Choose between API or webhook mode
**Price Sources:**
-**Multi-Exchange Averaging (Option B):** Query 2-3 exchanges, use average