feat: consolidate stripe webhook handling into HandleStripeWebhook listener
This commit is contained in:
36
app/Listeners/HandleStripeWebhook.php
Normal file
36
app/Listeners/HandleStripeWebhook.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
final class HandleStripeWebhook
|
||||
{
|
||||
public function handle(WebhookReceived $event): void
|
||||
{
|
||||
$type = $event->payload['type'] ?? null;
|
||||
$stripeCustomerId = $event->payload['data']['object']['customer'] ?? null;
|
||||
|
||||
if ($stripeCustomerId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = User::where('stripe_id', $stripeCustomerId)->first();
|
||||
|
||||
if ($user === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
match ($type) {
|
||||
'customer.subscription.created' => $this->bustPlanCache($user),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
private function bustPlanCache(User $user): void
|
||||
{
|
||||
Cache::tags(['plans'])->forget("plan_for_user_{$user->id}");
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Listeners\DowngradeUserOnSubscriptionDeleted;
|
||||
use App\Listeners\HandleStripeWebhook;
|
||||
use App\Services\ApiLogger;
|
||||
use App\Services\LlmPrediction\AnthropicPredictionProvider;
|
||||
use App\Services\LlmPrediction\GeminiPredictionProvider;
|
||||
@@ -41,7 +41,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
$this->configureDefaults();
|
||||
|
||||
Event::listen(WebhookReceived::class, DowngradeUserOnSubscriptionDeleted::class);
|
||||
Event::listen(WebhookReceived::class, HandleStripeWebhook::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
30
tests/Feature/Payments/HandleStripeWebhookTest.php
Normal file
30
tests/Feature/Payments/HandleStripeWebhookTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use App\Listeners\HandleStripeWebhook;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('busts the plan cache on customer.subscription.created', function (): void {
|
||||
$user = User::factory()->create(['stripe_id' => 'cus_created_1']);
|
||||
Cache::tags(['plans'])->put("plan_for_user_{$user->id}", 'stale', 3600);
|
||||
|
||||
(new HandleStripeWebhook)->handle(new WebhookReceived([
|
||||
'type' => 'customer.subscription.created',
|
||||
'data' => ['object' => ['customer' => 'cus_created_1']],
|
||||
]));
|
||||
|
||||
expect(Cache::tags(['plans'])->get("plan_for_user_{$user->id}"))->toBeNull();
|
||||
});
|
||||
|
||||
it('ignores subscription.created when the user is not found', function (): void {
|
||||
(new HandleStripeWebhook)->handle(new WebhookReceived([
|
||||
'type' => 'customer.subscription.created',
|
||||
'data' => ['object' => ['customer' => 'cus_unknown']],
|
||||
]));
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user