feat: implement comprehensive multi-provider payment processing system
- Add unified payment provider architecture with contract-based design - Implement 6 payment providers: Stripe, Lemon Squeezy, Polar, Oxapay, Crypto, Activation Keys - Create subscription management with lifecycle handling (create, cancel, pause, resume, update) - Add coupon system with usage tracking and trial extensions - Build Filament admin resources for payment providers, subscriptions, coupons, and trials - Implement payment orchestration service with provider registry and configuration management - Add comprehensive payment logging and webhook handling for all providers - Create customer analytics dashboard with revenue, churn, and lifetime value metrics - Add subscription migration service for provider switching - Include extensive test coverage for all payment functionality
This commit is contained in:
57
app/Filament/Resources/Coupons/CouponResource.php
Normal file
57
app/Filament/Resources/Coupons/CouponResource.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons;
|
||||
|
||||
use App\Filament\Resources\Coupons\Pages\CreateCoupon;
|
||||
use App\Filament\Resources\Coupons\Pages\EditCoupon;
|
||||
use App\Filament\Resources\Coupons\Pages\ListCoupons;
|
||||
use App\Filament\Resources\Coupons\Schemas\CouponForm;
|
||||
use App\Filament\Resources\Coupons\Tables\CouponsTable;
|
||||
use App\Models\Coupon;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class CouponResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Coupon::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedTicket;
|
||||
|
||||
protected static ?string $navigationLabel = 'Coupons';
|
||||
|
||||
protected static ?string $modelLabel = 'Coupon';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Coupons';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return CouponForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return CouponsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListCoupons::route('/'),
|
||||
'create' => CreateCoupon::route('/create'),
|
||||
'edit' => EditCoupon::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
11
app/Filament/Resources/Coupons/Pages/CreateCoupon.php
Normal file
11
app/Filament/Resources/Coupons/Pages/CreateCoupon.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateCoupon extends CreateRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
}
|
||||
19
app/Filament/Resources/Coupons/Pages/EditCoupon.php
Normal file
19
app/Filament/Resources/Coupons/Pages/EditCoupon.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditCoupon extends EditRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Coupons/Pages/ListCoupons.php
Normal file
19
app/Filament/Resources/Coupons/Pages/ListCoupons.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListCoupons extends ListRecords
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
140
app/Filament/Resources/Coupons/Schemas/CouponForm.php
Normal file
140
app/Filament/Resources/Coupons/Schemas/CouponForm.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Schemas;
|
||||
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CouponForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Basic Information')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('code')
|
||||
->label('Coupon Code')
|
||||
->required()
|
||||
->unique(ignoreRecord: true)
|
||||
->formatStateUsing(fn ($state) => Str::upper($state))
|
||||
->helperText('This code will be entered by customers to apply the discount'),
|
||||
|
||||
TextInput::make('name')
|
||||
->label('Display Name')
|
||||
->required()
|
||||
->helperText('Internal name for this coupon'),
|
||||
]),
|
||||
|
||||
Textarea::make('description')
|
||||
->label('Description')
|
||||
->rows(2)
|
||||
->helperText('Optional description for internal reference'),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('type')
|
||||
->label('Discount Type')
|
||||
->options([
|
||||
'percentage' => 'Percentage',
|
||||
'fixed' => 'Fixed Amount',
|
||||
])
|
||||
->required()
|
||||
->reactive()
|
||||
->afterStateUpdated(fn ($state, callable $set) => $set('value', $state === 'percentage' ? 10 : 10.00)
|
||||
),
|
||||
|
||||
TextInput::make('value')
|
||||
->label('Discount Value')
|
||||
->required()
|
||||
->numeric()
|
||||
->step(fn ($get) => $get('type') === 'percentage' ? 1 : 0.01)
|
||||
->suffix(fn ($get) => $get('type') === 'percentage' ? '%' : '$')
|
||||
->helperText(fn ($get) => $get('type') === 'percentage'
|
||||
? 'Percentage discount (e.g., 10 for 10%)'
|
||||
: 'Fixed amount discount in USD'
|
||||
),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Usage Limits')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('max_uses')
|
||||
->label('Maximum Uses')
|
||||
->numeric()
|
||||
->helperText('Leave blank for unlimited uses'),
|
||||
|
||||
TextInput::make('max_uses_per_user')
|
||||
->label('Max Uses Per User')
|
||||
->numeric()
|
||||
->helperText('Limit how many times one user can use this coupon'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('minimum_amount')
|
||||
->label('Minimum Order Amount')
|
||||
->numeric()
|
||||
->step(0.01)
|
||||
->prefix('$')
|
||||
->helperText('Minimum order amount required to use this coupon'),
|
||||
|
||||
TextInput::make('uses_count')
|
||||
->label('Current Uses')
|
||||
->numeric()
|
||||
->default(0)
|
||||
->disabled()
|
||||
->helperText('Number of times this coupon has been used'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Schedule')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('starts_at')
|
||||
->label('Start Date')
|
||||
->helperText('When this coupon becomes active'),
|
||||
|
||||
DateTimePicker::make('expires_at')
|
||||
->label('Expiration Date')
|
||||
->helperText('When this coupon expires'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Settings')
|
||||
->schema([
|
||||
Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true)
|
||||
->helperText('Only active coupons can be used'),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Metadata')
|
||||
->schema([
|
||||
KeyValue::make('metadata')
|
||||
->label('Custom Metadata')
|
||||
->addActionLabel('Add metadata')
|
||||
->keyLabel('Key')
|
||||
->valueLabel('Value')
|
||||
->helperText('Additional key-value data for this coupon'),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
152
app/Filament/Resources/Coupons/Tables/CouponsTable.php
Normal file
152
app/Filament/Resources/Coupons/Tables/CouponsTable.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class CouponsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('code')
|
||||
->label('Code')
|
||||
->searchable()
|
||||
->copyable()
|
||||
->copyMessage('Coupon code copied')
|
||||
->copyMessageDuration(1500),
|
||||
|
||||
TextColumn::make('name')
|
||||
->label('Name')
|
||||
->searchable()
|
||||
->limit(30),
|
||||
|
||||
TextColumn::make('type')
|
||||
->badge()
|
||||
->label('Type')
|
||||
->colors([
|
||||
'blue' => 'percentage',
|
||||
'green' => 'fixed',
|
||||
]),
|
||||
|
||||
TextColumn::make('formatted_discount')
|
||||
->label('Discount')
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('uses_count')
|
||||
->label('Used')
|
||||
->sortable()
|
||||
->alignCenter(),
|
||||
|
||||
TextColumn::make('remaining_uses')
|
||||
->label('Remaining')
|
||||
->getStateUsing(fn ($record) => $record->remaining_uses ?? '∞')
|
||||
->sortable()
|
||||
->alignCenter(),
|
||||
|
||||
IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean()
|
||||
->alignCenter(),
|
||||
|
||||
TextColumn::make('expires_at')
|
||||
->label('Expires')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color(fn ($record): string => $record->isExpiringSoon() ? 'warning' : 'default')
|
||||
->description(fn ($record): string => $record->expires_at ? $record->expires_at->diffForHumans() : ''
|
||||
),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Created')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('type')
|
||||
->options([
|
||||
'percentage' => 'Percentage',
|
||||
'fixed' => 'Fixed Amount',
|
||||
]),
|
||||
|
||||
SelectFilter::make('is_active')
|
||||
->options([
|
||||
'1' => 'Active',
|
||||
'0' => 'Inactive',
|
||||
]),
|
||||
|
||||
SelectFilter::make('status')
|
||||
->label('Status')
|
||||
->options([
|
||||
'valid' => 'Valid',
|
||||
'expired' => 'Expired',
|
||||
'used_up' => 'Used Up',
|
||||
])
|
||||
->query(fn ($query, $data) => match ($data['value']) {
|
||||
'valid' => $query->valid(),
|
||||
'expired' => $query->where('expires_at', '<', now()),
|
||||
'used_up' => $query->whereRaw('uses_count >= max_uses'),
|
||||
default => $query,
|
||||
}),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('duplicate')
|
||||
->label('Duplicate')
|
||||
->icon('heroicon-o-document-duplicate')
|
||||
->color('gray')
|
||||
->action(function ($record) {
|
||||
$newCoupon = $record->replicate();
|
||||
$newCoupon->code = $newCoupon->code.'_COPY';
|
||||
$newCoupon->uses_count = 0;
|
||||
$newCoupon->save();
|
||||
})
|
||||
->successNotificationTitle('Coupon duplicated successfully'),
|
||||
|
||||
Action::make('view_usage')
|
||||
->label('View Usage')
|
||||
->icon('heroicon-o-chart-bar')
|
||||
->color('blue')
|
||||
->url(fn ($record) => route('filament.admin.resources.coupons.usage', $record)),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
|
||||
BulkAction::make('bulk_deactivate')
|
||||
->label('Deactivate')
|
||||
->icon('heroicon-o-x-circle')
|
||||
->color('danger')
|
||||
->action(function (\Illuminate\Support\Collection $records) {
|
||||
$records->each->update(['is_active' => false]);
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
|
||||
BulkAction::make('bulk_activate')
|
||||
->label('Activate')
|
||||
->icon('heroicon-o-check-circle')
|
||||
->color('success')
|
||||
->action(function (\Illuminate\Support\Collection $records) {
|
||||
$records->each->update(['is_active' => true]);
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
])
|
||||
->poll('60s');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\PaymentProviderResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreatePaymentProvider extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PaymentProviderResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\PaymentProviderResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPaymentProvider extends EditRecord
|
||||
{
|
||||
protected static string $resource = PaymentProviderResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\PaymentProviderResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListPaymentProviders extends ListRecords
|
||||
{
|
||||
protected static string $resource = PaymentProviderResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\Pages\CreatePaymentProvider;
|
||||
use App\Filament\Resources\PaymentProviders\Pages\EditPaymentProvider;
|
||||
use App\Filament\Resources\PaymentProviders\Pages\ListPaymentProviders;
|
||||
use App\Filament\Resources\PaymentProviders\Schemas\PaymentProviderForm;
|
||||
use App\Filament\Resources\PaymentProviders\Tables\PaymentProvidersTable;
|
||||
use App\Models\PaymentProvider;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PaymentProviderResource extends Resource
|
||||
{
|
||||
protected static ?string $model = PaymentProvider::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCube;
|
||||
|
||||
protected static ?string $navigationLabel = 'Payment Providers';
|
||||
|
||||
protected static ?string $modelLabel = 'Payment Provider';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Payment Providers';
|
||||
|
||||
protected static string|null|\UnitEnum $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return PaymentProviderForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return PaymentProvidersTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListPaymentProviders::route('/'),
|
||||
'create' => CreatePaymentProvider::route('/create'),
|
||||
'edit' => EditPaymentProvider::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Schemas;
|
||||
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class PaymentProviderForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Basic Information')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label('Provider Name')
|
||||
->required()
|
||||
->unique(ignoreRecord: true)
|
||||
->helperText('Internal identifier for the provider'),
|
||||
|
||||
TextInput::make('display_name')
|
||||
->label('Display Name')
|
||||
->required()
|
||||
->helperText('Name shown to users'),
|
||||
]),
|
||||
|
||||
Textarea::make('description')
|
||||
->label('Description')
|
||||
->rows(3)
|
||||
->helperText('Brief description of the payment provider')
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
|
||||
Section::make('Capabilities')
|
||||
->schema([
|
||||
Grid::make(3)
|
||||
->schema([
|
||||
Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true)
|
||||
->helperText('Enable this provider for use'),
|
||||
|
||||
Toggle::make('supports_recurring')
|
||||
->label('Supports Recurring')
|
||||
->default(false)
|
||||
->helperText('Can handle subscription payments'),
|
||||
|
||||
Toggle::make('supports_one_time')
|
||||
->label('Supports One-Time')
|
||||
->default(true)
|
||||
->helperText('Can handle single payments'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('priority')
|
||||
->label('Priority')
|
||||
->numeric()
|
||||
->default(0)
|
||||
->helperText('Higher priority = shown first'),
|
||||
|
||||
Toggle::make('is_fallback')
|
||||
->label('Fallback Provider')
|
||||
->default(false)
|
||||
->helperText('Default provider when others fail'),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Configuration')
|
||||
->schema([
|
||||
KeyValue::make('configuration')
|
||||
->label('Provider Configuration')
|
||||
->addActionLabel('Add configuration')
|
||||
->keyLabel('Key')
|
||||
->valueLabel('Value')
|
||||
->helperText('API keys and other provider-specific settings')
|
||||
->columnSpanFull(),
|
||||
|
||||
KeyValue::make('supported_currencies')
|
||||
->label('Supported Currencies')
|
||||
->addActionLabel('Add currency')
|
||||
->keyLabel('Currency Code')
|
||||
->valueLabel('Display Name')
|
||||
->default(['USD' => 'US Dollar'])
|
||||
->helperText('Currencies this provider supports')
|
||||
->columnSpanFull(),
|
||||
|
||||
KeyValue::make('fee_structure')
|
||||
->label('Fee Structure')
|
||||
->addActionLabel('Add fee setting')
|
||||
->keyLabel('Fee Type')
|
||||
->valueLabel('Value')
|
||||
->helperText('Example: fixed_fee, percentage_fee')
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
|
||||
Section::make('Webhook Settings')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('webhook_url')
|
||||
->label('Webhook URL')
|
||||
->url()
|
||||
->helperText('Endpoint for provider webhooks'),
|
||||
|
||||
TextInput::make('webhook_secret')
|
||||
->label('Webhook Secret')
|
||||
->password()
|
||||
->helperText('Secret for webhook validation'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Tables;
|
||||
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PaymentProvidersTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')
|
||||
->label('Provider')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->weight('bold'),
|
||||
|
||||
TextColumn::make('display_name')
|
||||
->label('Display Name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('supports_recurring')
|
||||
->label('Recurring')
|
||||
->boolean()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('supports_one_time')
|
||||
->label('One-Time')
|
||||
->boolean()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('is_fallback')
|
||||
->label('Fallback')
|
||||
->boolean()
|
||||
->sortable()
|
||||
->color(fn ($record) => $record->is_fallback ? 'warning' : null),
|
||||
|
||||
TextColumn::make('priority')
|
||||
->label('Priority')
|
||||
->numeric()
|
||||
->sortable()
|
||||
->alignCenter(),
|
||||
|
||||
TextColumn::make('supported_currencies')
|
||||
->label('Currencies')
|
||||
->formatStateUsing(fn ($state) => is_array($state) ? implode(', ', array_keys($state)) : '')
|
||||
->limitList(2)
|
||||
->separator(', ')
|
||||
->tooltip(fn ($record) => is_array($record->supported_currencies) ? implode(', ', array_keys($record->supported_currencies)) : ''),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Created')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
|
||||
TextColumn::make('updated_at')
|
||||
->label('Updated')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('is_active')
|
||||
->label('Status')
|
||||
->options([
|
||||
'1' => 'Active',
|
||||
'0' => 'Inactive',
|
||||
]),
|
||||
|
||||
SelectFilter::make('supports_recurring')
|
||||
->label('Recurring Support')
|
||||
->options([
|
||||
'1' => 'Supports Recurring',
|
||||
'0' => 'No Recurring Support',
|
||||
]),
|
||||
|
||||
SelectFilter::make('supports_one_time')
|
||||
->label('One-Time Support')
|
||||
->options([
|
||||
'1' => 'Supports One-Time',
|
||||
'0' => 'No One-Time Support',
|
||||
]),
|
||||
|
||||
SelectFilter::make('is_fallback')
|
||||
->label('Fallback Status')
|
||||
->options([
|
||||
'1' => 'Is Fallback',
|
||||
'0' => 'Not Fallback',
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('test_connection')
|
||||
->label('Test Connection')
|
||||
->icon('heroicon-o-signal')
|
||||
->color('info')
|
||||
->action(function ($record) {
|
||||
$result = $record->testConnection();
|
||||
|
||||
if ($result['success']) {
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Connection Test Successful')
|
||||
->body('Provider is configured and responding correctly.')
|
||||
->success()
|
||||
->send();
|
||||
} else {
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Connection Test Failed')
|
||||
->body($result['error'])
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
|
||||
BulkAction::make('activate')
|
||||
->label('Activate Selected')
|
||||
->icon('heroicon-o-check')
|
||||
->action(function ($records) {
|
||||
$records->each->update(['is_active' => true]);
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Providers Activated')
|
||||
->body('Selected payment providers have been activated.')
|
||||
->success()
|
||||
->send();
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
|
||||
BulkAction::make('deactivate')
|
||||
->label('Deactivate Selected')
|
||||
->icon('heroicon-o-x-mark')
|
||||
->action(function ($records) {
|
||||
$records->where('is_fallback', false)->each->update(['is_active' => false]);
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Providers Deactivated')
|
||||
->body('Selected payment providers have been deactivated (fallback providers were skipped).')
|
||||
->warning()
|
||||
->send();
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
])
|
||||
->emptyStateDescription('No payment providers configured yet.')
|
||||
->emptyStateHeading('No Payment Providers')
|
||||
->emptyStateIcon('heroicon-o-rectangle-stack');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\SubscriptionResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateSubscription extends CreateRecord
|
||||
{
|
||||
protected static string $resource = SubscriptionResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\SubscriptionResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditSubscription extends EditRecord
|
||||
{
|
||||
protected static string $resource = SubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\SubscriptionResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListSubscriptions extends ListRecords
|
||||
{
|
||||
protected static string $resource = SubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Schemas;
|
||||
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class SubscriptionForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Basic Information')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('user_id')
|
||||
->label('User')
|
||||
->relationship('user', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->required()
|
||||
->createOptionForm([
|
||||
TextInput::make('name')
|
||||
->required(),
|
||||
TextInput::make('email')
|
||||
->email()
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Select::make('plan_id')
|
||||
->label('Plan')
|
||||
->relationship('plan', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('type')
|
||||
->label('Subscription Type')
|
||||
->options([
|
||||
'default' => 'Default',
|
||||
'premium' => 'Premium',
|
||||
'enterprise' => 'Enterprise',
|
||||
'trial' => 'Trial',
|
||||
])
|
||||
->required()
|
||||
->default('default')
|
||||
->helperText('Type of subscription'),
|
||||
|
||||
Select::make('status')
|
||||
->label('Status')
|
||||
->options([
|
||||
'active' => 'Active',
|
||||
'trialing' => 'Trial',
|
||||
'cancelled' => 'Cancelled',
|
||||
'paused' => 'Paused',
|
||||
'incomplete' => 'Incomplete',
|
||||
])
|
||||
->required()
|
||||
->default('active'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('provider')
|
||||
->label('Provider')
|
||||
->options([
|
||||
'stripe' => 'Stripe',
|
||||
'lemon_squeezy' => 'Lemon Squeezy',
|
||||
'polar' => 'Polar.sh',
|
||||
'oxapay' => 'OxaPay',
|
||||
'crypto' => 'Crypto',
|
||||
'activation_key' => 'Activation Key',
|
||||
])
|
||||
->required()
|
||||
->default('stripe'),
|
||||
|
||||
TextInput::make('provider_subscription_id')
|
||||
->label('Provider Subscription ID'),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Trial Management')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('trial_ends_at')
|
||||
->label('Trial Ends At'),
|
||||
|
||||
TextInput::make('quantity')
|
||||
->label('Quantity')
|
||||
->numeric()
|
||||
->default(1),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Billing Dates')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('starts_at')
|
||||
->label('Starts At'),
|
||||
|
||||
DateTimePicker::make('ends_at')
|
||||
->label('Ends At'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('cancelled_at')
|
||||
->label('Cancelled At'),
|
||||
|
||||
DateTimePicker::make('paused_at')
|
||||
->label('Paused At'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('resumed_at')
|
||||
->label('Resumed At'),
|
||||
|
||||
DateTimePicker::make('last_provider_sync')
|
||||
->label('Last Provider Sync'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Cancellation Details')
|
||||
->schema([
|
||||
Textarea::make('cancellation_reason')
|
||||
->label('Reason'),
|
||||
])
|
||||
->collapsible()
|
||||
->visible(fn ($get) => $get('status') === 'cancelled'),
|
||||
|
||||
Section::make('Provider Information')
|
||||
->schema([
|
||||
TextInput::make('stripe_id')
|
||||
->label('Stripe ID')
|
||||
->visible(fn ($get) => $get('provider') === 'stripe'),
|
||||
|
||||
TextInput::make('stripe_status')
|
||||
->label('Stripe Status')
|
||||
->visible(fn ($get) => $get('provider') === 'stripe'),
|
||||
|
||||
TextInput::make('stripe_price')
|
||||
->label('Stripe Price')
|
||||
->visible(fn ($get) => $get('provider') === 'stripe'),
|
||||
|
||||
Textarea::make('provider_data')
|
||||
->label('Provider Data')
|
||||
->rows(3)
|
||||
->helperText('JSON data from the payment provider'),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Migration Information')
|
||||
->schema([
|
||||
TextInput::make('migration_batch_id')
|
||||
->label('Migration Batch ID'),
|
||||
|
||||
Toggle::make('is_migrated')
|
||||
->label('Is Migrated'),
|
||||
|
||||
Textarea::make('legacy_data')
|
||||
->label('Legacy Data')
|
||||
->rows(3),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\Pages\CreateSubscription;
|
||||
use App\Filament\Resources\Subscriptions\Pages\EditSubscription;
|
||||
use App\Filament\Resources\Subscriptions\Pages\ListSubscriptions;
|
||||
use App\Filament\Resources\Subscriptions\Schemas\SubscriptionForm;
|
||||
use App\Filament\Resources\Subscriptions\Tables\SubscriptionsTable;
|
||||
use App\Models\Subscription;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class SubscriptionResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Subscription::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedDocumentCurrencyDollar;
|
||||
|
||||
protected static string|null|\UnitEnum $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return SubscriptionForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return SubscriptionsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListSubscriptions::route('/'),
|
||||
'create' => CreateSubscription::route('/create'),
|
||||
'edit' => EditSubscription::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class SubscriptionsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->modifyQueryUsing(function ($query) {
|
||||
$query->withSum('couponUsages as total_coupon_discount', 'discount_amount');
|
||||
})
|
||||
->columns([
|
||||
TextColumn::make('user.name')
|
||||
->label('User')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->limit(30),
|
||||
|
||||
TextColumn::make('plan.name')
|
||||
->label('Plan')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('type')
|
||||
->badge()
|
||||
->label('Type')
|
||||
->colors([
|
||||
'gray' => 'default',
|
||||
'blue' => 'premium',
|
||||
'purple' => 'enterprise',
|
||||
'warning' => 'trial',
|
||||
]),
|
||||
|
||||
TextColumn::make('status')
|
||||
->badge()
|
||||
->label('Status')
|
||||
->colors([
|
||||
'success' => 'active',
|
||||
'warning' => 'trialing',
|
||||
'danger' => 'cancelled',
|
||||
'secondary' => 'paused',
|
||||
'gray' => 'incomplete',
|
||||
]),
|
||||
|
||||
TextColumn::make('provider')
|
||||
->badge()
|
||||
->label('Provider')
|
||||
->colors([
|
||||
'blue' => 'stripe',
|
||||
'green' => 'lemon_squeezy',
|
||||
'purple' => 'polar',
|
||||
'orange' => 'oxapay',
|
||||
'gray' => 'crypto',
|
||||
'pink' => 'activation_key',
|
||||
]),
|
||||
|
||||
TextColumn::make('provider_subscription_id')
|
||||
->label('Provider ID')
|
||||
->searchable()
|
||||
->limit(20),
|
||||
|
||||
TextColumn::make('trial_ends_at')
|
||||
->label('Trial Ends')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color('warning')
|
||||
->description(fn ($record): string => $record->isOnTrial() ? $record->trial_ends_at->diffForHumans() : ''
|
||||
),
|
||||
|
||||
TextColumn::make('ends_at')
|
||||
->label('Ends At')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color(fn ($record): string => $record->ends_at && $record->ends_at->isPast() ? 'danger' : 'default'
|
||||
),
|
||||
|
||||
TextColumn::make('quantity')
|
||||
->label('Qty')
|
||||
->numeric()
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
IconColumn::make('hasExtendedTrial')
|
||||
->label('Trial Extended')
|
||||
->boolean()
|
||||
->getStateUsing(fn ($record) => $record->hasExtendedTrial())
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('total_coupon_discount')
|
||||
->label('Total Discount')
|
||||
->money('USD')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Created')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
|
||||
TextColumn::make('last_provider_sync')
|
||||
->label('Last Sync')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')
|
||||
->options([
|
||||
'active' => 'Active',
|
||||
'trialing' => 'Trial',
|
||||
'cancelled' => 'Cancelled',
|
||||
'paused' => 'Paused',
|
||||
'incomplete' => 'Incomplete',
|
||||
]),
|
||||
|
||||
SelectFilter::make('type')
|
||||
->label('Subscription Type')
|
||||
->options([
|
||||
'default' => 'Default',
|
||||
'premium' => 'Premium',
|
||||
'enterprise' => 'Enterprise',
|
||||
'trial' => 'Trial',
|
||||
]),
|
||||
|
||||
SelectFilter::make('provider')
|
||||
->options([
|
||||
'stripe' => 'Stripe',
|
||||
'lemon_squeezy' => 'Lemon Squeezy',
|
||||
'polar' => 'Polar.sh',
|
||||
'oxapay' => 'OxaPay',
|
||||
'crypto' => 'Crypto',
|
||||
'activation_key' => 'Activation Key',
|
||||
]),
|
||||
|
||||
SelectFilter::make('has_trial_extension')
|
||||
->label('Has Trial Extension')
|
||||
->options([
|
||||
'yes' => 'Yes',
|
||||
'no' => 'No',
|
||||
])
|
||||
->query(fn ($query, $data) => match ($data['value']) {
|
||||
'yes' => $query->whereHas('trialExtensions'),
|
||||
'no' => $query->whereDoesntHave('trialExtensions'),
|
||||
default => $query,
|
||||
}),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('extend_trial')
|
||||
->label('Extend Trial')
|
||||
->icon('heroicon-o-clock')
|
||||
->color('warning')
|
||||
->schema([
|
||||
\Filament\Forms\Components\TextInput::make('days')
|
||||
->label('Days to Extend')
|
||||
->numeric()
|
||||
->required()
|
||||
->default(7),
|
||||
\Filament\Forms\Components\TextInput::make('reason')
|
||||
->label('Reason')
|
||||
->required(),
|
||||
\Filament\Forms\Components\Select::make('extension_type')
|
||||
->label('Extension Type')
|
||||
->options([
|
||||
'manual' => 'Manual Grant',
|
||||
'automatic' => 'Automatic Extension',
|
||||
'compensation' => 'Compensation',
|
||||
])
|
||||
->default('manual'),
|
||||
])
|
||||
->action(function (array $data, $record) {
|
||||
$record->extendTrial(
|
||||
(int) $data['days'],
|
||||
$data['reason'],
|
||||
$data['extension_type'],
|
||||
auth()->user()
|
||||
);
|
||||
})
|
||||
->visible(fn ($record) => $record->isOnTrial()),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkAction::make('bulk_extend_trial')
|
||||
->label('Bulk Extend Trial')
|
||||
->icon('heroicon-o-clock')
|
||||
->color('warning')
|
||||
->schema([
|
||||
\Filament\Forms\Components\TextInput::make('days')
|
||||
->label('Days to Extend')
|
||||
->numeric()
|
||||
->required()
|
||||
->default(7),
|
||||
\Filament\Forms\Components\TextInput::make('reason')
|
||||
->label('Reason')
|
||||
->required(),
|
||||
])
|
||||
->action(function (array $data, \Illuminate\Support\Collection $records) {
|
||||
foreach ($records as $record) {
|
||||
if ($record->isOnTrial()) {
|
||||
$record->extendTrial(
|
||||
(int) $data['days'],
|
||||
$data['reason'],
|
||||
'manual',
|
||||
auth()->user()
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Pages;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
|
||||
use App\Models\Subscription;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateTrialExtension extends CreateRecord
|
||||
{
|
||||
protected static string $resource = TrialExtensionResource::class;
|
||||
|
||||
/**
|
||||
* Ensure user_id and original_trial_ends_at are always set from subscription before creating
|
||||
*/
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
// Always ensure user_id and original_trial_ends_at are set from subscription
|
||||
if (isset($data['subscription_id'])) {
|
||||
$subscription = Subscription::find($data['subscription_id']);
|
||||
if ($subscription) {
|
||||
$data['user_id'] = $subscription->user_id;
|
||||
$data['original_trial_ends_at'] = $subscription->trial_ends_at;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Pages;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditTrialExtension extends EditRecord
|
||||
{
|
||||
protected static string $resource = TrialExtensionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Pages;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListTrialExtensions extends ListRecords
|
||||
{
|
||||
protected static string $resource = TrialExtensionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Schemas;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class TrialExtensionForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Trial Extension Details')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('subscription_id')
|
||||
->label('Subscription')
|
||||
->options(function () {
|
||||
return Subscription::with(['user', 'plan'])
|
||||
->where('status', 'trialing')
|
||||
->orWhere('status', 'active')
|
||||
->get()
|
||||
->mapWithKeys(function ($subscription) {
|
||||
$label = "{$subscription->user->name} - {$subscription->plan->name}";
|
||||
if ($subscription->trial_ends_at) {
|
||||
$label .= " ({$subscription->trial_ends_at->format('M j, Y')})";
|
||||
}
|
||||
return [$subscription->id => $label];
|
||||
})
|
||||
->toArray();
|
||||
})
|
||||
->searchable()
|
||||
->preload()
|
||||
->required()
|
||||
->live()
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if ($state) {
|
||||
$subscription = Subscription::find($state);
|
||||
if ($subscription) {
|
||||
$set('user_id', $subscription->user_id);
|
||||
$set('original_trial_ends_at', $subscription->trial_ends_at);
|
||||
|
||||
// Set user display name
|
||||
if ($subscription->relationLoaded('user')) {
|
||||
$set('user_display', $subscription->user->name);
|
||||
} else {
|
||||
$subscription->load('user');
|
||||
$set('user_display', $subscription->user->name);
|
||||
}
|
||||
|
||||
self::calculateNewTrialEndDate($set, $get);
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
TextInput::make('user_display')
|
||||
->label('User')
|
||||
->disabled()
|
||||
->dehydrated(false),
|
||||
]),
|
||||
|
||||
// Hidden field for user_id to ensure it gets submitted
|
||||
TextInput::make('user_id')
|
||||
->hidden(),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('extension_days')
|
||||
->label('Extension Days')
|
||||
->numeric()
|
||||
->required()
|
||||
->default(7)
|
||||
->live(onBlur: true)
|
||||
->helperText('Number of days to extend the trial')
|
||||
->afterStateUpdated(fn ($state, callable $set, callable $get) => self::calculateNewTrialEndDate($set, $get)),
|
||||
|
||||
Select::make('extension_type')
|
||||
->label('Extension Type')
|
||||
->options([
|
||||
'manual' => 'Manual Grant',
|
||||
'automatic' => 'Automatic Extension',
|
||||
'compensation' => 'Compensation',
|
||||
])
|
||||
->default('manual')
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('original_trial_ends_at')
|
||||
->label('Original Trial End Date')
|
||||
->disabled()
|
||||
->dehydrated(false),
|
||||
|
||||
DateTimePicker::make('new_trial_ends_at')
|
||||
->label('New Trial End Date')
|
||||
->required()
|
||||
->readOnly()
|
||||
->helperText('This will be calculated automatically based on extension days'),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Additional Information')
|
||||
->schema([
|
||||
Textarea::make('reason')
|
||||
->label('Reason')
|
||||
->rows(3)
|
||||
->helperText('Reason for granting this trial extension'),
|
||||
|
||||
Select::make('granted_by_admin_id')
|
||||
->label('Granted By')
|
||||
->relationship('grantedByAdmin', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->default(fn () => auth()->id())
|
||||
->helperText('Admin who granted this extension'),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Timestamps')
|
||||
->schema([
|
||||
DateTimePicker::make('granted_at')
|
||||
->label('Granted At')
|
||||
->default(now())
|
||||
->required(),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Metadata')
|
||||
->schema([
|
||||
KeyValue::make('metadata')
|
||||
->label('Custom Metadata')
|
||||
->addActionLabel('Add metadata')
|
||||
->keyLabel('Key')
|
||||
->valueLabel('Value')
|
||||
->helperText('Additional key-value data for this trial extension'),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the new trial end date based on original date and extension days
|
||||
*/
|
||||
private static function calculateNewTrialEndDate(callable $set, callable $get): void
|
||||
{
|
||||
$subscriptionId = $get('subscription_id');
|
||||
$extensionDays = (int) $get('extension_days') ?: 0;
|
||||
|
||||
if (! $subscriptionId || ! $extensionDays) {
|
||||
$set('new_trial_ends_at', null);
|
||||
return;
|
||||
}
|
||||
|
||||
$subscription = Subscription::find($subscriptionId);
|
||||
if (! $subscription) {
|
||||
$set('new_trial_ends_at', null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Use the original trial end date if available, otherwise use current date
|
||||
$baseDate = $subscription->trial_ends_at ?: now();
|
||||
|
||||
// Calculate new end date by adding extension days
|
||||
$newEndDate = $baseDate->copy()->addDays($extensionDays);
|
||||
|
||||
// Format for DateTimePicker (Y-m-d H:i:s format)
|
||||
$set('new_trial_ends_at', $newEndDate->format('Y-m-d H:i:s'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class TrialExtensionsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('user.name')
|
||||
->label('User')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->limit(30),
|
||||
|
||||
TextColumn::make('subscription.plan.name')
|
||||
->label('Plan')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('extension_days')
|
||||
->label('Days Extended')
|
||||
->sortable()
|
||||
->alignCenter()
|
||||
->badge()
|
||||
->color('success'),
|
||||
|
||||
TextColumn::make('extension_type')
|
||||
->badge()
|
||||
->label('Type')
|
||||
->colors([
|
||||
'blue' => 'manual',
|
||||
'green' => 'automatic',
|
||||
'orange' => 'compensation',
|
||||
]),
|
||||
|
||||
TextColumn::make('original_trial_ends_at')
|
||||
->label('Original End')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('new_trial_ends_at')
|
||||
->label('New End')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color('success')
|
||||
->description(fn ($record): string => $record->new_trial_ends_at->diffForHumans()
|
||||
),
|
||||
|
||||
TextColumn::make('reason')
|
||||
->label('Reason')
|
||||
->limit(30)
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('grantedByAdmin.name')
|
||||
->label('Granted By')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('granted_at')
|
||||
->label('Granted At')
|
||||
->dateTime('M j, Y')
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('extension_type')
|
||||
->label('Extension Type')
|
||||
->options([
|
||||
'manual' => 'Manual Grant',
|
||||
'automatic' => 'Automatic Extension',
|
||||
'compensation' => 'Compensation',
|
||||
]),
|
||||
|
||||
SelectFilter::make('granted_by_admin_id')
|
||||
->label('Granted By')
|
||||
->relationship('grantedByAdmin', 'name')
|
||||
->searchable()
|
||||
->preload(),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('view_subscription')
|
||||
->label('View Subscription')
|
||||
->icon('heroicon-o-rectangle-stack')
|
||||
->color('blue')
|
||||
->url(fn ($record) => route('filament.' . filament()->getCurrentPanel()->getId() . '.resources.subscriptions.edit', $record->subscription_id))
|
||||
->openUrlInNewTab(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\Pages\CreateTrialExtension;
|
||||
use App\Filament\Resources\TrialExtensions\Pages\EditTrialExtension;
|
||||
use App\Filament\Resources\TrialExtensions\Pages\ListTrialExtensions;
|
||||
use App\Filament\Resources\TrialExtensions\Schemas\TrialExtensionForm;
|
||||
use App\Filament\Resources\TrialExtensions\Tables\TrialExtensionsTable;
|
||||
use App\Models\Subscription;
|
||||
use App\Models\TrialExtension;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class TrialExtensionResource extends Resource
|
||||
{
|
||||
protected static ?string $model = TrialExtension::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedClock;
|
||||
|
||||
protected static ?string $navigationLabel = 'Trial Extensions';
|
||||
|
||||
protected static ?string $modelLabel = 'Trial Extension';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Trial Extensions';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return TrialExtensionForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return TrialExtensionsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListTrialExtensions::route('/'),
|
||||
'create' => CreateTrialExtension::route('/create'),
|
||||
'edit' => EditTrialExtension::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user