From 7a25ca075953e0d376e7a3856c9d37d7329981ac Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Thu, 7 May 2026 13:35:39 -0300 Subject: [PATCH] fix: gate TrackBilling dispatch in StripeEventListener The TrackBilling job was being enqueued on every Stripe webhook event even with POSTHOG_ENABLED=false. handle() short-circuited via the isEnabled() check, so nothing reached PostHog, but the job still consumed queue worker cycles (20-300ms each) on accounts where Stripe fires its frequent customer.subscription.updated events. Adds the missing isEnabled() check at the dispatch site in trackPlanChange, plus the two test cases that would have caught this the first time around (TrackBilling not dispatched when enabled=false, and not dispatched when api_key is missing). Updates the listener test beforeEach to opt the suite into the enabled path so the existing assertDispatched() assertions continue to fire. --- app/Listeners/StripeEventListener.php | 7 +++++ .../Listeners/StripeEventListenerTest.php | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/Listeners/StripeEventListener.php b/app/Listeners/StripeEventListener.php index 3aecd923..23480839 100644 --- a/app/Listeners/StripeEventListener.php +++ b/app/Listeners/StripeEventListener.php @@ -9,6 +9,7 @@ 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 @@ private function resolvePlanFromSubscriptionItems(array $payload, Account $accou /** * 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 $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 9caceea9..a9da1b9d 100644 --- a/tests/Feature/Listeners/StripeEventListenerTest.php +++ b/tests/Feature/Listeners/StripeEventListenerTest.php @@ -18,6 +18,8 @@ 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 @@ 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 // ========================================