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.
This commit is contained in:
parent
0a9ca9a185
commit
7a25ca0759
2 changed files with 33 additions and 0 deletions
|
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// ========================================
|
||||
|
|
|
|||
Loading…
Reference in a new issue