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.

fix: gate TrackBilling dispatch in StripeEventListener

Paulo Castellano7a25ca075953
diff --git a/app/Listeners/StripeEventListener.php b/app/Listeners/StripeEventListener.php
index 3aecd9234..234808398 100644
--- a/app/Listeners/StripeEventListener.php
+++ b/app/Listeners/StripeEventListener.php
@@ -9,6 +9,7 @@ use App\Events\SubscriptionCreated;
use App\Jobs\PostHog\TrackBilling;
use App\Models\Account;
use App\Models\Plan;
+use App\Services\PostHogService;
use Illuminate\Support\Facades\Log;
use Laravel\Cashier\Events\WebhookReceived;
@@ -145,11 +146,17 @@ class StripeEventListener
/**
* Hand the lifecycle event off to the queue so the listener stays fast.
+ * Skip dispatch when PostHog is disabled — `TrackBilling::handle` would
+ * no-op anyway, but enqueuing it still costs queue worker cycles.
*
* @param array<string, mixed> $payload
*/
private function trackPlanChange(Account $account, BillingEvent $event, ?string $previousPlan, array $payload): void
{
+ if (! PostHogService::isEnabled()) {
+ return;
+ }
+
TrackBilling::dispatch((string) $account->id, $event, $payload, $previousPlan);
}
}
diff --git a/tests/Feature/Listeners/StripeEventListenerTest.php b/tests/Feature/Listeners/StripeEventListenerTest.php
index 9caceea98..a9da1b9d2 100644
--- a/tests/Feature/Listeners/StripeEventListenerTest.php
+++ b/tests/Feature/Listeners/StripeEventListenerTest.php
@@ -18,6 +18,8 @@ use Laravel\Cashier\Events\WebhookReceived;
use Laravel\Pennant\Feature;
beforeEach(function () {
+ config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
+
$this->account = Account::factory()->create(['stripe_id' => 'cus_test123']);
$this->user = User::factory()->create([
'account_id' => $this->account->id,
@@ -210,6 +212,30 @@ test('unknown event types do not dispatch TrackBilling', function () {
Bus::assertNotDispatched(TrackBilling::class);
});
+test('TrackBilling is not dispatched when PostHog is disabled', function () {
+ config(['services.posthog.enabled' => false]);
+ Bus::fake([TrackBilling::class]);
+
+ $this->listener->handle(new WebhookReceived([
+ 'type' => 'customer.subscription.created',
+ 'data' => ['object' => ['customer' => 'cus_test123', 'id' => 'sub_123']],
+ ]));
+
+ Bus::assertNotDispatched(TrackBilling::class);
+});
+
+test('TrackBilling is not dispatched when api key is missing', function () {
+ config(['services.posthog.api_key' => null]);
+ Bus::fake([TrackBilling::class]);
+
+ $this->listener->handle(new WebhookReceived([
+ 'type' => 'customer.subscription.created',
+ 'data' => ['object' => ['customer' => 'cus_test123', 'id' => 'sub_123']],
+ ]));
+
+ Bus::assertNotDispatched(TrackBilling::class);
+});
+
// ========================================
// Plan sync — domain logic
// ========================================