diff --git a/README.md b/README.md index a9f5326..9d9dfef 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ composer require retailcrm/api-client-php 3.0 Usage ----- -Example of the usage: +Example of the receipt of order: ```php $client = new \RetailCrm\ApiClient( @@ -43,4 +43,35 @@ if ($response->isSuccessful()) { // $order = $response->getOrder(); // $order['totalSumm']; } +``` + +Example of the order creating: +```php + +$client = new \RetailCrm\ApiClient( + 'https://demo.intarocrm.ru', + 'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH' +); + +try { + $response = $client->ordersCreate(array( + 'externalId' => 'some-shop-order-id', + 'firstName' => 'Vasily', + 'lastName' => 'Pupkin', + 'items' => array( + //... + ), + delivery => array( + 'code' => 'russian-post', + ) + )); +} catch (\RetailCrm\Exception\CurlException $e) { + echo "CRM connection error: " . $e->getMessage(); +} + +if ($response->isSuccessful() && 201 === $response->getStatusCode()) { + echo 'Order created successfully! Order ID in CRM - ' . $response->id; + // or $response['id']; + // or $response->getId(); +} ``` \ No newline at end of file diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index f79b6e3..a787f1f 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -79,6 +79,35 @@ class ApiClient return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, array('by' => $by)); } + /** + * Returns a orders history + * + * @param \DateTime $startDate (default: null) + * @param \DateTime $endDate (default: null) + * @param int $limit (default: 100) + * @param int $offset (default: 0) + * @return ApiResponse + */ + public function ordersHistory(\DateTime $startDate = null, \DateTime $endDate = null, $limit = 100, $offset = 0) + { + $parameters = array(); + + if ($startDate) { + $parameters['startDate'] = $startDate->format('Y-m-d H:i:s'); + } + if ($endDate) { + $parameters['endDate'] = $endDate->format('Y-m-d H:i:s'); + } + if ($limit) { + $parameters['limit'] = (int) $limit; + } + if ($offset) { + $parameters['offset'] = (int) $offset; + } + + return $this->client->makeRequest('/orders/history', Client::METHOD_GET, $parameters); + } + /** * Check ID parameter * diff --git a/tests/RetailCrm/Tests/ApiClientTest.php b/tests/RetailCrm/Tests/ApiClientTest.php index 108a97e..d4b80a5 100644 --- a/tests/RetailCrm/Tests/ApiClientTest.php +++ b/tests/RetailCrm/Tests/ApiClientTest.php @@ -113,7 +113,6 @@ class ApiClientTest extends TestCase $this->assertTrue($response->success); } - /** * @group unit * @expectedException \InvalidArgumentException @@ -124,4 +123,18 @@ class ApiClientTest extends TestCase $response = $client->ordersEdit(array('id' => 678678678), 'asdf'); } + + /** + * @group integration + */ + public function testOrdersHistory() + { + $client = static::getApiClient(); + + $response = $client->ordersHistory(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertTrue(isset($response['orders'])); + } } diff --git a/tests/RetailCrm/Tests/Http/ClientTest.php b/tests/RetailCrm/Tests/Http/ClientTest.php index 59b8260..6e46653 100644 --- a/tests/RetailCrm/Tests/Http/ClientTest.php +++ b/tests/RetailCrm/Tests/Http/ClientTest.php @@ -53,7 +53,7 @@ class ClientTest extends TestCase public function testMakeRequestSuccess() { $client = new Client('https://demo.intarocrm.ru/api/' . ApiClient::VERSION, array()); - $response = $client->makeRequest('/a', Client::METHOD_GET); + $response = $client->makeRequest('/orders', Client::METHOD_GET); $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); $this->assertEquals(403, $response->getStatusCode());