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:
@@ -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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user