retailCRM PHP API client
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • RetailCrm
    • Exception
    • Http
    • Response

Classes

  • ApiClient
  1 <?php
  2 
  3 namespace RetailCrm;
  4 
  5 use RetailCrm\Http\Client;
  6 use RetailCrm\Response\ApiResponse;
  7 
  8 /**
  9  *  retailCRM API client class
 10  */
 11 class ApiClient
 12 {
 13     const VERSION = 'v3';
 14 
 15     protected $client;
 16 
 17     /**
 18      * Client creating
 19      *
 20      * @param  string $url
 21      * @param  string $apiKey
 22      * @return mixed
 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      * Create a order
 37      *
 38      * @param  array       $order
 39      * @return ApiResponse
 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      * Edit a order
 54      *
 55      * @param  array       $order
 56      * @return ApiResponse
 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      * Upload array of the orders
 78      *
 79      * @param  array       $orders
 80      * @return ApiResponse
 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      * Get order by id or externalId
 95      *
 96      * @param  string      $id
 97      * @param  string      $by (default: 'externalId')
 98      * @return ApiResponse
 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      * Returns a orders history
109      *
110      * @param  \DateTime   $startDate (default: null)
111      * @param  \DateTime   $endDate (default: null)
112      * @param  int         $limit (default: 100)
113      * @param  int         $offset (default: 0)
114      * @param  bool        $skipMyChanges (default: true)
115      * @return ApiResponse
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      * Returns filtered orders list
147      *
148      * @param  array       $filter (default: array())
149      * @param  int         $page (default: null)
150      * @param  int         $limit (default: null)
151      * @return ApiResponse
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      * Returns statuses of the orders
172      *
173      * @param  array       $ids (default: array())
174      * @param  array       $externalIds (default: array())
175      * @return ApiResponse
176      */
177     public function ordersStatuses(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      * Save order IDs' (id and externalId) association in the CRM
193      *
194      * @param  array       $ids
195      * @return ApiResponse
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      * Create a customer
210      *
211      * @param  array       $customer
212      * @return ApiResponse
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      * Edit a customer
227      *
228      * @param  array       $customer
229      * @return ApiResponse
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      * Upload array of the customers
251      *
252      * @param  array       $customers
253      * @return ApiResponse
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      * Get customer by id or externalId
268      *
269      * @param  string      $id
270      * @param  string      $by (default: 'externalId')
271      * @return ApiResponse
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      * Returns filtered customers list
282      *
283      * @param  array       $filter (default: array())
284      * @param  int         $page (default: null)
285      * @param  int         $limit (default: null)
286      * @return ApiResponse
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      * Save customer IDs' (id and externalId) association in the CRM
307      *
308      * @param  array       $ids
309      * @return ApiResponse
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      * Returns deliveryServices list
324      *
325      * @return ApiResponse
326      */
327     public function deliveryServicesList()
328     {
329         return $this->client->makeRequest('/reference/delivery-services', Client::METHOD_GET);
330     }
331 
332     /**
333      * Returns deliveryTypes list
334      *
335      * @return ApiResponse
336      */
337     public function deliveryTypesList()
338     {
339         return $this->client->makeRequest('/reference/delivery-types', Client::METHOD_GET);
340     }
341 
342     /**
343      * Returns orderMethods list
344      *
345      * @return ApiResponse
346      */
347     public function orderMethodsList()
348     {
349         return $this->client->makeRequest('/reference/order-methods', Client::METHOD_GET);
350     }
351 
352     /**
353      * Returns orderTypes list
354      *
355      * @return ApiResponse
356      */
357     public function orderTypesList()
358     {
359         return $this->client->makeRequest('/reference/order-types', Client::METHOD_GET);
360     }
361 
362     /**
363      * Returns paymentStatuses list
364      *
365      * @return ApiResponse
366      */
367     public function paymentStatusesList()
368     {
369         return $this->client->makeRequest('/reference/payment-statuses', Client::METHOD_GET);
370     }
371 
372     /**
373      * Returns paymentTypes list
374      *
375      * @return ApiResponse
376      */
377     public function paymentTypesList()
378     {
379         return $this->client->makeRequest('/reference/payment-types', Client::METHOD_GET);
380     }
381 
382     /**
383      * Returns productStatuses list
384      *
385      * @return ApiResponse
386      */
387     public function productStatusesList()
388     {
389         return $this->client->makeRequest('/reference/product-statuses', Client::METHOD_GET);
390     }
391 
392     /**
393      * Returns statusGroups list
394      *
395      * @return ApiResponse
396      */
397     public function statusGroupsList()
398     {
399         return $this->client->makeRequest('/reference/status-groups', Client::METHOD_GET);
400     }
401 
402     /**
403      * Returns statuses list
404      *
405      * @return ApiResponse
406      */
407     public function statusesList()
408     {
409         return $this->client->makeRequest('/reference/statuses', Client::METHOD_GET);
410     }
411 
412     /**
413      * Edit deliveryService
414      *
415      * @param array $data delivery service data
416      * @return ApiResponse
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      * Edit deliveryType
435      *
436      * @param array $data delivery type data
437      * @return ApiResponse
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      * Edit orderMethod
456      *
457      * @param array $data order method data
458      * @return ApiResponse
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      * Edit orderType
477      *
478      * @param array $data order type data
479      * @return ApiResponse
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      * Edit paymentStatus
498      *
499      * @param array $data payment status data
500      * @return ApiResponse
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      * Edit paymentType
519      *
520      * @param array $data payment type data
521      * @return ApiResponse
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      * Edit productStatus
540      *
541      * @param array $data product status data
542      * @return ApiResponse
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      * Edit order status
561      *
562      * @param array $data status data
563      * @return ApiResponse
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      * Update CRM basic statistic
582      *
583      * @return ApiResponse
584      */
585     public function statisticUpdate()
586     {
587         return $this->client->makeRequest('/statistic/update', Client::METHOD_GET);
588     }
589 
590     /**
591      * Check ID parameter
592      *
593      * @param  string $by
594      * @return bool
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 
retailCRM PHP API client API documentation generated by ApiGen