1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Invoiced\OAuth1\Client\Server; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
7
|
|
|
use League\OAuth1\Client\Credentials\TokenCredentials; |
8
|
|
|
use League\OAuth1\Client\Server\Server; |
9
|
|
|
use League\OAuth1\Client\Signature\SignatureInterface; |
10
|
|
|
|
11
|
|
|
class Xero extends Server |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $responseType = 'xml'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $usePartnerApi = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $httpClientOptions = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function __construct($clientCredentials, SignatureInterface $signature = null) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($clientCredentials, $signature); |
34
|
|
|
|
35
|
|
|
if (is_array($clientCredentials)) { |
36
|
|
|
$this->parseConfiguration($clientCredentials); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets whether the Xero partner API should be used. |
42
|
|
|
* |
43
|
|
|
* @param bool $enable |
44
|
|
|
* |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
|
|
public function usePartnerApi($enable = true) |
48
|
|
|
{ |
49
|
|
|
$this->usePartnerApi = $enable; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks if the Xero partner API is used. |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function getUsePartnerApi() |
60
|
|
|
{ |
61
|
|
|
return $this->usePartnerApi; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a Guzzle HTTP client for the given URL. |
66
|
|
|
* |
67
|
|
|
* @return GuzzleHttpClient |
68
|
|
|
*/ |
69
|
|
|
public function createHttpClient() |
70
|
|
|
{ |
71
|
|
|
return new GuzzleHttpClient($this->httpClientOptions); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function urlTemporaryCredentials() |
75
|
|
|
{ |
76
|
|
|
if ($this->usePartnerApi) { |
77
|
|
|
return 'https://api-partner.network.xero.com/oauth/RequestToken'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return 'https://api.xero.com/oauth/RequestToken'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function urlAuthorization() |
84
|
|
|
{ |
85
|
|
|
return 'https://api.xero.com/oauth/Authorize'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function urlTokenCredentials() |
89
|
|
|
{ |
90
|
|
|
if ($this->usePartnerApi) { |
91
|
|
|
return 'https://api-partner.network.xero.com/oauth/AccessToken'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return 'https://api.xero.com/oauth/AccessToken'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function urlUserDetails() |
98
|
|
|
{ |
99
|
|
|
return $this->notSupportedByXero(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function userDetails($data, TokenCredentials $tokenCredentials) |
103
|
|
|
{ |
104
|
|
|
return $this->notSupportedByXero(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function userUid($data, TokenCredentials $tokenCredentials) |
108
|
|
|
{ |
109
|
|
|
return $this->notSupportedByXero(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function userEmail($data, TokenCredentials $tokenCredentials) |
113
|
|
|
{ |
114
|
|
|
return $this->notSupportedByXero(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function userScreenName($data, TokenCredentials $tokenCredentials) |
118
|
|
|
{ |
119
|
|
|
return $this->notSupportedByXero(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function notSupportedByXero() |
123
|
|
|
{ |
124
|
|
|
throw new Exception("Xero's API does not support retrieving the current user. Please see https://xero.uservoice.com/forums/5528-xero-accounting-api/suggestions/5688571-expose-which-user-connected-the-organization-via-o"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Parse configuration array to set attributes. |
129
|
|
|
* |
130
|
|
|
* @param array $configuration |
131
|
|
|
*/ |
132
|
|
|
private function parseConfiguration(array $configuration = array()) |
133
|
|
|
{ |
134
|
|
|
$configToPropertyMap = array( |
135
|
|
|
'partner' => 'usePartnerApi', |
136
|
|
|
'http_client' => 'httpClientOptions', |
137
|
|
|
); |
138
|
|
|
foreach ($configToPropertyMap as $config => $property) { |
139
|
|
|
if (isset($configuration[$config])) { |
140
|
|
|
$this->$property = $configuration[$config]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|