Clone the Repository
git clone --recurse-submodules https://github.com/controleonline/accounting.git
Ensure submodules are initialized (git submodule update --init --recursive).
API Documentation
/api/v1/accounts, /api/v1/invoices).First Use Case: Fetching Accounts
use ControleOnline\Accounting\Client;
$client = new Client(config('accounting.api_url'), config('accounting.api_key'));
$accounts = $client->getAccounts(); // Returns a collection of account objects
Laravel Configuration
Add to config/services.php:
'accounting' => [
'api_url' => env('ACCOUNTING_API_URL', 'https://api.controleonline.com'),
'api_key' => env('ACCOUNTING_API_KEY'),
],
Pattern: Wrap API calls in Laravel services to decouple logic from controllers.
namespace App\Services;
use ControleOnline\Accounting\Client;
class AccountingService {
protected $client;
public function __construct(Client $client) {
$this->client = $client;
}
public function getAccountBalance($accountId) {
return $this->client->getAccount($accountId)->balance;
}
}
Pattern: Trigger Laravel events for accounting actions (e.g., InvoiceCreated, PaymentProcessed).
// In a controller or service
event(new \App\Events\InvoiceCreated($invoiceData));
// Listen in EventServiceProvider
protected $listen = [
\App\Events\InvoiceCreated::class => [
\App\Listeners\SyncToAccounting::class,
],
];
Pattern: Sync Laravel models with the API using observers.
namespace App\Observers;
use App\Models\Invoice;
use ControleOnline\Accounting\Client;
class InvoiceObserver {
public function saved(Invoice $invoice) {
$client = app(Client::class);
$client->createInvoice($invoice->toArray());
}
}
Register in AppServiceProvider:
Invoice::observe(InvoiceObserver::class);
Pattern: Use Laravel’s throttle middleware for API calls.
Route::middleware(['throttle:60,1'])->group(function () {
Route::get('/accounting/sync', [AccountingController::class, 'sync']);
});
Pattern: Process webhooks from Controle Online (e.g., payment confirmations).
Route::post('/accounting/webhook', function (Request $request) {
$payload = $request->json()->all();
// Validate and process (e.g., update Laravel models)
});
.env is insecure. Use Laravel’s Vault or environment-based secrets.429 errors.Client wrapper:
use Illuminate\Support\Facades\Http;
$response = Http::retry(3, 100)->get($url);
due_date vs. vencimento).Mapper class to transform data:
class AccountingMapper {
public static function mapInvoice($apiData) {
return [
'due_date' => $apiData['vencimento'],
'status' => self::mapStatus($apiData['status']),
];
}
}
composer.json or .gitmodules:
"extra": {
"laravel": {
"submodule": {
"controleonline/api-platform-community": "a1b2c3d"
}
}
}
webhook package or manually verify signatures:
$expectedSignature = hash_hmac('sha256', $payload, config('accounting.webhook_secret'));
if (!hash_equals($request->header('X-Signature'), $expectedSignature)) {
abort(403);
}
Http::fake([
'api.controleonline.com/*' => Http::response([...], 200),
]);
Client class to add custom endpoints:
namespace App\Services;
use ControleOnline\Accounting\Client as BaseClient;
class CustomClient extends BaseClient {
public function getCustomReport($params) {
return $this->get('/reports/custom', $params);
}
}
Http::withOptions(['debug' => true])->get($url);
try-catch with JsonException:
try {
$response = $client->getAccounts();
} catch (\GuzzleHttp\Exception\RequestException $e) {
Log::error('Accounting API error: ' . $e->getResponse()->getBody());
}
How can I help you explore Laravel packages today?