|
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
|
|
|
|
|
24
|
|
|
class ShopifyAPIComponent extends Component { |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public $api_key; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
private $shop_domain; |
|
|
|
|
|
|
29
|
|
|
private $token; |
|
30
|
|
|
private $shared_secret; |
|
|
|
|
|
|
31
|
|
|
private $is_private_app; |
|
|
|
|
|
|
32
|
|
|
private $private_app_password; |
|
|
|
|
|
|
33
|
|
|
private $nonce; |
|
34
|
|
|
|
|
35
|
14 |
|
public function initialize(array $config = []) { |
|
36
|
|
|
|
|
37
|
14 |
|
parent::initialize($config); |
|
38
|
14 |
|
$this->api_key = ((isset($config['api_key'])) ? $config['api_key'] : Configure::read('Shopify.api_key')); |
|
39
|
14 |
|
$this->shared_secret = ((isset($config['shared_secret'])) ? $config['shared_secret'] : Configure::read('Shopify.shared_secret')); |
|
40
|
14 |
|
$this->scope = ((isset($config['scope'])) ? $config['scope'] : Configure::read('Shopify.scope')); |
|
|
|
|
|
|
41
|
14 |
|
$this->is_private_app = ((isset($config['is_private_app'])) ? $config['is_private_app'] : Configure::read('Shopify.is_private_app')); |
|
42
|
14 |
|
$this->private_app_password = ((isset($config['private_app_password'])) ? $config['private_app_password'] : Configure::read('Shopify.private_app_password')); |
|
43
|
|
|
|
|
44
|
14 |
|
} |
|
45
|
|
|
|
|
46
|
14 |
|
public function startup(Event $event) { |
|
47
|
14 |
|
$this->setController($event->subject()); |
|
48
|
14 |
|
} |
|
49
|
|
|
|
|
50
|
14 |
|
public function setController($controller) { |
|
51
|
14 |
|
$this->controller = $controller; |
|
|
|
|
|
|
52
|
14 |
|
if (!isset($this->controller->paginate)) { |
|
53
|
|
|
$this->controller->paginate = []; |
|
54
|
|
|
} |
|
55
|
14 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function setShopDomain($shop_domain) { |
|
|
|
|
|
|
58
|
|
|
return $this->shop_domain = $shop_domain; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getShopDomain() { |
|
|
|
|
|
|
62
|
|
|
return $this->shop_domain; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setAccessToken($token) { |
|
|
|
|
|
|
66
|
|
|
return $this->token = $token; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function callsMade() { |
|
70
|
|
|
return $this->shopApiCallLimitParam(0); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function callLimit() { |
|
74
|
|
|
return $this->shopApiCallLimitParam(1); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function callsLeft($response_headers) { |
|
|
|
|
|
|
78
|
|
|
return $this->callLimit() - $this->callsMade(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $method |
|
83
|
|
|
* @param string $path |
|
84
|
|
|
*/ |
|
85
|
|
|
public function call($method, $path, $params = array()) { |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
if (!$this->_isReady()) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (!in_array($method, array('POST', 'PUT', 'GET', 'DELETE'))) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$http = new Client([ |
|
96
|
|
|
'host' => $this->shop_domain, |
|
97
|
|
|
'scheme' => 'https', |
|
98
|
|
|
'headers' => (($this->is_private_app != 'true') ? (['X-Shopify-Access-Token' => $this->token]) : []), |
|
99
|
|
|
'auth' => (($this->is_private_app != 'true') ? [] : (['username' => $this->api_key, 'password' => $this->private_app_password])) |
|
100
|
|
|
]); |
|
101
|
|
|
|
|
102
|
|
|
$this->response = $http->{strtolower($method)}( |
|
103
|
|
|
$path, |
|
104
|
|
|
((in_array($method, array('POST', 'PUT'))) ? json_encode($params) : $params), |
|
105
|
|
|
((in_array($method, array('POST', 'PUT'))) ? ['type' => 'json'] : []) |
|
106
|
|
|
); |
|
107
|
|
|
$this->response = $this->response->json; |
|
108
|
|
|
|
|
109
|
|
|
return (is_array($this->response) && (count($this->response) > 0)) ? array_shift($this->response) : $this->response; |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param integer $index |
|
115
|
|
|
*/ |
|
116
|
|
|
private function shopApiCallLimitParam($index) { |
|
117
|
|
|
$params = explode("/", $this->response->getHeaderLine('http_x_shopify_shop_api_call_limit')); |
|
|
|
|
|
|
118
|
|
|
return (int) $params[$index]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getAuthorizeUrl($shop_domain, $redirect_url) { |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
$url = 'https://' . $shop_domain . '/admin/oauth/authorize?client_id=' . $this->api_key; |
|
|
|
|
|
|
124
|
|
|
$url .= '&scope=' . urlencode($this->scope); |
|
125
|
|
|
$url .= '&redirect_uri=' . urlencode($redirect_url); |
|
|
|
|
|
|
126
|
|
|
$url .= '&state=' . $this->getNonce($shop_domain); |
|
|
|
|
|
|
127
|
|
|
return $url; |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getAccessToken($shop_domain, $code) { |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
$this->shop_domain = $shop_domain; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$http = new Client([ |
|
136
|
|
|
'host' => $shop_domain, |
|
|
|
|
|
|
137
|
|
|
'scheme' => 'https' |
|
138
|
|
|
]); |
|
139
|
|
|
|
|
140
|
|
|
$response = $http->post('/admin/oauth/access_token', 'client_id=' . $this->api_key . |
|
141
|
|
|
'&client_secret=' . $this->shared_secret . |
|
142
|
|
|
'&code=' . $code); |
|
143
|
|
|
$response = $response->json; ; |
|
144
|
|
|
|
|
145
|
|
|
if (isset($response['access_token'])) { |
|
146
|
|
|
$this->token = $response['access_token']; |
|
147
|
|
|
return $this->token; |
|
148
|
|
|
} else { |
|
149
|
|
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function setNonce($shop_domain) { |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
return $this->nonce = md5(strtolower($shop_domain)); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
public function getNonce() { |
|
162
|
|
|
|
|
163
|
|
|
return $this->nonce; |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function validDomain($shop_domain) { |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
return true; |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getShopData() { |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
return $this->call('GET', '/admin/shop.json'); |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function validateHMAC($query) { |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
if (!is_array($query) || empty($query['hmac']) || !is_string($query['hmac']) || (isset($query['state']) && $query['state'] != $this->getNonce($query['shop']))) { |
|
|
|
|
|
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$dataString = array(); |
|
186
|
|
|
|
|
187
|
|
|
foreach ($query as $key => $value) { |
|
188
|
|
|
$key = $this->_urlEncode(str_replace('=', '%3D', $key)); |
|
189
|
|
|
$value = $this->_urlEncode($value); |
|
190
|
|
|
if ($key != 'hmac') { |
|
191
|
|
|
$dataString[] = $key . '=' . $value; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
sort($dataString); |
|
196
|
|
|
$string = implode("&", $dataString); |
|
197
|
|
|
return $query['hmac'] == hash_hmac('sha256', $string, $this->shared_secret); |
|
198
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $url |
|
203
|
|
|
*/ |
|
204
|
|
|
private function _urlEncode($url) { |
|
205
|
|
|
|
|
206
|
|
|
$url = str_replace('&', '%26', $url); |
|
207
|
|
|
$url = str_replace('%', '%25', $url); |
|
208
|
|
|
return $url; |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
private function _isReady() { |
|
|
|
|
|
|
213
|
|
|
return strlen($this->shop_domain) > 0 && strlen($this->token) > 0; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
?> |
|
|
|
|
|
|
219
|
|
|
|
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.