22
33namespace LifeOnScreen \LaravelQuickBooks ;
44
5+ use Carbon \Carbon ;
6+ use Cookie ;
57use Exception ;
68use QuickBooksOnline \API \DataService \DataService ;
9+ use Request ;
710
811/**
912 * Class Init
1013 * @package LifeOnScreen\LaravelQuickBooks
1114 */
1215class 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}
0 commit comments