Many Swedish companies use Fortnox for their accounting. When we build an internal system for a client, the system often must send invoices to Fortnox or read data from it. The Fortnox API uses OAuth, and the access token expires after a short time. Each integration must do the same work before it can send one request.
We wrote this work one time, as a package: Laravel Fortnox.
What the package does
- It does the OAuth flow with Fortnox. It gives a redirect route and a callback route.
- It keeps the access token. By default, it keeps the token in the Laravel cache.
- It refreshes the token before it expires.
- It gives a fluent API for the most common Fortnox resources.
- It gives filters, sorting and pagination for the lists.
Install the package
composer require bernskioldmedia/laravel-fortnox
Make an application in the Fortnox Developer Portal. Then add the credentials and the scopes to your .env file:
FORTNOX_CLIENT_ID=your-client-id
FORTNOX_CLIENT_SECRET=your-client-secret
FORTNOX_SCOPES=invoice,customer,bookkeeping
The Fortnox documentation has the list of scopes. Request only the scopes that your application needs.
Connect to Fortnox
The package adds two routes: /oauth/fortnox and /oauth/fortnox/callback. Send an administrator to the first route. Fortnox asks the administrator to give access. Then Fortnox sends the administrator back to the callback route, and the package keeps the token.
After the callback, the package sends the user back to the page they came from. It adds a status and a message to the session, so you can show the result.
By default, the package connects as a service account. The actions are then not tied to one Fortnox user. If each user must connect with their own Fortnox account, set use_service_account to false in config/fortnox.php.
Keep the token valid
The package refreshes the token when you use the API and the token expires in less than five minutes. If your application does not use the API often, the refresh token can also expire. To prevent this, schedule the refresh command:
use Illuminate\Support\Facades\Schedule;
Schedule::command('fortnox:refresh-access-token')->everyMinute()->withoutOverlapping();
The command does nothing when the token is still valid. Use --force to refresh the token immediately.
Read and write data
Use the Fortnox facade. Each resource has a method, for example invoices(), customers() or vouchers().
use BernskioldMedia\Fortnox\Facades\Fortnox;
$invoices = Fortnox::invoices()
->unpaid()
->from(now()->startOfMonth())
->sortByDesc('invoicedate')
->perPage(100)
->all();
$invoices->get(); // A collection of invoices
$invoices->hasMore(); // True when there are more pages
To get, create, update or delete one record, use the methods of the resource:
$customer = Fortnox::customers()->get(1042);
Fortnox::customers()->update(1042, ['Email' => '[email protected]']);
Some resources have more actions. For an invoice, you can bookkeep it, credit it, cancel it or send it by email:
Fortnox::invoices()->bookkeep(2051);
Fortnox::invoices()->sendViaEmail(2051);
The package supports these resources: absence transactions, accounts, account charts, contracts, customers, financial years, invoices, projects, SIE exports, suppliers, supplier invoices, supplier invoice payments and vouchers.
Keep the token somewhere else
The cache is good for most applications. If you clear the cache when you deploy, the token is lost, and an administrator must connect again. In this case, keep the token in a different place.
The package includes a second storage provider that uses Spatie's Laravel Settings package. You can also write your own provider. Implement the TokenStorage interface and set the class in config/fortnox.php:
'storage_provider' => \App\Fortnox\DatabaseTokenStorage::class,
A limit to know
The package does not cover the full Fortnox API. It covers the resources that we needed in our projects. If you need a different resource, you can add it with a small class. We are happy to receive a pull request.
Get the package
Laravel Fortnox is open source, with the MIT license. The code is on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.