feat: clear grace period on invoice.payment_succeeded

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-04-23 10:37:50 +01:00
parent b9d457578c
commit 2078c4b83e
2 changed files with 34 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ final class HandleStripeWebhook
'customer.subscription.created',
'customer.subscription.updated' => $this->bustPlanCache($user),
'customer.subscription.deleted' => $this->handleSubscriptionDeleted($user),
'invoice.payment_succeeded' => $this->handlePaymentSucceeded($user),
default => null,
};
}
@@ -44,6 +45,12 @@ final class HandleStripeWebhook
$this->bustPlanCache($user);
}
private function handlePaymentSucceeded(User $user): void
{
$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}");

View File

@@ -67,3 +67,30 @@ it('on customer.subscription.deleted disables whatsapp+sms prefs, clears grace,
expect($user->fresh()->grace_period_until)->toBeNull();
expect(Cache::tags(['plans'])->get("plan_for_user_{$user->id}"))->toBeNull();
});
it('on invoice.payment_succeeded clears grace_period_until and busts cache', function (): void {
$user = User::factory()->create([
'stripe_id' => 'cus_paid_1',
'grace_period_until' => now()->addDays(4),
]);
Cache::tags(['plans'])->put("plan_for_user_{$user->id}", 'stale', 3600);
(new HandleStripeWebhook)->handle(new WebhookReceived([
'type' => 'invoice.payment_succeeded',
'data' => ['object' => ['customer' => 'cus_paid_1']],
]));
expect($user->fresh()->grace_period_until)->toBeNull();
expect(Cache::tags(['plans'])->get("plan_for_user_{$user->id}"))->toBeNull();
});
it('invoice.payment_succeeded is a no-op when grace was never set', function (): void {
$user = User::factory()->create(['stripe_id' => 'cus_paid_2', 'grace_period_until' => null]);
(new HandleStripeWebhook)->handle(new WebhookReceived([
'type' => 'invoice.payment_succeeded',
'data' => ['object' => ['customer' => 'cus_paid_2']],
]));
expect($user->fresh()->grace_period_until)->toBeNull();
});