Refactor QB

This commit is contained in:
David Bomba 2024-08-21 13:22:46 +10:00
parent 9af3ffb77c
commit f8e06d7ca3
2 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,69 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Quickbooks;
use App\Models\Company;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Cache;
class AuthQuickbooksRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return is_array($this->getTokenContent());
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
//
];
}
/**
* Resolve one-time token instance.
*
* @return mixed
*/
public function getTokenContent()
{
if ($this->state) {
$this->token = $this->state;
}
$data = Cache::get($this->token);
return $data;
}
public function getContact(): ?User
{
return User::findOrFail($this->getTokenContent()['user_id']);
}
public function getCompany(): ?Company
{
return Company::where('company_key', $this->getTokenContent()['company_key'])->firstOrFail();
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Import\Quickbooks;
use App\Models\Company;
use QuickBooksOnline\API\DataService\DataService;
// quickbooks_realm_id
// quickbooks_refresh_token
// quickbooks_refresh_expires
class QuickbooksService
{
private DataService $sdk;
private Auth $auth;
public function __construct(private Company $company)
{
$this->init()
->auth();
}
private function init(): self
{
$this->sdk = DataService::Configure([
'ClientID' => config('services.quickbooks.client_id'),
'ClientSecret' => config('services.quickbooks.client_secret'),
'auth_mode' => 'oauth2',
'scope' => "com.intuit.quickbooks.accounting",
'RedirectURI' => 'https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl',
// 'RedirectURI' => route('quickbooks.authorized'),
]);
// if (env('APP_DEBUG')) {
// $sdk->setLogLocation(storage_path("logs/quickbooks.log"));
// $sdk->enableLog();
// }
$this->sdk->setMinorVersion("73");
$this->sdk->throwExceptionOnError(true);
return $this;
}
private function auth(): self
{
$wrapper = new SdkWrapper($this->sdk);
$this->auth = new Auth($wrapper);
return $this;
}
public function getSdk(): DataService
{
return $this->sdk;
}
public function getAuth(): Auth
{
return $this->auth;
}
}