Harden credit flows and add RevenueCat webhook

This commit is contained in:
2025-09-25 14:05:58 +02:00
parent 9248d7a3f5
commit 215d19f07e
18 changed files with 804 additions and 190 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace Tests\Unit;
use App\Jobs\ProcessRevenueCatWebhook;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class ProcessRevenueCatWebhookTest extends TestCase
{
use RefreshDatabase;
public function test_job_creates_purchase_and_updates_balance(): void
{
config()->set('services.revenuecat.product_mappings', 'pro_month:5');
$tenant = Tenant::factory()->create([
'event_credits_balance' => 1,
'subscription_tier' => 'free',
]);
$expiresAt = Carbon::now()->addDays(30)->setTimezone('UTC')->floorSecond();
$payload = [
'event' => [
'app_user_id' => 'tenant:' . $tenant->id,
'product_id' => 'pro_month',
'transaction_id' => 'txn-test-1',
'price' => 19.99,
'currency' => 'eur',
'expiration_at_ms' => (int) ($expiresAt->valueOf()),
],
];
$job = new ProcessRevenueCatWebhook($payload, 'evt-test-1');
$job->handle();
$tenant->refresh();
$this->assertSame(6, $tenant->event_credits_balance);
$this->assertSame('pro', $tenant->subscription_tier);
$this->assertNotNull($tenant->subscription_expires_at);
$this->assertSame($expiresAt->timestamp, $tenant->subscription_expires_at->timestamp);
$this->assertDatabaseHas('event_purchases', [
'tenant_id' => $tenant->id,
'provider' => 'revenuecat',
'external_receipt_id' => 'txn-test-1',
'events_purchased' => 5,
]);
$this->assertDatabaseHas('event_credits_ledger', [
'tenant_id' => $tenant->id,
'delta' => 5,
'reason' => 'purchase',
]);
$duplicateJob = new ProcessRevenueCatWebhook($payload, 'evt-test-1');
$duplicateJob->handle();
$this->assertSame(6, $tenant->fresh()->event_credits_balance);
}
}