ソースを参照

refactor(asaas): trim webhook and customer service to the matriz flow

Webhook handles only TBR_/FranchiseeAccountReceive; removes the orphaned ensureStudentCustomer.
ebagabee 1 ヶ月 前
コミット
a67152d595

+ 3 - 17
app/Jobs/ProcessAsaasWebhookJob.php

@@ -4,7 +4,6 @@
 
 use App\Enums\ReceivableStatus;
 use App\Models\FranchiseeAccountReceive;
-use App\Models\StudentContractInstallment;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
@@ -55,17 +54,11 @@ public function handle(): void
         $type = 'unknown';
 
         if ($externalReference) {
-            // Se for um recebível de Franquia
+            // Recebível de Franquia (TBR) cobrado pela conta da Matriz.
             if (str_starts_with($externalReference, 'TBR_')) {
                 $id = str_replace('TBR_', '', $externalReference);
                 $receive = FranchiseeAccountReceive::find($id);
                 $type = 'FranchiseeAccountReceive';
-            } 
-            // Se for parcela de aluno
-            elseif (str_starts_with($externalReference, 'STU_')) {
-                $id = str_replace('STU_', '', $externalReference);
-                $receive = StudentContractInstallment::find($id);
-                $type = 'StudentContractInstallment';
             }
             // Backward compatibility para faturas de teste geradas antes do prefixo
             elseif (is_numeric($externalReference)) {
@@ -77,14 +70,9 @@ public function handle(): void
         // Fallback por asaas_id caso externalReference venha nulo
         if (!$receive && $asaasId) {
             $receive = FranchiseeAccountReceive::where('asaas_id', $asaasId)->first();
-            
+
             if ($receive) {
                 $type = 'FranchiseeAccountReceive (by asaas_id)';
-            } else {
-                $receive = StudentContractInstallment::where('asaas_id', $asaasId)->first();
-                if ($receive) {
-                    $type = 'StudentContractInstallment (by asaas_id)';
-                }
             }
         }
 
@@ -97,8 +85,7 @@ public function handle(): void
             return;
         }
 
-        // Conversão de status: se a model usa string (como StudentContractInstallment)
-        // Precisamos comparar de forma segura
+        // Comparação segura de status (a model pode expor enum ou string).
         $currentStatus = $receive->status instanceof ReceivableStatus ? $receive->status->value : $receive->status;
         $newStatusValue = $newStatus instanceof ReceivableStatus ? $newStatus->value : $newStatus;
 
@@ -115,7 +102,6 @@ public function handle(): void
 
         if ($newStatusValue === ReceivableStatus::PAID->value) {
             $updateData['payment_date'] = $payment['paymentDate'] ?? $payment['confirmedDate'] ?? now();
-            // A tabela do Aluno usa 'paid_value', a tabela da Franquia também.
             $updateData['paid_value']   = $payment['value'] ?? $receive->value;
         }
 

+ 0 - 42
app/Services/Integrations/Asaas/AsaasCustomerService.php

@@ -54,46 +54,4 @@ public function ensureFranchiseeCustomer(Unit $unit): string
 
         return $response['id'];
     }
-
-    /**
-     * Garante que o Aluno exista como Customer na Subconta da Franquia.
-     * Retorna o ID do cliente no Asaas (cus_xxxxxx).
-     */
-    public function ensureStudentCustomer(\App\Models\Student $student, string $apiKey): string
-    {
-        $client = new AsaasClient($apiKey);
-        $cpf = preg_replace('/[^0-9]/', '', $student->document_number);
-
-        if (empty($cpf)) {
-            throw new Exception("Aluno {$student->name} não possui CPF cadastrado para cobrança.");
-        }
-
-        // Tenta buscar no Asaas se já existe
-        $existing = $client->get('/customers', ['cpfCnpj' => $cpf]);
-
-        if (isset($existing['data']) && count($existing['data']) > 0) {
-            return $existing['data'][0]['id'];
-        }
-
-        // Se não existe, cria um novo
-        $payload = [
-            'name' => $student->name,
-            'cpfCnpj' => $cpf,
-            'email' => $student->email,
-            'postalCode' => preg_replace('/[^0-9]/', '', $student->postal_code ?? ''),
-            'address' => $student->street,
-            'addressNumber' => $student->address_number ?? 'S/N',
-            'province' => $student->neighborhood,
-        ];
-
-        $mobilePhone = preg_replace('/[^0-9]/', '', $student->phone ?? '');
-        
-        if (strlen($mobilePhone) >= 10 && strlen($mobilePhone) <= 11) {
-            $payload['mobilePhone'] = $mobilePhone;
-        }
-
-        $response = $client->post('/customers', $payload);
-
-        return $response['id'];
-    }
 }