Skip to content

Commit 45217cd

Browse files
committed
Minor fixes
1 parent e585aad commit 45217cd

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

src/Shopify.php

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ class Shopify
4545
public function __construct(string $shopDomain, $credentials) {
4646
$this->shop_domain = $shopDomain;
4747

48-
// Initialize the GuzzleHttp Client
49-
$this->client = new Client([
50-
'base_url' => "https://{$this->shop_domain}/",
51-
'http_errors' => true,
52-
]);
53-
5448
// Populate the credentials
5549
if (is_string($credentials)) {
5650
$this->setToken($credentials);
@@ -60,6 +54,31 @@ public function __construct(string $shopDomain, $credentials) {
6054
} else {
6155
throw new Exception("Unexpected value provided for the credentials");
6256
}
57+
58+
// Initialize the client
59+
$this->initializeClient();
60+
}
61+
62+
/**
63+
* Initialize the GuzzleHttp/Client instance
64+
*
65+
* @return GuzzleHttp/Client $client
66+
*/
67+
protected function initializeClient()
68+
{
69+
if ($this->client) {
70+
return $this->client;
71+
}
72+
73+
$options = [
74+
'base_uri' => "https://{$this->shop_domain}/",
75+
'http_errors' => true,
76+
];
77+
if (!empty($this->token)) {
78+
$options['headers']['X-Shopify-Access-Token'] = $this->token;
79+
}
80+
81+
return $this->client = new Client($options);
6382
}
6483

6584
/**
@@ -71,7 +90,10 @@ public function __construct(string $shopDomain, $credentials) {
7190
public function setToken(string $token)
7291
{
7392
$this->token = $token;
74-
$this->client->setDefaultOption('headers/X-Shopify-Access-Token', $this->token);
93+
// Reset the client
94+
unset($this->client);
95+
$this->client = null;
96+
$this->initializeClient();
7597
}
7698

7799
/**
@@ -100,7 +122,7 @@ public function getAuthorizeUrl($scopes, string $redirectUrl, string $nonce, $on
100122
$args['grant_options[]'] = 'per-user';
101123
}
102124

103-
return $this->client->get('admin/oauth/authorize', ['query' => $args])->getUrl();
125+
return "https://{$this->shop_domain}/admin/oauth/authorize?" . http_build_query($args);
104126
}
105127

106128
/**
@@ -113,8 +135,10 @@ public function getAuthorizeUrl($scopes, string $redirectUrl, string $nonce, $on
113135
public function authorizeApplication(string $nonce, $requestData)
114136
{
115137
$requiredKeys = ['code', 'hmac', 'state', 'shop'];
116-
if (!in_array(array_keys[$requestData], $requiredKeys)) {
117-
throw new Exception("The provided request data is missing one of the following keys: " . implode(', ', $requiredKeys));
138+
foreach ($requiredKeys as $required) {
139+
if (!in_array($required, array_keys($requestData))) {
140+
throw new Exception("The provided request data is missing one of the following keys: " . implode(', ', $requiredKeys));
141+
}
118142
}
119143

120144
if ($requestData['state'] !== $nonce) {
@@ -142,7 +166,7 @@ public function authorizeApplication(string $nonce, $requestData)
142166
];
143167
$keyPatterns = array_merge($valuePatterns, ['=' => '%3D']);
144168
$key = str_replace(array_keys($keyPatterns), array_values($keyPatterns), $key);
145-
$value = str_replace(array_keys($valuePatterns), array_value($valuePatterns, $value));
169+
$value = str_replace(array_keys($valuePatterns), array_values($valuePatterns), $value);
146170

147171
$hmacSource[] = $key . '=' . $value;
148172
}
@@ -158,11 +182,21 @@ public function authorizeApplication(string $nonce, $requestData)
158182
}
159183

160184
// Make the access token request to Shopify
161-
$response = $this->client->request('POST', 'admin/oauth/access_token', ['body' => [
162-
'client_id' => $this->api_key,
163-
'client_secret' => $this->secret,
164-
'code' => $code,
165-
]]);
185+
try {
186+
$response = $this->client->request('POST', 'admin/oauth/access_token', [
187+
'body' => json_encode([
188+
'client_id' => $this->api_key,
189+
'client_secret' => $this->secret,
190+
'code' => $requestData['code'],
191+
]),
192+
'headers' => [
193+
'Content-Type' => 'application/json',
194+
],
195+
]);
196+
} catch (Exception $e) {
197+
// Pass the erroring response direct to browser
198+
die($e->getResponse()->getBody());
199+
}
166200

167201
// Decode the response from Shopify
168202
$data = json_decode($response->getBody());

0 commit comments

Comments
 (0)