feat(payment): implement database-driven payment provider system with encrypted configuration support

- Add PaymentProviderSeeder with initial provider data (Stripe, Lemon Squeezy, Polar, OxaPay, Crypto, Activation Key)
  - Create migration to disable JSON constraints and change configuration column from JSON to TEXT
  - Update PaymentProvider model cast from 'array' to 'encrypted:array' for secure configuration storage
This commit is contained in:
idevakk
2025-12-03 02:32:08 -08:00
parent 3b908484de
commit 724a2d9a1f
4 changed files with 301 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
// Detect DB driver
$driver = Schema::getConnection()->getDriverName();
// --- MySQL / MariaDB only ---
// Drop JSON check constraints if they exist (safely)
if (in_array($driver, ['mysql', 'mariadb'])) {
try {
// MySQL automatically names check constraints in different ways,
// so we attempt a generic drop and ignore failures.
DB::statement("ALTER TABLE `payment_providers` DROP CHECK `payment_providers.configuration`;");
} catch (\Throwable $e) {
// ignore if constraint does not exist
}
}
// --- PostgreSQL ---
// PostgreSQL may also require dropping check constraints but constraint names are random.
// We try to drop any constraint *that checks JSON*.
if ($driver === 'pgsql') {
try {
$constraint = DB::selectOne("
SELECT conname
FROM pg_constraint
JOIN pg_class ON pg_constraint.conrelid = pg_class.oid
WHERE relname = 'payment_providers'
AND pg_get_constraintdef(pg_constraint.oid) LIKE '%configuration%json%';
");
if (!empty($constraint->conname)) {
DB::statement("ALTER TABLE payment_providers DROP CONSTRAINT {$constraint->conname};");
}
} catch (\Throwable $e) {
// ignore if constraint does not exist
}
}
// --- SQLite & SQL Server ---
// No action needed:
// SQLite doesnt enforce JSON CHECK constraints
// SQL Server does not create JSON CHECK constraints automatically.
Schema::table('payment_providers', function (Blueprint $table) {
// Replace JSON type with LONGTEXT / TEXT depending on driver
// Laravel automatically maps this properly for each database.
$table->longText('configuration')->change();
});
}
public function down(): void
{
Schema::table('payment_providers', function (Blueprint $table) {
// revert to JSON column
// Laravel maps this to appropriate driver types
$table->json('configuration')->change();
});
// Optionally: you can restore JSON CHECK constraints here, but it's not required
}
};