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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user