implemented event package addons with filament resource, event-admin purchase path and notifications, showing up in purchase history
This commit is contained in:
102
tests/Feature/Api/Tenant/BillingAddonHistoryTest.php
Normal file
102
tests/Feature/Api/Tenant/BillingAddonHistoryTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\Tenant;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\EventPackageAddon;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use Tests\Feature\Tenant\TenantTestCase;
|
||||
|
||||
class BillingAddonHistoryTest extends TenantTestCase
|
||||
{
|
||||
public function test_tenant_can_list_addon_history(): void
|
||||
{
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 500,
|
||||
'max_guests' => 200,
|
||||
'gallery_days' => 60,
|
||||
]);
|
||||
|
||||
$event = Event::factory()->for($this->tenant)->create([
|
||||
'name' => ['de' => 'Gala', 'en' => 'Gala'],
|
||||
]);
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now()->subMonth(),
|
||||
'used_photos' => 0,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(30),
|
||||
]);
|
||||
|
||||
$firstAddon = EventPackageAddon::create([
|
||||
'event_package_id' => $eventPackage->id,
|
||||
'event_id' => $event->id,
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'addon_key' => 'extra_guests_50',
|
||||
'quantity' => 1,
|
||||
'extra_guests' => 50,
|
||||
'status' => 'completed',
|
||||
'amount' => 99.00,
|
||||
'currency' => 'EUR',
|
||||
'metadata' => ['label' => '+50 Gäste'],
|
||||
'purchased_at' => now()->subDay(),
|
||||
'receipt_payload' => ['receipt_url' => 'https://receipt.example/first'],
|
||||
]);
|
||||
|
||||
$secondAddon = EventPackageAddon::create([
|
||||
'event_package_id' => $eventPackage->id,
|
||||
'event_id' => $event->id,
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'addon_key' => 'extra_photos_200',
|
||||
'quantity' => 2,
|
||||
'extra_photos' => 200,
|
||||
'extra_guests' => 0,
|
||||
'status' => 'pending',
|
||||
'amount' => 149.00,
|
||||
'currency' => 'EUR',
|
||||
'metadata' => ['label' => '+200 Fotos'],
|
||||
'purchased_at' => now(),
|
||||
'receipt_payload' => ['receipt_url' => 'https://receipt.example/second'],
|
||||
]);
|
||||
|
||||
$otherTenant = Tenant::factory()->create();
|
||||
$otherEvent = Event::factory()->for($otherTenant)->create();
|
||||
$otherPackage = EventPackage::create([
|
||||
'event_id' => $otherEvent->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now()->subWeek(),
|
||||
'used_photos' => 0,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(30),
|
||||
]);
|
||||
|
||||
EventPackageAddon::create([
|
||||
'event_package_id' => $otherPackage->id,
|
||||
'event_id' => $otherEvent->id,
|
||||
'tenant_id' => $otherTenant->id,
|
||||
'addon_key' => 'extra_guests_999',
|
||||
'quantity' => 1,
|
||||
'extra_guests' => 999,
|
||||
'status' => 'completed',
|
||||
'amount' => 100.00,
|
||||
'currency' => 'EUR',
|
||||
'purchased_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->authenticatedRequest('GET', '/api/v1/tenant/billing/addons');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(2, 'data');
|
||||
$response->assertJsonPath('meta.total', 2);
|
||||
$response->assertJsonPath('data.0.id', $secondAddon->id);
|
||||
$response->assertJsonPath('data.0.receipt_url', 'https://receipt.example/second');
|
||||
$response->assertJsonPath('data.0.event.slug', $event->slug);
|
||||
$response->assertJsonPath('data.1.id', $firstAddon->id);
|
||||
}
|
||||
}
|
||||
52
tests/Feature/Api/Tenant/EventAddonsSummaryTest.php
Normal file
52
tests/Feature/Api/Tenant/EventAddonsSummaryTest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\Tenant;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\EventPackageAddon;
|
||||
use App\Models\Package;
|
||||
use Tests\Feature\Tenant\TenantTestCase;
|
||||
|
||||
class EventAddonsSummaryTest extends TenantTestCase
|
||||
{
|
||||
public function test_event_resource_includes_addons(): void
|
||||
{
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_guests' => 50,
|
||||
'max_photos' => 100,
|
||||
]);
|
||||
|
||||
$event = Event::factory()->for($this->tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 0,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(30),
|
||||
'extra_guests' => 20,
|
||||
]);
|
||||
|
||||
EventPackageAddon::create([
|
||||
'event_package_id' => $eventPackage->id,
|
||||
'event_id' => $event->id,
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'addon_key' => 'extra_guests_100',
|
||||
'quantity' => 1,
|
||||
'extra_guests' => 100,
|
||||
'status' => 'completed',
|
||||
'purchased_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->authenticatedRequest('GET', "/api/v1/tenant/events/{$event->slug}");
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.addons.0.key', 'extra_guests_100');
|
||||
$response->assertJsonPath('data.addons.0.extra_guests', 100);
|
||||
}
|
||||
}
|
||||
81
tests/Feature/Tenant/EventAddonCheckoutTest.php
Normal file
81
tests/Feature/Tenant/EventAddonCheckoutTest.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\EventPackageAddon;
|
||||
use App\Models\Package;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class EventAddonCheckoutTest extends TenantTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Config::set('package-addons.extra_photos_small', [
|
||||
'label' => 'Extra photos (500)',
|
||||
'price_id' => 'pri_addon_photos',
|
||||
'increments' => ['extra_photos' => 500],
|
||||
]);
|
||||
|
||||
Config::set('paddle.api_key', 'test_key');
|
||||
Config::set('paddle.base_url', 'https://paddle.test');
|
||||
Config::set('paddle.environment', 'sandbox');
|
||||
|
||||
// Fake Paddle response
|
||||
Http::fake([
|
||||
'*/checkout/links' => Http::response([
|
||||
'data' => [
|
||||
'url' => 'https://checkout.paddle.test/abcd',
|
||||
'id' => 'chk_addon_123',
|
||||
'expires_at' => now()->addHour()->toIso8601String(),
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_checkout_creates_pending_addon_record(): void
|
||||
{
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 100,
|
||||
'max_guests' => 50,
|
||||
'gallery_days' => 7,
|
||||
]);
|
||||
|
||||
$event = Event::factory()->for($this->tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 0,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(7),
|
||||
]);
|
||||
|
||||
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/addons/checkout", [
|
||||
'addon_key' => 'extra_photos_small',
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('checkout_id', 'chk_addon_123');
|
||||
|
||||
$this->assertDatabaseHas('event_package_addons', [
|
||||
'event_package_id' => $eventPackage->id,
|
||||
'addon_key' => 'extra_photos_small',
|
||||
'status' => 'pending',
|
||||
'quantity' => 2,
|
||||
'checkout_id' => 'chk_addon_123',
|
||||
]);
|
||||
|
||||
$addon = EventPackageAddon::where('event_package_id', $eventPackage->id)->latest()->first();
|
||||
$this->assertSame(1000, $addon->extra_photos); // increments * quantity
|
||||
}
|
||||
}
|
||||
78
tests/Feature/Tenant/EventAddonControllerTest.php
Normal file
78
tests/Feature/Tenant/EventAddonControllerTest.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\Package;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class EventAddonControllerTest extends TenantTestCase
|
||||
{
|
||||
public function test_tenant_admin_can_apply_addons(): void
|
||||
{
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 100,
|
||||
'max_guests' => 50,
|
||||
'gallery_days' => 7,
|
||||
]);
|
||||
|
||||
$event = Event::factory()->for($this->tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now()->subDay(),
|
||||
'used_photos' => 10,
|
||||
'used_guests' => 5,
|
||||
'gallery_expires_at' => Carbon::now()->addDays(7),
|
||||
]);
|
||||
|
||||
$originalExpiry = $eventPackage->gallery_expires_at->copy();
|
||||
|
||||
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/addons/apply", [
|
||||
'extra_photos' => 50,
|
||||
'extra_guests' => 25,
|
||||
'extend_gallery_days' => 3,
|
||||
'reason' => 'Manual boost for event',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.limits.photos.limit', 150);
|
||||
$response->assertJsonPath('data.limits.guests.limit', 75);
|
||||
|
||||
$eventPackage->refresh();
|
||||
|
||||
$this->assertSame(50, $eventPackage->extra_photos);
|
||||
$this->assertSame(25, $eventPackage->extra_guests);
|
||||
$this->assertSame(3, $eventPackage->extra_gallery_days);
|
||||
$this->assertTrue($eventPackage->gallery_expires_at->isSameDay($originalExpiry->addDays(3)));
|
||||
}
|
||||
|
||||
public function test_validation_fails_when_no_addons_provided(): void
|
||||
{
|
||||
$package = Package::factory()->endcustomer()->create();
|
||||
|
||||
$event = Event::factory()->for($this->tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 0,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(7),
|
||||
]);
|
||||
|
||||
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/addons/apply", []);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors('addons');
|
||||
}
|
||||
}
|
||||
83
tests/Feature/Tenant/EventAddonWebhookTest.php
Normal file
83
tests/Feature/Tenant/EventAddonWebhookTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\EventPackageAddon;
|
||||
use App\Models\Package;
|
||||
use App\Notifications\Addons\AddonPurchaseReceipt;
|
||||
use App\Services\Addons\EventAddonWebhookService;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
class EventAddonWebhookTest extends TenantTestCase
|
||||
{
|
||||
public function test_webhook_applies_addon_and_marks_completed(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
Config::set('package-addons.extra_guests', [
|
||||
'label' => 'Guests 100',
|
||||
'increments' => ['extra_guests' => 100],
|
||||
'price_id' => 'pri_guests',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_guests' => 50,
|
||||
]);
|
||||
|
||||
$event = Event::factory()->for($this->tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 0,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(7),
|
||||
]);
|
||||
|
||||
$addon = EventPackageAddon::create([
|
||||
'event_package_id' => $eventPackage->id,
|
||||
'event_id' => $event->id,
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'addon_key' => 'extra_guests',
|
||||
'quantity' => 1,
|
||||
'extra_guests' => 100,
|
||||
'status' => 'pending',
|
||||
'metadata' => [
|
||||
'addon_intent' => 'intent-123',
|
||||
],
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'transaction.completed',
|
||||
'data' => [
|
||||
'id' => 'txn_addon_1',
|
||||
'metadata' => [
|
||||
'addon_intent' => 'intent-123',
|
||||
'addon_key' => 'extra_guests',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$handler = app(EventAddonWebhookService::class);
|
||||
|
||||
$handled = $handler->handle($payload);
|
||||
|
||||
$this->assertTrue($handled);
|
||||
|
||||
$addon->refresh();
|
||||
$eventPackage->refresh();
|
||||
|
||||
$this->assertSame('completed', $addon->status);
|
||||
$this->assertSame('txn_addon_1', $addon->transaction_id);
|
||||
$this->assertSame(100, $eventPackage->extra_guests);
|
||||
|
||||
Notification::assertSentTimes(AddonPurchaseReceipt::class, 1);
|
||||
}
|
||||
}
|
||||
42
tests/Unit/Jobs/SyncPackageAddonToPaddleTest.php
Normal file
42
tests/Unit/Jobs/SyncPackageAddonToPaddleTest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Jobs\SyncPackageAddonToPaddle;
|
||||
use App\Models\PackageAddon;
|
||||
use App\Services\Paddle\PaddleAddonCatalogService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SyncPackageAddonToPaddleTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_creates_and_updates_price_and_product(): void
|
||||
{
|
||||
$addon = PackageAddon::create([
|
||||
'key' => 'extra_photos_500',
|
||||
'label' => '+500 Fotos',
|
||||
'extra_photos' => 500,
|
||||
'metadata' => ['price_eur' => 5],
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaddleAddonCatalogService::class);
|
||||
$service->shouldReceive('createProduct')
|
||||
->once()
|
||||
->andReturn(['id' => 'pro_addon_1']);
|
||||
$service->shouldReceive('createPrice')
|
||||
->once()
|
||||
->andReturn(['id' => 'pri_addon_1']);
|
||||
|
||||
$job = new SyncPackageAddonToPaddle($addon->id);
|
||||
$job->handle($service);
|
||||
|
||||
$addon->refresh();
|
||||
|
||||
$this->assertSame('pri_addon_1', $addon->price_id);
|
||||
$this->assertEquals('pro_addon_1', $addon->metadata['paddle_product_id']);
|
||||
$this->assertEquals('synced', $addon->metadata['paddle_sync_status']);
|
||||
}
|
||||
}
|
||||
43
tests/Unit/Services/EventAddonCatalogTest.php
Normal file
43
tests/Unit/Services/EventAddonCatalogTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services;
|
||||
|
||||
use App\Models\PackageAddon;
|
||||
use App\Services\Addons\EventAddonCatalog;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EventAddonCatalogTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_prefers_database_addons_over_config(): void
|
||||
{
|
||||
Config::set('package-addons', [
|
||||
'extra_photos_small' => [
|
||||
'label' => 'Config Photos',
|
||||
'price_id' => 'pri_config',
|
||||
'increments' => ['extra_photos' => 100],
|
||||
],
|
||||
]);
|
||||
|
||||
PackageAddon::create([
|
||||
'key' => 'extra_photos_small',
|
||||
'label' => 'DB Photos',
|
||||
'price_id' => 'pri_db',
|
||||
'extra_photos' => 200,
|
||||
'active' => true,
|
||||
'sort' => 1,
|
||||
]);
|
||||
|
||||
$catalog = $this->app->make(EventAddonCatalog::class);
|
||||
|
||||
$addon = $catalog->find('extra_photos_small');
|
||||
|
||||
$this->assertNotNull($addon);
|
||||
$this->assertSame('DB Photos', $addon['label']);
|
||||
$this->assertSame('pri_db', $addon['price_id']);
|
||||
$this->assertSame(200, $addon['increments']['extra_photos']);
|
||||
}
|
||||
}
|
||||
@@ -165,4 +165,40 @@ class PackageLimitEvaluatorTest extends TestCase
|
||||
$this->assertTrue($summary['can_upload_photos']);
|
||||
$this->assertTrue($summary['can_add_guests']);
|
||||
}
|
||||
|
||||
public function test_assess_photo_upload_respects_extra_limits(): void
|
||||
{
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 5,
|
||||
]);
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
$event = Event::factory()
|
||||
->for($tenant)
|
||||
->create();
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 5,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(14),
|
||||
'extra_photos' => 5,
|
||||
])->fresh(['package']);
|
||||
|
||||
$violation = $this->evaluator->assessPhotoUpload($tenant->fresh(), $event->id);
|
||||
|
||||
$this->assertNull($violation, 'Upload should be allowed within extra photo allowance');
|
||||
|
||||
$eventPackage->update(['used_photos' => 10]);
|
||||
|
||||
$violation = $this->evaluator->assessPhotoUpload($tenant->fresh(), $event->id);
|
||||
|
||||
$this->assertNotNull($violation, 'Upload should be blocked after exceeding base + extras');
|
||||
$this->assertSame('photo_limit_exceeded', $violation['code']);
|
||||
$this->assertSame(0, $violation['meta']['remaining']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,4 +145,41 @@ class PackageUsageTrackerTest extends TestCase
|
||||
|
||||
EventFacade::assertDispatched(EventPackageGuestLimitReached::class);
|
||||
}
|
||||
|
||||
public function test_effective_limits_include_extras(): void
|
||||
{
|
||||
EventFacade::fake([
|
||||
EventPackagePhotoLimitReached::class,
|
||||
]);
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 2,
|
||||
]);
|
||||
$event = Event::factory()->for($tenant)->create();
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 2,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(7),
|
||||
'extra_photos' => 2,
|
||||
])->fresh(['package']);
|
||||
|
||||
/** @var PackageUsageTracker $tracker */
|
||||
$tracker = app(PackageUsageTracker::class);
|
||||
|
||||
// Base limit reached but extras still available; no limit event expected yet.
|
||||
$tracker->recordPhotoUsage($eventPackage, 1, 1);
|
||||
EventFacade::assertNotDispatched(EventPackagePhotoLimitReached::class);
|
||||
|
||||
// Now consume extras and hit the effective limit.
|
||||
$eventPackage->used_photos = 4;
|
||||
$tracker->recordPhotoUsage($eventPackage, 3, 1);
|
||||
|
||||
EventFacade::assertDispatched(EventPackagePhotoLimitReached::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user