Feat: Creating PaymentMethods, Setting Fees, Availabilities
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Payment\Filament\Resources;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Columns\ToggleColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Modules\Core\Payment\Filament\Resources\PaymentMethodResource\Pages\ListPaymentMethods;
|
||||
use Modules\Core\Payment\Models\PaymentMethod;
|
||||
|
||||
/**
|
||||
* One row per payment type key (config('lunar.payments.types')), seeded by
|
||||
* InstallLunarCommand — never created/deleted here, only edited. `enabled`
|
||||
* toggles inline; `data.fee` (currently the only type-specific setting, for
|
||||
* cash-on-delivery's flat surcharge — see ApplyCashOnDeliveryFee) is edited
|
||||
* via a modal action rather than a dedicated form field, since not every
|
||||
* type has the same data keys.
|
||||
*/
|
||||
class PaymentMethodResource extends Resource
|
||||
{
|
||||
protected static ?string $model = PaymentMethod::class;
|
||||
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-credit-card';
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $modelLabel = 'Payment Method';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Payment Methods';
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('type')
|
||||
->label('Type'),
|
||||
ToggleColumn::make('enabled')
|
||||
->label('Enabled'),
|
||||
TextColumn::make('data.fee')
|
||||
->label('Fee')
|
||||
->formatStateUsing(fn (?int $state) => $state
|
||||
? number_format($state / 100, 2)
|
||||
: '—'),
|
||||
TextColumn::make('updated_at')
|
||||
->label('Last updated')
|
||||
->dateTime(),
|
||||
])
|
||||
->recordActions([
|
||||
static::editFeeAction(),
|
||||
])
|
||||
->defaultSort('type');
|
||||
}
|
||||
|
||||
/**
|
||||
* $data['fee'] is stored as an integer minor unit (cents), matching
|
||||
* Lunar's own Price convention everywhere else in this codebase — the
|
||||
* form collects/displays a decimal and converts at the boundary.
|
||||
*/
|
||||
private static function editFeeAction(): Action
|
||||
{
|
||||
return Action::make('edit_fee')
|
||||
->label('Edit fee')
|
||||
->icon('heroicon-o-pencil')
|
||||
->schema([
|
||||
TextInput::make('fee')
|
||||
->label('Fee')
|
||||
->numeric()
|
||||
->minValue(0)
|
||||
->step(0.01)
|
||||
->helperText('Flat surcharge added when this payment method is selected.'),
|
||||
])
|
||||
->fillForm(fn (PaymentMethod $record) => [
|
||||
'fee' => filled($record->data['fee'] ?? null) ? $record->data['fee'] / 100 : null,
|
||||
])
|
||||
->action(function (PaymentMethod $record, array $data) {
|
||||
$record->update([
|
||||
'data' => [
|
||||
...$record->data->toArray(),
|
||||
'fee' => filled($data['fee']) ? (int) round($data['fee'] * 100) : null,
|
||||
],
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListPaymentMethods::route('/'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function canDelete($record = null): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user