PHP code example of facturino / facturino-php

1. Go to this page and download the library: Download facturino/facturino-php 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/ */

    

facturino / facturino-php example snippets




\Facturino\Facturino::setApiKey('fac_test_xxx');

// Create a customer
$customer = \Facturino\Customer::create([
    'name' => 'ACME Corp',
    'type' => 'company',
    'email' => '[email protected]',
    'siret' => '73282932000074',
    'address' => [
        'line1' => '42 rue des Acacias',
        'postalCode' => '75001',
        'city' => 'Paris',
        'country' => 'FR',
    ],
]);

// Create and finalize an invoice
$invoice = \Facturino\Invoice::create([
    'customerId' => $customer['id'],
    'buyer' => [
        'companyName' => 'Acme SAS',
        'siret' => '55208131766522',
        'address' => ['line1' => '10 rue de la Paix', 'postalCode' => '75002', 'city' => 'Paris', 'country' => 'FR'],
    ],
    'lines' => [[
        'description' => 'Consulting',
        'quantity' => '1',       // decimal string
        'unit' => 'flat_rate',
        'unitPrice' => 10000,    // 100.00 EUR in centimes
        'vatRate' => 2000,       // 20.00% in centipercent
        'vatCode' => 'S',
        // 'vatexCode' => 'VATEX-FR-261', // optional: specific exemption code (BT-121)
    ]],
    'dates' => [
        'issued' => '2026-01-15',
        'due' => '2026-02-15',
    ],
    'payment' => [
        'terms' => 'Paiement à 30 jours', 'termsDays' => 30, 'method' => 'transfer',
        'latePaymentRate' => '10.00', 'collectionFee' => '40.00',
    ],
]);
$invoice = \Facturino\Invoice::finalize($invoice['id']);

// Send to PA (Plateforme Agreee)
\Facturino\Invoice::send($invoice['id']);

// One-shot alternative — finalize (and optionally deliver) in the create call:
//   \Facturino\Invoice::create([..., 'autoFinalize' => true,
//       'autoSend' => ['email' => true, 'pa' => true]]);

// Record a payment
$payment = \Facturino\Payment::create($invoice['id'], [
    'amount' => 12000,    // 120.00 EUR (100 HT + 20 TVA)
    'method' => 'transfer',
    'paidAt' => '2026-02-10',
]);

// Cancel a payment (kept as "cancelled" for the audit trail)
\Facturino\Payment::cancel($invoice['id'], $payment['id']);

// Iterate over all invoices automatically
foreach (\Facturino\Invoice::all(['limit' => 10]) as $invoice) {
    echo $invoice['id'] . ' ' . $invoice['status'] . "\n";
}

// Or access the first page directly
$collection = \Facturino\Invoice::all(['limit' => 25]);
$firstPage = $collection->getData();
$hasMore = $collection->hasMore();

// Clone a quote as a new draft (mirrors Invoice::clone)
$draft = \Facturino\Quote::clone('quo_xxx');

// Convert an accepted quote to a draft invoice
$invoice = \Facturino\Quote::convert('quo_xxx');

// Invoices issued from a given quote
$invoices = \Facturino\Invoice::all(['convertedFrom' => 'quo_xxx']);

// Inline related resources on a single invoice. `expand` is
// comma-separated and accepts `customer`, `items.product` and
// `credit_notes`. With `credit_notes` the response gains
// `expanded.credit_notes` (array) and `expanded.net_balance` (string).
$invoice = \Facturino\Invoice::retrieve('inv_xxx', [
    'expand' => 'customer,credit_notes',
]);

// Product filters: q (name prefix), category, active
$products = \Facturino\Product::all([
    'q' => 'consult',
    'category' => 'services',
    'active' => true,
]);

\Facturino\Customer::create([
    'name' => 'ACME Corp',
    'type' => 'company',
    'contacts' => [
        ['name' => 'Compta', 'email' => '[email protected]', 'role' => 'billing'],
        ['name' => 'IT', 'email' => '[email protected]', 'role' => 'technical'],
    ],
]);

\Facturino\Company::update('comp_xxx', [
    'creditNoteSettings' => ['numberingMode' => 'unified'],
]);

$payload = file_get_contents('php://input');
$sigHeader = $_SERVER['HTTP_FACTURINO_SIGNATURE'];
$endpointSecret = 'whsec_xxx'; // From your webhook endpoint

try {
    $event = \Facturino\Webhook::constructEvent($payload, $sigHeader, $endpointSecret);

    switch ($event['type']) {
        case 'invoice.finalized':
            $invoice = $event['data'];
            // Handle finalized invoice
            break;
        case 'invoice.paid':
            // Handle payment
            break;
    }

    http_response_code(200);
    echo json_encode(['received' => true]);
} catch (\Facturino\Exception\InvalidRequestException $e) {
    http_response_code(400);
    echo json_encode(['error' => $e->getMessage()]);
}

$invoice = \Facturino\Invoice::create(
    ['customerId' => 'cus_xxx', 'buyer' => [...], 'lines' => [...], 'dates' => [...], 'payment' => [...]],
    'idem_unique_request_id_123'
);

use Facturino\Exception\AuthenticationException;
use Facturino\Exception\InvalidRequestException;
use Facturino\Exception\RateLimitException;
use Facturino\Exception\ApiException;

try {
    $invoice = \Facturino\Invoice::retrieve('inv_nonexistent');
} catch (AuthenticationException $e) {
    // Invalid API key (401)
    echo 'Auth error: ' . $e->getMessage();
} catch (RateLimitException $e) {
    // Rate limit exceeded (429) — SDK retries automatically up to 3 times
    echo 'Rate limited: ' . $e->getMessage();
} catch (InvalidRequestException $e) {
    // Client error (400-499)
    echo 'Error: ' . $e->getMessage();
    echo 'Code: ' . $e->getErrorCode();
    echo 'Param: ' . $e->getParam();
    echo 'Hint: ' . $e->getHint();
} catch (ApiException $e) {
    // Server error (500+) — SDK retries automatically up to 3 times
    echo 'API error: ' . $e->getMessage();
}

// 150.00 EUR HT with 20% VAT
$item = [
    'description' => 'Service',
    'quantity' => '1',
    'unit' => 'flat_rate',
    'unitPrice' => 15000,   // 150.00 EUR
    'vatRate' => 2000,      // 20.00%
    'vatCode' => 'S',
];

$result = \Facturino\Invoice::getPdf('inv_xxx');

if (isset($result['url'])) {
    // PDF already exists — download from signed URL
    $pdfUrl = $result['url'];
} else {
    // Async generation — poll the job
    $jobId = $result['id'];
    do {
        sleep(2);
        $job = \Facturino\Job::retrieve($jobId);
    } while ($job['status'] === 'pending');

    if ($job['status'] === 'completed') {
        $pdfUrl = $job['url'];
    }
}

\Facturino\Facturino::setApiKey('fac_test_xxx');

// Reset test data and load fixtures
\Facturino\Sandbox::resetData();

// Simulate PA status changes
\Facturino\Sandbox::simulateStatus('inv_xxx', 'deposited');
\Facturino\Sandbox::simulateStatus('inv_xxx', 'approved');

// Override API base URL (for testing or proxying)
\Facturino\Facturino::setApiBase('https://localhost:5001/api');
bash
composer 
bash
git clone https://github.com/facturino/facturino-php.git
cd facturino-php
composer install
vendor/bin/phpunit