From 9ae5ec0ed3b9d947bfaff261db6850051c6f2520 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:57:41 +0200 Subject: [PATCH] Update app/Livewire/Subscription/Index.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- app/Livewire/Subscription/Index.php | 46 +++++++++++++++++------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/app/Livewire/Subscription/Index.php b/app/Livewire/Subscription/Index.php index 3e77768a0..8a9cc456f 100644 --- a/app/Livewire/Subscription/Index.php +++ b/app/Livewire/Subscription/Index.php @@ -50,29 +50,37 @@ class Index extends Component public function getStripeStatus() { - $subscription = currentTeam()->subscription; - $stripe = new \Stripe\StripeClient(config('subscription.stripe_api_key')); - $customer = $stripe->customers->retrieve(currentTeam()->subscription->stripe_customer_id); - if ($customer) { - $subscriptions = $stripe->subscriptions->all(['customer' => $customer->id]); - $currentTeam = currentTeam()->id ?? null; - if (count($subscriptions->data) > 0 && $currentTeam) { - $foundSubscription = collect($subscriptions->data)->firstWhere('metadata.team_id', $currentTeam); - if ($foundSubscription) { - $status = data_get($foundSubscription, 'status'); - $subscription->update([ - 'stripe_subscription_id' => $foundSubscription->id, - ]); - if ($status === 'unpaid') { - $this->isUnpaid = true; + try { + $subscription = currentTeam()->subscription; + $stripe = new \Stripe\StripeClient(config('subscription.stripe_api_key')); + $customer = $stripe->customers->retrieve(currentTeam()->subscription->stripe_customer_id); + if ($customer) { + $subscriptions = $stripe->subscriptions->all(['customer' => $customer->id]); + $currentTeam = currentTeam()->id ?? null; + if (count($subscriptions->data) > 0 && $currentTeam) { + $foundSubscription = collect($subscriptions->data)->firstWhere('metadata.team_id', $currentTeam); + if ($foundSubscription) { + $status = data_get($foundSubscription, 'status'); + $subscription->update([ + 'stripe_subscription_id' => $foundSubscription->id, + ]); + if ($status === 'unpaid') { + $this->isUnpaid = true; + } } } + if (count($subscriptions->data) === 0) { + $this->isCancelled = true; + } } - if (count($subscriptions->data) === 0) { - $this->isCancelled = true; - } + } catch (\Exception $e) { + // Log the error + logger()->error('Stripe API error: ' . $e->getMessage()); + // Set a flag to show an error message to the user + $this->addError('stripe', 'Could not retrieve subscription information. Please try again later.'); + } finally { + $this->loading = false; } - $this->loading = false; } public function render()