Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
128 lines (109 loc) · 3.73 KB

File metadata and controls

128 lines (109 loc) · 3.73 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
// Autoload composer installed libraries
require __DIR__ . '/../vendor/autoload.php';
/**
* Function to retrieve persisted data for the example.
*
* @param string $key
*
* @return null|string|int
*/
function getValue(string $key)
{
$storage = json_decode(file_get_contents('storage.json'), true, 512, JSON_THROW_ON_ERROR);
if (array_key_exists($key, $storage)) {
return $storage[$key];
}
return null;
}
/**
* Function to persist some data for the example.
*
* @param string|int $value
*/
function setValue(string $key, $value)
{
$storage = json_decode(file_get_contents('storage.json'), true, 512, JSON_THROW_ON_ERROR);
$storage[$key] = $value;
file_put_contents('storage.json', json_encode($storage, JSON_THROW_ON_ERROR));
}
/**
* 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.
*/
function tokenUpdateCallback(\Picqer\Financials\Exact\Connection $connection): void
{
// 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
*/
function connect(): \Picqer\Financials\Exact\Connection
{
$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 an authorization code, authorize first to set up 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();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.