HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux Bradford-Sitios 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: www-data (33)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/api_matriculas/app/Services/ApiTokuService.php
<?php

namespace App\Services;

use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ApiTokuService
{
    protected string $baseUrl = "https://api.trytoku.com";
    protected string $apiToken = ''; // apiToken
    protected string $accountKey;  // Llave única de cuenta de cobro (cuando aplica)

    public function __construct()
    {
        $firmaki = getConfiguration('toku');
        if (!$firmaki['status']) {
            throw new Exception("Integración está deshabilitada", 400);
        }

        $this->baseUrl = (!empty($firmaki['url_toku']) ? $firmaki['url_toku'] : 'https://api.trytoku.com');
        if (empty($this->baseUrl)) {
            throw new Exception("URL Api no configurada", 400);
        }

        $this->apiToken = $firmaki['api_token'];
        if (empty($this->apiToken)) {
            throw new Exception("APIKEY no configurada", 400);
        }
    }

    private function request(string $method, string $uri, array $options = [])
    {
        $headers = [
            'x-api-key'     => "{$this->apiToken}",
            'Content-Type'  => 'application/json',
            'Accept'        => 'application/json',
        ];

        $response = Http::withHeaders($headers)
            ->timeout(30)
            ->$method("{$this->baseUrl}{$uri}", $options);

        if ($response->failed()) {
            $errorResponse = json_decode($response->body(), true);
            $error = isset($errorResponse['error']['message']) ? $errorResponse['error']['message'] : $errorResponse['message'] ?? $response->body();
            throw new \Exception("Error en petición a Toku: $error", $response->status());
        }

        return $response->json();
    }

    // -------------------------
    // 🔹 CUSTOMERS
    // -------------------------
    public function createCustomer(array $data)
    {
        return $this->request('post', '/customers', $data);
    }
    public function updateCustomer(string $id, array $data)
    {
        return $this->request('put', "/customers/{$id}", $data);
    }
    public function getCustomer(string $id)
    {
        return $this->request('get', "/customers/{$id}");
    }
    public function deleteCustomer(string $id)
    {
        return $this->request('delete', "/customers/{$id}");
    }

    // -------------------------
    // 🔹 SUBSCRIPTIONS
    // -------------------------
    public function createSubscription(array $data)
    {
        return $this->request('post', '/subscriptions', $data);
    }
    public function updateSubscription(string $id, array $data)
    {
        return $this->request('put', "/subscriptions/{$id}", $data);
    }
    public function getSubscription(string $id)
    {
        return $this->request('get', "/subscriptions/{$id}");
    }
    public function deleteSubscription(string $id)
    {
        return $this->request('delete', "/subscriptions/{$id}");
    }
    public function getSubscriptionsByCustomer(string $customerId)
    {
        return $this->request('get', "/subscriptions/customer/{$customerId}");
    }

    // -------------------------
    // 🔹 INVOICES
    // -------------------------
    public function createInvoice(array $data)
    {
        return $this->request('post', '/invoices', $data);
    }
    public function updateInvoice(string $id, array $data)
    {
        return $this->request('put', "/invoices/{$id}", $data);
    }
    public function getInvoice(string $id)
    {
        return $this->request('get', "/invoices/{$id}");
    }
    public function voidInvoice(string $id, bool $delete = false)
    {
        return $this->request('post', "/invoices/{$id}/void", ['delete_invoice' => $delete]);
    }
    public function deleteInvoice(string $id)
    {
        return $this->request('delete', "/invoices/{$id}");
    }
    public function getInvoicesByCustomer(string $customerId)
    {
        return $this->request('get', "/invoices/customer/{$customerId}");
    }

    // -------------------------
    // 🔹 TRANSACTIONS
    // -------------------------
    public function getTransactions(array $filters = [])
    {
        return $this->request('get', '/transactions', $filters);
    }
    public function getTransaction(string $id)
    {
        return $this->request('get', "/transactions/{$id}");
    }
    public function getTransactionFees(string $id)
    {
        return $this->request('get', "/transactions/{$id}/fees");
    }

    // -------------------------
    // 🔹 PAYMENTS
    // -------------------------
    public function createOrUpdatePayments(array $payments)
    {
        return $this->request('post', '/payments', ['payments' => $payments]);
    }
    public function updatePayment(array $data)
    {
        return $this->request('put', '/payments', $data);
    }
    public function deletePayments(array $payments)
    {
        return $this->request('delete', '/payments', ['payments' => $payments]);
    }
    public function getPayments(array $filters = [])
    {
        return $this->request('get', '/organization/payments', $filters);
    }
    public function getPayment(string $id)
    {
        return $this->request('get', "/payments/{$id}");
    }

    // -------------------------
    // 🔹 PAYMENT METHODS
    // -------------------------
    public function getPaymentMethods(array $filters = [])
    {
        return $this->request('get', '/organization/payment_methods', $filters);
    }
    public function getPaymentMethod(array $filters = [])
    {
        return $this->request('get', '/payment_method', $filters);
    }

    public function deletePaymentMethod(string $id)
    {
        return $this->request('delete', "/payment_methods/{$id}");
    }

    public function getCustomerPaymentMethods(string $customerId, array $params = [])
    {
        return $this->request('get', "/payment-methods/customers/{$customerId}", $params);
    }

    public function associatePaymentMethod(array $data)
    {
        return $this->request('post', '/subscriptions/associate/payment-method', $data);
    }
    public function associatePaymentMethodsBatch(array $data)
    {
        return $this->request('post', '/subscriptions/associate/payment-method/batch', ['data' => $data]);
    }

    // -------------------------
    // 🔹 WEBHOOKS
    // -------------------------
    public function createWebhook(array $data)
    {
        return $this->request('post', '/webhook_endpoints', $data);
    }
    public function updateWebhook(string $id, array $data)
    {
        return $this->request('put', "/webhook_endpoints/{$id}", $data);
    }
    public function getWebhook(string $id)
    {
        return $this->request('get', "/webhook_endpoints/{$id}");
    }
    public function getWebhooks()
    {
        return $this->request('get', '/webhook_endpoints');
    }
    public function testWebhook(array $data)
    {
        return $this->request('post', '/dummy_webhook', $data);
    }

    // -------------------------
    // 🔹 REDIRECTIONS
    // -------------------------
    public function createRedirection(array $data)
    {
        return $this->request('post', '/redirection', $data);
    }
    public function updateRedirection(array $data)
    {
        return $this->request('put', '/redirection', $data);
    }

    // -------------------------
    // 🔹 SETTLEMENTS (PAYOUTS)
    // -------------------------
}