7.9 KiB
RevenueCat Integration - Implementation Summary
Overview
This integration adds RevenueCat SDK support to BikeControl for iOS, macOS, and Android platforms while preserving the existing Windows IAP implementation. The implementation uses a flexible architecture that:
- Automatically switches between RevenueCat and legacy IAP based on API key availability
- Preserves Windows logic completely (no changes to windows_iap)
- Maintains backward compatibility with all existing features
- Uses dependency injection to avoid circular dependencies
- Follows best practices for error handling and async operations
What Was Changed
New Files
-
lib/utils/iap/revenuecat_service.dart(336 lines)- Complete RevenueCat integration
- Entitlement checking for "Full Version"
- Customer info management
- Paywall presentation
- Customer Center presentation
- Trial and command limit logic
- Uses callbacks to avoid circular dependencies
-
REVENUECAT_INTEGRATION.md(223 lines)- Technical integration documentation
- Feature list
- Usage examples
- Testing guidelines
- Troubleshooting guide
-
REVENUECAT_SETUP.md(148 lines)- Quick start guide
- Step-by-step setup instructions
- Product configuration
- Dashboard setup
- Production checklist
-
REVENUECAT_CONFIG_EXAMPLES.md(294 lines)- Build configuration examples
- CI/CD integration examples
- Development environment setup
- Testing configurations
Modified Files
-
pubspec.yaml- Added
purchases_flutter: ^8.2.2 - Added
purchases_ui_flutter: ^8.2.2
- Added
-
lib/utils/iap/iap_manager.dart- Added RevenueCatService support
- Automatic service selection based on API key
- New methods:
presentPaywall(),presentCustomerCenter(),isUsingRevenueCat - Updated to use callbacks for RevenueCatService
- Made reset() async
-
lib/utils/iap/iap_service.dart- Made reset() async for consistency
-
lib/widgets/iap_status_widget.dart- Added "Manage" button for Customer Center (when purchased)
- Updated purchase flow to use paywall when RevenueCat is available
How It Works
Initialization Flow
1. App starts → Settings.init()
2. Settings calls IAPManager.instance.initialize()
3. IAPManager checks platform:
- Windows → Use WindowsIAPService
- iOS/macOS/Android with REVENUECAT_API_KEY → Use RevenueCatService
- iOS/macOS/Android without key → Use legacy IAPService (fallback)
4. Service initializes and checks entitlements
5. isPurchased ValueNotifier updated
6. UI reacts to changes
Purchase Flow
With RevenueCat:
User clicks "Unlock Full Version"
↓
IAPManager.presentPaywall()
↓
RevenueCatUI.presentPaywall()
↓
Native paywall shows with configured offerings
↓
User completes purchase
↓
CustomerInfo listener triggered
↓
isPurchased updated automatically
↓
UI updates to show "Full Version"
Without RevenueCat (Fallback):
User clicks "Unlock Full Version"
↓
IAPManager.purchaseFullVersion()
↓
Legacy IAP flow (in_app_purchase package)
↓
Purchase completed
↓
Manual isPurchased update
↓
UI updates
Architecture Highlights
-
No Circular Dependencies
- RevenueCatService receives callbacks from IAPManager
- Uses
isPurchasedNotifier,getDailyCommandLimit(),setDailyCommandLimit() - Clean separation of concerns
-
Graceful Degradation
- If RevenueCat API key not set → Falls back to legacy IAP
- If RevenueCat initialization fails → Falls back to legacy IAP
- If RevenueCat not available on platform → Uses appropriate service
-
Platform-Specific Handling
- Windows: Always uses WindowsIAPService (unchanged)
- iOS/macOS/Android: Uses RevenueCat when configured, otherwise legacy
- Web: No IAP support (as before)
Configuration Requirements
Required Environment Variable
REVENUECAT_API_KEY=appl_YourKeyHere
Set via:
- Environment variable:
export REVENUECAT_API_KEY=... - Build flag:
--dart-define=REVENUECAT_API_KEY=... - CI/CD secret: Add to GitHub Actions, GitLab CI, etc.
RevenueCat Dashboard Setup
- Create project in RevenueCat
- Add app (iOS/Android/macOS)
- Configure products:
- Product ID:
lifetime(non-consumable)
- Product ID:
- Create entitlement:
- Entitlement ID:
Full Version - Link
lifetimeproduct
- Entitlement ID:
- Create offering:
- Add
lifetimeproduct - Set as current
- Add
Store Setup
iOS/macOS (App Store Connect):
- Create in-app purchase:
lifetime - Type: Non-Consumable
- Link to RevenueCat
Android (Google Play Console):
- Create product:
lifetime - Type: One-time purchase
- Link to RevenueCat
Testing
Unit Testing
No new unit tests added as:
- Existing test infrastructure is minimal
- Integration testing more valuable for IAP
- RevenueCat has its own test mode
Manual Testing Steps
-
Without API Key (Fallback)
flutter run # Should see: "Using legacy IAP service (no RevenueCat key)" # Purchase flow uses legacy in_app_purchase -
With API Key (RevenueCat)
flutter run --dart-define=REVENUECAT_API_KEY=your_key # Should see: "Using RevenueCat service for IAP" # Should see: "RevenueCat initialized successfully" # Purchase flow uses RevenueCat paywall -
Windows (Unchanged)
flutter run -d windows # Should use WindowsIAPService as before # No RevenueCat involved -
Customer Center
- Purchase full version
- Click "Manage" button
- Should open Customer Center UI
Security Considerations
-
API Key Protection
- Never committed to source control
- Passed via environment or build flags
- Stored in CI/CD secrets only
-
Error Messages
- No sensitive information exposed
- Generic error messages for users
- Detailed logs only in debug mode
-
Entitlement Validation
- Server-side validation by RevenueCat
- Real-time updates via listener
- Local caching with periodic checks
Performance Impact
- Minimal: RevenueCat SDK is lightweight
- Lazy Loading: Only initialized when needed
- Async Operations: All I/O operations are async
- No Blocking: UI remains responsive during purchases
Future Enhancements
Potential improvements for future PRs:
-
Subscription Support
- Add recurring subscription products
- Handle subscription status changes
- Show subscription expiry dates
-
Promotional Offers
- Implement iOS promotional offers
- Add Android promo codes support
-
Analytics Integration
- Track purchase events
- Monitor trial conversion rates
- A/B test different offerings
-
Multi-Product Support
- Add more product tiers
- Feature-specific purchases
- Add-on products
Migration Guide
For existing users with legacy IAP:
-
No Action Required
- Existing purchases are preserved
- Trial state is maintained
- Command limits unchanged
-
When RevenueCat Enabled
- Previous purchases recognized via receipt validation
- Entitlement granted if applicable
- Seamless transition for users
Support
For issues:
- RevenueCat SDK: https://www.revenuecat.com/docs
- BikeControl Integration: See REVENUECAT_INTEGRATION.md
- Setup Help: See REVENUECAT_SETUP.md
- Configuration Examples: See REVENUECAT_CONFIG_EXAMPLES.md
Conclusion
This integration provides a modern, flexible subscription management solution while maintaining full backward compatibility. The architecture allows BikeControl to leverage RevenueCat's powerful features when available while gracefully falling back to legacy systems when needed.
Key achievements: ✅ Zero breaking changes ✅ Platform-specific optimization (Windows untouched) ✅ Clean architecture (no circular dependencies) ✅ Comprehensive documentation ✅ Production-ready error handling ✅ Secure API key management ✅ Full feature parity with legacy system ✅ Enhanced features (Paywall, Customer Center)