PHP code example of iamfredric / fortnox

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

    

iamfredric / fortnox example snippets


\Iamfredric\Fortnox\Fortnox::setClientCredentials(
    clientId: 'your-app-client-id',
    clientSecret: 'your-app-client-secret',
    redirectUrl: 'http://your-app-url/fortnox/callback',
    scope: 'your scopes separated by spaces' 
);

// Redirect user to Fortnox for authentication
$url = \Iamfredric\Fortnox\Fortnox::authUrl();

// In your response handler, when fortnox redirects user back to your app,
$response = \Iamfredric\Fortnox\Fortnox::verifyAuthCode($_GET['code']);
$response['access_token'];
$response['refresh_token'];
$response['scope'];
$response['expires_in'];
$response['token_type'];

// You need to pass an object that implements \Iamfredric\Fortnox\Contracts\Authenticatable
// to the authenticateAs method. This object will be used to get the access token and refresh 
// your access token when it expires.
\Iamfredric\Fortnox\Fortnox::authenticateAs(new class implements \Iamfredric\Fortnox\Contracts\Authenticatable {
    public function getFortnoxAccessToken(): string
    {
        return 'your-access-token';
    }
 
    public function getFortnoxRefreshToken(): string
    {
        return 'your-refresh-token'; 
    }

    public function getFortnoxExpiresAt(): DateTime
    {
        return new DateTime('Expiration date for token');
    }

    public function onFortnoxUpdate($data): void
    {
        // Update your database with the new access token and refresh token
    }
});

use \Iamfredric\Fortnox\Resources\Customer;

$customers = Customer::all()

foreach ($customers as $customer) {
    /**  @var $customer Customer */
}

use \Iamfredric\Fortnox\Resources\Customer;

$customer = Customer::find(1);

use \Iamfredric\Fortnox\Resources\Customer;

$customer = Customer::create([
    'Name' => 'Acme INC'
]);

use \Iamfredric\Fortnox\Resources\Customer;

$customer = Customer::find(1);
// Or
$customer = new Customer([
    "CustomerNumber" => "1",
])

$customer->update([
    'Name' => 'Acme INC'; 
])
// Or
$customer->Name = 'Acme INC';
$customer->save();

use \Iamfredric\Fortnox\Resources\Customer;

$customer = Customer::find(1);

$customer->delete();

class Order extends \Iamfredric\Fortnox\Resources\Resource
{
    protected function getIdKey(): string
    {
        return 'DocumentNumber';
    }
}

Order::all();
$order = Order::find(1);
$order->update([]);
$order->save();
$order->delete();