feat: fold subscription deletion handling into HandleStripeWebhook
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
@@ -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}");
|
||||
|
||||
Reference in New Issue
Block a user