File tree Expand file tree Collapse file tree 3 files changed +33
-14
lines changed Expand file tree Collapse file tree 3 files changed +33
-14
lines changed Original file line number Diff line number Diff line change 9
9
10
10
class XeroController extends Controller
11
11
{
12
+ private static $ scope = 'openid offline_access email profile accounting.contacts accounting.contacts.read accounting.transactions accounting.transactions.read ' ;
13
+
12
14
public function index ()
13
15
{
14
16
$ url = self ::join_links (Director::absoluteBaseURL () . 'xero ' );
15
17
16
18
$ provider = XeroFactory::singleton ()->getProvider ();
17
19
18
20
if (!isset ($ _GET ['code ' ])) {
21
+ $ scope = $ this ->config ()->get ('scope ' );
22
+
19
23
// If we don't have an authorization code then get one
20
24
$ authUrl = $ provider ->getAuthorizationUrl ([
21
- 'scope ' => ' openid offline_access email profile accounting.contacts accounting.transactions '
25
+ 'scope ' => $ scope
22
26
]);
23
27
24
28
$ _SESSION ['oauth2state ' ] = $ provider ->getState ();
@@ -50,7 +54,7 @@ public function index()
50
54
$ tenants = $ provider ->getTenants ($ token );
51
55
52
56
foreach ($ tenants as $ tenant ) {
53
- $ id = $ tenant ->id ;
57
+ $ id = $ tenant ->tenantId ;
54
58
55
59
$ obj ->XeroTenantId = $ id ;
56
60
$ obj ->write ();
Original file line number Diff line number Diff line change @@ -29,21 +29,34 @@ public function setupApplication()
29
29
{
30
30
$ provider = $ this ->getProvider ();
31
31
$ config = SiteConfig::current_site_config ();
32
+ $ refresh = !$ config ->XeroTokenRefreshExpires || $ config ->dbObject ('XeroTokenRefreshExpires ' )->InPast ();
32
33
33
- $ newAccessToken = $ provider ->getAccessToken ('refresh_token ' , [
34
- 'refresh_token ' => $ config ->XeroRefreshToken
35
- ]);
34
+ if ($ refresh ) {
35
+ $ newAccessToken = $ provider ->getAccessToken ('refresh_token ' , [
36
+ 'refresh_token ' => $ config ->XeroRefreshToken
37
+ ]);
36
38
37
- $ config ->XeroAccessToken = $ newAccessToken ->getToken ();
39
+ $ accessToken = $ newAccessToken ->getToken ();
40
+ $ refreshToken = $ newAccessToken ->getRefreshToken ();
38
41
39
- $ refresh = $ newAccessToken ->getRefreshToken ();
42
+ $ config ->XeroRefreshToken = $ refreshToken ;
43
+ $ config ->XeroAccessToken = $ accessToken ;
44
+ $ config ->XeroTokenRefreshExpires = $ newAccessToken ->getExpires ();
40
45
41
- if ($ refresh ) {
42
- $ config ->XeroRefreshToken = $ refresh ;
46
+ $ this ->tenants = $ provider ->getTenants ($ newAccessToken );
47
+
48
+ $ config ->XeroTenants = serialize ($ this ->tenants );
49
+ $ config ->write ();
50
+ } else {
51
+ $ accessToken = $ config ->XeroAccessToken ;
52
+
53
+ $ this ->tenants = unserialize ($ config ->XeroTenants );
43
54
}
44
55
45
- $ this ->tenants = $ provider ->getTenants ($ newAccessToken );
46
- $ config ->write ();
56
+ $ this ->application = new \XeroPHP \Application (
57
+ $ accessToken ,
58
+ $ config ->XeroTenantId
59
+ );
47
60
}
48
61
49
62
/**
Original file line number Diff line number Diff line change @@ -13,8 +13,10 @@ class XeroSiteConfigExtension extends DataExtension
13
13
{
14
14
private static $ db = [
15
15
'XeroTenantId ' => 'Varchar(200) ' ,
16
- 'XeroAccessToken ' => 'Varchar(200) ' ,
17
- 'XeroRefreshToken ' => 'Varchar(200) '
16
+ 'XeroAccessToken ' => 'Text ' ,
17
+ 'XeroRefreshToken ' => 'Text ' ,
18
+ 'XeroTokenRefreshExpires ' => 'Datetime ' ,
19
+ 'XeroTenants ' => 'Text '
18
20
];
19
21
20
22
public function updateCMSFields (FieldList $ fields )
@@ -40,7 +42,7 @@ public function updateCMSFields(FieldList $fields)
40
42
$ tenantRecords = XeroFactory::singleton ()->getTenants ($ this ->owner ->XeroAccessToken );
41
43
42
44
foreach ($ tenantRecords as $ tenant ) {
43
- $ tenants [$ tenant ->id ] = $ tenant ->tenantName ;
45
+ $ tenants [$ tenant ->tenantId ] = $ tenant ->tenantName ;
44
46
}
45
47
46
48
$ fields ->addFieldsToTab ('Root.Xero ' , [
You can’t perform that action at this time.
0 commit comments