F
Filament6mo ago
Pede

Multi-Tenancy with Laravel Spark

What I am trying to do: Im trying to add laravel spark on top of multi-tenancy teams. What I did so far: Installed laravel spark and followed this guide: https://spark.laravel.com/docs/spark-stripe/cookbook.html#team-billing and installed the "filament/spark-billing-provider"
->tenantBillingProvider(new SparkBillingProvider());
->tenantBillingProvider(new SparkBillingProvider());
to my panel. What is the issue: My Multi-Tenancy (team selector) now has the "Manage Subscription" option an it links to: http://127.0.0.1:8000/app/billing/team/2 It opens the spark billing portal correctly and shows "Managing billing for Test Team." so it seems to work correctly. However when I get redirected back from the stripe payment it lands on url: http://127.0.0.1:8000/app/billing?checkout=subscription_started Which resulted in 404 error. If I manualy add "/team/2" to the url everything works as expected. http://127.0.0.1:8000/app/billing/team/2?checkout=subscription_started So my question is: How can I manipulate the url from the stripe success redirect?
Cookbook | Laravel Spark
A perfect starting point for your next great idea.
Solution:
Redirect is fixed in Spark release v4.2.11. Thank you @Nathan for reaching out to the Laravel Spark team.
Jump to solution
3 Replies
Nathan
Nathan6mo ago
Hi @Pede I am trying to setup as well, but i get a blank page when trying to access the "manage subscription" (https://github.com/filamentphp/filament/discussions/10099) Would you be able to share your SparkServiceProvider?
Pede
Pede6mo ago
I was hopeing for some help 🥲 But sure here you go:
<?php

namespace App\Providers;

use App\Models\Team;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Spark\Plan;
use Spark\Spark;
use Laravel\Cashier\Cashier;
use Filament\Facades\Filament;

class SparkServiceProvider extends ServiceProvider
{

public function register(): void
{
Spark::ignoreMigrations();
}

public function boot(): void
{

// Instruct Cashier to use the `Team` model instead of the `User` model...
Cashier::useCustomerModel(Team::class);


// Resolve the current team...
Spark::billable(Team::class)->resolve(function (Request $request) {
return $request->user()->currentTeam;
});

// Verify that the current user owns the team...
Spark::billable(Team::class)->authorize(function (Team $billable, Request $request) {
return $request->user() &&
$request->user()->team_id == $billable->team_id;
});

Spark::billable(Team::class)->checkPlanEligibility(function (Team $billable, Plan $plan) {
// if ($billable->projects > 5 && $plan->name == 'Basic') {
// throw ValidationException::withMessages([
// 'plan' => 'You have too many projects for the selected plan.'
// ]);
// }
});
}
}
<?php

namespace App\Providers;

use App\Models\Team;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Spark\Plan;
use Spark\Spark;
use Laravel\Cashier\Cashier;
use Filament\Facades\Filament;

class SparkServiceProvider extends ServiceProvider
{

public function register(): void
{
Spark::ignoreMigrations();
}

public function boot(): void
{

// Instruct Cashier to use the `Team` model instead of the `User` model...
Cashier::useCustomerModel(Team::class);


// Resolve the current team...
Spark::billable(Team::class)->resolve(function (Request $request) {
return $request->user()->currentTeam;
});

// Verify that the current user owns the team...
Spark::billable(Team::class)->authorize(function (Team $billable, Request $request) {
return $request->user() &&
$request->user()->team_id == $billable->team_id;
});

Spark::billable(Team::class)->checkPlanEligibility(function (Team $billable, Plan $plan) {
// if ($billable->projects > 5 && $plan->name == 'Basic') {
// throw ValidationException::withMessages([
// 'plan' => 'You have too many projects for the selected plan.'
// ]);
// }
});
}
}
Solution
Pede
Pede6mo ago
Redirect is fixed in Spark release v4.2.11. Thank you @Nathan for reaching out to the Laravel Spark team.