diff --git a/src/XeroController.php b/src/XeroController.php index 5c8fbba..0138101 100644 --- a/src/XeroController.php +++ b/src/XeroController.php @@ -9,6 +9,8 @@ class XeroController extends Controller { + private static $scope = 'openid offline_access email profile accounting.contacts accounting.contacts.read accounting.transactions accounting.transactions.read'; + public function index() { $url = self::join_links(Director::absoluteBaseURL() . 'xero'); @@ -16,9 +18,11 @@ public function index() $provider = XeroFactory::singleton()->getProvider(); if (!isset($_GET['code'])) { + $scope = $this->config()->get('scope'); + // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl([ - 'scope' => 'openid offline_access email profile accounting.contacts accounting.transactions' + 'scope' => $scope ]); $_SESSION['oauth2state'] = $provider->getState(); @@ -50,7 +54,7 @@ public function index() $tenants = $provider->getTenants($token); foreach ($tenants as $tenant) { - $id = $tenant->id; + $id = $tenant->tenantId; $obj->XeroTenantId = $id; $obj->write(); diff --git a/src/XeroFactory.php b/src/XeroFactory.php index 77590c8..8283dbd 100644 --- a/src/XeroFactory.php +++ b/src/XeroFactory.php @@ -29,21 +29,34 @@ public function setupApplication() { $provider = $this->getProvider(); $config = SiteConfig::current_site_config(); + $refresh = !$config->XeroTokenRefreshExpires || $config->dbObject('XeroTokenRefreshExpires')->InPast(); - $newAccessToken = $provider->getAccessToken('refresh_token', [ - 'refresh_token' => $config->XeroRefreshToken - ]); + if ($refresh) { + $newAccessToken = $provider->getAccessToken('refresh_token', [ + 'refresh_token' => $config->XeroRefreshToken + ]); - $config->XeroAccessToken = $newAccessToken->getToken(); + $accessToken = $newAccessToken->getToken(); + $refreshToken = $newAccessToken->getRefreshToken(); - $refresh = $newAccessToken->getRefreshToken(); + $config->XeroRefreshToken = $refreshToken; + $config->XeroAccessToken = $accessToken; + $config->XeroTokenRefreshExpires = $newAccessToken->getExpires(); - if ($refresh) { - $config->XeroRefreshToken = $refresh; + $this->tenants = $provider->getTenants($newAccessToken); + + $config->XeroTenants = serialize($this->tenants); + $config->write(); + } else { + $accessToken = $config->XeroAccessToken; + + $this->tenants = unserialize($config->XeroTenants); } - $this->tenants = $provider->getTenants($newAccessToken); - $config->write(); + $this->application = new \XeroPHP\Application( + $accessToken, + $config->XeroTenantId + ); } /** diff --git a/src/XeroSiteConfigExtension.php b/src/XeroSiteConfigExtension.php index 436baba..49694ae 100644 --- a/src/XeroSiteConfigExtension.php +++ b/src/XeroSiteConfigExtension.php @@ -13,8 +13,10 @@ class XeroSiteConfigExtension extends DataExtension { private static $db = [ 'XeroTenantId' => 'Varchar(200)', - 'XeroAccessToken' => 'Varchar(200)', - 'XeroRefreshToken' => 'Varchar(200)' + 'XeroAccessToken' => 'Text', + 'XeroRefreshToken' => 'Text', + 'XeroTokenRefreshExpires' => 'Datetime', + 'XeroTenants' => 'Text' ]; public function updateCMSFields(FieldList $fields) @@ -40,7 +42,7 @@ public function updateCMSFields(FieldList $fields) $tenantRecords = XeroFactory::singleton()->getTenants($this->owner->XeroAccessToken); foreach ($tenantRecords as $tenant) { - $tenants[$tenant->id] = $tenant->tenantName; + $tenants[$tenant->tenantId] = $tenant->tenantName; } $fields->addFieldsToTab('Root.Xero', [