feat: handle invoice.payment_failed — set grace period and queue reminders

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-04-23 10:56:26 +01:00
parent 5b17f4cae4
commit b7175169f0
2 changed files with 63 additions and 0 deletions

View File

@@ -1,10 +1,12 @@
<?php
use App\Jobs\SendPaymentFailedReminderJob;
use App\Listeners\HandleStripeWebhook;
use App\Models\User;
use App\Models\UserNotificationPreference;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Queue;
use Laravel\Cashier\Events\WebhookReceived;
uses(RefreshDatabase::class);
@@ -94,3 +96,44 @@ it('invoice.payment_succeeded is a no-op when grace was never set', function ():
expect($user->fresh()->grace_period_until)->toBeNull();
});
it('on invoice.payment_failed sets grace_period_until 5 days out and queues both reminders', function (): void {
Queue::fake();
$user = User::factory()->create(['stripe_id' => 'cus_failed_1', 'grace_period_until' => null]);
$before = now();
(new HandleStripeWebhook)->handle(new WebhookReceived([
'type' => 'invoice.payment_failed',
'data' => ['object' => ['customer' => 'cus_failed_1']],
]));
$grace = $user->fresh()->grace_period_until;
expect($grace)->not->toBeNull();
expect($grace->greaterThanOrEqualTo($before->copy()->addDays(5)->subSecond()))->toBeTrue();
expect($grace->lessThanOrEqualTo($before->copy()->addDays(5)->addSeconds(5)))->toBeTrue();
Queue::assertPushed(SendPaymentFailedReminderJob::class, function ($job) use ($user) {
return $job->userId === $user->id && $job->day === 3;
});
Queue::assertPushed(SendPaymentFailedReminderJob::class, function ($job) use ($user) {
return $job->userId === $user->id && $job->day === 5;
});
Queue::assertPushed(SendPaymentFailedReminderJob::class, 2);
});
it('repeat invoice.payment_failed within grace does not re-dispatch reminders', function (): void {
Queue::fake();
$existingGrace = now()->addDays(3)->startOfSecond();
$user = User::factory()->create(['stripe_id' => 'cus_failed_2', 'grace_period_until' => $existingGrace]);
(new HandleStripeWebhook)->handle(new WebhookReceived([
'type' => 'invoice.payment_failed',
'data' => ['object' => ['customer' => 'cus_failed_2']],
]));
// grace_period_until unchanged (same value)
expect($user->fresh()->grace_period_until->equalTo($existingGrace))->toBeTrue();
// No new jobs queued
Queue::assertNothingPushed();
});