forked from picqer/exact-php-client
/
example.php
88 lines (72 loc) · 2.46 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
require 'vendor/autoload.php';
function getValue($key)
{
$storage = json_decode(file_get_contents('storage.json'), true);
if (array_key_exists($key, $storage)) {
return $storage[$key];
}
return null;
}
function setValue($key, $value)
{
$storage = json_decode(file_get_contents('storage.json'), true);
$storage[$key] = $value;
file_put_contents('storage.json', json_encode($storage));
}
function authorize()
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('YOUR_REDIRECT_URL');
$connection->setExactClientId('YOUR_CLIENT_ID');
$connection->setExactClientSecret('YOUR_CLIENT_SECRET');
$connection->redirectForAuthorization();
}
function connect()
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('YOUR_REDIRECT_URL');
$connection->setExactClientId('YOUR_CLIENT_ID');
$connection->setExactClientSecret('YOUR_CLIENT_SECRET');
if (getValue('authorizationcode')) // Retrieves authorizationcode from database
{
$connection->setAuthorizationCode(getValue('authorizationcode'));
}
if (getValue('accesstoken')) // Retrieves accesstoken from database
{
$connection->setAccessToken(getValue('accesstoken'));
}
if (getValue('refreshtoken')) // Retrieves refreshtoken from database
{
$connection->setRefreshToken(getValue('refreshtoken'));
}
if (getValue('expires_in')) // Retrieves expires from database
{
$connection->setTokenExpires(getValue('expires_in'));
}
// Make the client connect and exchange tokens
try {
$connection->connect();
} catch (\Exception $e) {
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}
// Save the new tokens for next connections
setValue('accesstoken', $connection->getAccessToken());
setValue('refreshtoken', $connection->getRefreshToken());
setValue('expires_in', $connection->getTokenExpires());
return $connection;
}
if (isset($_GET['code']) && is_null(getValue('authorizationcode'))) {
setValue('authorizationcode', $_GET['code']);
}
$connection = connect();
try {
$journals = new \Picqer\Financials\Exact\Journal($connection);
$result = $journals->get();
foreach ($result as $journal) {
echo 'journal: ' . $journal->Description . '<br>';
}
echo 'done';
} catch (\Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}