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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user