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:
idevakk
2025-11-19 09:37:00 -08:00
parent 0560016f33
commit 27ac13948c
83 changed files with 15613 additions and 103 deletions

View File

@@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('payment_events', function (Blueprint $table) {
$table->id();
$table->string('event_type', 100);
$table->string('level', 20)->default('info');
$table->json('data');
$table->nullableMorphs('user');
$table->string('request_id')->nullable();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
// Indexes for performance
$table->index(['event_type', 'created_at']);
$table->index(['user_type', 'user_id', 'created_at']);
$table->index(['level', 'created_at']);
$table->index('expires_at');
$table->index('request_id');
// Full-text index for searching data (if supported)
if (config('database.default') === 'mysql') {
$table->fullText('data');
}
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('payment_events');
}
};

View File

@@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('payment_providers', function (Blueprint $table) {
$table->id();
$table->string('name', 50)->unique();
$table->string('display_name');
$table->text('description')->nullable();
$table->boolean('is_active')->default(true);
$table->json('configuration');
$table->boolean('supports_recurring')->default(false);
$table->boolean('supports_one_time')->default(true);
$table->json('supported_currencies')->default('[]');
$table->string('webhook_url')->nullable();
$table->string('webhook_secret')->nullable();
$table->json('fee_structure')->nullable();
$table->integer('priority')->default(0);
$table->boolean('is_fallback')->default(false);
$table->timestamps();
// Indexes
$table->index('is_active');
$table->index('priority');
$table->index('is_fallback');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('payment_providers');
}
};

View File

@@ -0,0 +1,76 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('subscriptions', function (Blueprint $table) {
// Provider tracking
$table->string('provider', 50)->default('stripe')->after('user_id');
$table->string('provider_subscription_id')->nullable()->after('stripe_id');
// Unified status tracking
$table->string('unified_status', 50)->default('active')->after('stripe_status');
$table->timestamp('cancelled_at')->nullable()->after('trial_ends_at');
$table->string('cancellation_reason')->nullable()->after('cancelled_at');
$table->timestamp('paused_at')->nullable()->after('cancellation_reason');
$table->timestamp('resumed_at')->nullable()->after('paused_at');
// Migration tracking
$table->string('migration_batch_id')->nullable()->after('resumed_at');
$table->boolean('is_migrated')->default(false)->after('migration_batch_id');
$table->json('legacy_data')->nullable()->after('is_migrated');
// Sync and audit
$table->timestamp('synced_at')->nullable()->after('legacy_data');
$table->json('provider_data')->nullable()->after('synced_at');
$table->timestamp('last_provider_sync')->nullable()->after('provider_data');
// Indexes
$table->index('provider');
$table->index('provider_subscription_id');
$table->index('unified_status');
$table->index('is_migrated');
$table->index('migration_batch_id');
$table->index('synced_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('subscriptions', function (Blueprint $table) {
$table->dropIndex(['provider']);
$table->dropIndex(['provider_subscription_id']);
$table->dropIndex(['unified_status']);
$table->dropIndex(['is_migrated']);
$table->dropIndex(['migration_batch_id']);
$table->dropIndex(['synced_at']);
$table->dropColumn([
'provider',
'provider_subscription_id',
'unified_status',
'cancelled_at',
'cancellation_reason',
'paused_at',
'resumed_at',
'migration_batch_id',
'is_migrated',
'legacy_data',
'synced_at',
'provider_data',
'last_provider_sync',
]);
});
}
};

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('subscriptions', function (Blueprint $table) {
$table->foreignId('plan_id')->nullable()->after('user_id')->constrained()->nullOnDelete();
$table->string('status', 50)->default('active')->after('unified_status');
$table->timestamp('starts_at')->nullable()->after('ends_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('subscriptions', function (Blueprint $table) {
$table->dropForeign(['plan_id']);
$table->dropColumn(['plan_id', 'status', 'starts_at']);
});
}
};

View File

@@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('coupons', function (Blueprint $table) {
$table->id();
$table->string('code')->unique();
$table->string('name');
$table->text('description')->nullable();
$table->enum('type', ['percentage', 'fixed']);
$table->decimal('value', 10, 2);
$table->decimal('minimum_amount', 10, 2)->nullable();
$table->integer('max_uses')->nullable();
$table->integer('uses_count')->default(0);
$table->integer('max_uses_per_user')->nullable();
$table->timestamp('starts_at')->nullable();
$table->timestamp('expires_at')->nullable();
$table->boolean('is_active')->default(true);
$table->json('metadata')->nullable();
$table->timestamps();
$table->index(['code', 'is_active']);
$table->index(['expires_at', 'is_active']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('coupons');
}
};

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('coupon_usages', function (Blueprint $table) {
$table->id();
$table->foreignId('coupon_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('subscription_id')->nullable()->constrained()->onDelete('cascade');
$table->decimal('discount_amount', 10, 2);
$table->string('currency', 3)->default('USD');
$table->timestamp('used_at');
$table->json('metadata')->nullable();
$table->timestamps();
$table->index(['coupon_id', 'user_id']);
$table->index(['user_id', 'used_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('coupon_usages');
}
};

View File

@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('trial_extensions', function (Blueprint $table) {
$table->id();
$table->foreignId('subscription_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->integer('extension_days');
$table->string('reason')->nullable();
$table->enum('extension_type', ['manual', 'automatic', 'compensation']);
$table->timestamp('original_trial_ends_at');
$table->timestamp('new_trial_ends_at');
$table->timestamp('granted_at');
$table->foreignId('granted_by_admin_id')->nullable()->constrained('users')->onDelete('set null');
$table->json('metadata')->nullable();
$table->timestamps();
$table->index(['subscription_id', 'extension_type']);
$table->index(['user_id', 'granted_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('trial_extensions');
}
};

View File

@@ -0,0 +1,42 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('subscription_changes', function (Blueprint $table) {
$table->id();
$table->foreignId('subscription_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->enum('change_type', ['plan_change', 'cancellation', 'pause', 'resume', 'migration', 'provider_change']);
$table->string('change_description');
$table->json('old_values')->nullable();
$table->json('new_values')->nullable();
$table->string('reason')->nullable();
$table->timestamp('effective_at');
$table->timestamp('processed_at')->nullable();
$table->boolean('is_processed')->default(false);
$table->json('metadata')->nullable();
$table->timestamps();
$table->index(['subscription_id', 'change_type']);
$table->index(['user_id', 'effective_at']);
$table->index(['is_processed', 'processed_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscription_changes');
}
};

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('subscriptions', function (Blueprint $table) {
// Make Stripe-specific fields nullable for unified payment system
$table->string('stripe_id')->nullable()->change();
$table->string('stripe_status')->nullable()->change();
$table->string('stripe_price')->nullable()->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('subscriptions', function (Blueprint $table) {
// Revert Stripe fields back to NOT NULL
$table->string('stripe_id')->nullable(false)->change();
$table->string('stripe_status')->nullable(false)->change();
$table->string('stripe_price')->nullable(false)->change();
});
}
};