Skip to content
Log in

SDKs

Le SDK PHP de B2Brouter vous permet d’intégrer la facturation électronique et le reporting fiscal dans votre application PHP sans avoir à gérer des requêtes HTTP brutes.

  • Zéro dépendance — PHP 7.4+ avec extensions standard (cURL, JSON, mbstring)
  • Retry automatique — les erreurs réseau sont gérées avec un backoff exponentiel
  • Prise en charge de la paginationIterator et Countable pour les grands jeux de résultats
  • Prise en charge complète des tax reports — Verifactu, TicketBAI, SDI, KSeF, ZATCA, et plus encore
Terminal window
composer require b2brouter/b2brouter-php
<?php
require_once 'vendor/autoload.php';
use B2BRouter\B2BRouterClient;
$client = new B2BRouterClient('your-api-key');
$accountId = 'your-account-id';
$invoice = $client->invoices->create($accountId, [
'invoice' => [
'number' => 'INV-2025-001',
'date' => '2025-01-15',
'due_date' => '2025-02-15',
'currency' => 'EUR',
'contact' => [
'name' => 'Acme Corporation',
'tin_value' => 'ESB12345678',
'country' => 'ES',
'email' => 'billing@acme.com',
],
'invoice_lines_attributes' => [
[
'description' => 'Professional Services',
'quantity' => 10,
'price' => 100.00,
'taxes_attributes' => [
['name' => 'IVA', 'category' => 'S', 'percent' => 21.0]
]
]
]
]
]);
echo "Invoice created: {$invoice['id']}\n";
$client = new B2BRouterClient('your-api-key', [
// 'api_base' => 'https://api.b2brouter.net', // Sandbox & Production (par défaut)
// 'api_base' => 'https://api-staging.b2brouter.net', // Staging
'api_version' => '2026-04-20',
'timeout' => 80,
'max_retries' => 3,
]);

Le SDK utilise par défaut https://api.b2brouter.net. Utilisez une clé test_ pour le sandbox ou une clé de production pour la production. Définissez api_base sur https://api-staging.b2brouter.net uniquement si vous avez besoin de l’environnement staging.

Les intégrateurs et plugins peuvent ajouter leur identité à l’en-tête User-Agent :

$client = new B2BRouterClient('your-api-key', [
'app_info' => [
'name' => 'MyApp', // requis
'version' => '1.0.0', // optionnel
'url' => 'https://myapp.com', // optionnel
],
]);
$invoice = $client->invoices->create($accountId, [
'invoice' => [
'number' => 'INV-2025-001',
'date' => '2025-01-15',
'currency' => 'EUR',
'contact' => [
'name' => 'Customer Name',
'tin_value' => 'ESB12345678',
'country' => 'ES',
],
'invoice_lines_attributes' => [
[
'description' => 'Service',
'quantity' => 1,
'price' => 1000.00,
'taxes_attributes' => [
['name' => 'IVA', 'category' => 'S', 'percent' => 21.0]
]
]
]
],
'send_after_import' => false,
]);
$invoice = $client->invoices->retrieve($invoiceId);
$invoice = $client->invoices->update($invoiceId, [
'invoice' => ['extra_info' => 'Payment terms: 30 days net']
]);
$client->invoices->delete($invoiceId);
$invoices = $client->invoices->all($accountId, [
'limit' => 25,
'offset' => 0,
'date_from' => '2025-01-01',
'date_to' => '2025-12-31',
]);
foreach ($invoices as $invoice) {
echo "{$invoice['number']}: €{$invoice['total']}\n";
}
echo "Total: {$invoices->getTotal()}, has more: " . ($invoices->hasMore() ? 'yes' : 'no') . "\n";
$pdf = $client->invoices->downloadPdf($invoiceId);
file_put_contents('invoice.pdf', $pdf);
$client->invoices->send($invoiceId);
$client->invoices->acknowledge($invoiceId, ['ack' => true]);
$accounts = $client->accounts->all(['limit' => 25]);
$account = $client->accounts->retrieve($accountId);
$account = $client->accounts->create(['account' => [/* ... */]]);
$account = $client->accounts->update($accountId, ['account' => [/* ... */]]);
$client->accounts->delete($accountId);
$client->accounts->unarchive($accountId);

Les déclarations fiscales sont générées automatiquement lorsque vous envoyez des factures, à condition d’avoir configuré TaxReportSettings pour le compte. Consultez Tax Report Settings pour les instructions de configuration.

$client->taxReportSettings->create($accountId, [
'tax_report_setting' => [
'code' => 'verifactu',
'start_date' => '2025-01-01',
'auto_generate' => true,
'auto_send' => true,
]
]);
$taxReportId = $invoice['tax_report_ids'][0];
$taxReport = $client->taxReports->retrieve($taxReportId);
echo "State: {$taxReport['state']}\n";
if (!empty($taxReport['qr'])) {
file_put_contents('qr.png', base64_decode($taxReport['qr']));
}
$xml = $client->taxReports->download($taxReportId);
$client->taxReports->update($taxReportId, [
'tax_report' => ['tax_inclusive_amount' => 133.1, /* ... */]
]);
ÉtatSignification
processingEnchaînement et soumission en cours
registeredAccepté par l’administration fiscale
registered_with_errorsSoumis avec avertissements
errorÉchec de la soumission
annulledAnnulé
use B2BRouter\Exception\ApiErrorException;
use B2BRouter\Exception\AuthenticationException;
use B2BRouter\Exception\PermissionException;
use B2BRouter\Exception\ResourceNotFoundException;
use B2BRouter\Exception\InvalidRequestException;
use B2BRouter\Exception\ApiConnectionException;
try {
$invoice = $client->invoices->create($accountId, ['invoice' => [/* ... */]]);
} catch (AuthenticationException $e) {
// 401 — invalid API key
} catch (PermissionException $e) {
// 403 — insufficient permissions
} catch (ResourceNotFoundException $e) {
// 404
} catch (InvalidRequestException $e) {
// 400 / 422 — validation errors
$details = $e->getJsonBody();
} catch (ApiConnectionException $e) {
// network error
} catch (ApiErrorException $e) {
// any other API error
error_log("Request ID: {$e->getRequestId()}");
}

Enregistrez toujours le Request ID lorsque vous signalez un problème au support : il identifie de manière unique la requête ayant échoué.