... | ... |
@@ -303,7 +303,7 @@ class Shopware |
303 | 303 |
return $this->sendRequest('search/order-line-item',$options,'POST'); |
304 | 304 |
} |
305 | 305 |
|
306 |
- public function queryAPI(string $strUrlFragment, ?string $strBody, string $strMethod = 'GET', bool $blnAuthenticate=true): ResponseInterface |
|
306 |
+ public function queryAPI(string $strUrlFragment, null|string|array $body, string $strMethod = 'GET', bool $blnAuthenticate=true): ResponseInterface |
|
307 | 307 |
{ |
308 | 308 |
$options = [ |
309 | 309 |
'headers' => [ |
... | ... |
@@ -317,9 +317,14 @@ class Shopware |
317 | 317 |
$options['headers'][] = $this->getAuthentication(); |
318 | 318 |
} |
319 | 319 |
|
320 |
- if ($strBody !== null) |
|
320 |
+ if ($body !== null) |
|
321 | 321 |
{ |
322 |
- $options['body'] = $strBody; |
|
322 |
+ if (is_array($body)) |
|
323 |
+ { |
|
324 |
+ $options['body'] = json_encode($body); |
|
325 |
+ } else { |
|
326 |
+ $options['body'] = $body; |
|
327 |
+ } |
|
323 | 328 |
} |
324 | 329 |
|
325 | 330 |
return $this->sendRequest($strUrlFragment,$options,$strMethod); |
... | ... |
@@ -164,6 +164,145 @@ class Shopware |
164 | 164 |
return $this->sendRequest('search/product',$options,'POST'); |
165 | 165 |
} |
166 | 166 |
|
167 |
+ public function findOrdersByFilter(array $filter, bool $blnShort=false): ResponseInterface |
|
168 |
+ { |
|
169 |
+ $options = [ |
|
170 |
+ 'headers' => [ |
|
171 |
+ $this->getAuthentication(), |
|
172 |
+ 'Content-Type: application/json', |
|
173 |
+ 'Accept: application/json' |
|
174 |
+ ], |
|
175 |
+ 'body' => json_encode([ |
|
176 |
+ 'filter' => $filter, |
|
177 |
+ 'sort' => [ |
|
178 |
+ [ |
|
179 |
+ 'field' => 'createdAt', |
|
180 |
+ 'order' => 'ASC', |
|
181 |
+ 'naturalSorting' => true |
|
182 |
+ ] |
|
183 |
+ ], |
|
184 |
+ 'includes' => [ |
|
185 |
+ 'order' => ['id', 'orderNumber', 'createdAt', 'billingAddress', 'deliveries', 'lineItems','taxStatus','orderCustomer','amountTotal','shippingTotal','transactions'], |
|
186 |
+ 'order_delivery' => ['shippingOrderAddress','shippingMethod'], |
|
187 |
+ 'country' => ['name','iso'], |
|
188 |
+ 'order_line_item' => ['position', 'quantity', 'label', 'unitPrice', 'totalPrice', 'price','priceDefinition', 'productId', 'product', 'type', 'payload'], |
|
189 |
+ 'product' => ['ean','productNumber','name'], |
|
190 |
+ 'payment_method' => ['id','name','shortName'], |
|
191 |
+ 'shipping_method' => ['id','name'], |
|
192 |
+ 'order_transaction' => ['amount','paymentMethod'], |
|
193 |
+ ], |
|
194 |
+ 'associations' => [ |
|
195 |
+ 'transactions' => [ |
|
196 |
+ 'associations' => [ |
|
197 |
+ 'paymentMethod' => [] |
|
198 |
+ ] |
|
199 |
+ ], |
|
200 |
+ 'billingAddress' => [ |
|
201 |
+ 'associations' => [ |
|
202 |
+ 'country' => [] |
|
203 |
+ ] |
|
204 |
+ ], |
|
205 |
+ 'deliveries' => [ |
|
206 |
+ 'associations' => [ |
|
207 |
+ 'shippingOrderAddress' => [ |
|
208 |
+ 'associations' => [ |
|
209 |
+ 'country' => [] |
|
210 |
+ ] |
|
211 |
+ ], |
|
212 |
+ 'shippingMethod' => [] |
|
213 |
+ ] |
|
214 |
+ ], |
|
215 |
+ 'lineItems' => [ |
|
216 |
+ 'associations' => [ |
|
217 |
+ 'product' => [] |
|
218 |
+ ] |
|
219 |
+ ], |
|
220 |
+ ] |
|
221 |
+ ]) |
|
222 |
+ ]; |
|
223 |
+ |
|
224 |
+ if ($blnShort) |
|
225 |
+ { |
|
226 |
+ $options['body'] = json_encode( |
|
227 |
+ array_merge( |
|
228 |
+ json_decode($options['body'],true), |
|
229 |
+ [ |
|
230 |
+ 'fields' => [ |
|
231 |
+ 'orderNumber' |
|
232 |
+ ] |
|
233 |
+ ] |
|
234 |
+ ) |
|
235 |
+ ); |
|
236 |
+ } |
|
237 |
+ |
|
238 |
+ return $this->sendRequest('search/order',$options,'POST'); |
|
239 |
+ } |
|
240 |
+ |
|
241 |
+ public function getOrderAddressById(string $strId, bool $blnShort=false): ResponseInterface |
|
242 |
+ { |
|
243 |
+ $options = [ |
|
244 |
+ 'headers' => [ |
|
245 |
+ $this->getAuthentication(), |
|
246 |
+ 'Content-Type: application/json', |
|
247 |
+ 'Accept: application/json' |
|
248 |
+ ] |
|
249 |
+ ]; |
|
250 |
+ |
|
251 |
+ return $this->sendRequest('order-address/'.$strId,$options,'GET'); |
|
252 |
+ } |
|
253 |
+ |
|
254 |
+ public function getOrder(string $strId, ?string $pathSuffix=null): ResponseInterface |
|
255 |
+ { |
|
256 |
+ $options = [ |
|
257 |
+ 'headers' => [ |
|
258 |
+ $this->getAuthentication(), |
|
259 |
+ 'Content-Type: application/json', |
|
260 |
+ 'Accept: application/json' |
|
261 |
+ ] |
|
262 |
+ ]; |
|
263 |
+ |
|
264 |
+ return $this->sendRequest('order/'.$strId.($pathSuffix ? '/'.$pathSuffix : ''),$options,'GET'); |
|
265 |
+ } |
|
266 |
+ |
|
267 |
+ public function getOrderLineItemsByFilter(array $filter, bool $blnShort=false): ResponseInterface |
|
268 |
+ { |
|
269 |
+ $options = [ |
|
270 |
+ 'headers' => [ |
|
271 |
+ $this->getAuthentication(), |
|
272 |
+ 'Content-Type: application/json', |
|
273 |
+ 'Accept: application/json' |
|
274 |
+ ], |
|
275 |
+ 'body' => json_encode([ |
|
276 |
+ 'filter' => $filter, |
|
277 |
+ 'sort' => [ |
|
278 |
+ [ |
|
279 |
+ 'field' => 'createdAt', |
|
280 |
+ 'order' => 'ASC', |
|
281 |
+ 'naturalSorting' => true |
|
282 |
+ ] |
|
283 |
+ ] |
|
284 |
+ ]) |
|
285 |
+ ]; |
|
286 |
+ |
|
287 |
+ if ($blnShort) |
|
288 |
+ { |
|
289 |
+ $options['body'] = json_encode( |
|
290 |
+ array_merge( |
|
291 |
+ json_decode($options['body'],true), |
|
292 |
+ [ |
|
293 |
+ 'fields' => [ |
|
294 |
+ 'id', |
|
295 |
+ 'label', |
|
296 |
+ 'quantity', |
|
297 |
+ ] |
|
298 |
+ ] |
|
299 |
+ ) |
|
300 |
+ ); |
|
301 |
+ } |
|
302 |
+ |
|
303 |
+ return $this->sendRequest('search/order-line-item',$options,'POST'); |
|
304 |
+ } |
|
305 |
+ |
|
167 | 306 |
public function queryAPI(string $strUrlFragment, ?string $strBody, string $strMethod = 'GET', bool $blnAuthenticate=true): ResponseInterface |
168 | 307 |
{ |
169 | 308 |
$options = [ |
... | ... |
@@ -202,7 +202,7 @@ class Shopware |
202 | 202 |
return false; |
203 | 203 |
} |
204 | 204 |
|
205 |
- public function addOrUpdateProductBySku(string $strSku, array $arrData)/*: bool*/ |
|
205 |
+ public function addOrUpdateProductBySku(string $strSku, array $arrData): bool |
|
206 | 206 |
{ |
207 | 207 |
$blnInsert = true; |
208 | 208 |
// Update or insert check |
... | ... |
@@ -240,6 +240,69 @@ class Shopware |
240 | 240 |
return in_array($response->getStatusCode(),[200,201,202,203,204]); |
241 | 241 |
} |
242 | 242 |
|
243 |
+ public function truncatePropertiesForProductBySku(string $strSku, string|array|null $groupIds = null): bool |
|
244 |
+ { |
|
245 |
+ if ($groupIds !== null && !is_array($groupIds)) |
|
246 |
+ { |
|
247 |
+ $groupIds = (array) $groupIds; |
|
248 |
+ } |
|
249 |
+ |
|
250 |
+ // Look for product |
|
251 |
+ if (($searchResponse = $this->getProductsForSku($strSku))->getStatusCode() == 200) |
|
252 |
+ { |
|
253 |
+ $Product = json_decode($searchResponse->getContent()); |
|
254 |
+ |
|
255 |
+ if (!$Product->total) |
|
256 |
+ { |
|
257 |
+ return false; |
|
258 |
+ } |
|
259 |
+ |
|
260 |
+ // Get property ids |
|
261 |
+ $arrPropertiesPayload = []; |
|
262 |
+ foreach (System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.properties') as $groupId => $properties) |
|
263 |
+ { |
|
264 |
+ if ($groupIds !== null && !in_array($groupId,$groupIds)) |
|
265 |
+ { |
|
266 |
+ continue; |
|
267 |
+ } |
|
268 |
+ |
|
269 |
+ foreach (array_keys($properties) as $propertyId) |
|
270 |
+ { |
|
271 |
+ $arrPropertiesPayload[] = [ |
|
272 |
+ 'productId' => $Product->data[0]->id, |
|
273 |
+ 'optionId' => $propertyId |
|
274 |
+ ]; |
|
275 |
+ } |
|
276 |
+ } |
|
277 |
+ |
|
278 |
+ // Build request data |
|
279 |
+ $arrData = [ |
|
280 |
+ [ |
|
281 |
+ 'entity' => 'product_property', |
|
282 |
+ 'action' => 'delete', |
|
283 |
+ 'payload' => $arrPropertiesPayload |
|
284 |
+ ] |
|
285 |
+ ]; |
|
286 |
+ |
|
287 |
+ $options = [ |
|
288 |
+ 'headers' => [ |
|
289 |
+ $this->getAuthentication(), |
|
290 |
+ 'Content-Type: application/json', |
|
291 |
+ 'Accept: application/json', |
|
292 |
+ 'fail-on-error: false' |
|
293 |
+ ], |
|
294 |
+ 'body' => json_encode($arrData) |
|
295 |
+ ]; |
|
296 |
+ |
|
297 |
+ // Send request |
|
298 |
+ $response = $this->sendRequest('_action/sync',$options,'POST'); |
|
299 |
+ |
|
300 |
+ return in_array($response->getStatusCode(),[200]); |
|
301 |
+ } |
|
302 |
+ |
|
303 |
+ return false; |
|
304 |
+ } |
|
305 |
+ |
|
243 | 306 |
protected function createUuid($data = null) { |
244 | 307 |
// Generate 16 bytes (128 bits) of random data or use the data passed into the function. |
245 | 308 |
$data = $data ?? random_bytes(16); |
... | ... |
@@ -217,24 +217,9 @@ class Shopware |
217 | 217 |
} |
218 | 218 |
|
219 | 219 |
// Prepare product data |
220 |
- $arrData = [ |
|
221 |
- 'taxId' => '018e65c0485071508949c072f8dc18bd', |
|
222 |
- 'id' => $blnInsert ? $this->createUuid() : $Product->data[0]->id, |
|
223 |
- 'price' => [ |
|
224 |
- [ |
|
225 |
- 'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca', |
|
226 |
- 'gross' => 9.9, |
|
227 |
- 'net' => 9.9/119*100, |
|
228 |
- 'linked' => true |
|
229 |
- ] |
|
230 |
- ], |
|
231 |
- 'productNumber' => $strSku, |
|
232 |
- 'stock' => 9999999, |
|
233 |
- 'name' => 'Superheldenumhang', |
|
234 |
- 'customFields' => [ |
|
235 |
- 'custom_wine_attributes_jahrgang' => "2020" |
|
236 |
- ] |
|
237 |
- ]; |
|
220 |
+ $arrData = array_merge([ |
|
221 |
+ 'id' => $blnInsert ? $this->createUuid() : $Product->data[0]->id |
|
222 |
+ ], $arrData); |
|
238 | 223 |
|
239 | 224 |
$options = [ |
240 | 225 |
'headers' => [ |
... | ... |
@@ -204,24 +204,36 @@ class Shopware |
204 | 204 |
|
205 | 205 |
public function addOrUpdateProductBySku(string $strSku, array $arrData)/*: bool*/ |
206 | 206 |
{ |
207 |
+ $blnInsert = true; |
|
207 | 208 |
// Update or insert check |
208 |
- $blnInsert = !$this->checkBySkuIfExists($strSku); |
|
209 |
+ if (($searchResponse = $this->getProductsForSku($strSku))->getStatusCode() == 200) |
|
210 |
+ { |
|
211 |
+ $Product = json_decode($searchResponse->getContent()); |
|
212 |
+ |
|
213 |
+ if ($Product->total) |
|
214 |
+ { |
|
215 |
+ $blnInsert = false; |
|
216 |
+ } |
|
217 |
+ } |
|
209 | 218 |
|
210 | 219 |
// Prepare product data |
211 | 220 |
$arrData = [ |
212 | 221 |
'taxId' => '018e65c0485071508949c072f8dc18bd', |
213 |
- 'id' => $this->createUuid(), |
|
222 |
+ 'id' => $blnInsert ? $this->createUuid() : $Product->data[0]->id, |
|
214 | 223 |
'price' => [ |
215 | 224 |
[ |
216 | 225 |
'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca', |
217 | 226 |
'gross' => 9.9, |
218 |
- 'net' => 0, |
|
227 |
+ 'net' => 9.9/119*100, |
|
219 | 228 |
'linked' => true |
220 | 229 |
] |
221 | 230 |
], |
222 | 231 |
'productNumber' => $strSku, |
223 | 232 |
'stock' => 9999999, |
224 |
- 'name' => 'Superheldenumhang' |
|
233 |
+ 'name' => 'Superheldenumhang', |
|
234 |
+ 'customFields' => [ |
|
235 |
+ 'custom_wine_attributes_jahrgang' => "2020" |
|
236 |
+ ] |
|
225 | 237 |
]; |
226 | 238 |
|
227 | 239 |
$options = [ |
... | ... |
@@ -236,9 +248,11 @@ class Shopware |
236 | 248 |
if ($blnInsert) |
237 | 249 |
{ |
238 | 250 |
$response = $this->sendRequest('product',$options,'POST'); |
251 |
+ } else { |
|
252 |
+ $response = $this->sendRequest('product/'.$Product->data[0]->id,$options,'PATCH'); |
|
239 | 253 |
} |
240 | 254 |
|
241 |
- return $response->getStatusCode() == 200; |
|
255 |
+ return in_array($response->getStatusCode(),[200,201,202,203,204]); |
|
242 | 256 |
} |
243 | 257 |
|
244 | 258 |
protected function createUuid($data = null) { |
... | ... |
@@ -185,4 +185,73 @@ class Shopware |
185 | 185 |
|
186 | 186 |
return $this->sendRequest($strUrlFragment,$options,$strMethod); |
187 | 187 |
} |
188 |
+ |
|
189 |
+ protected function checkBySkuIfExists(string $strSku): bool |
|
190 |
+ { |
|
191 |
+ $Result = $this->getProductsForSku($strSku,false,true); |
|
192 |
+ |
|
193 |
+ if ($Result->getStatusCode() == 200) |
|
194 |
+ { |
|
195 |
+ $Content = json_decode($Result->getContent()); |
|
196 |
+ |
|
197 |
+ if ($Content->total) |
|
198 |
+ { |
|
199 |
+ return true; |
|
200 |
+ } |
|
201 |
+ } |
|
202 |
+ return false; |
|
203 |
+ } |
|
204 |
+ |
|
205 |
+ public function addOrUpdateProductBySku(string $strSku, array $arrData)/*: bool*/ |
|
206 |
+ { |
|
207 |
+ // Update or insert check |
|
208 |
+ $blnInsert = !$this->checkBySkuIfExists($strSku); |
|
209 |
+ |
|
210 |
+ // Prepare product data |
|
211 |
+ $arrData = [ |
|
212 |
+ 'taxId' => '018e65c0485071508949c072f8dc18bd', |
|
213 |
+ 'id' => $this->createUuid(), |
|
214 |
+ 'price' => [ |
|
215 |
+ [ |
|
216 |
+ 'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca', |
|
217 |
+ 'gross' => 9.9, |
|
218 |
+ 'net' => 0, |
|
219 |
+ 'linked' => true |
|
220 |
+ ] |
|
221 |
+ ], |
|
222 |
+ 'productNumber' => $strSku, |
|
223 |
+ 'stock' => 9999999, |
|
224 |
+ 'name' => 'Superheldenumhang' |
|
225 |
+ ]; |
|
226 |
+ |
|
227 |
+ $options = [ |
|
228 |
+ 'headers' => [ |
|
229 |
+ $this->getAuthentication(), |
|
230 |
+ 'Content-Type: application/json', |
|
231 |
+ 'Accept: application/json' |
|
232 |
+ ], |
|
233 |
+ 'body' => json_encode($arrData) |
|
234 |
+ ]; |
|
235 |
+ |
|
236 |
+ if ($blnInsert) |
|
237 |
+ { |
|
238 |
+ $response = $this->sendRequest('product',$options,'POST'); |
|
239 |
+ } |
|
240 |
+ |
|
241 |
+ return $response->getStatusCode() == 200; |
|
242 |
+ } |
|
243 |
+ |
|
244 |
+ protected function createUuid($data = null) { |
|
245 |
+ // Generate 16 bytes (128 bits) of random data or use the data passed into the function. |
|
246 |
+ $data = $data ?? random_bytes(16); |
|
247 |
+ assert(strlen($data) == 16); |
|
248 |
+ |
|
249 |
+ // Set version to 0100 |
|
250 |
+ $data[6] = chr(ord($data[6]) & 0x0f | 0x40); |
|
251 |
+ // Set bits 6-7 to 10 |
|
252 |
+ $data[8] = chr(ord($data[8]) & 0x3f | 0x80); |
|
253 |
+ |
|
254 |
+ // Output the 36 character UUID. |
|
255 |
+ return bin2hex($data); |
|
256 |
+ } |
|
188 | 257 |
} |
... | ... |
@@ -15,6 +15,7 @@ namespace vonRotenberg\ShopwareApiBundle\API; |
15 | 15 |
use Contao\System; |
16 | 16 |
use Symfony\Component\Serializer\SerializerInterface; |
17 | 17 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
18 |
+use Symfony\Contracts\HttpClient\ResponseInterface; |
|
18 | 19 |
|
19 | 20 |
class Shopware |
20 | 21 |
{ |
... | ... |
@@ -61,7 +62,14 @@ class Shopware |
61 | 62 |
|
62 | 63 |
$relEndpoint = '/' . ltrim($relEndpoint,'/'); |
63 | 64 |
|
64 |
- return $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options); |
|
65 |
+ $request = $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options); |
|
66 |
+ |
|
67 |
+ if ($request->getStatusCode() == 401) |
|
68 |
+ { |
|
69 |
+ throw new \RuntimeException('Unauthorized'); |
|
70 |
+ } |
|
71 |
+ |
|
72 |
+ return $request; |
|
65 | 73 |
} |
66 | 74 |
|
67 | 75 |
protected function getAccessToken() |
... | ... |
@@ -84,20 +92,16 @@ class Shopware |
84 | 92 |
return json_decode($content); |
85 | 93 |
} |
86 | 94 |
|
87 |
- return null; |
|
95 |
+ throw new \RuntimeException('Can\'t retrieve access token. Check credentials.'); |
|
88 | 96 |
} |
89 | 97 |
|
90 | 98 |
protected function getAuthentication() |
91 | 99 |
{ |
92 |
- if (($Token = $this->getAccessToken()) !== null) |
|
93 |
- { |
|
94 |
- return 'Authorization: ' . $Token->token_type . ' ' . $Token->access_token; |
|
95 |
- } |
|
96 |
- |
|
97 |
- return null; |
|
100 |
+ $Token = $this->getAccessToken(); |
|
101 |
+ return 'Authorization: ' . $Token->token_type . ' ' . $Token->access_token; |
|
98 | 102 |
} |
99 | 103 |
|
100 |
- public function isShopwareRunning() |
|
104 |
+ public function isShopwareRunning(): bool |
|
101 | 105 |
{ |
102 | 106 |
$options = [ |
103 | 107 |
'headers' => [ |
... | ... |
@@ -117,10 +121,8 @@ class Shopware |
117 | 121 |
return false; |
118 | 122 |
} |
119 | 123 |
|
120 |
- public function getProductsForSku(string $strSku, bool $blnWildcardSearch=false, bool $blnShort=false) |
|
124 |
+ public function getProductsForSku(string $strSku, bool $blnWildcardSearch=false, bool $blnShort=false): ResponseInterface |
|
121 | 125 |
{ |
122 |
- |
|
123 |
- |
|
124 | 126 |
$options = [ |
125 | 127 |
'headers' => [ |
126 | 128 |
$this->getAuthentication(), |
... | ... |
@@ -159,13 +161,28 @@ class Shopware |
159 | 161 |
); |
160 | 162 |
} |
161 | 163 |
|
162 |
- $response = $this->sendRequest('search/product',$options,'POST'); |
|
164 |
+ return $this->sendRequest('search/product',$options,'POST'); |
|
165 |
+ } |
|
163 | 166 |
|
164 |
- if ($response->getStatusCode() == 200) |
|
167 |
+ public function queryAPI(string $strUrlFragment, ?string $strBody, string $strMethod = 'GET', bool $blnAuthenticate=true): ResponseInterface |
|
168 |
+ { |
|
169 |
+ $options = [ |
|
170 |
+ 'headers' => [ |
|
171 |
+ 'Content-Type: application/json', |
|
172 |
+ 'Accept: application/json' |
|
173 |
+ ] |
|
174 |
+ ]; |
|
175 |
+ |
|
176 |
+ if ($blnAuthenticate) |
|
165 | 177 |
{ |
166 |
- return json_decode($response->getContent()); |
|
178 |
+ $options['headers'][] = $this->getAuthentication(); |
|
167 | 179 |
} |
168 | 180 |
|
169 |
- return false; |
|
181 |
+ if ($strBody !== null) |
|
182 |
+ { |
|
183 |
+ $options['body'] = $strBody; |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ return $this->sendRequest($strUrlFragment,$options,$strMethod); |
|
170 | 187 |
} |
171 | 188 |
} |
... | ... |
@@ -117,8 +117,10 @@ class Shopware |
117 | 117 |
return false; |
118 | 118 |
} |
119 | 119 |
|
120 |
- public function getProductsForSku(string $strSku) |
|
120 |
+ public function getProductsForSku(string $strSku, bool $blnWildcardSearch=false, bool $blnShort=false) |
|
121 | 121 |
{ |
122 |
+ |
|
123 |
+ |
|
122 | 124 |
$options = [ |
123 | 125 |
'headers' => [ |
124 | 126 |
$this->getAuthentication(), |
... | ... |
@@ -128,14 +130,35 @@ class Shopware |
128 | 130 |
'body' => json_encode([ |
129 | 131 |
'filter' => [ |
130 | 132 |
[ |
131 |
- 'type' => 'equals', |
|
133 |
+ 'type' => !$blnWildcardSearch ? 'equals' : 'contains', |
|
132 | 134 |
'field' => 'productNumber', |
133 | 135 |
'value' => $strSku |
134 | 136 |
] |
137 |
+ ], |
|
138 |
+ 'sort' => [ |
|
139 |
+ [ |
|
140 |
+ 'field' => 'productNumber', |
|
141 |
+ 'order' => 'ASC', |
|
142 |
+ 'naturalSorting' => true |
|
143 |
+ ] |
|
135 | 144 |
] |
136 | 145 |
]) |
137 | 146 |
]; |
138 | 147 |
|
148 |
+ if ($blnShort) |
|
149 |
+ { |
|
150 |
+ $options['body'] = json_encode( |
|
151 |
+ array_merge( |
|
152 |
+ json_decode($options['body'],true), |
|
153 |
+ [ |
|
154 |
+ 'fields' => [ |
|
155 |
+ 'name' |
|
156 |
+ ] |
|
157 |
+ ] |
|
158 |
+ ) |
|
159 |
+ ); |
|
160 |
+ } |
|
161 |
+ |
|
139 | 162 |
$response = $this->sendRequest('search/product',$options,'POST'); |
140 | 163 |
|
141 | 164 |
if ($response->getStatusCode() == 200) |
... | ... |
@@ -116,4 +116,33 @@ class Shopware |
116 | 116 |
|
117 | 117 |
return false; |
118 | 118 |
} |
119 |
+ |
|
120 |
+ public function getProductsForSku(string $strSku) |
|
121 |
+ { |
|
122 |
+ $options = [ |
|
123 |
+ 'headers' => [ |
|
124 |
+ $this->getAuthentication(), |
|
125 |
+ 'Content-Type: application/json', |
|
126 |
+ 'Accept: application/json' |
|
127 |
+ ], |
|
128 |
+ 'body' => json_encode([ |
|
129 |
+ 'filter' => [ |
|
130 |
+ [ |
|
131 |
+ 'type' => 'equals', |
|
132 |
+ 'field' => 'productNumber', |
|
133 |
+ 'value' => $strSku |
|
134 |
+ ] |
|
135 |
+ ] |
|
136 |
+ ]) |
|
137 |
+ ]; |
|
138 |
+ |
|
139 |
+ $response = $this->sendRequest('search/product',$options,'POST'); |
|
140 |
+ |
|
141 |
+ if ($response->getStatusCode() == 200) |
|
142 |
+ { |
|
143 |
+ return json_decode($response->getContent()); |
|
144 |
+ } |
|
145 |
+ |
|
146 |
+ return false; |
|
147 |
+ } |
|
119 | 148 |
} |
... | ... |
@@ -13,17 +13,23 @@ declare(strict_types=1); |
13 | 13 |
namespace vonRotenberg\ShopwareApiBundle\API; |
14 | 14 |
|
15 | 15 |
use Contao\System; |
16 |
+use Symfony\Component\Serializer\SerializerInterface; |
|
16 | 17 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
17 | 18 |
|
18 | 19 |
class Shopware |
19 | 20 |
{ |
21 |
+ protected $serializer; |
|
20 | 22 |
protected $httpClient; |
21 | 23 |
protected $clientId; |
22 | 24 |
protected $clientSecret; |
23 | 25 |
protected $apiEndpoint; |
24 | 26 |
|
25 |
- public function __construct(HttpClientInterface $httpClient) |
|
27 |
+ private $token; |
|
28 |
+ private $tokenType; |
|
29 |
+ |
|
30 |
+ public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient) |
|
26 | 31 |
{ |
32 |
+ $this->serializer = $serializer; |
|
27 | 33 |
$this->httpClient = $httpClient; |
28 | 34 |
|
29 | 35 |
$this->clientId = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_id'); |
... | ... |
@@ -58,7 +64,7 @@ class Shopware |
58 | 64 |
return $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options); |
59 | 65 |
} |
60 | 66 |
|
61 |
- public function testApiRequest() |
|
67 |
+ protected function getAccessToken() |
|
62 | 68 |
{ |
63 | 69 |
$options = [ |
64 | 70 |
'body' => json_encode([ |
... | ... |
@@ -80,4 +86,34 @@ class Shopware |
80 | 86 |
|
81 | 87 |
return null; |
82 | 88 |
} |
89 |
+ |
|
90 |
+ protected function getAuthentication() |
|
91 |
+ { |
|
92 |
+ if (($Token = $this->getAccessToken()) !== null) |
|
93 |
+ { |
|
94 |
+ return 'Authorization: ' . $Token->token_type . ' ' . $Token->access_token; |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ return null; |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ public function isShopwareRunning() |
|
101 |
+ { |
|
102 |
+ $options = [ |
|
103 |
+ 'headers' => [ |
|
104 |
+ $this->getAuthentication(), |
|
105 |
+ 'Content-Type: application/json', |
|
106 |
+ 'Accept: application/json' |
|
107 |
+ ], |
|
108 |
+ ]; |
|
109 |
+ |
|
110 |
+ $response = $this->sendRequest('_info/health-check',$options,'GET'); |
|
111 |
+ |
|
112 |
+ if ($response->getStatusCode() == 200) |
|
113 |
+ { |
|
114 |
+ return true; |
|
115 |
+ } |
|
116 |
+ |
|
117 |
+ return false; |
|
118 |
+ } |
|
83 | 119 |
} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,83 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+declare(strict_types=1); |
|
4 |
+ |
|
5 |
+/* |
|
6 |
+ * This file is part of vonRotenberg Shopware API Bundle. |
|
7 |
+ * |
|
8 |
+ * (c) vonRotenberg |
|
9 |
+ * |
|
10 |
+ * @license proprietary |
|
11 |
+ */ |
|
12 |
+ |
|
13 |
+namespace vonRotenberg\ShopwareApiBundle\API; |
|
14 |
+ |
|
15 |
+use Contao\System; |
|
16 |
+use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
17 |
+ |
|
18 |
+class Shopware |
|
19 |
+{ |
|
20 |
+ protected $httpClient; |
|
21 |
+ protected $clientId; |
|
22 |
+ protected $clientSecret; |
|
23 |
+ protected $apiEndpoint; |
|
24 |
+ |
|
25 |
+ public function __construct(HttpClientInterface $httpClient) |
|
26 |
+ { |
|
27 |
+ $this->httpClient = $httpClient; |
|
28 |
+ |
|
29 |
+ $this->clientId = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_id'); |
|
30 |
+ $this->clientSecret = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.client_secret'); |
|
31 |
+ $this->apiEndpoint = System::getContainer()->getParameter('vonrotenberg_shopware_api.credentials.api_endpoint'); |
|
32 |
+ } |
|
33 |
+ |
|
34 |
+ protected function getClientId() |
|
35 |
+ { |
|
36 |
+ return $this->clientId; |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ protected function getClientSecret() |
|
40 |
+ { |
|
41 |
+ return $this->clientSecret; |
|
42 |
+ } |
|
43 |
+ |
|
44 |
+ protected function getApiEndpoint() |
|
45 |
+ { |
|
46 |
+ return rtrim($this->apiEndpoint,'/'); |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+ protected function sendRequest(string $relEndpoint, array $options, string $method = 'GET',bool $blnFQDNEndpoint=false) |
|
50 |
+ { |
|
51 |
+ if ($blnFQDNEndpoint) |
|
52 |
+ { |
|
53 |
+ return $this->httpClient->request($method,$relEndpoint,$options); |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ $relEndpoint = '/' . ltrim($relEndpoint,'/'); |
|
57 |
+ |
|
58 |
+ return $this->httpClient->request($method,$this->getApiEndpoint().$relEndpoint,$options); |
|
59 |
+ } |
|
60 |
+ |
|
61 |
+ public function testApiRequest() |
|
62 |
+ { |
|
63 |
+ $options = [ |
|
64 |
+ 'body' => json_encode([ |
|
65 |
+ 'grant_type' => 'client_credentials', |
|
66 |
+ 'client_id' => $this->clientId, |
|
67 |
+ 'client_secret' => $this->clientSecret |
|
68 |
+ ]), |
|
69 |
+ 'headers' => ['Content-Type: application/json', 'Accept: application/json'], |
|
70 |
+ ]; |
|
71 |
+ |
|
72 |
+ $response = $this->sendRequest('oauth/token',$options,'POST'); |
|
73 |
+ |
|
74 |
+ if ($response->getStatusCode() == 200) |
|
75 |
+ { |
|
76 |
+ $content = $response->getContent(); |
|
77 |
+ |
|
78 |
+ return json_decode($content); |
|
79 |
+ } |
|
80 |
+ |
|
81 |
+ return null; |
|
82 |
+ } |
|
83 |
+} |