@@ -25,88 +25,152 @@ composer require fiscalapi/fiscalapi
2525
2626## ⚙️ Configuración
2727
28- Puedes usar el SDK tanto en aplicaciones sin inyección de dependencias como en proyectos que usan DI ( Laravel, Symfony, etc.). A continuación se describen ambas formas:
28+ El SDK se puede utilizar tanto en aplicaciones básicas de PHP como en frameworks que utilizan inyección de dependencias (como Laravel o Symfony).
2929
30- ### A) Aplicaciones sin Inyección de Dependencias
30+ ### 1. Configuración de Variables de Entorno
3131
32- 1 . ** Crea tu objeto de configuración** con [ tus credenciales] ( https://docs.fiscalapi.com/credentials-info ) :
33- ``` php
34- $settings = [
35- 'apiUrl' => 'https://test.fiscalapi.com', // https://live.fiscalapi.com (producción)
36- 'apiKey' => '<tu _api_key >',
37- 'tenant' => '<tenant >'
38- ];
39- ```
32+ Crea o actualiza tu archivo ` .env ` con las siguientes variables:
4033
41- 2. **Crea la instancia del cliente**:
42- ```php
43- $fiscalApi = new \Fiscalapi\Services\FiscalApiClient($settings);
44- ```
34+ ```
35+ # FiscalAPI Configuration
36+ FISCALAPI_URL=https://test.fiscalapi.com # https://live.fiscalapi.com (produción)
37+ FISCALAPI_KEY=tu_api_key
38+ FISCALAPI_TENANT=tu_tenant_id
39+ FISCALAPI_DEBUG=false
40+ FISCALAPI_VERIFY_SSL=true
41+ FISCALAPI_API_VERSION=v4
42+ FISCALAPI_TIMEZONE=America/Mexico_City
43+ ```
4544
46- Para ejemplos completos, consulta [vanilla-php-examples](https://github.com/FiscalAPI/fiscalapi-samples-php-vanilla).
45+ ### Configuración con inyección de dependencias
4746
48- ---
47+ #### Laravel
4948
50- ### B) Aplicaciones con Inyección de Dependencias (Laravel, Symfony, etc.)
49+ ##### Paso 1: Crear el archivo de configuración
5150
52- 1. **Agrega la sección de configuración** en tu archivo de configuración:
51+ Crea el archivo ` config/fiscalapi.php ` :
52+
53+ ``` php
54+ <?php
55+
56+ return [
57+ 'apiUrl' => env('FISCALAPI_URL', 'https://test.fiscalapi.com'),
58+ 'apiKey' => env('FISCALAPI_KEY', ''),
59+ 'tenant' => env('FISCALAPI_TENANT', ''),
60+ 'debug' => env('FISCALAPI_DEBUG', false),
61+ 'verifySsl' => env('FISCALAPI_VERIFY_SSL', true),
62+ 'apiVersion' => env('FISCALAPI_API_VERSION', 'v4'),
63+ 'timeZone' => env('FISCALAPI_TIMEZONE', 'America/Mexico_City')
64+ ];
65+ ```
5366
54- **Laravel** (`config/fiscalapi.php`):
55- ```php
56- <?php
57- return [
58- 'apiUrl' => env('FISCALAPI_URL', 'https://test.fiscalapi.com'),
59- 'apiKey' => env('FISCALAPI_KEY', ''),
60- 'tenant' => env('FISCALAPI_TENANT', '')
61- ];
62- ```
67+ ##### Paso 2: Generar un Service Provider con Artisan
6368
64- 2. **Registra los servicios** en el contenedor (por ejemplo, en un Service Provider) :
69+ Utiliza el siguiente comando Artisan para generar el Service Provider:
6570
66- **Laravel**:
67- ```php
68- <?php
69-
70- namespace App\Providers;
71-
72- use Fiscalapi\Services\FiscalApiClient;
73- use Illuminate\Support\ServiceProvider;
74-
75- class FiscalApiServiceProvider extends ServiceProvider
71+ ``` bash
72+ php artisan make:provider FiscalApiServiceProvider
73+ ```
74+
75+ ##### Paso 3: Modificar el Service Provider generado
76+
77+ Abre el archivo creado en ` app/Providers/FiscalApiServiceProvider.php ` y modifícalo de la siguiente manera:
78+
79+ ``` php
80+ <?php
81+
82+ namespace App\Providers;
83+
84+ use Fiscalapi\Http\FiscalApiSettings;
85+ use Fiscalapi\Services\FiscalApiClient;
86+ use Illuminate\Support\ServiceProvider;
87+
88+ class FiscalApiServiceProvider extends ServiceProvider
89+ {
90+ /**
91+ * Register services.
92+ */
93+ public function register(): void
7694 {
77- public function register()
78- {
79- $this->app->singleton(FiscalApiClient::class, function ($app) {
80- return new FiscalApiClient(config('fiscalapi'));
81- });
82- }
95+ $this->mergeConfigFrom(
96+ __DIR__.'/../../config/fiscalapi.php', 'fiscalapi'
97+ );
98+
99+ $this->app->singleton(FiscalApiClient::class, function ($app) {
100+ $config = config('fiscalapi');
101+
102+ $settings = new FiscalApiSettings(
103+ $config['apiUrl'],
104+ $config['apiKey'],
105+ $config['tenant'],
106+ $config['debug'] ?? false,
107+ $config['verifySsl'] ?? true,
108+ $config['apiVersion'] ?? 'v4',
109+ $config['timeZone'] ?? 'America/Mexico_City'
110+ );
111+
112+ return new FiscalApiClient($settings);
113+ });
83114 }
84- ```
85-
86- 3. **Inyecta** `FiscalApiClient` donde lo requieras:
87115
88- ```php
89- <?php
90-
91- namespace App\Http\Controllers;
92-
93- use Fiscalapi\Services\FiscalApiClient;
94-
95- class InvoicesController extends Controller
116+ /**
117+ * Bootstrap services.
118+ */
119+ public function boot(): void
96120 {
97- private $fiscalApi;
98-
99- public function __construct(FiscalApiClient $fiscalApi)
100- {
101- $this->fiscalApi = $fiscalApi;
102- }
103-
104- // Usa $this->fiscalApi en tus métodos de controlador...
121+ $this->publishes([
122+ __DIR__.'/../../config/fiscalapi.php' => config_path('fiscalapi.php'),
123+ ], 'fiscalapi-config');
105124 }
106- ```
125+ }
126+
127+ ```
128+
129+ ##### Paso 4: Registrar el Service Provider
130+
131+ Dependiendo de tu versión de Laravel, registra el provider en la ubicación adecuada:
132+
133+ ** Laravel 8 y anteriores** - En ` config/app.php ` :
134+ ``` php
135+ 'providers' => [
136+ // Otros providers...
137+ App\Providers\FiscalApiServiceProvider::class,
138+ ],
139+ ```
140+
141+ ** Laravel 9+** - En ` bootstrap/providers.php ` :
142+ ``` php
143+ return [
144+ // Otros providers...
145+ App\Providers\AppServiceProvider::class,
146+ App\Providers\FiscalApiServiceProvider::class,
147+ ];
148+ ```
107149
108- Para más ejemplos, revisa [samples-laravel](https://github.com/FiscalAPI/fiscalapi-samples-php-laravel).
109150
151+ ##### Usar el cliente mediante inyección de dependencias donde lo necesites
152+
153+ ``` php
154+ <?php
155+ namespace App\Http\Controllers;
156+
157+ use Fiscalapi\Services\FiscalApiClient;
158+
159+ class FacturasController extends Controller
160+ {
161+ private $fiscalApi;
162+
163+ public function __construct(FiscalApiClient $fiscalApi)
164+ {
165+ $this->fiscalApi = $fiscalApi;
166+ }
167+
168+ public function createInvoice()
169+ {
170+ // Usar $this->fiscalApi para generar facturas
171+ }
172+ }
173+ ```
110174
111175## 🔄 Modos de Operación
112176
@@ -350,8 +414,8 @@ Este proyecto está licenciado bajo la Licencia **MPL**. Consulta el archivo [LI
350414
351415- [ Documentación Oficial] ( https://docs.fiscalapi.com )
352416- [ Portal de FiscalAPI] ( https://fiscalapi.com )
353- - [ Ejemplos PHP] ( https://github.com/FiscalAPI/fiscalapi-samples- php-vanilla )
354- - [ Ejemplos Laravel] ( https://github.com/FiscalAPI/fiscalapi-samples-php- laravel )
417+ - [ Ejemplos PHP] ( https://github.com/FiscalAPI/fiscalapi-php/blob/main/examples.php )
418+ - [ Ejemplos Laravel] ( https://github.com/FiscalAPI/fiscalapi-samples-laravel )
355419
356420
357421---
0 commit comments