Last active
December 16, 2023 14:14
-
-
Save neverything/b21b4affc28a25b706c134d1d22f9910 to your computer and use it in GitHub Desktop.
Handle free users with Filament and Laravel Spark using a custom Middleware. Learn more: https://silvanhagen.com/free-users-filament-laravel-spark/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// namespace and more... | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function boot(): void | |
{ | |
// Bind our implementation | |
$this->app->bind( | |
'Filament\Billing\Providers\Http\Middleware\VerifySparkBillableIsSubscribed', | |
VerifyProjectIsBillable::class | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// namespace and more ... | |
class FeedbackResource extends Resource | |
{ | |
// rest of the class... | |
public static function isTenantSubscriptionRequired(Panel $panel): bool | |
{ | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// namespace and use... | |
class Project extends Model implements HasCurrentTenantLabel | |
{ | |
use Billable, HasFactory; | |
// rest of the class... | |
public function isBillable(): bool | |
{ | |
return ! $this->free_forever; | |
} | |
public function isOnTrialOrSubscribed(): bool | |
{ | |
return $this->onTrial() || $this->subscribed(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Filament\Billing\Providers\Http\Middleware\VerifySparkBillableIsSubscribed; | |
use Filament\Facades\Filament; | |
class VerifyProjectIsBillable extends VerifySparkBillableIsSubscribed | |
{ | |
public function handle($request, $next, $subscription = 'default', $plan = null) | |
{ | |
$project = Filament::getTenant(); | |
if ($this->shouldRedirectToBilling($project, $request)) { | |
return parent::handle($request, $next, $subscription, $plan); | |
} | |
return $next($request); | |
} | |
protected function shouldRedirectToBilling($project, $request): bool | |
{ | |
return $project | |
&& $project->isBillable() | |
&& ! $project->isOnTrialOrSubscribed(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment