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\Shopify\Controller\Component; |
17
|
|
|
|
18
|
|
|
use Cake\Core\Configure; |
19
|
|
|
use Cake\Controller\Component; |
20
|
|
|
use Cake\Routing\Router; |
21
|
|
|
use Cake\Network\Http\Client; |
22
|
|
|
use Cake\Event\Event; |
23
|
|
|
use Cake\Network\Exception\NotImplementedException; |
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
|
|
|
|
42
|
|
|
parent::initialize($config); |
43
|
|
|
|
44
|
|
|
$this->api_key = isset($config['api_key']) ? $config['api_key'] : ''; |
45
|
|
|
|
46
|
|
|
if (!empty($this->api_key)) { |
47
|
|
|
|
48
|
|
|
$this->shared_secret = Configure::read('Multidimensional/Shopify.' . $this->api_key . '.shared_secret'); |
49
|
|
|
$this->scope = Configure::read('Multidimensional/Shopify.' . $this->api_key . '.scope'); |
|
|
|
|
50
|
|
|
$this->is_private_app = Configure::read('Multidimensional/Shopify.' . $this->api_key . '.is_private_app'); |
51
|
|
|
$this->private_app_password = Configure::read('Multidimensional/Shopify.' . $this->api_key . '.private_app_password'); |
52
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
|
55
|
|
|
throw new NotImplementedException(__('Shopify API key not found')); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$this->shared_secret) { |
60
|
|
|
|
61
|
|
|
throw new NotImplementedException(__('Shopify shared secret not found')); |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function startup(Event $event) |
68
|
|
|
{ |
69
|
|
|
$this->setController($event->subject()); |
70
|
|
|
} |
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
|
|
|
public function setShopDomain($shopDomain) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return $this->shop_domain = $shopDomain; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getShopDomain() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return $this->shop_domain; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setAccessToken($token) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
return $this->token = $token; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function callsMade() |
97
|
|
|
{ |
98
|
|
|
return $this->shopApiCallLimitParam(0); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function callLimit() |
102
|
|
|
{ |
103
|
|
|
return $this->shopApiCallLimitParam(1); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function callsLeft($responseHeaders) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
return $this->callLimit() - $this->callsMade(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $method |
113
|
|
|
* @param string $path |
114
|
|
|
*/ |
115
|
|
|
public function call($method, $path, $params = []) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
if (!$this->_isReady()) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$http = new Client([ |
127
|
|
|
'host' => $this->shop_domain, |
128
|
|
|
'scheme' => 'https', |
129
|
|
|
'headers' => (($this->is_private_app != 'true') ? (['X-Shopify-Access-Token' => $this->token]) : []), |
130
|
|
|
'auth' => (($this->is_private_app != 'true') ? [] : (['username' => $this->api_key, 'password' => $this->private_app_password])) |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$this->response = $http->{strtolower($method)}( |
134
|
|
|
$path, |
135
|
|
|
((in_array($method, ['POST', 'PUT'])) ? json_encode($params) : $params), |
136
|
|
|
((in_array($method, ['POST', 'PUT'])) ? ['type' => 'json'] : []) |
137
|
|
|
); |
138
|
|
|
$this->response = $this->response->json; |
139
|
|
|
|
140
|
|
|
return (is_array($this->response) && (count($this->response) > 0)) ? array_shift($this->response) : $this->response; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param int $index |
145
|
|
|
*/ |
146
|
|
|
private function shopApiCallLimitParam($index) |
147
|
|
|
{ |
148
|
|
|
$params = explode("/", $this->response->getHeaderLine('http_x_shopify_shop_api_call_limit')); |
|
|
|
|
149
|
|
|
|
150
|
|
|
return (int)$params[$index]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getAuthorizeUrl($shopDomain, $redirectUrl) |
154
|
|
|
{ |
155
|
|
|
|
156
|
|
|
$url = 'https://' . $shopDomain . '/admin/oauth/authorize?client_id=' . $this->api_key; |
157
|
|
|
$url .= '&scope=' . urlencode($this->scope); |
158
|
|
|
$url .= '&redirect_uri=' . urlencode($redirectUrl); |
159
|
|
|
$url .= '&state=' . $this->getNonce($shopDomain); |
|
|
|
|
160
|
|
|
|
161
|
|
|
return $url; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getAccessToken($shopDomain, $code) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
|
167
|
|
|
$this->shop_domain = $shopDomain; |
168
|
|
|
|
169
|
|
|
$http = new Client([ |
170
|
|
|
'host' => $shopDomain, |
171
|
|
|
'scheme' => 'https' |
172
|
|
|
]); |
173
|
|
|
|
174
|
|
|
$response = $http->post('/admin/oauth/access_token', 'client_id=' . $this->api_key . |
175
|
|
|
'&client_secret=' . $this->shared_secret . |
176
|
|
|
'&code=' . $code); |
177
|
|
|
$response = $response->json; |
178
|
|
|
; |
179
|
|
|
|
180
|
|
|
if (isset($response['access_token'])) { |
181
|
|
|
$this->token = $response['access_token']; |
182
|
|
|
|
183
|
|
|
return $this->token; |
184
|
|
|
} else { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setNonce($shopDomain) |
190
|
|
|
{ |
191
|
|
|
|
192
|
|
|
return $this->nonce = md5(strtolower($shopDomain)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
public function getNonce() |
197
|
|
|
{ |
198
|
|
|
|
199
|
|
|
return $this->nonce; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function validDomain($shopDomain) |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
|
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getShopData() |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
|
211
|
|
|
return $this->call('GET', '/admin/shop.json'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function validateHMAC($query) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
|
217
|
|
|
if (!is_array($query) || empty($query['hmac']) || !is_string($query['hmac']) || (isset($query['state']) && $query['state'] != $this->getNonce($query['shop']))) { |
|
|
|
|
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$dataString = []; |
222
|
|
|
|
223
|
|
|
foreach ($query as $key => $value) { |
224
|
|
|
$key = $this->_urlEncode(str_replace('=', '%3D', $key)); |
225
|
|
|
$value = $this->_urlEncode($value); |
226
|
|
|
if ($key != 'hmac') { |
227
|
|
|
$dataString[] = $key . '=' . $value; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
sort($dataString); |
232
|
|
|
$string = implode("&", $dataString); |
233
|
|
|
|
234
|
|
|
return $query['hmac'] == hash_hmac('sha256', $string, $this->shared_secret); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $url |
239
|
|
|
*/ |
240
|
|
|
private function _urlEncode($url) |
241
|
|
|
{ |
242
|
|
|
|
243
|
|
|
$url = str_replace('&', '%26', $url); |
244
|
|
|
$url = str_replace('%', '%25', $url); |
245
|
|
|
|
246
|
|
|
return $url; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
private function _isReady() |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
return strlen($this->shop_domain) > 0 && strlen($this->token) > 0; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
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
.