|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakePHPify : CakePHP Plugin for Shopify API Authentication |
|
4
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
|
5
|
|
|
* Github : https://github.com/multidimension-al/cakephpify |
|
6
|
|
|
* |
|
7
|
|
|
* Licensed under The MIT License |
|
8
|
|
|
* For full copyright and license information, please see the LICENSE file |
|
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright (c) Multidimension.al (http://multidimension.al) |
|
12
|
|
|
* @link https://github.com/multidimension-al/cakephpify CakePHPify Github |
|
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Multidimensional\Cakephpify\Controller\Component; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\Controller\Component; |
|
19
|
|
|
use Cake\Core\Configure; |
|
20
|
|
|
use Cake\Event\Event; |
|
21
|
|
|
use Cake\Network\Exception\NotImplementedException; |
|
22
|
|
|
use Cake\Network\Http\Client; |
|
23
|
|
|
use Cake\Routing\Router; |
|
24
|
|
|
|
|
25
|
|
|
class ShopifyAPIComponent extends Component |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
public $apiKey; |
|
29
|
|
|
|
|
30
|
|
|
private $shopDomain; |
|
31
|
|
|
private $token; |
|
32
|
|
|
private $sharedSecret; |
|
33
|
|
|
private $privateApp; |
|
34
|
|
|
private $privateAppPassword; |
|
35
|
|
|
private $nonce; |
|
36
|
|
|
|
|
37
|
|
|
public $controller = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $config |
|
|
|
|
|
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
24 |
|
public function initialize(array $config = []) |
|
44
|
|
|
{ |
|
45
|
24 |
|
parent::initialize($config); |
|
46
|
|
|
|
|
47
|
24 |
|
$this->apiKey = isset($config['apiKey']) ? $config['apiKey'] : ''; |
|
48
|
|
|
|
|
49
|
24 |
|
if (!empty($this->apiKey)) { |
|
50
|
24 |
|
$this->sharedSecret = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.sharedSecret'); |
|
51
|
24 |
|
$this->scope = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.scope'); |
|
|
|
|
|
|
52
|
24 |
|
$this->privateApp = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.privateApp'); |
|
53
|
24 |
|
$this->privateAppPassword = Configure::read('Multidimensional/Cakephpify.' . $this->apiKey . '.privateAppPassword'); |
|
54
|
24 |
|
} else { |
|
55
|
|
|
throw new NotImplementedException(__('Shopify API key not found')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
24 |
|
if (!$this->sharedSecret) { |
|
59
|
|
|
throw new NotImplementedException(__('Shopify Shared Secret not found')); |
|
60
|
|
|
} |
|
61
|
24 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Event $event |
|
|
|
|
|
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
24 |
|
public function startup(Event $event) |
|
68
|
|
|
{ |
|
69
|
24 |
|
$this->setController($event->subject()); |
|
70
|
24 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param controller $controller |
|
|
|
|
|
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
24 |
|
public function setController($controller) |
|
77
|
|
|
{ |
|
78
|
24 |
|
$this->controller = $controller; |
|
79
|
24 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $shopDomain |
|
|
|
|
|
|
83
|
|
|
* @return string|null |
|
84
|
|
|
*/ |
|
85
|
6 |
|
public function setShopDomain($shopDomain) |
|
86
|
|
|
{ |
|
87
|
6 |
|
return $this->shopDomain = $shopDomain; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string|null |
|
92
|
|
|
*/ |
|
93
|
6 |
|
public function getShopDomain() |
|
94
|
|
|
{ |
|
95
|
6 |
|
return $this->shopDomain; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $shopDomain |
|
|
|
|
|
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
6 |
|
public function validDomain($shopDomain) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
6 |
|
return preg_match('/^([A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?)\.myshopify\.com$/i', $shopDomain) !== 0; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $token |
|
|
|
|
|
|
109
|
|
|
* @return string|null |
|
110
|
|
|
*/ |
|
111
|
6 |
|
public function setAccessToken($token) |
|
112
|
|
|
{ |
|
113
|
6 |
|
return $this->token = $token; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string|null |
|
118
|
|
|
*/ |
|
119
|
6 |
|
public function getAccessToken() |
|
120
|
|
|
{ |
|
121
|
6 |
|
return $this->token; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return int|null |
|
126
|
|
|
*/ |
|
127
|
|
|
public function callsMade() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->shopApiCallLimitParam(0); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return int|null |
|
134
|
|
|
*/ |
|
135
|
|
|
public function callLimit() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->shopApiCallLimitParam(1); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param Response $responseHeaders |
|
|
|
|
|
|
142
|
|
|
* @return int|null |
|
|
|
|
|
|
143
|
|
|
*/ |
|
144
|
|
|
public function callsLeft($responseHeaders) |
|
|
|
|
|
|
145
|
|
|
{ |
|
146
|
|
|
return $this->callLimit() - $this->callsMade(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $method |
|
|
|
|
|
|
151
|
|
|
* @param string $path |
|
|
|
|
|
|
152
|
|
|
* @param array $params |
|
|
|
|
|
|
153
|
|
|
* @return array|null |
|
154
|
|
|
*/ |
|
155
|
|
|
public function call($method, $path, $params = []) |
|
156
|
|
|
{ |
|
157
|
|
|
if (!$this->_isReady()) { |
|
158
|
|
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) { |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$http = new Client( |
|
166
|
|
|
[ |
|
167
|
|
|
'host' => $this->shopDomain, |
|
168
|
|
|
'scheme' => 'https', |
|
169
|
|
|
'headers' => (($this->privateApp != 'true') ? (['X-Shopify-Access-Token' => $this->token]) : []), |
|
170
|
|
|
'auth' => (($this->privateApp != 'true') ? [] : (['username' => $this->apiKey, 'password' => $this->privateAppPassword])) |
|
171
|
|
|
] |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
$this->response = $http->{strtolower($method)}( |
|
175
|
|
|
$path, |
|
176
|
|
|
((in_array($method, ['POST', 'PUT'])) ? json_encode($params) : $params), |
|
177
|
|
|
((in_array($method, ['POST', 'PUT'])) ? ['type' => 'json'] : []) |
|
178
|
|
|
); |
|
179
|
|
|
$this->response = $this->response->json; |
|
180
|
|
|
|
|
181
|
|
|
return (is_array($this->response) && (count($this->response) > 0)) ? array_shift($this->response) : $this->response; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param int $index |
|
|
|
|
|
|
186
|
|
|
* @return int |
|
187
|
|
|
*/ |
|
188
|
|
|
private function shopApiCallLimitParam($index) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$params = explode("/", $this->response->getHeaderLine('http_x_shopify_shop_api_call_limit')); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
return (int)$params[$index]; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $shopDomain |
|
|
|
|
|
|
197
|
|
|
* @param string $redirectUrl |
|
|
|
|
|
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getAuthorizeUrl($shopDomain, $redirectUrl) |
|
201
|
|
|
{ |
|
202
|
|
|
$url = 'https://' . $shopDomain . '/admin/oauth/authorize?client_id=' . $this->apiKey; |
|
203
|
|
|
$url .= '&scope=' . urlencode($this->scope); |
|
204
|
|
|
$url .= '&redirect_uri=' . urlencode($redirectUrl); |
|
205
|
|
|
$url .= '&state=' . $this->getNonce($shopDomain); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
return $url; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param string $shopDomain |
|
|
|
|
|
|
212
|
|
|
* @param string $code |
|
|
|
|
|
|
213
|
|
|
* @return string|false |
|
214
|
|
|
*/ |
|
215
|
|
|
public function requestAccessToken($shopDomain, $code) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->shopDomain = $shopDomain; |
|
218
|
|
|
|
|
219
|
|
|
$http = new Client( |
|
220
|
|
|
[ |
|
221
|
|
|
'host' => $shopDomain, |
|
222
|
|
|
'scheme' => 'https' |
|
223
|
|
|
] |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
$response = $http->post( |
|
227
|
|
|
'/admin/oauth/access_token', |
|
228
|
|
|
'client_id=' . $this->apiKey . |
|
229
|
|
|
'&client_secret=' . $this->sharedSecret . |
|
230
|
|
|
'&code=' . $code |
|
231
|
|
|
); |
|
232
|
|
|
$response = $response->json; |
|
233
|
|
|
; |
|
234
|
|
|
|
|
235
|
|
|
if (isset($response['access_token'])) { |
|
236
|
|
|
$this->token = $response['access_token']; |
|
237
|
|
|
|
|
238
|
|
|
return $this->token; |
|
239
|
|
|
} else { |
|
240
|
|
|
return false; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param string $shopDomain |
|
|
|
|
|
|
246
|
|
|
* @return string|null |
|
247
|
|
|
*/ |
|
248
|
6 |
|
public function setNonce($shopDomain) |
|
249
|
|
|
{ |
|
250
|
6 |
|
return $this->nonce = md5(strtolower($shopDomain)); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return string|null |
|
255
|
|
|
*/ |
|
256
|
6 |
|
public function getNonce() |
|
257
|
|
|
{ |
|
258
|
6 |
|
return $this->nonce; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return json |
|
|
|
|
|
|
263
|
|
|
*/ |
|
264
|
|
|
public function getShopData() |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->call('GET', '/admin/shop.json'); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param array $query |
|
|
|
|
|
|
271
|
|
|
* @return bool |
|
272
|
|
|
*/ |
|
273
|
6 |
|
public function validateHMAC($query) |
|
|
|
|
|
|
274
|
|
|
{ |
|
275
|
6 |
|
if (!is_array($query) || empty($query['hmac']) || !is_string($query['hmac']) || (isset($query['state']) && $query['state'] != $this->getNonce($query['shop']))) { |
|
|
|
|
|
|
276
|
6 |
|
return false; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
6 |
|
$dataString = []; |
|
280
|
|
|
|
|
281
|
6 |
|
foreach ($query as $key => $value) { |
|
282
|
6 |
|
$key = $this->_urlEncode(str_replace('=', '%3D', $key)); |
|
283
|
6 |
|
$value = $this->_urlEncode($value); |
|
284
|
6 |
|
if ($key != 'hmac') { |
|
285
|
|
|
$dataString[] = $key . '=' . $value; |
|
286
|
|
|
} |
|
287
|
6 |
|
} |
|
288
|
|
|
|
|
289
|
6 |
|
sort($dataString); |
|
290
|
6 |
|
$string = implode("&", $dataString); |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
6 |
|
return $query['hmac'] === hash_hmac('sha256', $string, $this->sharedSecret); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param string $url |
|
|
|
|
|
|
297
|
|
|
* @return string |
|
298
|
|
|
*/ |
|
299
|
6 |
|
private function _urlEncode($url) |
|
300
|
|
|
{ |
|
301
|
6 |
|
$url = str_replace('&', '%26', $url); |
|
302
|
6 |
|
$url = str_replace('%', '%25', $url); |
|
303
|
|
|
|
|
304
|
6 |
|
return $url; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @return bool |
|
309
|
|
|
*/ |
|
310
|
|
|
private function _isReady() |
|
|
|
|
|
|
311
|
|
|
{ |
|
312
|
|
|
return strlen($this->shopDomain) > 0 && strlen($this->token) > 0; |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|