A Laravel package for the Smartbill API, offering full compatibility with Laravel versions 11 and 12.
You can install the package via composer:
composer require andreilungeanu/smartbillYou can publish the configuration file with:
php artisan vendor:publish --provider="AndreiLungeanu\Smartbill\SmartbillServiceProvider"This will create a config/smartbill.php file in your application's config directory. You should add your Smartbill API credentials to your .env file:
SMARTBILL_USERNAME=your-username
SMARTBILL_API_TOKEN=your-api-token
You can interact with the API in two primary ways:
This is the most convenient method for use within a Laravel application.
use AndreiLungeanu\Smartbill\Facades\Smartbill;
$response = Smartbill::invoices()->create($invoiceData);This is useful for dependency injection within your own classes.
use AndreiLungeanu\Smartbill\Smartbill;
$smartbill = app(Smartbill::class);
$response = $smartbill->invoices()->create($invoiceData);Both of these methods work seamlessly because Laravel's service container automatically handles the creation of the required HTTP client and injects it into the package.
Direct instantiation with new Smartbill() is no longer possible due to the new dependency injection requirement. If you need to use this package outside of a Laravel application or wish to manually construct the object, you must now provide a configured Illuminate\Http\Client\PendingRequest instance to its constructor.
use AndreiLungeanu\Smartbill\Smartbill;
use Illuminate\Support\Facades\Http;
// Manually create and configure the HTTP client
$client = Http::withBasicAuth('your-username', 'your-api-token')
->baseUrl('https://ws.smartbill.ro/SBORO/api')
->acceptJson();
// Pass the configured client to the constructor
$smartbill = new Smartbill($client);
$response = $smartbill->invoices()->create($invoiceData);This example shows how to create a new invoice and retrieve its number.
use AndreiLungeanu\Smartbill\Facades\Smartbill;
$invoiceData = [
"companyVatCode" => "YOUR_COMPANY_VAT_CODE",
"client" => [
"name" => "UPBIT WEB DESIGN SRL",
"vatCode" => "39521446",
"isTaxPayer" => true,
"address" => "str. Suhurlui, nr. 8",
"city" => "Pechea",
"county" => "Galati",
"country" => "Romania",
"email" => "[email protected]",
"saveToDb" => false
],
"issueDate" => now()->format('Y-m-d'),
"seriesName" => "YOUR_INVOICE_SERIES",
"isDraft" => false,
"dueDate" => now()->addDays(14)->format('Y-m-d'),
"deliveryDate" => now()->format('Y-m-d'),
"products" => [
[
"name" => "Produs 1",
"isDiscount" => false,
"measuringUnitName" => "buc",
"currency" => "RON",
"quantity" => 1,
"price" => 10,
"saveToDb" => false,
"isService" => false
]
]
];
// The create method returns an array with the API response
try {
$response = Smartbill::invoices()->create($invoiceData);
// You can now access the invoice number
$invoiceNumber = $response['number']; // "0044"
} catch (\AndreiLungeanu\Smartbill\Exceptions\SmartbillApiException $e) {
// Handle Smartbill API errors
Log::error('Smartbill API error: ' . $e->getMessage());
// Optionally, show a user-friendly message or handle as needed
}A successful create call will return an array decoded from the following JSON structure:
{
"errorText": "",
"message": "",
"number": "0044",
"series": "SBINV",
"url": ""
}This example shows how to download the PDF content of an existing invoice.
use AndreiLungeanu\Smartbill\Facades\Smartbill;
use Illuminate\Support\Facades\Storage;
$cif = 'YOUR_COMPANY_VAT_CODE';
$series = 'SBINV';
$number = '0044';
// The getPdf method returns the raw PDF content as a string on success,
// or an array with error details if not found or failed.
try {
$pdfContent = Smartbill::invoices()->getPdf($cif, $series, $number);
Storage::disk('local')->put("invoices/{$series}-{$number}.pdf", $pdfContent);
} catch (\AndreiLungeanu\Smartbill\Exceptions\SmartbillApiException $e) {
Log::error('Smartbill PDF error: ' . $e->getMessage());
// Optionally, show a user-friendly message or handle as needed
}This is just a small sample of the available methods. For a complete list of all available endpoints and their parameters, please see the full documentation.
When working with the Smartbill API, there are a few known issues to be aware of:
-
Internal Server Errors on Invalid Request Data: The API may return a
500 Internal Server Errorwhen the request payload contains invalid data, such as a typo in a required field name.For example, sending
numeinstead ofnamein the client object will trigger a500error:$invoiceData = [ "companyVatCode" => "YOUR_COMPANY_VAT_CODE", "client" => [ "nume" => "Test Client SRL", // Incorrect: should be "name" "vatCode" => "12345678", // ... ], // ... ];
Ideally, the API should respond with a
400 Bad Requeststatus and a helpful error message detailing which field is incorrect. Instead, it returns a generic500error, which makes debugging difficult as it incorrectly suggests a server-side failure rather than a client-side mistake.
While this package attempts to mitigate these issues where possible, the fundamental problems lie with the API's implementation. We are awaiting fixes from the Smartbill provider to ensure more reliable and standards-compliant behavior.
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.