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 $api_key; |
|
|
|
|
29
|
|
|
|
30
|
|
|
private $shop_domain; |
|
|
|
|
31
|
|
|
private $token; |
32
|
|
|
private $shared_secret; |
|
|
|
|
33
|
|
|
private $is_private_app; |
|
|
|
|
34
|
|
|
private $private_app_password; |
|
|
|
|
35
|
|
|
private $nonce; |
36
|
|
|
|
37
|
|
|
public $controller = null; |
38
|
|
|
|
39
|
|
|
public function initialize(array $config = []) |
40
|
|
|
{ |
41
|
|
|
parent::initialize($config); |
42
|
|
|
|
43
|
|
|
$this->api_key = isset($config['api_key']) ? $config['api_key'] : ''; |
44
|
|
|
|
45
|
|
|
if (!empty($this->api_key)) { |
46
|
|
|
$this->shared_secret = Configure::read('Multidimensional/Cakephpify.' . $this->api_key . '.shared_secret'); |
47
|
|
|
$this->scope = Configure::read('Multidimensional/Cakephpify.' . $this->api_key . '.scope'); |
|
|
|
|
48
|
|
|
$this->is_private_app = Configure::read('Multidimensional/Cakephpify.' . $this->api_key . '.is_private_app'); |
49
|
|
|
$this->private_app_password = Configure::read('Multidimensional/Cakephpify.' . $this->api_key . '.private_app_password'); |
50
|
|
|
} else { |
51
|
|
|
throw new NotImplementedException(__('Shopify API key not found')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$this->shared_secret) { |
55
|
|
|
throw new NotImplementedException(__('Shopify shared secret not found')); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Event $event |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function startup(Event $event) |
64
|
|
|
{ |
65
|
|
|
$this->setController($event->subject()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $controller |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function setController($controller) |
73
|
|
|
{ |
74
|
|
|
$this->controller = $controller; |
75
|
|
|
if (!isset($this->controller->paginate)) { |
76
|
|
|
$this->controller->paginate = []; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $shopDomain |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
|
|
public function setShopDomain($shopDomain) |
85
|
|
|
{ |
86
|
|
|
return $this->shop_domain = $shopDomain; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
|
|
public function getShopDomain() |
93
|
|
|
{ |
94
|
|
|
return $this->shop_domain; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $token |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
|
|
public function setAccessToken($token) |
102
|
|
|
{ |
103
|
|
|
return $this->token = $token; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return int|null |
108
|
|
|
*/ |
109
|
|
|
public function callsMade() |
110
|
|
|
{ |
111
|
|
|
return $this->shopApiCallLimitParam(0); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return int|null |
116
|
|
|
*/ |
117
|
|
|
public function callLimit() |
118
|
|
|
{ |
119
|
|
|
return $this->shopApiCallLimitParam(1); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $responseHeaders |
124
|
|
|
* @return int|null |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
public function callsLeft($responseHeaders) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
return $this->callLimit() - $this->callsMade(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $method |
133
|
|
|
* @param string $path |
134
|
|
|
* @param array $params |
135
|
|
|
* @return array|null |
136
|
|
|
*/ |
137
|
|
|
public function call($method, $path, $params = []) |
138
|
|
|
{ |
139
|
|
|
if (!$this->_isReady()) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$http = new Client( |
148
|
|
|
[ |
149
|
|
|
'host' => $this->shop_domain, |
150
|
|
|
'scheme' => 'https', |
151
|
|
|
'headers' => (($this->is_private_app != 'true') ? (['X-Shopify-Access-Token' => $this->token]) : []), |
152
|
|
|
'auth' => (($this->is_private_app != 'true') ? [] : (['username' => $this->api_key, 'password' => $this->private_app_password])) |
153
|
|
|
] |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$this->response = $http->{strtolower($method)}( |
157
|
|
|
$path, |
158
|
|
|
((in_array($method, ['POST', 'PUT'])) ? json_encode($params) : $params), |
159
|
|
|
((in_array($method, ['POST', 'PUT'])) ? ['type' => 'json'] : []) |
160
|
|
|
); |
161
|
|
|
$this->response = $this->response->json; |
162
|
|
|
|
163
|
|
|
return (is_array($this->response) && (count($this->response) > 0)) ? array_shift($this->response) : $this->response; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param int $index |
168
|
|
|
* @return int |
169
|
|
|
*/ |
170
|
|
|
private function shopApiCallLimitParam($index) |
171
|
|
|
{ |
172
|
|
|
$params = explode("/", $this->response->getHeaderLine('http_x_shopify_shop_api_call_limit')); |
|
|
|
|
173
|
|
|
|
174
|
|
|
return (int) $params[$index]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $shopDomain |
179
|
|
|
* @param string $redirectUrl |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getAuthorizeUrl($shopDomain, $redirectUrl) |
183
|
|
|
{ |
184
|
|
|
$url = 'https://' . $shopDomain . '/admin/oauth/authorize?client_id=' . $this->api_key; |
185
|
|
|
$url .= '&scope=' . urlencode($this->scope); |
186
|
|
|
$url .= '&redirect_uri=' . urlencode($redirectUrl); |
187
|
|
|
$url .= '&state=' . $this->getNonce($shopDomain); |
|
|
|
|
188
|
|
|
|
189
|
|
|
return $url; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $shopDomain |
194
|
|
|
* @param string $code |
195
|
|
|
* @return string|bool |
196
|
|
|
*/ |
197
|
|
|
public function getAccessToken($shopDomain, $code) |
198
|
|
|
{ |
199
|
|
|
$this->shop_domain = $shopDomain; |
200
|
|
|
|
201
|
|
|
$http = new Client( |
202
|
|
|
[ |
203
|
|
|
'host' => $shopDomain, |
204
|
|
|
'scheme' => 'https' |
205
|
|
|
] |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$response = $http->post( |
209
|
|
|
'/admin/oauth/access_token', 'client_id=' . $this->api_key . |
210
|
|
|
'&client_secret=' . $this->shared_secret . |
211
|
|
|
'&code=' . $code |
212
|
|
|
); |
213
|
|
|
$response = $response->json; |
214
|
|
|
; |
215
|
|
|
|
216
|
|
|
if (isset($response['access_token'])) { |
217
|
|
|
$this->token = $response['access_token']; |
218
|
|
|
|
219
|
|
|
return $this->token; |
220
|
|
|
} else { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $shopDomain |
227
|
|
|
* @return string|null |
228
|
|
|
*/ |
229
|
|
|
public function setNonce($shopDomain) |
230
|
|
|
{ |
231
|
|
|
return $this->nonce = md5(strtolower($shopDomain)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return string|null |
236
|
|
|
*/ |
237
|
|
|
public function getNonce() |
238
|
|
|
{ |
239
|
|
|
return $this->nonce; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $shopDomain |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
|
|
public function validDomain($shopDomain) |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
return true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return json |
|
|
|
|
253
|
|
|
*/ |
254
|
|
|
public function getShopData() |
255
|
|
|
{ |
256
|
|
|
return $this->call('GET', '/admin/shop.json'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param array $query |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function validateHMAC($query) |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if (!is_array($query) || empty($query['hmac']) || !is_string($query['hmac']) || (isset($query['state']) && $query['state'] != $this->getNonce($query['shop']))) { |
|
|
|
|
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$dataString = []; |
270
|
|
|
|
271
|
|
|
foreach ($query as $key => $value) { |
272
|
|
|
$key = $this->_urlEncode(str_replace('=', '%3D', $key)); |
273
|
|
|
$value = $this->_urlEncode($value); |
274
|
|
|
if ($key != 'hmac') { |
275
|
|
|
$dataString[] = $key . '=' . $value; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
sort($dataString); |
280
|
|
|
$string = implode("&", $dataString); |
281
|
|
|
|
282
|
|
|
return $query['hmac'] === hash_hmac('sha256', $string, $this->shared_secret); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param string $url |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
private function _urlEncode($url) |
290
|
|
|
{ |
291
|
|
|
$url = str_replace('&', '%26', $url); |
292
|
|
|
$url = str_replace('%', '%25', $url); |
293
|
|
|
|
294
|
|
|
return $url; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
private function _isReady() |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
return strlen($this->shop_domain) > 0 && strlen($this->token) > 0; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.