Przeglądaj źródła

feat: add campo token em client_payment_methods

Gustavo Mantovani 1 miesiąc temu
rodzic
commit
702ea13737

+ 7 - 4
app/Http/Requests/ClientPaymentMethodRequest.php

@@ -15,10 +15,11 @@ class ClientPaymentMethodRequest extends FormRequest
     {
         $rules = [
             'client_id' => ['sometimes', 'integer', 'exists:clients,id'],
-            'card_number' => ['sometimes', 'string', 'min:13', 'max:19'],
+            'token' => ['sometimes', 'string', 'max:255'],
+            'card_number' => ['sometimes', 'nullable', 'string', 'min:13', 'max:19'],
             'holder_name' => ['sometimes', 'string', 'max:255'],
             'expiration' => ['sometimes', 'string', 'regex:/^(0[1-9]|1[0-2])\/\d{4}$/'],
-            'cvv' => ['sometimes', 'string', 'min:3', 'max:4'],
+            'cvv' => ['sometimes', 'nullable', 'string', 'min:3', 'max:4'],
             'card_name' => ['nullable', 'string', 'max:255'],
             'brand' => ['nullable', 'string', 'max:50'],
             'last_four_digits' => ['sometimes', 'string', 'size:4'],
@@ -27,10 +28,11 @@ class ClientPaymentMethodRequest extends FormRequest
 
         if ($this->isMethod('POST')) {
             $rules['client_id'] = ['required', 'integer', 'exists:clients,id'];
-            $rules['card_number'] = ['required', 'string', 'min:13', 'max:19'];
+            $rules['token'] = ['required', 'string', 'max:255'];
+            $rules['card_number'] = ['sometimes', 'nullable', 'string', 'min:13', 'max:19'];
             $rules['holder_name'] = ['required', 'string', 'max:255'];
             $rules['expiration'] = ['required', 'string', 'regex:/^(0[1-9]|1[0-2])\/\d{4}$/'];
-            $rules['cvv'] = ['required', 'string', 'min:3', 'max:4'];
+            $rules['cvv'] = ['sometimes', 'nullable', 'string', 'min:3', 'max:4'];
             $rules['last_four_digits'] = ['required', 'string', 'size:4'];
         }
 
@@ -42,6 +44,7 @@ class ClientPaymentMethodRequest extends FormRequest
         return [
             'client_id.required' => 'O cliente é obrigatório.',
             'client_id.exists' => 'Cliente não encontrado.',
+            'token.required' => 'O token do cartão é obrigatório.',
             'card_number.required' => 'O número do cartão é obrigatório.',
             'card_number.min' => 'O número do cartão deve ter no mínimo 13 dígitos.',
             'card_number.max' => 'O número do cartão deve ter no máximo 19 dígitos.',

+ 1 - 2
app/Http/Resources/ClientPaymentMethodResource.php

@@ -17,8 +17,7 @@ class ClientPaymentMethodResource extends JsonResource
             'card_name' => $this->card_name,
             'brand' => $this->brand,
             'last_four_digits' => $this->last_four_digits,
-            'cvv' => $this->cvv,
-            'card_number' => '**** **** **** ' . $this->last_four_digits,
+            'card_number' => $this->last_four_digits ? '**** **** **** ' . $this->last_four_digits : null,
             'is_active' => $this->is_active,
             'created_at' => $this->created_at?->format('Y-m-d H:i'),
             'updated_at' => $this->updated_at?->format('Y-m-d H:i'),

+ 4 - 1
app/Models/ClientPaymentMethod.php

@@ -13,6 +13,7 @@ class ClientPaymentMethod extends Model
 
     protected $fillable = [
         'client_id',
+        'token',
         'card_number',
         'holder_name',
         'expiration',
@@ -24,6 +25,7 @@ class ClientPaymentMethod extends Model
     ];
 
     protected $casts = [
+        'token' => 'encrypted',
         'card_number' => 'encrypted',
         'cvv' => 'encrypted',
         'is_active' => 'boolean',
@@ -33,7 +35,8 @@ class ClientPaymentMethod extends Model
     ];
 
     protected $hidden = [
-        // 'card_number',
+        'token',
+        'card_number',
         'cvv',
     ];
 

+ 26 - 0
database/migrations/2026_05_15_000001_add_token_to_client_payment_methods_table.php

@@ -0,0 +1,26 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('client_payment_methods', function (Blueprint $table) {
+            $table->text('token')->nullable()->after('client_id');
+            $table->text('card_number')->nullable()->change();
+            $table->text('cvv')->nullable()->change();
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('client_payment_methods', function (Blueprint $table) {
+            $table->dropColumn('token');
+            $table->text('card_number')->nullable(false)->change();
+            $table->text('cvv')->nullable(false)->change();
+        });
+    }
+};