feat(subscription): enhance subscription management with loading states and Stripe status checks
This commit is contained in:
@@ -12,19 +12,30 @@ class Index extends Component
|
||||
|
||||
public bool $alreadySubscribed = false;
|
||||
|
||||
public bool $isUnpaid = false;
|
||||
|
||||
public bool $isCancelled = false;
|
||||
|
||||
public bool $isMember = false;
|
||||
|
||||
public bool $loading = true;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
if (! isCloud()) {
|
||||
return redirect(RouteServiceProvider::HOME);
|
||||
}
|
||||
if (auth()->user()?->isMember()) {
|
||||
return redirect()->route('dashboard');
|
||||
$this->isMember = true;
|
||||
}
|
||||
if (data_get(currentTeam(), 'subscription') && isSubscriptionActive()) {
|
||||
return redirect()->route('subscription.show');
|
||||
}
|
||||
$this->settings = instanceSettings();
|
||||
$this->alreadySubscribed = currentTeam()->subscription()->exists();
|
||||
if (! $this->alreadySubscribed) {
|
||||
$this->loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function stripeCustomerPortal()
|
||||
@@ -37,6 +48,33 @@ class Index extends Component
|
||||
return redirect($session->url);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count($subscriptions->data) === 0) {
|
||||
$this->isCancelled = true;
|
||||
}
|
||||
}
|
||||
$this->loading = false;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.subscription.index');
|
||||
|
@@ -192,8 +192,6 @@ class Team extends Model implements SendsDiscord, SendsEmail, SendsPushover, Sen
|
||||
public function subscriptionEnded()
|
||||
{
|
||||
$this->subscription->update([
|
||||
'stripe_subscription_id' => null,
|
||||
'stripe_plan_id' => null,
|
||||
'stripe_cancel_at_period_end' => false,
|
||||
'stripe_invoice_paid' => false,
|
||||
'stripe_trial_already_ended' => false,
|
||||
|
Reference in New Issue
Block a user