1 | <?php |
||
2 | |||
3 | // Autoload composer installed libraries |
||
4 | require __DIR__ . '/../vendor/autoload.php'; |
||
5 | |||
6 | /** |
||
7 | * Function to retrieve persisted data for the example. |
||
8 | * |
||
9 | * @param string $key |
||
10 | * |
||
11 | * @return null|string |
||
12 | */ |
||
13 | function getValue($key) |
||
14 | { |
||
15 | $storage = json_decode(file_get_contents('storage.json'), true); |
||
16 | if (array_key_exists($key, $storage)) { |
||
17 | return $storage[$key]; |
||
18 | } |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Function to persist some data for the example. |
||
23 | * |
||
24 | * @param string $key |
||
25 | * @param string $value |
||
26 | */ |
||
27 | function setValue($key, $value) |
||
28 | { |
||
29 | $storage = json_decode(file_get_contents('storage.json'), true); |
||
30 | $storage[$key] = $value; |
||
31 | file_put_contents('storage.json', json_encode($storage)); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Function to authorize with Exact, this redirects to Exact login promt and retrieves authorization code |
||
36 | * to set up requests for oAuth tokens. |
||
37 | */ |
||
38 | function authorize() |
||
39 | { |
||
40 | $connection = new \Picqer\Financials\Exact\Connection(); |
||
41 | $connection->setRedirectUrl('__REDIRECT_URL__'); |
||
42 | $connection->setExactClientId('__CLIENT_ID__'); |
||
43 | $connection->setExactClientSecret('__CLIENT_SECRET__'); |
||
44 | $connection->redirectForAuthorization(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Callback function that sets values that expire and are refreshed by Connection. |
||
49 | * |
||
50 | * @param \Picqer\Financials\Exact\Connection $connection |
||
51 | */ |
||
52 | function tokenUpdateCallback(\Picqer\Financials\Exact\Connection $connection) |
||
53 | { |
||
54 | // Save the new tokens for next connections |
||
55 | setValue('accesstoken', $connection->getAccessToken()); |
||
56 | setValue('refreshtoken', $connection->getRefreshToken()); |
||
57 | |||
58 | // Save expires time for next connections |
||
59 | setValue('expires_in', $connection->getTokenExpires()); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Function to connect to Exact, this creates the client and automatically retrieves oAuth tokens if needed. |
||
64 | * |
||
65 | * @throws Exception |
||
66 | * |
||
67 | * @return \Picqer\Financials\Exact\Connection |
||
68 | */ |
||
69 | function connect() |
||
70 | { |
||
71 | $connection = new \Picqer\Financials\Exact\Connection(); |
||
72 | $connection->setRedirectUrl('__REDIRECT_URL__'); |
||
73 | $connection->setExactClientId('__CLIENT_ID__'); |
||
74 | $connection->setExactClientSecret('__CLIENT_SECRET__'); |
||
75 | |||
76 | // Retrieves authorizationcode from database |
||
77 | if (getValue('authorizationcode')) { |
||
78 | $connection->setAuthorizationCode(getValue('authorizationcode')); |
||
79 | } |
||
80 | |||
81 | // Retrieves accesstoken from database |
||
82 | if (getValue('accesstoken')) { |
||
83 | $connection->setAccessToken(getValue('accesstoken')); |
||
84 | } |
||
85 | |||
86 | // Retrieves refreshtoken from database |
||
87 | if (getValue('refreshtoken')) { |
||
88 | $connection->setRefreshToken(getValue('refreshtoken')); |
||
89 | } |
||
90 | |||
91 | // Retrieves expires timestamp from database |
||
92 | if (getValue('expires_in')) { |
||
93 | $connection->setTokenExpires(getValue('expires_in')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
94 | } |
||
95 | |||
96 | // Set callback to save newly generated tokens |
||
97 | $connection->setTokenUpdateCallback('tokenUpdateCallback'); |
||
98 | |||
99 | // Make the client connect and exchange tokens |
||
100 | try { |
||
101 | $connection->connect(); |
||
102 | } catch (\Exception $e) { |
||
103 | throw new Exception('Could not connect to Exact: ' . $e->getMessage()); |
||
104 | } |
||
105 | |||
106 | return $connection; |
||
107 | } |
||
108 | |||
109 | // If authorization code is returned from Exact, save this to use for token request |
||
110 | if (isset($_GET['code']) && is_null(getValue('authorizationcode'))) { |
||
111 | setValue('authorizationcode', $_GET['code']); |
||
112 | } |
||
113 | |||
114 | // If we do not have a authorization code, authorize first to setup tokens |
||
115 | if (getValue('authorizationcode') === null) { |
||
116 | authorize(); |
||
117 | } |
||
118 | |||
119 | // Create the Exact client |
||
120 | $connection = connect(); |
||
121 | |||
122 | // Get the journals from our administration |
||
123 | try { |
||
124 | $journals = new \Picqer\Financials\Exact\Journal($connection); |
||
125 | $result = $journals->get(); |
||
126 | foreach ($result as $journal) { |
||
127 | echo 'Journal: ' . $journal->Description . '<br>'; |
||
128 | } |
||
129 | } catch (\Exception $e) { |
||
130 | echo get_class($e) . ' : ' . $e->getMessage(); |
||
131 | } |
||
132 |