1 <?php
2
3 namespace RetailCrm;
4
5 use RetailCrm\Http\Client;
6 use RetailCrm\Response\ApiResponse;
7
8 9 10
11 class ApiClient
12 {
13 const VERSION = 'v3';
14
15 protected $client;
16
17 18 19 20 21 22 23
24 public function __construct($url, $apiKey)
25 {
26 if ('/' != substr($url, strlen($url) - 1, 1)) {
27 $url .= '/';
28 }
29
30 $url = $url . 'api/' . self::VERSION;
31
32 $this->client = new Client($url, array('apiKey' => $apiKey));
33 }
34
35 36 37 38 39 40
41 public function ordersCreate(array $order)
42 {
43 if (!sizeof($order)) {
44 throw new \InvalidArgumentException('Parameter `order` must contains a data');
45 }
46
47 return $this->client->makeRequest("/orders/create", Client::METHOD_POST, array(
48 'order' => json_encode($order)
49 ));
50 }
51
52 53 54 55 56 57
58 public function ordersEdit(array $order, $by = 'externalId')
59 {
60 if (!sizeof($order)) {
61 throw new \InvalidArgumentException('Parameter `order` must contains a data');
62 }
63
64 $this->checkIdParameter($by);
65
66 if (!isset($order[$by])) {
67 throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by));
68 }
69
70 return $this->client->makeRequest("/orders/" . $order[$by] . "/edit", Client::METHOD_POST, array(
71 'order' => json_encode($order),
72 'by' => $by,
73 ));
74 }
75
76 77 78 79 80 81
82 public function ordersUpload(array $orders)
83 {
84 if (!sizeof($orders)) {
85 throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders');
86 }
87
88 return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, array(
89 'orders' => json_encode($orders),
90 ));
91 }
92
93 94 95 96 97 98 99
100 public function ordersGet($id, $by = 'externalId')
101 {
102 $this->checkIdParameter($by);
103
104 return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, array('by' => $by));
105 }
106
107 108 109 110 111 112 113 114 115 116
117 public function ordersHistory(
118 \DateTime $startDate = null,
119 \DateTime $endDate = null,
120 $limit = 100,
121 $offset = 0,
122 $skipMyChanges = true
123 ) {
124 $parameters = array();
125
126 if ($startDate) {
127 $parameters['startDate'] = $startDate->format('Y-m-d H:i:s');
128 }
129 if ($endDate) {
130 $parameters['endDate'] = $endDate->format('Y-m-d H:i:s');
131 }
132 if ($limit) {
133 $parameters['limit'] = (int) $limit;
134 }
135 if ($offset) {
136 $parameters['offset'] = (int) $offset;
137 }
138 if ($skipMyChanges) {
139 $parameters['skipMyChanges'] = (bool) $skipMyChanges;
140 }
141
142 return $this->client->makeRequest('/orders/history', Client::METHOD_GET, $parameters);
143 }
144
145 146 147 148 149 150 151 152
153 public function ordersList(array $filter = array(), $page = null, $limit = null)
154 {
155 $parameters = array();
156
157 if (sizeof($filter)) {
158 $parameters['filter'] = $filter;
159 }
160 if (null !== $page) {
161 $parameters['page'] = (int) $page;
162 }
163 if (null !== $limit) {
164 $parameters['limit'] = (int) $limit;
165 }
166
167 return $this->client->makeRequest('/orders', Client::METHOD_GET, $parameters);
168 }
169
170 171 172 173 174 175 176
177 public function (array $ids = array(), array $externalIds = array())
178 {
179 $parameters = array();
180
181 if (sizeof($ids)) {
182 $parameters['ids'] = $ids;
183 }
184 if (sizeof($externalIds)) {
185 $parameters['externalIds'] = $externalIds;
186 }
187
188 return $this->client->makeRequest('/orders/statuses', Client::METHOD_GET, $parameters);
189 }
190
191 192 193 194 195 196
197 public function ordersFixExternalIds(array $ids)
198 {
199 if (!sizeof($ids)) {
200 throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair');
201 }
202
203 return $this->client->makeRequest("/orders/fix-external-ids", Client::METHOD_POST, array(
204 'orders' => json_encode($ids),
205 ));
206 }
207
208 209 210 211 212 213
214 public function customersCreate(array $customer)
215 {
216 if (!sizeof($customer)) {
217 throw new \InvalidArgumentException('Parameter `customer` must contains a data');
218 }
219
220 return $this->client->makeRequest("/customers/create", Client::METHOD_POST, array(
221 'customer' => json_encode($customer)
222 ));
223 }
224
225 226 227 228 229 230
231 public function customersEdit(array $customer, $by = 'externalId')
232 {
233 if (!sizeof($customer)) {
234 throw new \InvalidArgumentException('Parameter `customer` must contains a data');
235 }
236
237 $this->checkIdParameter($by);
238
239 if (!isset($customer[$by])) {
240 throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by));
241 }
242
243 return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, array(
244 'customer' => json_encode($customer),
245 'by' => $by,
246 ));
247 }
248
249 250 251 252 253 254
255 public function customersUpload(array $customers)
256 {
257 if (!sizeof($customers)) {
258 throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers');
259 }
260
261 return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, array(
262 'customers' => json_encode($customers),
263 ));
264 }
265
266 267 268 269 270 271 272
273 public function customersGet($id, $by = 'externalId')
274 {
275 $this->checkIdParameter($by);
276
277 return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, array('by' => $by));
278 }
279
280 281 282 283 284 285 286 287
288 public function customersList(array $filter = array(), $page = null, $limit = null)
289 {
290 $parameters = array();
291
292 if (sizeof($filter)) {
293 $parameters['filter'] = $filter;
294 }
295 if (null !== $page) {
296 $parameters['page'] = (int) $page;
297 }
298 if (null !== $limit) {
299 $parameters['limit'] = (int) $limit;
300 }
301
302 return $this->client->makeRequest('/customers', Client::METHOD_GET, $parameters);
303 }
304
305 306 307 308 309 310
311 public function customersFixExternalIds(array $ids)
312 {
313 if (!sizeof($ids)) {
314 throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair');
315 }
316
317 return $this->client->makeRequest("/customers/fix-external-ids", Client::METHOD_POST, array(
318 'customers' => json_encode($ids),
319 ));
320 }
321
322 323 324 325 326
327 public function deliveryServicesList()
328 {
329 return $this->client->makeRequest('/reference/delivery-services', Client::METHOD_GET);
330 }
331
332 333 334 335 336
337 public function deliveryTypesList()
338 {
339 return $this->client->makeRequest('/reference/delivery-types', Client::METHOD_GET);
340 }
341
342 343 344 345 346
347 public function orderMethodsList()
348 {
349 return $this->client->makeRequest('/reference/order-methods', Client::METHOD_GET);
350 }
351
352 353 354 355 356
357 public function orderTypesList()
358 {
359 return $this->client->makeRequest('/reference/order-types', Client::METHOD_GET);
360 }
361
362 363 364 365 366
367 public function paymentStatusesList()
368 {
369 return $this->client->makeRequest('/reference/payment-statuses', Client::METHOD_GET);
370 }
371
372 373 374 375 376
377 public function paymentTypesList()
378 {
379 return $this->client->makeRequest('/reference/payment-types', Client::METHOD_GET);
380 }
381
382 383 384 385 386
387 public function productStatusesList()
388 {
389 return $this->client->makeRequest('/reference/product-statuses', Client::METHOD_GET);
390 }
391
392 393 394 395 396
397 public function statusGroupsList()
398 {
399 return $this->client->makeRequest('/reference/status-groups', Client::METHOD_GET);
400 }
401
402 403 404 405 406
407 public function statusesList()
408 {
409 return $this->client->makeRequest('/reference/statuses', Client::METHOD_GET);
410 }
411
412 413 414 415 416 417
418 public function deliveryServicesEdit(array $data)
419 {
420 if (!isset($data['code'])) {
421 throw new \InvalidArgumentException('Data must contain "code" parameter.');
422 }
423
424 return $this->client->makeRequest(
425 '/reference/delivery-services/' . $data['code'] . '/edit',
426 Client::METHOD_POST,
427 array(
428 'deliveryService' => json_encode($data)
429 )
430 );
431 }
432
433 434 435 436 437 438
439 public function deliveryTypesEdit(array $data)
440 {
441 if (!isset($data['code'])) {
442 throw new \InvalidArgumentException('Data must contain "code" parameter.');
443 }
444
445 return $this->client->makeRequest(
446 '/reference/delivery-types/' . $data['code'] . '/edit',
447 Client::METHOD_POST,
448 array(
449 'deliveryType' => json_encode($data)
450 )
451 );
452 }
453
454 455 456 457 458 459
460 public function orderMethodsEdit(array $data)
461 {
462 if (!isset($data['code'])) {
463 throw new \InvalidArgumentException('Data must contain "code" parameter.');
464 }
465
466 return $this->client->makeRequest(
467 '/reference/order-methods/' . $data['code'] . '/edit',
468 Client::METHOD_POST,
469 array(
470 'orderMethod' => json_encode($data)
471 )
472 );
473 }
474
475 476 477 478 479 480
481 public function orderTypesEdit(array $data)
482 {
483 if (!isset($data['code'])) {
484 throw new \InvalidArgumentException('Data must contain "code" parameter.');
485 }
486
487 return $this->client->makeRequest(
488 '/reference/order-types/' . $data['code'] . '/edit',
489 Client::METHOD_POST,
490 array(
491 'orderType' => json_encode($data)
492 )
493 );
494 }
495
496 497 498 499 500 501
502 public function paymentStatusesEdit(array $data)
503 {
504 if (!isset($data['code'])) {
505 throw new \InvalidArgumentException('Data must contain "code" parameter.');
506 }
507
508 return $this->client->makeRequest(
509 '/reference/payment-statuses/' . $data['code'] . '/edit',
510 Client::METHOD_POST,
511 array(
512 'paymentStatus' => json_encode($data)
513 )
514 );
515 }
516
517 518 519 520 521 522
523 public function paymentTypesEdit(array $data)
524 {
525 if (!isset($data['code'])) {
526 throw new \InvalidArgumentException('Data must contain "code" parameter.');
527 }
528
529 return $this->client->makeRequest(
530 '/reference/payment-types/' . $data['code'] . '/edit',
531 Client::METHOD_POST,
532 array(
533 'paymentType' => json_encode($data)
534 )
535 );
536 }
537
538 539 540 541 542 543
544 public function productStatusesEdit(array $data)
545 {
546 if (!isset($data['code'])) {
547 throw new \InvalidArgumentException('Data must contain "code" parameter.');
548 }
549
550 return $this->client->makeRequest(
551 '/reference/product-statuses/' . $data['code'] . '/edit',
552 Client::METHOD_POST,
553 array(
554 'productStatus' => json_encode($data)
555 )
556 );
557 }
558
559 560 561 562 563 564
565 public function statusesEdit(array $data)
566 {
567 if (!isset($data['code'])) {
568 throw new \InvalidArgumentException('Data must contain "code" parameter.');
569 }
570
571 return $this->client->makeRequest(
572 '/reference/statuses/' . $data['code'] . '/edit',
573 Client::METHOD_POST,
574 array(
575 'status' => json_encode($data)
576 )
577 );
578 }
579
580 581 582 583 584
585 public function statisticUpdate()
586 {
587 return $this->client->makeRequest('/statistic/update', Client::METHOD_GET);
588 }
589
590 591 592 593 594 595
596 protected function checkIdParameter($by)
597 {
598 $allowedForBy = array('externalId', 'id');
599 if (!in_array($by, $allowedForBy)) {
600 throw new \InvalidArgumentException(sprintf(
601 'Value "%s" for parameter "by" is not valid. Allowed values are %s.',
602 $by,
603 implode(', ', $allowedForBy)
604 ));
605 }
606
607 return true;
608 }
609 }
610