apilo - refresh token fix

This commit is contained in:
2025-11-25 10:27:03 +00:00
parent 7140100a08
commit 97274ccde1
5 changed files with 103 additions and 70 deletions

View File

@@ -3,27 +3,27 @@
//error_reporting(LC_ALL);
//ini_set('display_errors', 1);
// ?XDEBUG_SESSION_START=PHPSTORM
//apilo_importInvoices();
function apilo_importInvoices()
{
$apilo_config = apilo_loadConfiguration();
$db = $GLOBALS['db'];
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'");
$offset = intval($db->fetchByAssoc($dbRes)['last_id']);
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
brecho($apilo_config);
if (isset($invoices->errors)) {
if (isset($invoices->error)) {
if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
//$apilo_config = apilo_loadConfiguration();
//$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
$apilo_config = apilo_loadConfiguration();
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
} else {
return false;
}
}
brecho($invoices);
brecho(count($invoices->documents));
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents));
@@ -56,7 +56,7 @@ function apilo_loadInvoices($token, $offset)
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
$params = [
'type' => 1,
'limit' => 50,
'limit' => 100,
'offset' => $offset
];
$url .= '?' . http_build_query($params);
@@ -105,19 +105,19 @@ function apilo_refreshToken($refreshToken, $clientId, $clientSecret)
$err = curl_error($curl);
curl_close($curl);
brecho($response);
brecho($httpCode);
if ($httpCode !== 200) {
if ($httpCode !== 201) {
return false;
}
$tokenData = json_decode($response, true);
brecho($tokenData);
var_dump($tokenData);
if (isset($tokenData['accessToken'])) {
global $db;
$db->query("UPDATE config SET value='" . $tokenData['access_token'] . "' WHERE category='apilo' AND name='access_token'");
if (isset($tokenData['refresh_token'])) {
$db->query("UPDATE config SET value='" . $tokenData['refresh_token'] . "' WHERE category='apilo' AND name='refresh_token'");
$db->query("UPDATE config SET value='" . $tokenData['accessToken'] . "' WHERE category='apilo' AND name='token'");
if (isset($tokenData['refreshToken'])) {
$db->query("UPDATE config SET value='" . $tokenData['refreshToken'] . "' WHERE category='apilo' AND name='refreshToken'");
}
return true;
}