134 lines
4.5 KiB
PHP
134 lines
4.5 KiB
PHP
<?php
|
|
|
|
use App\Models\Organization;
|
|
use App\Models\Setting;
|
|
use Flux\Flux;
|
|
use Illuminate\Support\Collection;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
|
|
new #[Title('Plan pricing')] class extends Component
|
|
{
|
|
public function mount(): void
|
|
{
|
|
if (! auth()->user()->isAdmin()) {
|
|
abort(403);
|
|
}
|
|
|
|
$this->amounts = $this->plans()
|
|
->mapWithKeys(fn (array $plan): array => [
|
|
$plan['key'] => self::formatMajorUnits(Organization::planAmount($plan['key'])),
|
|
])
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* The price of each configured plan, keyed by plan, in major units so that
|
|
* it can be edited and shown as an administrator typed it.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
public array $amounts = [];
|
|
|
|
/**
|
|
* Get every configured plan, including any that cannot be sold yet, so a
|
|
* price can be set before the Stripe price is in place.
|
|
*
|
|
* @return Collection<string, array{key: string, name: string, interval: string, purchasable: bool}>
|
|
*/
|
|
#[Computed]
|
|
public function plans(): Collection
|
|
{
|
|
$configured = config('plans.plans');
|
|
$available = Organization::availablePlans();
|
|
|
|
if (! is_array($configured)) {
|
|
return collect();
|
|
}
|
|
|
|
return collect($configured)->map(function ($plan, string $key) use ($available): array {
|
|
$plan = is_array($plan) ? $plan : [];
|
|
|
|
return [
|
|
'key' => $key,
|
|
'name' => is_string($plan['name'] ?? null) ? $plan['name'] : $key,
|
|
'interval' => is_string($plan['interval'] ?? null) ? $plan['interval'] : 'month',
|
|
'purchasable' => array_key_exists($key, $available),
|
|
];
|
|
});
|
|
}
|
|
|
|
public function saveAmounts(): void
|
|
{
|
|
$validated = $this->validate([
|
|
'amounts' => ['required', 'array'],
|
|
'amounts.*' => ['required', 'numeric', 'min:0', 'decimal:0,2'],
|
|
]);
|
|
|
|
foreach ($validated['amounts'] as $key => $amount) {
|
|
Setting::put(Setting::planAmountKey($key), (string) Setting::toMinorUnits((string) $amount));
|
|
}
|
|
|
|
unset($this->plans);
|
|
|
|
Flux::toast(variant: 'success', text: __('Plan pricing updated.'));
|
|
}
|
|
|
|
/**
|
|
* Render a stored amount, in minor units, as the major units an
|
|
* administrator edits it in.
|
|
*/
|
|
protected static function formatMajorUnits(int $amount): string
|
|
{
|
|
return intdiv($amount, 100).'.'.str_pad((string) ($amount % 100), 2, '0', STR_PAD_LEFT);
|
|
}
|
|
}; ?>
|
|
|
|
<div class="flex h-full w-full flex-1 flex-col gap-6">
|
|
<div>
|
|
<flux:heading size="xl">{{ __('Plan pricing') }}</flux:heading>
|
|
<flux:text class="mt-1">{{ __('Set the price shown for each plan on the pricing page and at signup.') }}</flux:text>
|
|
</div>
|
|
|
|
@if ($this->plans->isEmpty())
|
|
<flux:callout variant="warning">{{ __('No plans are configured.') }}</flux:callout>
|
|
@else
|
|
<form wire:submit="saveAmounts" class="flex max-w-2xl flex-col gap-6">
|
|
<flux:card class="flex flex-col gap-5">
|
|
@foreach ($this->plans as $plan)
|
|
<flux:field>
|
|
<flux:label>{{ $plan['name'] }}</flux:label>
|
|
|
|
<flux:input
|
|
wire:model="amounts.{{ $plan['key'] }}"
|
|
:value="$amounts[$plan['key']] ?? null"
|
|
type="number"
|
|
step="0.01"
|
|
min="0"
|
|
inputmode="decimal"
|
|
data-test="plan-amount-input-{{ $plan['key'] }}"
|
|
/>
|
|
|
|
<flux:text size="sm" class="text-zinc-500 dark:text-zinc-400">
|
|
{{ __('Billed every :interval.', ['interval' => $plan['interval']]) }}
|
|
|
|
@unless ($plan['purchasable'])
|
|
{{ __('This plan is not on sale yet, because it has no Stripe price configured.') }}
|
|
@endunless
|
|
</flux:text>
|
|
|
|
<flux:error name="amounts.{{ $plan['key'] }}" />
|
|
</flux:field>
|
|
@endforeach
|
|
</flux:card>
|
|
|
|
<div class="flex justify-end">
|
|
<flux:button type="submit" variant="primary" data-test="save-plan-amounts-button">
|
|
{{ __('Save') }}
|
|
</flux:button>
|
|
</div>
|
|
</form>
|
|
@endif
|
|
</div>
|