Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

refactor(billing): replace MonthlyCreditsLimit Pennant feature with BillingCycle

Paulo Castellanoaacc4390a275
diff --git a/app/Actions/Workspace/CreateWorkspace.php b/app/Actions/Workspace/CreateWorkspace.php
index d591ba8cd..4c42fe67b 100644
--- a/app/Actions/Workspace/CreateWorkspace.php
+++ b/app/Actions/Workspace/CreateWorkspace.php
@@ -42,7 +42,6 @@ class CreateWorkspace
return $workspace;
});
- $user->account?->forgetPlanFeatureCache();
$user->account?->syncWorkspaceQuantity();
return $workspace;
diff --git a/app/Actions/Workspace/DeleteWorkspace.php b/app/Actions/Workspace/DeleteWorkspace.php
index aa889ecda..e43e8cbe3 100644
--- a/app/Actions/Workspace/DeleteWorkspace.php
+++ b/app/Actions/Workspace/DeleteWorkspace.php
@@ -20,7 +20,6 @@ class DeleteWorkspace
$workspace->delete();
- $account?->forgetPlanFeatureCache();
$account?->syncWorkspaceQuantity();
if (PostHogService::isEnabled()) {
diff --git a/app/Features/MonthlyCreditsLimit.php b/app/Features/MonthlyCreditsLimit.php
deleted file mode 100644
index b4dc719c2..000000000
--- a/app/Features/MonthlyCreditsLimit.php
+++ /dev/null
@@ -1,18 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace App\Features;
-
-use App\Models\Account;
-use App\Support\BillingCycle;
-
-class MonthlyCreditsLimit
-{
- public string $name = 'monthly-credits-limit';
-
- public function resolve(Account $scope): int
- {
- return BillingCycle::for($scope)->creditAllotment();
- }
-}
diff --git a/app/Http/Controllers/App/BillingController.php b/app/Http/Controllers/App/BillingController.php
index fae503e84..d085906fa 100644
--- a/app/Http/Controllers/App/BillingController.php
+++ b/app/Http/Controllers/App/BillingController.php
@@ -143,7 +143,6 @@ class BillingController extends Controller
}
$subscription->swap($yearlyPriceId);
- $account->forgetPlanFeatureCache();
return redirect()->route('app.billing.index')
->with('flash.success', __('billing.flash.switched_to_yearly'));
diff --git a/app/Http/Controllers/App/Settings/UsageController.php b/app/Http/Controllers/App/Settings/UsageController.php
index 13e240e72..7940ef58a 100644
--- a/app/Http/Controllers/App/Settings/UsageController.php
+++ b/app/Http/Controllers/App/Settings/UsageController.php
@@ -4,14 +4,12 @@ declare(strict_types=1);
namespace App\Http\Controllers\App\Settings;
-use App\Features\MonthlyCreditsLimit;
use App\Http\Controllers\App\Controller;
use App\Support\BillingCycle;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
-use Laravel\Pennant\Feature;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
class UsageController extends Controller
@@ -39,7 +37,7 @@ class UsageController extends Controller
'socialAccountCount' => $totalSocialAccounts,
'memberCount' => $totalMembers,
'creditsUsed' => BillingCycle::for($account)->usedCredits(),
- 'monthlyCreditsLimit' => Feature::for($account)->value(MonthlyCreditsLimit::class),
+ 'monthlyCreditsLimit' => BillingCycle::for($account)->creditAllotment(),
],
]);
}
diff --git a/app/Listeners/StripeEventListener.php b/app/Listeners/StripeEventListener.php
index 4a701c0b4..14e0cd192 100644
--- a/app/Listeners/StripeEventListener.php
+++ b/app/Listeners/StripeEventListener.php
@@ -56,7 +56,6 @@ class StripeEventListener
'plan_id' => $plan->id,
'trial_ends_at' => null,
]);
- $account->forgetPlanFeatureCache();
}
$this->trackPlanChange($account, BillingEvent::Created, $previousPlan, $payload);
@@ -71,7 +70,6 @@ class StripeEventListener
if ($plan = $this->resolvePlanFromSubscriptionItems($payload, $account)) {
$account->update(['plan_id' => $plan->id]);
- $account->forgetPlanFeatureCache();
}
$this->trackPlanChange($account, BillingEvent::Updated, $previousPlan, $payload);
@@ -89,7 +87,6 @@ class StripeEventListener
$previousPlan = $account->plan?->name;
$account->update(['plan_id' => null]);
- $account->forgetPlanFeatureCache();
$this->trackPlanChange($account, BillingEvent::Cancelled, $previousPlan, $payload);
}
diff --git a/app/Models/Account.php b/app/Models/Account.php
index ce5454ba9..b8e31b00a 100644
--- a/app/Models/Account.php
+++ b/app/Models/Account.php
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Models;
-use App\Features\MonthlyCreditsLimit;
use App\Models\Traits\HasUsage;
use Carbon\CarbonInterface;
use Database\Factories\AccountFactory;
@@ -15,7 +14,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Log;
use Laravel\Cashier\Billable;
-use Laravel\Pennant\Feature;
use Throwable;
class Account extends Model
@@ -46,13 +44,6 @@ class Account extends Model
'trial_ends_at' => 'datetime',
];
- public function forgetPlanFeatureCache(): void
- {
- Feature::for($this)->forget([
- MonthlyCreditsLimit::class,
- ]);
- }
-
public function owner(): BelongsTo
{
return $this->belongsTo(User::class, 'owner_id');
diff --git a/app/Models/Traits/HasUsage.php b/app/Models/Traits/HasUsage.php
index 0ae065029..f37b2ff23 100644
--- a/app/Models/Traits/HasUsage.php
+++ b/app/Models/Traits/HasUsage.php
@@ -4,20 +4,18 @@ declare(strict_types=1);
namespace App\Models\Traits;
-use App\Features\MonthlyCreditsLimit;
use App\Models\Account;
use App\Models\Invite;
use App\Models\Post;
use App\Support\BillingCycle;
use Illuminate\Support\Facades\Cache;
-use Laravel\Pennant\Feature;
/**
* Provides account-level usage counts and plan-resolved feature limits.
*
- * The `featureLimits()` call resolves the Pennant credit feature for the account,
- * which writes to the features cache table on first access. The cache is invalidated
- * explicitly via `Account::forgetPlanFeatureCache()` on plan and workspace changes.
+ * `featureLimits()` resolves the account's per-cycle credit allotment directly
+ * from BillingCycle, computed fresh from the plan, workspace count, and billing
+ * interval — no caching, so there is nothing to invalidate.
*/
trait HasUsage
{
@@ -56,7 +54,7 @@ trait HasUsage
public function featureLimits(): array
{
return [
- 'monthlyCreditsLimit' => Feature::for($this)->value(MonthlyCreditsLimit::class),
+ 'monthlyCreditsLimit' => BillingCycle::for($this)->creditAllotment(),
];
}
diff --git a/tests/Feature/Billing/WorkspaceQuantitySyncTest.php b/tests/Feature/Billing/WorkspaceQuantitySyncTest.php
index 1113ae2b3..8f2bae64e 100644
--- a/tests/Feature/Billing/WorkspaceQuantitySyncTest.php
+++ b/tests/Feature/Billing/WorkspaceQuantitySyncTest.php
@@ -54,7 +54,6 @@ test('creating a workspace syncs the stripe quantity', function () {
$user = User::factory()->create();
$account = mock(Account::class)->makePartial();
- $account->shouldReceive('forgetPlanFeatureCache')->once();
$account->shouldReceive('syncWorkspaceQuantity')->once();
$user->setRelation('account', $account);
@@ -69,7 +68,6 @@ test('deleting a workspace syncs the stripe quantity', function () {
]);
$account = mock(Account::class)->makePartial();
- $account->shouldReceive('forgetPlanFeatureCache')->once();
$account->shouldReceive('syncWorkspaceQuantity')->once();
$workspace->setRelation('account', $account);
diff --git a/tests/Feature/Listeners/StripeEventListenerTest.php b/tests/Feature/Listeners/StripeEventListenerTest.php
index f3ebf327b..74ffc0ffb 100644
--- a/tests/Feature/Listeners/StripeEventListenerTest.php
+++ b/tests/Feature/Listeners/StripeEventListenerTest.php
@@ -4,16 +4,13 @@ declare(strict_types=1);
use App\Enums\Plan\Slug;
use App\Enums\PostHog\BillingEvent;
-use App\Features\MonthlyCreditsLimit;
use App\Jobs\PostHog\TrackBilling;
use App\Listeners\StripeEventListener;
use App\Models\Account;
use App\Models\Plan;
use App\Models\User;
use Illuminate\Support\Facades\Bus;
-use Illuminate\Support\Facades\DB;
use Laravel\Cashier\Events\WebhookReceived;
-use Laravel\Pennant\Feature;
beforeEach(function () {
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
@@ -439,18 +436,12 @@ test('subscription created forwards a null previous plan when account had none',
// Pennant feature cache invalidation
// ========================================
-test('subscription updated flushes the pennant cache when the plan changes', function () {
+test('subscription updated maps the plan by price id', function () {
$starter = Plan::query()->where('slug', 'starter')->firstOrFail();
$pro = Plan::query()->where('slug', 'pro')->firstOrFail();
$this->account->update(['plan_id' => $starter->id]);
- // Prime the Pennant cache.
- Feature::for($this->account)->value(MonthlyCreditsLimit::class);
-
- expect(DB::table('features')->where('scope', 'account|'.$this->account->id)->count())
- ->toBeGreaterThan(0);
-
$this->listener->handle(new WebhookReceived([
'type' => 'customer.subscription.updated',
'data' => ['object' => [
@@ -459,9 +450,6 @@ test('subscription updated flushes the pennant cache when the plan changes', fun
]],
]));
- expect(DB::table('features')->where('scope', 'account|'.$this->account->id)->count())
- ->toBe(0);
-
expect($this->account->fresh()->plan_id)->toBe($pro->id);
});
@@ -480,20 +468,14 @@ test('subscription updated maps an archived legacy plan by price id', function (
expect($this->account->fresh()->plan_id)->toBe($pro->id);
});
-test('subscription deleted flushes the pennant cache', function () {
+test('subscription deleted clears the plan_id', function () {
$starter = Plan::query()->where('slug', 'starter')->firstOrFail();
$this->account->update(['plan_id' => $starter->id]);
- Feature::for($this->account)->value(MonthlyCreditsLimit::class);
-
- expect(DB::table('features')->where('scope', 'account|'.$this->account->id)->count())
- ->toBeGreaterThan(0);
-
$this->listener->handle(new WebhookReceived([
'type' => 'customer.subscription.deleted',
'data' => ['object' => ['customer' => 'cus_test123']],
]));
- expect(DB::table('features')->where('scope', 'account|'.$this->account->id)->count())
- ->toBe(0);
+ expect($this->account->fresh()->plan_id)->toBeNull();
});
diff --git a/tests/Feature/Models/AccountModelTest.php b/tests/Feature/Models/AccountModelTest.php
deleted file mode 100644
index 04457dc4d..000000000
--- a/tests/Feature/Models/AccountModelTest.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-use App\Enums\Plan\Slug;
-use App\Features\MonthlyCreditsLimit;
-use App\Models\Account;
-use App\Models\Plan;
-use App\Models\Workspace;
-use Illuminate\Support\Facades\DB;
-use Laravel\Pennant\Feature;
-
-test('forgetPlanFeatureCache drops the cached monthly credits feature', function () {
- $plan = Plan::where('slug', Slug::Workspace)->first();
-
- $account = Account::factory()->create(['plan_id' => $plan->id]);
- Workspace::factory()->create(['account_id' => $account->id]);
-
- Feature::for($account)->value(MonthlyCreditsLimit::class);
-
- expect(DB::table('features')->where('scope', 'account|'.$account->id)->count())
- ->toBe(1);
-
- $account->forgetPlanFeatureCache();
-
- expect(DB::table('features')->where('scope', 'account|'.$account->id)->count())
- ->toBe(0);
-
- expect(Feature::for($account)->value(MonthlyCreditsLimit::class))->toBe(2500);
-});
diff --git a/tests/Unit/Features/MonthlyCreditsLimitTest.php b/tests/Unit/Features/MonthlyCreditsLimitTest.php
deleted file mode 100644
index 261ba4fdc..000000000
--- a/tests/Unit/Features/MonthlyCreditsLimitTest.php
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-use App\Enums\Plan\Slug;
-use App\Features\MonthlyCreditsLimit;
-use App\Models\Account;
-use App\Models\Plan;
-use App\Models\Workspace;
-
-test('resolves to the billing cycle credit allotment', function () {
- $plan = Plan::where('slug', Slug::Workspace)->first();
- $account = Account::factory()->create(['plan_id' => $plan->id, 'trial_ends_at' => null]);
- Workspace::factory()->count(2)->create(['account_id' => $account->id]);
-
- expect((new MonthlyCreditsLimit)->resolve($account))->toBe(5000);
-});
-
-test('resolves to zero when the account has no workspaces', function () {
- $plan = Plan::where('slug', Slug::Workspace)->first();
- $account = Account::factory()->create(['plan_id' => $plan->id, 'trial_ends_at' => null]);
-
- expect((new MonthlyCreditsLimit)->resolve($account))->toBe(0);
-});