Skip to content

Commit 71266f9

Browse files
authored
Merge pull request #1 from LifeOnScreen/development
[Feature] Improved QuickBooks integration
2 parents 2afc3d1 + fe245c0 commit 71266f9

File tree

7 files changed

+84
-90
lines changed

7 files changed

+84
-90
lines changed

readme.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,38 @@ QB_BASE_URL=
4545

4646
### Connect QuickBooks account
4747

48-
Go to `{yourdomain}/quickbooks/connect` to
49-
connect your QuickBooks account with your application.
48+
To connect your application with your QuickBooks company you can use `QuickbooksConnect` helper.
49+
Helper has two methods:
50+
* `getAuthorizationUrl` -> Returns redirect URL and puts `quickbooks_auth` cookie into Laravel cookie queue.
51+
Cookie is valid for 30 minutes.
52+
* `processHook` -> Validates `quickbooks_auth` cookie and sets realm id, access token and refresh token.
53+
54+
Usage example:
55+
56+
```php
57+
namespace App\Http\Controllers;
58+
59+
use LifeOnScreen\LaravelQuickBooks\QuickbooksConnect;
60+
use Cookie;
61+
62+
class QuickBooksController extends Controller
63+
{
64+
public function connect()
65+
{
66+
return redirect(QuickbooksConnect::getAuthorizationUrl())
67+
->withCookies(Cookie::getQueuedCookies());
68+
}
69+
70+
public function refreshTokens()
71+
{
72+
if (QuickbooksConnect::processHook()) {
73+
return 'Tokens successfully refreshed.';
74+
}
75+
76+
return 'There were some problems refreshing tokens.';
77+
}
78+
}
79+
```
5080

5181
### Sync Eloquent model to QuickBooks
5282

