From d8aeff53fa74bf4ad08b99a47b236d3d7caa91ce Mon Sep 17 00:00:00 2001 From: Steven Fox <62109327+steven-fox@users.noreply.github.com> Date: Sun, 18 May 2025 10:28:47 -0400 Subject: [PATCH 1/2] improve $user handling and add error logging --- app/Livewire/MobilePricing.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/Livewire/MobilePricing.php b/app/Livewire/MobilePricing.php index 46ac17a4..da60b759 100644 --- a/app/Livewire/MobilePricing.php +++ b/app/Livewire/MobilePricing.php @@ -6,6 +6,7 @@ use App\Models\User; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use Livewire\Component; @@ -24,11 +25,25 @@ public function handlePurchaseRequest(array $data) public function createCheckoutSession(string $plan, ?User $user = null) { - if (! ($user ??= Auth::user())) { + // If a user isn't passed into this method, Livewire will instantiate + // a new User. So we need to check that the user exists before using it, + // and then use the authenticated user as a fallback if the user is + // logged in. + $user = $user?->exists ? $user : Auth::user(); + + if (! $user) { + // TODO: return a flash message or notification to the user that there + // was an error. + Log::error('Failed to create checkout session. User does not exist and user is not authenticated.'); + return; } if (! ($subscription = Subscription::tryFrom($plan))) { + // TODO: return a flash message or notification to the user that there + // was an error. + Log::error('Failed to create checkout session. Invalid subscription plan name provided.'); + return; } From fd08934707b878221d7cf3430b7d4095a91ba916 Mon Sep 17 00:00:00 2001 From: Steven Fox <62109327+steven-fox@users.noreply.github.com> Date: Sun, 18 May 2025 10:30:22 -0400 Subject: [PATCH 2/2] fix comment wording --- app/Livewire/MobilePricing.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Livewire/MobilePricing.php b/app/Livewire/MobilePricing.php index da60b759..de60e00b 100644 --- a/app/Livewire/MobilePricing.php +++ b/app/Livewire/MobilePricing.php @@ -27,8 +27,7 @@ public function createCheckoutSession(string $plan, ?User $user = null) { // If a user isn't passed into this method, Livewire will instantiate // a new User. So we need to check that the user exists before using it, - // and then use the authenticated user as a fallback if the user is - // logged in. + // and then use the authenticated user as a fallback. $user = $user?->exists ? $user : Auth::user(); if (! $user) {