fix(plans): prevent deletion of plans with active subscriptions

- Fix bulk delete and individual delete actions using before() hook with halt()
  - Add daily/weekly billing cycle options to plan resource and Polar provider
  - Enhance payment confirmation with dynamic polling and loading states
  - Add graceful handling for deleted plans in subscription display
  - Update Polar provider to support dynamic billing cycles
This commit is contained in:
idevakk
2025-12-07 02:23:14 -08:00
parent 1b438cbf89
commit 5fabec1f9d
7 changed files with 225 additions and 93 deletions

View File

@@ -3,7 +3,6 @@
namespace App\Livewire;
use App\Models\Subscription;
use App\Services\Payments\PaymentOrchestrator;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Log;
@@ -25,6 +24,8 @@ class PaymentConfirmation extends Component
public $errorMessage = null;
public $isChecking = false;
protected $listeners = ['$refresh'];
public function mount($subscription = null, $sessionToken = null): void
@@ -100,95 +101,60 @@ class PaymentConfirmation extends Component
$this->redirect(route('dashboard'));
}
$this->isChecking = false;
return;
}
// Set loading state
$this->isChecking = true;
// Increment poll count first
$this->pollCount++;
try {
$orchestrator = app(PaymentOrchestrator::class);
$user = auth()->user();
Log::info('PaymentConfirmation: Checking subscription status', [
Log::info('PaymentConfirmation: Syncing subscription with provider', [
'subscription_id' => $this->subscription->id,
'provider_subscription_id' => $this->subscription->provider_subscription_id,
'provider' => $this->subscription->provider,
'provider_checkout_id' => $this->subscription->provider_checkout_id,
'poll_count' => $this->pollCount,
]);
// Check status via provider (only if we have a provider subscription ID)
if (empty($this->subscription->provider_subscription_id)) {
Log::info('PaymentConfirmation: Skipping provider status check - no provider subscription ID yet', [
// Use the same sync method as the billing page
$syncSuccess = $this->subscription->syncWithProvider();
// Refresh the subscription from database to get updated status
$this->subscription->refresh();
Log::info('PaymentConfirmation: Subscription sync completed', [
'subscription_id' => $this->subscription->id,
'sync_success' => $syncSuccess,
'current_status' => $this->subscription->status,
'provider_subscription_id' => $this->subscription->provider_subscription_id,
]);
// Check if subscription is now active
if (in_array($this->subscription->status, ['active', 'trialing'])) {
$this->status = 'activated';
$this->showConfetti = true;
// Stop polling when activated
$this->pollCount = $this->maxPolls;
Log::info('PaymentConfirmation: Subscription activated successfully', [
'subscription_id' => $this->subscription->id,
'provider' => $this->subscription->provider,
'provider_checkout_id' => $this->subscription->provider_checkout_id,
'poll_count' => $this->pollCount,
'final_status' => $this->subscription->status,
]);
// Don't update status, just continue polling
return;
}
$statusResult = $orchestrator->checkSubscriptionStatus(
$user,
$this->subscription->provider,
$this->subscription->provider_subscription_id
);
if ($statusResult['success']) {
$providerStatus = $statusResult['status'];
Log::info('PaymentConfirmation: Provider status received', [
'provider_status' => $providerStatus,
'subscription_id' => $this->subscription->id,
]);
// Update local subscription if status changed
if ($providerStatus !== $this->subscription->status) {
$this->subscription->status = $providerStatus;
$this->subscription->save();
Log::info('PaymentConfirmation: Updated local subscription status', [
'old_status' => $this->subscription->getOriginal('status'),
'new_status' => $providerStatus,
]);
}
// Check if subscription is now active
if ($providerStatus === 'active') {
$this->status = 'activated';
$this->showConfetti = true;
// Stop polling when activated
$this->pollCount = $this->maxPolls;
Log::info('PaymentConfirmation: Subscription activated successfully', [
'subscription_id' => $this->subscription->id,
]);
return;
}
} else {
// Handle status check failure
Log::warning('PaymentConfirmation: Provider status check failed', [
'error' => $statusResult['error'] ?? 'Unknown error',
'subscription_id' => $this->subscription->id,
'retry_suggested' => $statusResult['retry_suggested'] ?? false,
]);
// If retry is suggested (e.g., webhook not processed yet), continue polling
if (! ($statusResult['retry_suggested'] ?? false)) {
// If retry is not suggested, we might want to show an error
// but for now, continue polling to be safe
}
}
// Continue polling if not active and max polls not reached
if ($this->pollCount < $this->maxPolls) {
$this->status = 'verifying';
} else {
// Max polls reached, check final status
// Max polls reached, determine final status
$this->status = in_array($this->subscription->status, ['active', 'trialing'])
? 'activated'
: 'pending';
@@ -200,7 +166,7 @@ class PaymentConfirmation extends Component
}
} catch (\Exception $e) {
Log::error('PaymentConfirmation: Error checking subscription status', [
Log::error('PaymentConfirmation: Error syncing subscription', [
'subscription_id' => $this->subscription->id,
'error' => $e->getMessage(),
'poll_count' => $this->pollCount,
@@ -211,6 +177,9 @@ class PaymentConfirmation extends Component
$this->errorMessage = 'Unable to verify payment status after multiple attempts. Please check your subscription page.';
$this->status = 'error';
}
} finally {
// Always reset loading state
$this->isChecking = false;
}
}