1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Invoiced\OAuth1\Client\Server; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use League\OAuth1\Client\Credentials\ClientCredentials; |
9
|
|
|
use League\OAuth1\Client\Credentials\TokenCredentials; |
10
|
|
|
use League\OAuth1\Client\Server\Server; |
11
|
|
|
use League\OAuth1\Client\Signature\SignatureInterface; |
12
|
|
|
|
13
|
|
|
class Xero extends Server |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $responseType = 'xml'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $usePartnerApi = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $httpClientOptions = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function __construct($clientCredentials, SignatureInterface $signature = null) |
34
|
|
|
{ |
35
|
|
|
if (is_array($clientCredentials)) { |
36
|
|
|
$this->parseConfiguration($clientCredentials); |
37
|
|
|
|
38
|
|
|
$clientCredentials = $this->createClientCredentials($clientCredentials); |
39
|
|
|
|
40
|
|
|
if (!$signature && $clientCredentials instanceof RsaClientCredentials) { |
41
|
|
|
$signature = new RsaSha1Signature($clientCredentials); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
parent::__construct($clientCredentials, $signature); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets whether the Xero partner API should be used. |
50
|
|
|
* |
51
|
|
|
* @param bool $enable |
52
|
|
|
* |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
|
|
public function usePartnerApi($enable = true) |
56
|
|
|
{ |
57
|
|
|
$this->usePartnerApi = $enable; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks if the Xero partner API is used. |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function getUsePartnerApi() |
68
|
|
|
{ |
69
|
|
|
return $this->usePartnerApi; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates a Guzzle HTTP client for the given URL. |
74
|
|
|
* |
75
|
|
|
* @return GuzzleHttpClient |
76
|
|
|
*/ |
77
|
|
|
public function createHttpClient() |
78
|
|
|
{ |
79
|
|
|
return new GuzzleHttpClient($this->httpClientOptions); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function urlTemporaryCredentials() |
83
|
|
|
{ |
84
|
|
|
if ($this->usePartnerApi) { |
85
|
|
|
return 'https://api-partner.network.xero.com/oauth/RequestToken'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return 'https://api.xero.com/oauth/RequestToken'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function urlAuthorization() |
92
|
|
|
{ |
93
|
|
|
return 'https://api.xero.com/oauth/Authorize'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function urlTokenCredentials() |
97
|
|
|
{ |
98
|
|
|
if ($this->usePartnerApi) { |
99
|
|
|
return 'https://api-partner.network.xero.com/oauth/AccessToken'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return 'https://api.xero.com/oauth/AccessToken'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function urlUserDetails() |
106
|
|
|
{ |
107
|
|
|
return $this->notSupportedByXero(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function userDetails($data, TokenCredentials $tokenCredentials) |
111
|
|
|
{ |
112
|
|
|
return $this->notSupportedByXero(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function userUid($data, TokenCredentials $tokenCredentials) |
116
|
|
|
{ |
117
|
|
|
return $this->notSupportedByXero(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function userEmail($data, TokenCredentials $tokenCredentials) |
121
|
|
|
{ |
122
|
|
|
return $this->notSupportedByXero(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function userScreenName($data, TokenCredentials $tokenCredentials) |
126
|
|
|
{ |
127
|
|
|
return $this->notSupportedByXero(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function notSupportedByXero() |
131
|
|
|
{ |
132
|
|
|
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"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Parse configuration array to set attributes. |
137
|
|
|
* |
138
|
|
|
* @param array $configuration |
139
|
|
|
*/ |
140
|
|
|
private function parseConfiguration(array $configuration = array()) |
141
|
|
|
{ |
142
|
|
|
$configToPropertyMap = array( |
143
|
|
|
'partner' => 'usePartnerApi', |
144
|
|
|
'http_client' => 'httpClientOptions', |
145
|
|
|
); |
146
|
|
|
foreach ($configToPropertyMap as $config => $property) { |
147
|
|
|
if (isset($configuration[$config])) { |
148
|
|
|
$this->$property = $configuration[$config]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Creates a client credentials instance from an array of credentials. |
155
|
|
|
* |
156
|
|
|
* @param array $clientCredentials |
157
|
|
|
* |
158
|
|
|
* @return ClientCredentials |
159
|
|
|
*/ |
160
|
|
|
protected function createClientCredentials(array $clientCredentials) |
161
|
|
|
{ |
162
|
|
|
$keys = array('identifier', 'secret'); |
163
|
|
|
|
164
|
|
|
foreach ($keys as $key) { |
165
|
|
|
if (!isset($clientCredentials[$key])) { |
166
|
|
|
throw new InvalidArgumentException("Missing client credentials key [$key] from options."); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (isset($clientCredentials['rsa_private_key']) && isset($clientCredentials['rsa_public_key'])) { |
171
|
|
|
$_clientCredentials = new RsaClientCredentials(); |
172
|
|
|
$_clientCredentials->setRsaPrivateKey($clientCredentials['rsa_private_key']); |
173
|
|
|
$_clientCredentials->setRsaPublicKey($clientCredentials['rsa_public_key']); |
174
|
|
|
} else { |
175
|
|
|
$_clientCredentials = new ClientCredentials(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$_clientCredentials->setIdentifier($clientCredentials['identifier']); |
179
|
|
|
$_clientCredentials->setSecret($clientCredentials['secret']); |
180
|
|
|
|
181
|
|
|
if (isset($clientCredentials['callback_uri'])) { |
182
|
|
|
$_clientCredentials->setCallbackUri($clientCredentials['callback_uri']); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $_clientCredentials; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|