-
Notifications
You must be signed in to change notification settings - Fork 200
/
example.php
131 lines (113 loc) · 3.71 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// Autoload composer installed libraries
require __DIR__ . '/../vendor/autoload.php';
/**
* Function to retrieve persisted data for the example.
*
* @param string $key
*
* @return null|string
*/
function getValue($key)
{
$storage = json_decode(file_get_contents('storage.json'), true);
if (array_key_exists($key, $storage)) {
return $storage[$key];
}
}
/**
* Function to persist some data for the example.
*
* @param string $key
* @param string $value
*/
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 to authorize with Exact, this redirects to Exact login promt and retrieves authorization code
* to set up requests for oAuth tokens.
*/
function authorize()
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('__REDIRECT_URL__');
$connection->setExactClientId('__CLIENT_ID__');
$connection->setExactClientSecret('__CLIENT_SECRET__');
$connection->redirectForAuthorization();
}
/**
* Callback function that sets values that expire and are refreshed by Connection.
*
* @param \Picqer\Financials\Exact\Connection $connection
*/
function tokenUpdateCallback(\Picqer\Financials\Exact\Connection $connection)
{
// Save the new tokens for next connections
setValue('accesstoken', $connection->getAccessToken());
setValue('refreshtoken', $connection->getRefreshToken());
// Save expires time for next connections
setValue('expires_in', $connection->getTokenExpires());
}
/**
* Function to connect to Exact, this creates the client and automatically retrieves oAuth tokens if needed.
*
* @throws Exception
*
* @return \Picqer\Financials\Exact\Connection
*/
function connect()
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('__REDIRECT_URL__');
$connection->setExactClientId('__CLIENT_ID__');
$connection->setExactClientSecret('__CLIENT_SECRET__');
// Retrieves authorizationcode from database
if (getValue('authorizationcode')) {
$connection->setAuthorizationCode(getValue('authorizationcode'));
}
// Retrieves accesstoken from database
if (getValue('accesstoken')) {
$connection->setAccessToken(getValue('accesstoken'));
}
// Retrieves refreshtoken from database
if (getValue('refreshtoken')) {
$connection->setRefreshToken(getValue('refreshtoken'));
}
// Retrieves expires timestamp from database
if (getValue('expires_in')) {
$connection->setTokenExpires(getValue('expires_in'));
}
// Set callback to save newly generated tokens
$connection->setTokenUpdateCallback('tokenUpdateCallback');
// Make the client connect and exchange tokens
try {
$connection->connect();
} catch (\Exception $e) {
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}
return $connection;
}
// If authorization code is returned from Exact, save this to use for token request
if (isset($_GET['code']) && is_null(getValue('authorizationcode'))) {
setValue('authorizationcode', $_GET['code']);
}
// If we do not have a authorization code, authorize first to setup tokens
if (getValue('authorizationcode') === null) {
authorize();
}
// Create the Exact client
$connection = connect();
// Get the journals from our administration
try {
$journals = new \Picqer\Financials\Exact\Journal($connection);
$result = $journals->get();
foreach ($result as $journal) {
echo 'Journal: ' . $journal->Description . '<br>';
}
} catch (\Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}