diff --git a/src/Model/Request/References/SubscriptionsEditRequest.php b/src/Model/Request/References/SubscriptionsEditRequest.php new file mode 100644 index 000000000..b65e7d45f --- /dev/null +++ b/src/Model/Request/References/SubscriptionsEditRequest.php @@ -0,0 +1,44 @@ +subscription = $subscription; + } + } +} diff --git a/src/Model/Response/References/SubscriptionsResponse.php b/src/Model/Response/References/SubscriptionsResponse.php new file mode 100644 index 000000000..678367576 --- /dev/null +++ b/src/Model/Response/References/SubscriptionsResponse.php @@ -0,0 +1,30 @@ +") + * @JMS\SerializedName("subscriptions") + */ + public $subscriptions; +} diff --git a/src/ResourceGroup/References.php b/src/ResourceGroup/References.php index a2b943eb2..f810547d3 100644 --- a/src/ResourceGroup/References.php +++ b/src/ResourceGroup/References.php @@ -26,6 +26,7 @@ use RetailCrm\Api\Model\Request\References\SitesEditRequest; use RetailCrm\Api\Model\Request\References\StatusesEditRequest; use RetailCrm\Api\Model\Request\References\StoresEditRequest; +use RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest; use RetailCrm\Api\Model\Request\References\UnitsEditRequest; use RetailCrm\Api\Model\Response\IdResponse; use RetailCrm\Api\Model\Response\References\CostGroupsResponse; @@ -47,6 +48,7 @@ use RetailCrm\Api\Model\Response\References\StatusesResponse; use RetailCrm\Api\Model\Response\References\StatusGroupsResponse; use RetailCrm\Api\Model\Response\References\StoresResponse; +use RetailCrm\Api\Model\Response\References\SubscriptionsResponse; use RetailCrm\Api\Model\Response\References\UnitsResponse; use RetailCrm\Api\Model\Response\SuccessResponse; @@ -2166,6 +2168,135 @@ public function storesEdit(string $code, StoresEditRequest $request): SuccessRes return $response; } + /** + * Makes GET "/api/v5/reference/subscriptions" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * try { + * $response = $client->references->subscriptions(); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * + * return; + * } + * + * echo 'Subscriptions: ' . print_r($response->subscriptions, true); + * ``` + * + * @return \RetailCrm\Api\Model\Response\References\SubscriptionsResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function subscriptions(): SubscriptionsResponse + { + /** @var SubscriptionsResponse $response */ + $response = $this->sendRequest( + RequestMethod::GET, + 'reference/subscriptions', + null, + SubscriptionsResponse::class + ); + return $response; + } + + /** + * Makes POST "/api/v5/reference/subscriptions/{channel}/{code}/edit" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * use RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory; + * use RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * $entity = new SubscriptionCategory(); + * $entity->channel = 'email'; + * $entity->name = 'Новости'; + * $entity->active = true; + * $entity->autoSubscribe = false; + * $entity->ordering = 10; + * + * try { + * $response = $client->references->subscriptionsEdit( + * 'email', + * 'news', + * new SubscriptionsEditRequest($entity) + * ); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * + * return; + * } + * + * echo 'Created subscription category with ID: ' . $response->id; + * ``` + * + * @param string $channel + * @param string $code + * @param \RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest $request + * + * @return \RetailCrm\Api\Model\Response\IdResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function subscriptionsEdit(string $channel, string $code, SubscriptionsEditRequest $request): IdResponse + { + $sendRequest = $request; + + if (null !== $request->subscription) { + $subscription = clone $request->subscription; + $subscription->channel = $channel; + $sendRequest = new SubscriptionsEditRequest($subscription); + } + + /** @var IdResponse $response */ + $response = $this->sendRequest( + RequestMethod::POST, + 'reference/subscriptions/' . $channel . '/' . $code . '/edit', + $sendRequest, + IdResponse::class + ); + return $response; + } + /** * Makes GET "/api/v5/reference/units" request. * diff --git a/tests/src/ResourceGroup/ReferencesTest.php b/tests/src/ResourceGroup/ReferencesTest.php index eb96d07cc..aa403e4a4 100644 --- a/tests/src/ResourceGroup/ReferencesTest.php +++ b/tests/src/ResourceGroup/ReferencesTest.php @@ -15,6 +15,7 @@ use RetailCrm\Api\Enum\RequestMethod; use RetailCrm\Api\Model\Callback\Entity\Delivery\SerializedStoreWeekOpeningHours; use RetailCrm\Api\Model\Callback\Entity\Delivery\StoreWorkTime; +use RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory; use RetailCrm\Api\Model\Entity\Orders\Delivery\CourierPhone; use RetailCrm\Api\Model\Entity\References\CostGroup; use RetailCrm\Api\Model\Entity\References\CostItem; @@ -51,6 +52,7 @@ use RetailCrm\Api\Model\Request\References\SitesEditRequest; use RetailCrm\Api\Model\Request\References\StatusesEditRequest; use RetailCrm\Api\Model\Request\References\StoresEditRequest; +use RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest; use RetailCrm\Api\Model\Request\References\UnitsEditRequest; use RetailCrm\TestUtils\Factory\TestClientFactory; use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase; @@ -3712,6 +3714,82 @@ public function testStoresEdit(): void self::assertModelEqualsToResponse($json, $response); } + public function testSubscriptions(): void + { + $json = <<<'EOF' +{ + "success": true, + "subscriptions": [ + { + "id": 2, + "channel": "email", + "name": "Без тематики", + "code": "default_marketing", + "active": true, + "autoSubscribe": true, + "ordering": 1 + }, + { + "id": 4, + "channel": "waba", + "name": "Новости", + "code": "news", + "active": true, + "autoSubscribe": false, + "ordering": 10 + } + ] +} +EOF; + + $mock = static::createApiMockBuilder('reference/subscriptions'); + $mock->matchMethod(RequestMethod::GET) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->references->subscriptions(); + + self::assertModelEqualsToResponse($json, $response); + } + + public function testSubscriptionsEdit(): void + { + $json = <<<'EOF' +{ + "success": true, + "id": 18 +} +EOF; + + $entity = new SubscriptionCategory(); + $entity->name = 'Новости'; + $entity->active = true; + $entity->autoSubscribe = false; + $entity->ordering = 10; + + $request = new SubscriptionsEditRequest($entity); + $expectedEntity = new SubscriptionCategory(); + $expectedEntity->channel = 'email'; + $expectedEntity->name = 'Новости'; + $expectedEntity->active = true; + $expectedEntity->autoSubscribe = false; + $expectedEntity->ordering = 10; + $expectedRequest = new SubscriptionsEditRequest($expectedEntity); + + $mock = static::createApiMockBuilder('reference/subscriptions/email/news/edit'); + $mock->matchMethod(RequestMethod::POST) + ->matchBody(self::encodeForm($expectedRequest)) + ->reply(201) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->references->subscriptionsEdit('email', 'news', $request); + + self::assertModelEqualsToResponse($json, $response); + self::assertNull($request->subscription->channel); + } + public function testUnits(): void { $json = <<<'EOF'