ahmedessam / sub-sphere
A scalable, modular, and cleanly-structured Laravel package to manage subscription plans, pricing, features, and usage
Requires
- php: ^8.1
- illuminate/database: ^10.0|^11.0
- illuminate/events: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- nesbot/carbon: ^2.0|^3.0
- spatie/laravel-translatable: ^6.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
README
๐น About the Package
SubSphere is a powerful, headless, and backend-only Laravel package designed to manage subscription plans, pricing tiers, feature usage, and billing logic. Built with modularity and scalability in mind, it provides a complete subscription management solution without any built-in UI, giving you full control over the presentation layer.
๐ง Headless & Backend-Only
SubSphere focuses purely on business logic, providing a robust API that you can integrate with any frontend framework, admin panel, or custom UI.
๐งฉ Optional Filament v3 Integration
Seamlessly integrate with Filament v3 for a beautiful admin interface to manage plans, subscriptions, and analytics.
๐ Built-in Spatie Laravel Translatable Support
Full multi-language support for plan names, descriptions, and all user-facing content using Spatie's Laravel Translatable package.
โ Requirements
- PHP: ^8.1
- Laravel: ^10.0 | ^11.0
- Dependencies:
spatie/laravel-translatable
: ^6.0illuminate/support
nesbot/carbon
๐ก Features List
๐ Subscription Management
- โ Multi-tiered Plans & Pricing - Create complex pricing structures with multiple tiers
- ๐งช Trial Subscriptions - Flexible trial periods with automatic conversion
- ๐ Subscription Lifecycle - Complete handling of start, cancel, resume, expire, and renewal
- โฑ Grace Period Support - Configurable grace periods for expired subscriptions
- ๐ฐ Multi-currency Support - Handle multiple currencies with configurable defaults
๐ Feature Usage & Limits
- ๐ Usage Tracking - Track feature consumption with configurable limits
- โฐ Reset Logic - Automatic usage resets (daily/monthly/yearly)
- ๐ Real-time Monitoring - Monitor feature usage in real-time
- ๐ซ Limit Enforcement - Automatic enforcement of feature limits
๐ Automation & Events
- ๐ Laravel Scheduler Integration - Automated background tasks
- โก Event-driven Architecture - Comprehensive event system
- ๐ง Email Notifications - Built-in notification system
- ๐ Artisan Commands - Management commands for automation
๐งฉ Developer Experience
- ๐ฏ Service-based Design - Clean, modular architecture
- ๐งช Complete Test Coverage - Comprehensive test suite
- ๐ Rich Documentation - Detailed documentation and examples
- ๐ค Multi-language Ready - Full internationalization support
๐ฅ Installation
Install the package via Composer:
composer require ahmedessam/sub-sphere
โ๏ธ Publishing Config & Migrations
Publish Configuration
php artisan vendor:publish --tag="sub-sphere-config"
Publish and Run Migrations
php artisan vendor:publish --tag="sub-sphere-migrations"
php artisan migrate
Publish Language Files (Optional)
php artisan vendor:publish --tag="sub-sphere-lang"
Publish All Assets
php artisan vendor:publish --provider="AhmedEssam\SubSphere\Providers\SubSphereServiceProvider"
๐ Getting Started
1. Create Your First Plan
use AhmedEssam\SubSphere\Models\Plan; use AhmedEssam\SubSphere\Models\PlanPricing; // Create a plan $plan = Plan::create([ 'name' => ['en' => 'Professional Plan', 'ar' => 'ุงูุฎุทุฉ ุงูุงุญุชุฑุงููุฉ'], 'slug' => 'professional', 'description' => ['en' => 'Perfect for growing businesses'], 'is_active' => true, ]); // Add pricing $pricing = $plan->pricings()->create([ 'price' => 29.99, 'currency' => 'USD', 'duration_in_days' => 30, 'is_recurring' => true, ]); // Add features $plan->features()->create([ 'name' => ['en' => 'API Calls'], 'key' => 'api_calls', 'limit_value' => 10000, 'limit_type' => 'limited', 'reset_period' => 'monthly', ]);
2. Subscribe a User to a Plan
use AhmedEssam\SubSphere\Services\SubscriptionService; $subscriptionService = app(SubscriptionService::class); // Create subscription $subscription = $subscriptionService->createSubscription( subscriber: $user, planId: $plan->id, pricingId: $pricing->id ); // Or start a trial $trialSubscription = $subscriptionService->startTrial( subscriber: $user, planId: $plan->id, pricingId: $pricing->id, trialDays: 14 );
3. Use Features & Track Usage
// Check if user can use a feature if ($user->subscription->canConsumeFeature('api_calls', 100)) { // User can make 100 API calls $user->subscription->consumeFeature('api_calls', 100); // Your API logic here... } // Check remaining usage $remaining = $user->subscription->getRemainingUsage('api_calls'); echo "API calls remaining: {$remaining}"; // Check if feature is unlimited if ($user->subscription->hasUnlimitedUsage('api_calls')) { echo "Unlimited API calls available!"; }
๐ Running Commands
SubSphere includes several Artisan commands for automation:
Reset Feature Usage
# Reset all usage counters based on their reset periods php artisan sub-sphere:reset-usage # Dry run to see what would be reset php artisan sub-sphere:reset-usage --dry-run # Reset specific period php artisan sub-sphere:reset-usage --period=monthly # Limit number of records processed php artisan sub-sphere:reset-usage --limit=1000
Expire Subscriptions
# Expire subscriptions that have passed their grace period php artisan sub-sphere:expire-subscriptions # Dry run to see what would be expired php artisan sub-sphere:expire-subscriptions --dry-run # Limit number of subscriptions processed php artisan sub-sphere:expire-subscriptions --limit=100
Renew Subscriptions
# Renew eligible subscriptions php artisan sub-sphere:renew-subscriptions # Dry run mode php artisan sub-sphere:renew-subscriptions --dry-run
๐ Available Events
SubSphere dispatches several events that you can listen to:
// Subscription Events AhmedEssam\SubSphere\Events\SubscriptionCreated::class AhmedEssam\SubSphere\Events\SubscriptionStarted::class AhmedEssam\SubSphere\Events\SubscriptionCanceled::class AhmedEssam\SubSphere\Events\SubscriptionExpired::class AhmedEssam\SubSphere\Events\SubscriptionRenewed::class AhmedEssam\SubSphere\Events\SubscriptionChanged::class // Trial Events AhmedEssam\SubSphere\Events\TrialStarted::class // Feature Events AhmedEssam\SubSphere\Events\FeatureUsed::class AhmedEssam\SubSphere\Events\FeatureUsageReset::class // Renewal Events AhmedEssam\SubSphere\Events\SubscriptionRenewalFailed::class
Example Event Listener
use AhmedEssam\SubSphere\Events\SubscriptionCreated; class SendWelcomeEmail { public function handle(SubscriptionCreated $event) { // Send welcome email Mail::to($event->subscriber->email) ->send(new WelcomeEmail($event->subscription)); } }
๐ Feature Usage & Limits
Feature Types
- Limited: Features with specific usage limits (e.g., 1000 API calls)
- Unlimited: Features with no usage restrictions
- Boolean: Simple on/off features
Reset Periods
- Daily: Usage resets every day at midnight
- Monthly: Usage resets on the first day of each month
- Yearly: Usage resets on January 1st
- Never: Usage never resets automatically
Usage Tracking Example
// Define a feature $plan->features()->create([ 'name' => ['en' => 'Storage Space'], 'key' => 'storage_gb', 'limit_value' => 100, // 100 GB 'limit_type' => 'limited', 'reset_period' => 'monthly', ]); // Track usage $subscription = $user->subscription; // Use 5 GB of storage $subscription->consumeFeature('storage_gb', 5); // Check usage $used = $subscription->getUsedAmount('storage_gb'); // 5 $remaining = $subscription->getRemainingUsage('storage_gb'); // 95 $percentage = $subscription->getUsagePercentage('storage_gb'); // 5% // Check if can use more if ($subscription->canConsumeFeature('storage_gb', 50)) { // User can use 50 more GB }
๐ Multi-language Support (Spatie Translatable)
SubSphere fully supports multiple languages using Spatie's Laravel Translatable:
Configure Supported Locales
// config/sub-sphere.php return [ 'locales' => ['en', 'ar', 'es', 'fr'], 'fallback_locale' => 'en', ];
Create Multilingual Plans
$plan = Plan::create([ 'name' => [ 'en' => 'Professional Plan', 'ar' => 'ุงูุฎุทุฉ ุงูุงุญุชุฑุงููุฉ', 'es' => 'Plan Profesional', 'fr' => 'Plan Professionnel', ], 'description' => [ 'en' => 'Perfect for growing businesses', 'ar' => 'ู ุซุงููุฉ ููุดุฑูุงุช ุงููุงู ูุฉ', 'es' => 'Perfecto para empresas en crecimiento', 'fr' => 'Parfait pour les entreprises en croissance', ], 'slug' => 'professional', ]); // Get localized name echo $plan->getLocalizedName('ar'); // ุงูุฎุทุฉ ุงูุงุญุชุฑุงููุฉ echo $plan->name; // Uses current app locale
Email Translations
All email templates support RTL/LTR layouts and use Laravel's translation system:
// Email subject will be translated based on user's locale $subject = __('sub-sphere::subscription.subjects.subscription_created', [ 'plan_name' => $plan->getLocalizedName() ]);
๐งฉ Optional Filament Integration
SubSphere provides optional integration with Filament v3 for a beautiful admin interface.
Installation
composer require filament/filament
php artisan vendor:publish --tag="sub-sphere-filament"
Features
- ๐ Dashboard Widgets - Subscription analytics and metrics
- ๐ Resource Management - Full CRUD for plans, subscriptions, and features
- ๐ Usage Analytics - Visual charts and reports
- ๐ Real-time Notifications - Live subscription updates
- ๐จ Customizable Interface - Tailored for subscription management
Configuration
The package configuration is located at config/sub-sphere.php
:
return [ // Grace period in days for expired subscriptions 'grace_period_days' => 3, // Auto-renewal settings 'auto_renewal_default' => true, // Currency settings 'currency' => [ 'default' => 'USD', 'supported_currencies' => ['USD', 'EUR', 'GBP', 'EGP'], ], // Trial settings 'trial' => [ 'default_days' => 14, 'max_days' => 90, 'min_days' => 1, ], // Plan change settings 'plan_changes' => [ 'allow_downgrades' => true, 'allow_plan_change_during_trial' => true, 'prevent_downgrade_with_excess_usage' => true, ], // Supported locales 'locales' => ['en', 'ar'], 'fallback_locale' => 'en', ];
Testing
Run the test suite:
composer test
Run tests with coverage:
composer test-coverage
Format code:
composer format
๐ชช License
SubSphere is open-sourced software licensed under the MIT license.
๐ Author
Ahmed Essam
- Email: aahmedessam30@gmail.com
- GitHub: @ahmedessam30
๐ค Contributing
Thank you for considering contributing to SubSphere! Please see CONTRIBUTING.md for details.
Development Setup
- Clone the repository
- Install dependencies:
composer install
- Run tests:
composer test
- Follow PSR-12 coding standards
Reporting Issues
Please use the GitHub issue tracker to report bugs or request features.
Built with โค๏ธ for the Laravel community