Skip to content

Commit

Permalink
feat: multiple plan support
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenpaauw committed Nov 24, 2024
1 parent 9c89001 commit c0d5d46
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/Stripe/BillingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

final class BillingProvider implements Provider
{
/**
* @param string|BackedEnum|array<array-key, string|BackedEnum> $plans
*/
public function __construct(
private readonly string|BackedEnum $plan = 'default',
private readonly string|BackedEnum|array $plans = 'default',
) {}

public function getRouteAction(): string|Closure|array
Expand All @@ -32,6 +35,6 @@ public function getRouteAction(): string|Closure|array

public function getSubscribedMiddleware(): string
{
return RedirectIfUserNotSubscribed::plan($this->plan);
return RedirectIfUserNotSubscribed::plan($this->plans);
}
}
42 changes: 30 additions & 12 deletions src/Stripe/RedirectIfUserNotSubscribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Filament\Pages\Dashboard;
use Illuminate\Config\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Laravel\Cashier\SubscriptionBuilder;
use Maartenpaauw\Filament\Cashier\Plan;
use Maartenpaauw\Filament\Cashier\TenantRepository;
Expand All @@ -26,19 +28,26 @@ public function __construct(
*
* @throws Exception
*/
public function handle(Request $request, Closure $next, string $plan = 'default'): Response
public function handle(Request $request, Closure $next, string ...$plans): Response
{
$tenant = TenantRepository::make()->current();
$plan = new Plan($this->repository, $plan);

if ($plan->hasGenericTrial() && $tenant->onGenericTrial()) {
return $next($request);
}
/** @var array<array-key, Plan> $instances */
$instances = Arr::map($plans, fn (string $plan): Plan => new Plan($this->repository, $plan));

foreach ($instances as $plan) {
if ($plan->hasGenericTrial() && $tenant->onGenericTrial()) {
return $next($request);
}

if ($tenant->subscribedToProduct($plan->productId(), $plan->type())) {
return $next($request);
if ($tenant->subscribedToProduct($plan->productId(), $plan->type())) {
return $next($request);
}
}

/** @var Plan $plan */
$plan = Arr::first($instances);

return $tenant
->newSubscription($plan->type(), $plan->isMeteredPrice() ? [] : $plan->priceId())
->when(
Expand All @@ -64,11 +73,20 @@ public function handle(Request $request, Closure $next, string $plan = 'default'
->redirect();
}

public static function plan(string|BackedEnum $plan = 'default'): string
/**
* @param string|BackedEnum|array<array-key, string|BackedEnum> $plans
*/
public static function plan(string|BackedEnum|array $plans = 'default'): string
{
return sprintf('%s:%s', self::class, match (true) {
$plan instanceof BackedEnum => strval($plan->value),
default => $plan,
});
return sprintf(
'%s:%s',
self::class,
Collection::wrap($plans)
->map(static fn (string|BackedEnum $plan): string => match (true) {
$plan instanceof BackedEnum => strval($plan->value),
default => $plan,
})
->join(','),
);
}
}
12 changes: 12 additions & 0 deletions tests/BillingProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@
[Plan::Advanced, 'Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:advanced'],
[Plan::Premium, 'Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:premium'],
]);

it('should use the defined string plans', function (): void {
expect(new BillingProvider('basic,advanced,premium'))
->getSubscribedMiddleware()
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:basic,advanced,premium');
});

it('should use the defined enum plans', function (): void {
expect(new BillingProvider(Plan::cases()))
->getSubscribedMiddleware()
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:basic,advanced,premium');
});
11 changes: 11 additions & 0 deletions tests/RedirectIfUserNotSubscribedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed;
use Workbench\App\enums\Plan;

it('should add the plan as parameter', function () {
expect(RedirectIfUserNotSubscribed::plan('basic'))
Expand All @@ -13,3 +14,13 @@
expect(RedirectIfUserNotSubscribed::plan())
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:default');
});

it('should add the string plans as parameter', function () {
expect(RedirectIfUserNotSubscribed::plan('basic,advanced,premium'))
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:basic,advanced,premium');
});

it('should add the enum plans as parameter', function () {
expect(RedirectIfUserNotSubscribed::plan(Plan::cases()))
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:basic,advanced,premium');
});

0 comments on commit c0d5d46

Please sign in to comment.