From f223cf14cecc51d23343389cb88e1b796f8f362c Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Sun, 14 Jun 2026 16:12:49 -0300 Subject: [PATCH] Fire checkout.completed reliably for trial-with-card checkouts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A trial-with-card subscription is already subscribed() (status trialing) by the time the webhook lands, so /billing/processing usually mounts already-active and the false->true poll transition the event depended on never happened — only 3 of 66 real subscriptions emitted checkout.completed. Complete the purchase from whichever path runs first (onMounted when already active, or the poll transition), de-duplicated per checkout session via a one-time Cache::add gate on session_id so back-button/refresh can't re-fire. --- .../Controllers/App/BillingController.php | 11 +++++- resources/js/pages/billing/Processing.vue | 39 ++++++++++++------- tests/Feature/BillingControllerTest.php | 18 +++++++++ 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/App/BillingController.php b/app/Http/Controllers/App/BillingController.php index 3edfbd03..3ea14e0d 100644 --- a/app/Http/Controllers/App/BillingController.php +++ b/app/Http/Controllers/App/BillingController.php @@ -8,6 +8,7 @@ use App\Models\Plan; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Gate; use Inertia\Inertia; use Inertia\Response; @@ -88,9 +89,17 @@ public function processing(Request $request): Response|RedirectResponse $account = $request->user()->account; $sessionId = $request->query('session_id'); + // Consume the checkout session once: `fromCheckout` is true only the first + // time this session_id is seen, so a back-button/refresh to the success URL + // can't re-fire `checkout.completed`. `Cache::add` is atomic — it returns + // true only when the key didn't exist yet. + $fromCheckout = is_string($sessionId) && $sessionId !== '' + && Cache::add("checkout_tracked:{$sessionId}", true, now()->addDay()); + return Inertia::render('billing/Processing', [ 'subscriptionActive' => $account && $account->subscribed(Account::SUBSCRIPTION_NAME), - 'conversion' => is_string($sessionId) && $sessionId !== '' && $account?->stripe_id + 'fromCheckout' => $fromCheckout, + 'conversion' => $fromCheckout && $account?->stripe_id ? fn () => $this->buildConversionData($account, $sessionId) : null, ]); diff --git a/resources/js/pages/billing/Processing.vue b/resources/js/pages/billing/Processing.vue index 878bde10..5220708f 100644 --- a/resources/js/pages/billing/Processing.vue +++ b/resources/js/pages/billing/Processing.vue @@ -1,7 +1,7 @@ diff --git a/tests/Feature/BillingControllerTest.php b/tests/Feature/BillingControllerTest.php index 55fafc56..ec6d7b0a 100644 --- a/tests/Feature/BillingControllerTest.php +++ b/tests/Feature/BillingControllerTest.php @@ -183,10 +183,28 @@ $response->assertInertia(fn ($page) => $page ->component('billing/Processing', false) ->has('subscriptionActive') + ->where('fromCheckout', false) ->where('conversion', null) ); }); +test('billing processing exposes fromCheckout=true only the first time a session_id is seen', function () { + config(['trypost.self_hosted' => false]); + + $sessionId = 'cs_test_'.fake()->uuid(); + + $first = $this->actingAs($this->user) + ->get(route('app.billing.processing', ['session_id' => $sessionId])); + $first->assertOk(); + $first->assertInertia(fn ($page) => $page->where('fromCheckout', true)); + + // A back-button / refresh to the same success URL must not re-fire the event. + $second = $this->actingAs($this->user) + ->get(route('app.billing.processing', ['session_id' => $sessionId])); + $second->assertOk(); + $second->assertInertia(fn ($page) => $page->where('fromCheckout', false)); +}); + test('billing processing exposes null conversion when session_id query param is missing', function () { config(['trypost.self_hosted' => false]);