Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Http/Controllers/CustomerLicenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public function index(): View
default => 'No accounts connected',
};

// Total purchases (licenses + plugins)
$totalPurchases = $licenseCount + $pluginLicenseCount;
// Total purchases (licenses + plugins + products)
$productLicenseCount = $user->productLicenses()->count();
$totalPurchases = $licenseCount + $pluginLicenseCount + $productLicenseCount;

return view('customer.dashboard', compact(
'licenseCount',
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/PurchaseHistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,28 @@ public function index(): View
];
});

// Fetch product licenses (e.g. Plugin Dev Kit, Masterclass)
$productLicenses = $user->productLicenses()
->with('product')
->orderBy('purchased_at', 'desc')
->get()
->map(function ($productLicense) {
return [
'type' => 'product',
'name' => $productLicense->product->name ?? 'Product',
'description' => $productLicense->product->description ?? null,
'price' => $productLicense->price_paid,
'currency' => $productLicense->currency,
'purchased_at' => $productLicense->purchased_at,
'expires_at' => null,
'is_active' => true,
'href' => $productLicense->product ? route('products.show', $productLicense->product) : null,
];
});

// Combine and sort by purchased_at descending
$purchases = $licenses->concat($pluginLicenses)
->concat($productLicenses)
->sortByDesc('purchased_at')
->values();

Expand Down
8 changes: 8 additions & 0 deletions resources/views/customer/purchase-history/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<div class="grid size-10 place-items-center rounded-lg bg-blue-100 text-blue-600 dark:bg-blue-900/30 dark:text-blue-400">
<x-heroicon-o-key class="size-5" />
</div>
@elseif($purchase['type'] === 'product')
<div class="grid size-10 place-items-center rounded-lg bg-amber-100 text-amber-600 dark:bg-amber-900/30 dark:text-amber-400">
<x-heroicon-o-shopping-bag class="size-5" />
</div>
@else
<div class="grid size-10 place-items-center rounded-lg bg-purple-100 text-purple-600 dark:bg-purple-900/30 dark:text-purple-400">
<x-heroicon-o-puzzle-piece class="size-5" />
Expand All @@ -63,6 +67,10 @@
<span class="ml-2 inline-flex items-center rounded-full bg-blue-100 px-2.5 py-0.5 text-xs font-medium text-blue-800 dark:bg-blue-900/30 dark:text-blue-400">
License
</span>
@elseif($purchase['type'] === 'product')
<span class="ml-2 inline-flex items-center rounded-full bg-amber-100 px-2.5 py-0.5 text-xs font-medium text-amber-800 dark:bg-amber-900/30 dark:text-amber-400">
Product
</span>
@else
<span class="ml-2 inline-flex items-center rounded-full bg-purple-100 px-2.5 py-0.5 text-xs font-medium text-purple-800 dark:bg-purple-900/30 dark:text-purple-400">
Plugin
Expand Down
122 changes: 122 additions & 0 deletions tests/Feature/PurchaseHistoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Tests\Feature;

use App\Features\ShowAuthButtons;
use App\Models\Product;
use App\Models\ProductLicense;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Pennant\Feature;
use Tests\TestCase;

class PurchaseHistoryTest extends TestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

Feature::define(ShowAuthButtons::class, true);
}

public function test_guest_cannot_view_purchase_history(): void
{
$response = $this->get('/customer/purchase-history');

$response->assertRedirect('/login');
}

public function test_purchase_history_shows_product_licenses(): void
{
$user = User::factory()->create();
$product = Product::factory()->create(['name' => 'Plugin Dev Kit']);

ProductLicense::factory()->create([
'user_id' => $user->id,
'product_id' => $product->id,
'price_paid' => 4900,
'currency' => 'USD',
'purchased_at' => now()->subDay(),
]);

$response = $this->actingAs($user)->get('/customer/purchase-history');

$response->assertStatus(200);
$response->assertSee('Plugin Dev Kit');
$response->assertSee('Product');
$response->assertSee('$49.00');
}

public function test_purchase_history_shows_multiple_product_types(): void
{
$user = User::factory()->create();

$devKit = Product::factory()->create(['name' => 'Plugin Dev Kit']);
$masterclass = Product::factory()->create(['name' => 'The NativePHP Masterclass']);

ProductLicense::factory()->create([
'user_id' => $user->id,
'product_id' => $devKit->id,
'price_paid' => 4900,
'purchased_at' => now()->subDays(2),
]);

ProductLicense::factory()->create([
'user_id' => $user->id,
'product_id' => $masterclass->id,
'price_paid' => 10100,
'purchased_at' => now()->subDay(),
]);

$response = $this->actingAs($user)->get('/customer/purchase-history');

$response->assertStatus(200);
$response->assertSee('Plugin Dev Kit');
$response->assertSee('The NativePHP Masterclass');
}

public function test_dashboard_total_purchases_includes_product_licenses(): void
{
$user = User::factory()->create();
$product = Product::factory()->create();

ProductLicense::factory()->create([
'user_id' => $user->id,
'product_id' => $product->id,
]);

$response = $this->actingAs($user)->get('/dashboard');

$response->assertStatus(200);
$response->assertViewHas('totalPurchases', 1);
}

public function test_purchase_history_shows_empty_state_when_no_purchases(): void
{
$user = User::factory()->create();

$response = $this->actingAs($user)->get('/customer/purchase-history');

$response->assertStatus(200);
$response->assertSee('No purchases yet');
}

public function test_product_purchases_show_as_active(): void
{
$user = User::factory()->create();
$product = Product::factory()->create(['name' => 'Plugin Dev Kit']);

ProductLicense::factory()->create([
'user_id' => $user->id,
'product_id' => $product->id,
'purchased_at' => now(),
]);

$response = $this->actingAs($user)->get('/customer/purchase-history');

$response->assertStatus(200);
$response->assertSee('Active');
}
}