2024-08-26 12:17:51 +02:00
|
|
|
<?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\Quickbooks;
|
|
|
|
|
|
|
|
use App\Factory\ClientContactFactory;
|
|
|
|
use App\Factory\ClientFactory;
|
|
|
|
use App\Factory\InvoiceFactory;
|
|
|
|
use App\Factory\ProductFactory;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Services\Quickbooks\Jobs\QuickbooksSync;
|
|
|
|
use QuickBooksOnline\API\Core\CoreConstants;
|
|
|
|
use QuickBooksOnline\API\DataService\DataService;
|
|
|
|
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
|
|
|
use App\Services\Quickbooks\Transformers\InvoiceTransformer;
|
|
|
|
use App\Services\Quickbooks\Transformers\PaymentTransformer;
|
|
|
|
use App\Services\Quickbooks\Transformers\ProductTransformer;
|
|
|
|
|
|
|
|
// quickbooks_realm_id
|
|
|
|
// quickbooks_refresh_token
|
|
|
|
// quickbooks_refresh_expires
|
|
|
|
class QuickbooksService
|
|
|
|
{
|
|
|
|
public DataService $sdk;
|
|
|
|
|
|
|
|
private bool $testMode = true;
|
|
|
|
|
|
|
|
public function __construct(private Company $company)
|
|
|
|
{
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function init(): self
|
|
|
|
{
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'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' => $this->testMode ? 'https://above-distinctly-teal.ngrok-free.app/quickbooks/authorized' : 'https://invoicing.co/quickbooks/authorized',
|
|
|
|
'baseUrl' => $this->testMode ? CoreConstants::SANDBOX_DEVELOPMENT : CoreConstants::QBO_BASEURL,
|
|
|
|
];
|
|
|
|
|
|
|
|
$merged = array_merge($config, $this->ninjaAccessToken());
|
|
|
|
|
|
|
|
$this->sdk = DataService::Configure($merged);
|
|
|
|
|
|
|
|
$this->sdk->setLogLocation(storage_path("logs/quickbooks.log"));
|
|
|
|
$this->sdk->enableLog();
|
|
|
|
|
|
|
|
$this->sdk->setMinorVersion("73");
|
|
|
|
$this->sdk->throwExceptionOnError(true);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function ninjaAccessToken()
|
|
|
|
{
|
|
|
|
return isset($this->company->quickbooks->accessTokenKey) ? [
|
|
|
|
'accessTokenKey' => $this->company->quickbooks->accessTokenKey,
|
|
|
|
'refreshTokenKey' => $this->company->quickbooks->refresh_token,
|
|
|
|
'QBORealmID' => $this->company->quickbooks->realmID,
|
|
|
|
] : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sdk(): SdkWrapper
|
|
|
|
{
|
|
|
|
return new SdkWrapper($this->sdk, $this->company);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* //@todo - refactor to a job
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function syncFromQb()
|
|
|
|
{
|
2024-08-27 03:49:01 +02:00
|
|
|
QuickbooksSync::dispatch($this->company->id, $this->company->db);
|
2024-08-26 12:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|