Pārlūkot izejas kodu

feat(company-payment-account): tabela e model da conta Asaas da matriz (linha única)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ebagabee 4 nedēļas atpakaļ
vecāks
revīzija
ac285f9970

+ 49 - 0
app/Models/CompanyPaymentAccount.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * Conta Asaas da franqueadora (matriz). Linha única.
+ *
+ * @property int $id
+ * @property string|null $asaas_api_key
+ * @property string|null $asaas_webhook_id
+ * @property string $status
+ * @property \Illuminate\Support\Carbon|null $onboarded_at
+ */
+class CompanyPaymentAccount extends Model
+{
+    public const STATUS_PENDING = 'pending';
+    public const STATUS_ACTIVE  = 'active';
+
+    protected $table = 'company_payment_accounts';
+
+    protected $guarded = ['id'];
+
+    protected $casts = [
+        'asaas_api_key' => 'encrypted',
+        'onboarded_at'  => 'datetime',
+        'created_at'    => 'datetime',
+        'updated_at'    => 'datetime',
+    ];
+
+    /**
+     * A (única) conta da matriz.
+     */
+    public static function current(): self
+    {
+        return static::query()->first() ?? new self();
+    }
+
+    /**
+     * Chave efetiva da matriz: banco → fallback .env.
+     */
+    public static function resolveApiKey(): ?string
+    {
+        $key = static::query()->first()?->asaas_api_key;
+
+        return filled($key) ? $key : config('services.asaas.api_key');
+    }
+}

+ 31 - 0
database/migrations/2026_07_01_000004_create_company_payment_accounts_table.php

@@ -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
+{
+    /**
+     * Conta Asaas da FRANQUEADORA (matriz). Linha única.
+     *
+     * Permite a franqueadora informar/trocar a própria chave pela UI, sem depender
+     * do .env nem acionar o suporte. O AsaasClient resolve: chave do banco → .env.
+     */
+    public function up(): void
+    {
+        Schema::create('company_payment_accounts', function (Blueprint $table) {
+            $table->id();
+            $table->text('asaas_api_key')->nullable();
+            $table->string('asaas_webhook_id')->nullable();
+            $table->string('status')->default('pending');
+            $table->timestamp('onboarded_at')->nullable();
+            $table->timestamps();
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::dropIfExists('company_payment_accounts');
+    }
+};