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 // ========================================