feat: fold subscription deletion handling into HandleStripeWebhook

This commit is contained in:
Ovidiu U
2026-04-23 10:34:05 +01:00
parent 25b79f095b
commit b9d457578c
4 changed files with 41 additions and 79 deletions

View File

@@ -1,37 +0,0 @@
<?php
namespace App\Listeners;
use App\Models\User;
use App\Models\UserNotificationPreference;
use Illuminate\Support\Facades\Cache;
use Laravel\Cashier\Events\WebhookReceived;
class DowngradeUserOnSubscriptionDeleted
{
public function handle(WebhookReceived $event): void
{
if (($event->payload['type'] ?? null) !== 'customer.subscription.deleted') {
return;
}
$stripeCustomerId = $event->payload['data']['object']['customer'] ?? null;
if (! $stripeCustomerId) {
return;
}
$user = User::where('stripe_id', $stripeCustomerId)->first();
if (! $user) {
return;
}
UserNotificationPreference::query()
->where('user_id', $user->id)
->whereIn('channel', ['whatsapp', 'sms'])
->update(['enabled' => false]);
Cache::tags(['plans'])->forget("plan_for_user_{$user->id}");
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Listeners;
use App\Models\User;
use App\Models\UserNotificationPreference;
use Illuminate\Support\Facades\Cache;
use Laravel\Cashier\Events\WebhookReceived;
@@ -26,10 +27,23 @@ final class HandleStripeWebhook
match ($type) {
'customer.subscription.created',
'customer.subscription.updated' => $this->bustPlanCache($user),
'customer.subscription.deleted' => $this->handleSubscriptionDeleted($user),
default => null,
};
}
private function handleSubscriptionDeleted(User $user): void
{
UserNotificationPreference::query()
->where('user_id', $user->id)
->whereIn('channel', ['whatsapp', 'sms'])
->update(['enabled' => false]);
$user->forceFill(['grace_period_until' => null])->save();
$this->bustPlanCache($user);
}
private function bustPlanCache(User $user): void
{
Cache::tags(['plans'])->forget("plan_for_user_{$user->id}");