geovanefss / laravel-api-moloni
A PHP package for interacting with Moloni APIs
Requires
- php: >=7.4
- guzzlehttp/guzzle: ^7.0
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- phpunit/phpunit: ^9.0
README
https://packagist.org/packages/geovanefss/laravel-api-moloni
Installation
To install this package, ensure you have Composer installed on your system. Then, run the following command in your project directory:
composer require geovanefss/laravel-api-moloni
Environment Configuration
In your .env
file, define the necessary environment variables to configure the Moloni API. This will keep your credentials secure and allow for easy configuration changes.
MOLONI_GRANT_TYPE=password MOLONI_CLIENT_ID=your_client_id MOLONI_CLIENT_SECRET=your_client_secret MOLONI_USERNAME=your_username MOLONI_PASSWORD=your_password
You can adjust these values based on your Moloni API credentials and usage requirements.
Usage Example
Below is an example demonstrating how to use this package in your Laravel project:
<?php require_once('vendor/autoload.php'); use Dotenv\Dotenv; use Geovanefss\LaravelApiMoloni\Exceptions\ApiException; use Geovanefss\LaravelApiMoloni\Exceptions\TokenException; use Geovanefss\LaravelApiMoloni\Exceptions\ValidationException; use Geovanefss\LaravelApiMoloni\Moloni; try { // Load environment variables $dotenv = Dotenv::createUnsafeImmutable(__DIR__); $dotenv->safeLoad(); // Set Moloni API configuration using environment variables $configs = [ // Required (see: https://www.moloni.pt/dev/autenticacao/) 'grant_type' => getenv('MOLONI_GRANT_TYPE'), // Required 'client_id' => getenv('MOLONI_CLIENT_ID'), // Required 'client_secret' => getenv('MOLONI_CLIENT_SECRET'), // Required (for authorize) 'response_type' => getenv('MOLONI_RESPONSE_TYPE'), // Required (for authorize and grant_type: authorization_code) 'redirect_uri' => getenv('MOLONI_REDIRECT_URI'), // Required (for grant_type: authorization_code) 'authorization_code' => getenv('MOLONI_AUTHORIZATION_CODE'), // Required (for grant_type: password) 'username' => getenv('MOLONI_USERNAME'), // Required (for grant_type: password) 'password' => getenv('MOLONI_PASSWORD'), // Required (for grant_type: refresh_token) 'refresh_token' => getenv('MOLONI_REFRESH_TOKEN'), ]; // Initialize Moloni instance $moloni = new Moloni($configs); // Start Debug API $moloni->startDebug(); // default is to not Debug // Stop Debug API $moloni->stopDebug(); // default is to not Debug // Stop Validate API $moloni->stopValidate(); // default is to Validate // Start Validate API $moloni->startValidate(); // default is to Validate // Example: Fetch user profile from Moloni API $resp = $moloni->myProfile()->getMe(); // Output the response in a readable format var_dump(json_encode($resp, JSON_PRETTY_PRINT)); } catch (ApiException | ValidationException | TokenException $e) { // Handle Moloni API-specific exceptions var_dump(get_class($e) . ': ' . $e->toString()); } catch (Exception $e) { // Handle general exceptions var_dump(get_class($e) . ': ' . $e->getMessage()); }
Explanation
Using Environment Variables
By placing the configuration values in the .env
file, you avoid hard-coding sensitive data like client credentials and user passwords. This makes the application:
- Easier to configure: Modify
.env
for different environments (e.g., production, staging, development). - More secure: Credentials are not exposed in source code.
Steps Breakdown:
- Environment Setup: Define all the necessary environment variables for the Moloni API configuration in the
.env
file. - Load Environment Variables: The
Dotenv
library is used to load these variables dynamically. - Configure the API: Pass the loaded environment variables to the
Moloni
API configuration array.
Moloni's API Documentation
For more details, please refer to the official Moloni API documentation: