1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

get and refresh tokens

This commit is contained in:
karneaud 2024-08-08 12:50:29 -04:00
parent 32c0006825
commit c7817f9bfd

View File

@ -8,6 +8,9 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use QuickBooksOnline\API\DataService\DataService; use QuickBooksOnline\API\DataService\DataService;
use App\Services\Import\Quickbooks\Auth as QuickbooksService;
use App\Services\Import\Quickbooks\Repositories\CompanyTokensRepository;
class QuickbooksSDKFactory class QuickbooksSDKFactory
{ {
@ -18,14 +21,16 @@ class QuickbooksSDKFactory
if(($user = Auth::user())) if(($user = Auth::user()))
{ {
$company = $user->company(); $company = $user->company();
MultiDB::findAndSetDbByCompanyKey($company->company_key);
// Retrieve token from the database $tokens = (new CompanyTokensRepository($company->company_key));
if(($quickbooks = DB::table('companies')->where('id', $company->id)->select(['quickbook_refresh_token','quickbooks_realm_id'])->first())) { $tokens = array_filter($tokens->get());
$refreshTokenKey = $quickbooks->quickbooks_refresh_token; if(!empty($tokens)) {
$QBORealmID = $quickbooks->quickbooks_realm_id; $keys = ['refreshTokenKey','QBORealmID'];
// Retrieve value from cache if(array_key_exists('access_token', $tokens)) {
$accessTokenKey = Cache::get($company->company_key); $keys = array_merge(['accessTokenKey'] ,$keys);
$tokens = compact('accessTokenKey','refreskTokenKey','QBORealmID'); }
$tokens = array_combine($keys, array_values($tokens));
} }
} }
@ -40,6 +45,12 @@ class QuickbooksSDKFactory
$sdk->setMinorVersion("73"); $sdk->setMinorVersion("73");
$sdk->throwExceptionOnError(true); $sdk->throwExceptionOnError(true);
if(array_key_exists('refreshTokenKey', $config) && !array_key_exists('accessTokenKey', $config))
{
$auth = new QuickbooksService($sdk);
$tokens = $auth->refreshTokens();
$auth->saveTokens($tokens);
}
return $sdk; return $sdk;
} }