Bläddra i källkod

feat: fundação da integração com Asaas (Phase 1)

ebagabee 1 vecka sedan
förälder
incheckning
6fe5f8a8cd

+ 3 - 0
.env.example

@@ -64,3 +64,6 @@ AWS_BUCKET=
 AWS_URL=https://
 AWS_USE_PATH_STYLE_ENDPOINT=false
 
+ASAAS_BASE_URL=https://sandbox.asaas.com/api/v3
+ASAAS_API_KEY=
+ASAAS_WEBHOOK_TOKEN=

+ 51 - 0
app/Console/Commands/AsaasSmokeTestCommand.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Services\Integrations\Asaas\AsaasClient;
+use Illuminate\Console\Command;
+use Illuminate\Support\Str;
+
+class AsaasSmokeTestCommand extends Command
+{
+    protected $signature = 'asaas:smoke-test';
+    protected $description = 'Executa um teste de fumaça criando um cliente e uma cobrança no Asaas Sandbox';
+
+    public function handle()
+    {
+        $this->info('Iniciando teste de fumaça com o Asaas...');
+
+        $client = new AsaasClient();
+
+        try {
+            $this->info('1. Criando Customer de teste...');
+            $customer = $client->post('/customers', [
+                'name' => 'Teste Fumaça ' . Str::random(5),
+                'cpfCnpj' => '12345678909', // CPF matematicamente válido para testes
+                'email' => 'teste@ginasticacerebro.com.br',
+            ]);
+
+            $customerId = $customer['id'];
+            $this->info("Customer criado com sucesso: $customerId");
+
+            $this->info('2. Criando Cobrança (Payment) PIX...');
+            $payment = $client->post('/payments', [
+                'customer' => $customerId,
+                'billingType' => 'PIX',
+                'value' => 10.50,
+                'dueDate' => now()->addDays(5)->format('Y-m-d'),
+                'description' => 'Cobrança Teste de Fumaça',
+            ]);
+
+            $this->info('Cobrança criada com sucesso: ' . $payment['id']);
+            $this->info('Link de pagamento: ' . $payment['invoiceUrl']);
+
+            $this->info('Teste finalizado com sucesso! 🎉');
+        } catch (\Exception $e) {
+            $this->error('Falha no teste: ' . $e->getMessage());
+            return Command::FAILURE;
+        }
+
+        return Command::SUCCESS;
+    }
+}

+ 22 - 0
app/Exceptions/AsaasException.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Exceptions;
+
+use Exception;
+use Illuminate\Http\Client\Response;
+
+class AsaasException extends Exception
+{
+    public array $errors;
+
+    public function __construct(Response $response)
+    {
+        $status = $response->status();
+        $body = $response->json();
+
+        $this->errors = $body['errors'] ?? [];
+        $message = "Asaas API Error ($status): " . collect($this->errors)->pluck('description')->join(', ');
+
+        parent::__construct($message, $status);
+    }
+}

+ 49 - 0
app/Services/Integrations/Asaas/AsaasClient.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Services\Integrations\Asaas;
+
+use App\Exceptions\AsaasException;
+use Illuminate\Http\Client\PendingRequest;
+use Illuminate\Support\Facades\Http;
+
+class AsaasClient
+{
+    protected string $baseUrl;
+    protected string $apiKey;
+
+    public function __construct(?string $apiKey = null)
+    {
+        $this->baseUrl = config('services.asaas.base_url');
+        // Allows overriding the API key for subaccounts
+        $this->apiKey = $apiKey ?? config('services.asaas.api_key');
+    }
+
+    protected function request(): PendingRequest
+    {
+        return Http::withHeaders([
+            'access_token' => $this->apiKey,
+        ])->baseUrl($this->baseUrl);
+    }
+
+    public function get(string $endpoint, array $query = [])
+    {
+        $response = $this->request()->get($endpoint, $query);
+
+        if ($response->failed()) {
+            throw new AsaasException($response);
+        }
+
+        return $response->json();
+    }
+
+    public function post(string $endpoint, array $data = [])
+    {
+        $response = $this->request()->post($endpoint, $data);
+
+        if ($response->failed()) {
+            throw new AsaasException($response);
+        }
+
+        return $response->json();
+    }
+}

+ 6 - 0
config/services.php

@@ -35,4 +35,10 @@
         ],
     ],
 
+    'asaas' => [
+        'base_url' => env('ASAAS_BASE_URL', 'https://sandbox.asaas.com/api/v3'),
+        'api_key' => env('ASAAS_API_KEY'),
+        'webhook_token' => env('ASAAS_WEBHOOK_TOKEN'),
+    ],
+
 ];