feat: product pricing plans
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
a3894d2f37
commit
768c814762
@@ -3,12 +3,14 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\PricingPlan;
|
||||
use App\Models\Product;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Laravel\Pennant\Feature;
|
||||
|
||||
class ProductController extends Controller
|
||||
@@ -227,6 +229,11 @@ class ProductController extends Controller
|
||||
'screenshots.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'remove_screenshots' => 'nullable|array',
|
||||
'remove_screenshots.*' => 'integer',
|
||||
'plans' => 'nullable|array',
|
||||
'plans.*.name' => 'required|string|max:100',
|
||||
'plans.*.price' => 'required|numeric|min:0|max:99999',
|
||||
'plans.*.period' => ['required', Rule::in(PricingPlan::PERIODS)],
|
||||
'plans.*.description' => 'nullable|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -235,6 +242,18 @@ class ProductController extends Controller
|
||||
*/
|
||||
protected function syncDetails(Request $request, Product $product): void
|
||||
{
|
||||
// The submitted plan list replaces all existing plans
|
||||
$product->pricingPlans()->delete();
|
||||
foreach (array_values((array) $request->input('plans', [])) as $i => $plan) {
|
||||
$product->pricingPlans()->create([
|
||||
'name' => $plan['name'],
|
||||
'price' => $plan['price'],
|
||||
'period' => $plan['period'],
|
||||
'description' => $plan['description'] ?? null,
|
||||
'position' => $i,
|
||||
]);
|
||||
}
|
||||
|
||||
// Only rows belonging to this product may be removed
|
||||
$ids = array_filter((array) $request->input('remove_screenshots', []));
|
||||
if ($ids) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PricingPlan extends Model
|
||||
{
|
||||
public const PERIODS = ['monthly', 'yearly', 'one-time'];
|
||||
|
||||
protected $fillable = ['product_id', 'name', 'price', 'period', 'description', 'position'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['price' => 'decimal:2'];
|
||||
}
|
||||
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Price formatted for display, e.g. "€9.99" ("€0" for free plans).
|
||||
*/
|
||||
public function formattedPrice(): string
|
||||
{
|
||||
return (float) $this->price == 0 ? '€0' : '€'.number_format((float) $this->price, 2);
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,14 @@ class Product extends Model
|
||||
return $this->hasMany(ProductScreenshot::class)->orderBy('position')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product's pricing plans in display order.
|
||||
*/
|
||||
public function pricingPlans(): HasMany
|
||||
{
|
||||
return $this->hasMany(PricingPlan::class)->orderBy('position')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user that owns the product.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('pricing_plans', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('name', 100);
|
||||
$table->decimal('price', 8, 2);
|
||||
$table->string('period');
|
||||
$table->string('description')->nullable();
|
||||
$table->unsignedInteger('position')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('pricing_plans');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
@props(['plans' => []])
|
||||
|
||||
@php
|
||||
$initial = collect(old('plans', $plans))->map(fn ($p) => [
|
||||
'name' => $p['name'] ?? '',
|
||||
'price' => $p['price'] ?? '',
|
||||
'period' => $p['period'] ?? 'monthly',
|
||||
'description' => $p['description'] ?? '',
|
||||
])->values()->all();
|
||||
$input = 'block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 sm:text-sm';
|
||||
@endphp
|
||||
|
||||
<!-- Pricing plans -->
|
||||
<div x-data="{ plans: @js($initial) }">
|
||||
<label class="block text-sm font-medium text-gray-700">Pricing plans</label>
|
||||
<div class="mt-2 space-y-3">
|
||||
<template x-for="(plan, i) in plans" :key="i">
|
||||
<div class="grid grid-cols-1 sm:grid-cols-12 gap-2 p-3 border border-gray-200 rounded-md">
|
||||
<input type="text" :name="`plans[${i}][name]`" x-model="plan.name" placeholder="Name" maxlength="100" class="{{ $input }} sm:col-span-3">
|
||||
<input type="number" :name="`plans[${i}][price]`" x-model="plan.price" placeholder="Price (EUR)" min="0" max="99999" step="0.01" class="{{ $input }} sm:col-span-2">
|
||||
<select :name="`plans[${i}][period]`" x-model="plan.period" class="{{ $input }} sm:col-span-2">
|
||||
@foreach(\App\Models\PricingPlan::PERIODS as $period)
|
||||
<option value="{{ $period }}">{{ $period }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<input type="text" :name="`plans[${i}][description]`" x-model="plan.description" placeholder="Description (optional)" maxlength="255" class="{{ $input }} sm:col-span-4">
|
||||
<button type="button" @click="plans.splice(i, 1)" class="sm:col-span-1 text-sm text-red-600 hover:text-red-800">Remove</button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
@foreach($errors->get('plans.*') as $messages)
|
||||
<p class="mt-2 text-sm text-red-600">{{ $messages[0] }}</p>
|
||||
@endforeach
|
||||
@error('plans')
|
||||
<p class="mt-2 text-sm text-red-600">{{ $message }}</p>
|
||||
@enderror
|
||||
<button type="button" @click="plans.push({ name: '', price: '', period: 'monthly', description: '' })"
|
||||
class="mt-3 inline-flex items-center px-3 py-1.5 border border-gray-300 rounded-md text-sm text-gray-700 hover:bg-gray-50">Add plan</button>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
@props(['product'])
|
||||
|
||||
@php($plans = $product->pricingPlans)
|
||||
|
||||
@if($plans->isNotEmpty())
|
||||
<div data-pricing {{ $attributes->class('bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6 mb-6') }}>
|
||||
<h2 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Pricing</h2>
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
@foreach($plans as $plan)
|
||||
<div class="rounded-lg border border-gray-200 dark:border-gray-700 p-4">
|
||||
<h3 class="font-medium text-gray-900 dark:text-white">{{ $plan->name }}</h3>
|
||||
<p class="mt-2 text-2xl font-bold text-gray-900 dark:text-white">{{ $plan->formattedPrice() }}</p>
|
||||
<p class="text-sm text-gray-500 dark:text-gray-400">{{ $plan->period }}</p>
|
||||
@if($plan->description)
|
||||
<p class="mt-2 text-sm text-gray-600 dark:text-gray-300">{{ $plan->description }}</p>
|
||||
@endif
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
@@ -207,6 +207,8 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<x-product.plans-form />
|
||||
|
||||
<!-- Info Alert -->
|
||||
<div class="rounded-md bg-blue-50 p-4">
|
||||
<div class="flex">
|
||||
|
||||
@@ -251,6 +251,8 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<x-product.plans-form :plans="$product->pricingPlans->toArray()" />
|
||||
|
||||
<!-- Info Alert -->
|
||||
<div class="rounded-md bg-blue-50 p-4">
|
||||
<div class="flex">
|
||||
|
||||
@@ -181,7 +181,7 @@
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<!-- slot:pricing -->
|
||||
<x-product.pricing :product="$product" />
|
||||
|
||||
<!-- slot:alternatives -->
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
use App\Models\PricingPlan;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
function pricingPayload(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'title' => 'Priced Product',
|
||||
'description' => 'A product description',
|
||||
'website_url' => 'https://example.com',
|
||||
'category_id' => makeCategory()->id,
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
test('submitter adds plans', function () {
|
||||
$user = userWithRole('user');
|
||||
|
||||
$this->actingAs($user)->post(route('products.store'), pricingPayload([
|
||||
'plans' => [
|
||||
['name' => 'Free', 'price' => '0', 'period' => 'monthly', 'description' => 'Basics'],
|
||||
['name' => 'Team', 'price' => '9.99', 'period' => 'monthly'],
|
||||
],
|
||||
]))->assertRedirect()->assertSessionHasNoErrors();
|
||||
|
||||
$plans = Product::firstOrFail()->pricingPlans;
|
||||
expect($plans->pluck('name')->all())->toBe(['Free', 'Team']);
|
||||
expect($plans->pluck('position')->all())->toBe([0, 1]);
|
||||
expect((float) $plans[1]->price)->toBe(9.99);
|
||||
});
|
||||
|
||||
test('invalid plans fail validation and save nothing', function (array $plan, string $key) {
|
||||
$user = userWithRole('user');
|
||||
|
||||
$this->actingAs($user)->post(route('products.store'), pricingPayload([
|
||||
'plans' => [array_merge(['name' => 'Pro', 'price' => '5', 'period' => 'yearly'], $plan)],
|
||||
]))->assertSessionHasErrors($key);
|
||||
|
||||
expect(Product::count())->toBe(0);
|
||||
expect(PricingPlan::count())->toBe(0);
|
||||
})->with([
|
||||
'negative price' => [['price' => '-1'], 'plans.0.price'],
|
||||
'non numeric price' => [['price' => 'abc'], 'plans.0.price'],
|
||||
'bad period' => [['period' => 'weekly'], 'plans.0.period'],
|
||||
'missing name' => [['name' => ''], 'plans.0.name'],
|
||||
]);
|
||||
|
||||
test('update replaces the plan list', function () {
|
||||
$user = userWithRole('user');
|
||||
$product = Product::factory()->pending()->create(['user_id' => $user->id]);
|
||||
$product->pricingPlans()->create(['name' => 'Old', 'price' => 1, 'period' => 'monthly', 'position' => 0]);
|
||||
$product->pricingPlans()->create(['name' => 'Older', 'price' => 2, 'period' => 'yearly', 'position' => 1]);
|
||||
|
||||
$this->actingAs($user)->put(route('products.update', $product), pricingPayload([
|
||||
'category_id' => $product->category_id,
|
||||
'plans' => [['name' => 'New', 'price' => '3', 'period' => 'one-time']],
|
||||
]))->assertRedirect();
|
||||
|
||||
expect($product->pricingPlans()->pluck('name')->all())->toBe(['New']);
|
||||
});
|
||||
|
||||
test('show page prints price and period', function () {
|
||||
$product = Product::factory()->approved()->create();
|
||||
$product->pricingPlans()->create(['name' => 'Free', 'price' => 0, 'period' => 'monthly', 'position' => 0]);
|
||||
$product->pricingPlans()->create(['name' => 'Team', 'price' => 9.99, 'period' => 'monthly', 'position' => 1]);
|
||||
|
||||
$this->get(route('products.show', $product))->assertOk()
|
||||
->assertSee('€9.99')->assertSee('monthly')->assertSee('€0');
|
||||
});
|
||||
|
||||
test('no plans, no pricing section', function () {
|
||||
$product = Product::factory()->approved()->create();
|
||||
|
||||
$this->get(route('products.show', $product))->assertOk()->assertDontSee('data-pricing', false);
|
||||
});
|
||||
Reference in New Issue
Block a user