src/Controllers/QuickbooksController.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/LaravelQuickBooksServiceProvider.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class LaravelQuickBooksServiceProvider extends ServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
$this->loadRoutesFrom(__DIR__ . '/Routes/routes.php');
17-
$this->loadViewsFrom(__DIR__ . '/path/to/views', 'courier');
18-
1916
// Publishing is only necessary when using the CLI.
2017
if ($this->app->runningInConsole()) {
2118

src/QuickBooksEntity.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ abstract class QuickBooksEntity extends Model implements QuickBookable
3030
*/
3131
protected $quickBooksConnection;
3232

33-
/**
34-
* QuickBooksEntity constructor.
35-
* @param array $attributes
36-
*/
37-
public function __construct(array $attributes = [])
38-
{
39-
$this->quickBooksConnection = App::make(QuickBooksConnection::class);
40-
parent::__construct($attributes);
41-
}
42-
4333
abstract protected function getQuickBooksArray(): array;
4434

4535
/**
@@ -140,6 +130,7 @@ protected function updateToQuickBooks(array $updateArray): bool
140130
*/
141131
public function syncToQuickbooks(): bool
142132
{
133+
$this->quickBooksConnection = App::make(QuickBooksConnection::class);
143134
$syncArray = $this->getQuickBooksArray();
144135
if (empty($this->quickbooks_id)) {
145136
return $this->insertToQuickBooks($syncArray);

src/QuickBooksResources.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LifeOnScreen\LaravelQuickBooks;
44

5-
use QuickBooksOnline\API\Facades AS QuickBooksFacades;
5+
use QuickBooksOnline\API\Facades as QuickBooksFacades;
66

77
/**
88
* Class QuickBooksResources

src/QuickbooksConnect.php

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,89 @@
22

33
namespace LifeOnScreen\LaravelQuickBooks;
44

5+
use Carbon\Carbon;
6+
use Cookie;
57
use Exception;
68
use QuickBooksOnline\API\DataService\DataService;
9+
use Request;
710

811
/**
912
* Class Init
1013
* @package LifeOnScreen\LaravelQuickBooks
1114
*/
1215
class QuickbooksConnect
1316
{
14-
/**
15-
* @var DataService
16-
*/
17-
private $dataService;
1817

1918
/**
20-
* Init constructor.
19+
* @return string
2120
* @throws \QuickBooksOnline\API\Exception\SdkException
2221
*/
23-
public function __construct()
22+
public static function getAuthorizationUrl(): string
2423
{
25-
$this->dataService = DataService::Configure([
26-
'auth_mode' => config('quickbooks.data-service.auth-mode'),
27-
'ClientID' => config('quickbooks.data-service.client-id'),
28-
'ClientSecret' => config('quickbooks.data-service.client-secret'),
29-
'RedirectURI' => config('quickbooks.data-service.redirect-uri'),
30-
'scope' => config('quickbooks.data-service.scope'),
31-
'baseUrl' => config('quickbooks.data-service.base-url')
32-
]);
33-
}
24+
$cookieValue = str_random(32);
25+
$validUntil = Carbon::now()->addMinutes(30)->timestamp;
26+
Cookie::queue(Cookie::make('quickbooks_auth', $cookieValue, 30));
27+
option(['qb-auth' => "{$cookieValue}|{$validUntil}"]);
3428

35-
/**
36-
* @throws \QuickBooksOnline\API\Exception\SdkException
37-
*/
38-
public function getAuthorizationUrl()
39-
{
40-
$OAuth2LoginHelper = $this->dataService->getOAuth2LoginHelper();
41-
42-
return $OAuth2LoginHelper->getAuthorizationCodeURL();
29+
return self::getDataService()->getOAuth2LoginHelper()->getAuthorizationCodeURL();
4330
}
4431

4532
/**
46-
* @param $requestCode
47-
* @param $realmID
33+
* Set realm id, access token and refresh token.
4834
* @return bool
4935
*/
50-
public function processHook(string $realmID, string $requestCode): bool
36+
public static function processHook(): bool
5137
{
38+
$realmID = Request::get('realmId');
39+
$requestCode = Request::get('code');
40+
if (empty($realmID) || empty($requestCode) || !self::cookieIsValid()) {
41+
return false;
42+
}
43+
5244
try {
53-
$OAuth2LoginHelper = $this->dataService->getOAuth2LoginHelper();
45+
46+
$OAuth2LoginHelper = self::getDataService()->getOAuth2LoginHelper();
5447
$accessTokenObj = $OAuth2LoginHelper->exchangeAuthorizationCodeForToken($requestCode, $realmID);
5548

5649
$accessTokenValue = $accessTokenObj->getAccessToken();
5750
$refreshTokenValue = $accessTokenObj->getRefreshToken();
5851
option(['qb-realm-id' => $realmID]);
5952
option(['qb-access-token' => $accessTokenValue]);
6053
option(['qb-refresh-token' => $refreshTokenValue]);
54+
6155
return true;
6256
} catch (Exception $e) {
6357
return false;
6458
}
59+
}
60+
61+
/**
62+
* Get QuickBooksOnline\API\DataService\DataService object.
63+
* @throws \QuickBooksOnline\API\Exception\SdkException
64+
*/
65+
protected static function getDataService(): DataService
66+
{
67+
return DataService::Configure([
68+
'auth_mode' => config('quickbooks.data-service.auth-mode'),
69+
'ClientID' => config('quickbooks.data-service.client-id'),
70+
'ClientSecret' => config('quickbooks.data-service.client-secret'),
71+
'RedirectURI' => config('quickbooks.data-service.redirect-uri'),
72+
'scope' => config('quickbooks.data-service.scope'),
73+
'baseUrl' => config('quickbooks.data-service.base-url')
74+
]);
75+
}
76+
77+
/**
78+
* Checks if the cookie is valid.
79+
* @return bool
80+
*/
81+
protected static function cookieIsValid(): bool
82+
{
83+
$validCookie = explode('|', option('qb-auth'));
84+
if ($validCookie[0] === Cookie::get('quickbooks_auth') && (int)$validCookie[1] > time()) {
85+
return true;
86+
}
6587

88+
return false;
6689
}
6790
}

src/Routes/routes.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)