PHP code example of tudorr89 / fgo-php-api-sdk

1. Go to this page and download the library: Download tudorr89/fgo-php-api-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tudorr89 / fgo-php-api-sdk example snippets


use FgoApi\Client;
use FgoApi\Enums\Environment;
use FgoApi\Types\AddressClient;
use FgoApi\Types\InvoiceLine;

$client = new Client(
    codUnic:     'YOUR_CUI',
    privateKey:  'YOUR_PRIVATE_KEY',
    platformUrl: 'https://your-app.com',
    environment: Environment::Test,
);

// Create an invoice
$invoice = $client->invoices()->create(
    series:      'BV',
    currency:    'RON',
    invoiceType: 'Factura',
    clientData:  new AddressClient(
        name:    'Ionescu Popescu',
        country: 'RO',
        county:  'Bucuresti',
        type:    'PF',
    ),
    lines: [
        new InvoiceLine(
            name:      'Servicii Consultanta',
            quantity:   2,
            unit:      'ORE',
            vatRate:   19,
            unitPrice: 150.00,
        ),
    ],
);

echo "Invoice: {$invoice->series}{$invoice->number}\n";
echo "PDF: {$invoice->pdfLink}\n";

use FgoApi\Laravel\Fgo;
use FgoApi\Types\AddressClient;
use FgoApi\Types\InvoiceLine;

$invoice = Fgo::invoices()->create(
    series:      'BV',
    currency:    'RON',
    invoiceType: 'Factura',
    clientData:  new AddressClient(name: 'Acme SRL'),
    lines:       [new InvoiceLine(name: 'Service', quantity: 1, unit: 'BUC', vatRate: 19, unitPrice: 100)],
);

use FgoApi\Client;

public function __construct(private readonly Client $fgo) {}

namespace App\Fgo;

use App\Models\Tenant;
use FgoApi\Laravel\Contracts\CredentialsResolver;

class TenantCredentialsResolver implements CredentialsResolver
{
    public function resolve(?string $key = null): array
    {
        $tenant = $key
            ? Tenant::findOrFail($key)
            : Tenant::current();

        return [
            'cod_unic'     => $tenant->fgo_cod_unic,
            'private_key'  => decrypt($tenant->fgo_private_key),
            'platform_url' => $tenant->platform_url,
            'environment'  => $tenant->fgo_environment, // "test" or "production"
        ];
    }
}

'resolver' => \App\Fgo\TenantCredentialsResolver::class,

use FgoApi\Laravel\Fgo;

// Current tenant — resolver is called with null
Fgo::invoices()->create(...);

// Specific tenant — resolver is called with the key, result cached for the request
Fgo::for($tenantId)->invoices()->create(...);

// Ad-hoc credentials (e.g. testing, one-off jobs)
Fgo::make([
    'cod_unic'     => '...',
    'private_key'  => '...',
    'platform_url' => 'https://my-app.test',
    'environment'  => 'test',
])->invoices()->getStatus('001', 'BV');

// After rotating credentials
Fgo::forget($tenantId);

// in a service provider
config(['fgo.resolver' => fn (?string $key) => Tenant::resolve($key)->fgoConfig()]);

use FgoApi\Hash;

// Invoice creation — Y', 'Client Name');

// Invoice operations (print, cancel, etc.) — ata
Hash::forArticle('CUI', 'PRIVATE_KEY');

// Full create example
$result = $client->invoices()->create(
    series:          'BV',
    currency:        'RON',
    invoiceType:     'Factura',
    clientData:      new AddressClient(
        name:       'SC Example SRL',
        fiscalCode: 'RO12345678',
        email:      '[email protected]',
        phone:      '0712345678',
        country:    'RO',
        county:     'Cluj',
        locality:   'Cluj-Napoca',
        address:    'Str. Principala, Nr. 10',
        type:       'PJ',
    ),
    lines: [
        new InvoiceLine(
            name:        'Dezvoltare Software',
            quantity:    1,
            unit:        'BUC',
            vatRate:     19,
            unitPrice:   5000.00,
            description: 'Modul facturare — luna aprilie',
        ),
    ],
    number:          null,
    issueDate:       date('Y-m-d'),
    dueDate:         null,
    checkDuplicate:  false,
    vatOnCollection: false,
);

// $result->number, $result->series, $result->pdfLink, $result->paymentLink, $result->stockInfo[]

$types = $client->nomenclatures()->invoiceTypes();
// [ { name: "Normal", value: "Factura" }, { name: "Simplified", value: "FacturaSimplificata" } ]

$result = $client->articles()->list(page: 1, perPage: 50);
// $result->total, $result->articles[] — each Article has name, unitPrice, stock, barcode, etc.

$warehouses = $client->warehouses()->list();
// { code: "WH001", name: "Main Warehouse" }

use FgoApi\Enums\Environment;

// Test (UAT)
new Client(..., environment: Environment::Test);

// Production
new Client(..., environment: Environment::Production);

// Custom URL
new Client(..., environment: 'https://custom-fgo.example.com/v1');

try {
    $invoice = $client->invoices()->create(...);
} catch (ValidationException $e) {
    // 400 / `Errors` map from API
    print_r($e->getErrors());
} catch (AuthenticationException $e) {
    // 401 / 403 — wrong CUI or private key
} catch (RateLimitException $e) {
    sleep(max(1, $e->getRetryAfter()));
    // ...retry
} catch (FgoApiException $e) {
    // All other API errors
}
bash
php artisan vendor:publish --tag=fgo-config
bash
git clone https://github.com/tudorr89/fgo-php-api-sdk
cd fgo-php-api-sdk
composer install

# Static analysis
composer analyse

# Run tests
composer test