diff --git a/src/Model/Entity/Customers/Subscription.php b/src/Model/Entity/Customers/Subscription.php new file mode 100644 index 0000000..e6beb38 --- /dev/null +++ b/src/Model/Entity/Customers/Subscription.php @@ -0,0 +1,53 @@ +channel = 'waba'; + * $subscription->subscription = 'category'; + * $subscription->active = false; + * $subscription->messageId = 123; + * $request = new CustomersSubscriptionsRequest(); + * $request->by = ByIdentifier::ID; + * $request->site = 'aliexpress'; + * $request->subscriptions = [$subscription]; + * + * try { + * $response = $client->customers->subscriptions(4770, $request); + * } 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; + * } + * ``` + * + * @param int|string $identifier + * @param \RetailCrm\Api\Model\Request\Customers\CustomersSubscriptionsRequest $request + * + * @return \RetailCrm\Api\Model\Response\SuccessResponse + * @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($identifier, CustomersSubscriptionsRequest $request): SuccessResponse + { + /** @var SuccessResponse $response */ + $response = $this->sendRequest( + RequestMethod::POST, + 'customers/' . $identifier . '/subscriptions', + $request, + SuccessResponse::class + ); + return $response; + } } diff --git a/tests/src/ResourceGroup/CustomersTest.php b/tests/src/ResourceGroup/CustomersTest.php index 3b99ed9..2c26914 100644 --- a/tests/src/ResourceGroup/CustomersTest.php +++ b/tests/src/ResourceGroup/CustomersTest.php @@ -22,6 +22,7 @@ use RetailCrm\Api\Model\Entity\Customers\CustomerNote; use RetailCrm\Api\Model\Entity\Customers\CustomerPhone; use RetailCrm\Api\Model\Entity\Customers\CustomerTag; use RetailCrm\Api\Model\Entity\Customers\SerializedCustomerReference; +use RetailCrm\Api\Model\Entity\Customers\Subscription; use RetailCrm\Api\Model\Entity\FixExternalRow; use RetailCrm\Api\Model\Filter\Customers\CustomerFilter; use RetailCrm\Api\Model\Filter\Customers\CustomerHistoryFilter; @@ -35,6 +36,7 @@ use RetailCrm\Api\Model\Request\Customers\CustomersHistoryRequest; use RetailCrm\Api\Model\Request\Customers\CustomersNotesCreateRequest; use RetailCrm\Api\Model\Request\Customers\CustomersNotesRequest; use RetailCrm\Api\Model\Request\Customers\CustomersRequest; +use RetailCrm\Api\Model\Request\Customers\CustomersSubscriptionsRequest; use RetailCrm\Api\Model\Request\Customers\CustomersUploadRequest; use RetailCrm\TestUtils\Factory\TestClientFactory; use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase; @@ -2773,4 +2775,35 @@ EOF; self::assertModelEqualsToResponse($json, $response); } + + public function testCustomersSubscriptions(): void + { + $json = <<<'EOF' +{ + "success": true +} +EOF; + + $subscription = new Subscription(); + $subscription->channel = 'waba'; + $subscription->subscription = 'category'; + $subscription->active = false; + $subscription->messageId = 123; + + $request = new CustomersSubscriptionsRequest(); + $request->by = ByIdentifier::ID; + $request->site = 'aliexpress'; + $request->subscriptions = [$subscription]; + + $mock = static::createApiMockBuilder('customers/4770/subscriptions'); + $mock->matchMethod(RequestMethod::POST) + ->matchBody(static::encodeForm($request)) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->customers->subscriptions(4770, $request); + + self::assertModelEqualsToResponse($json, $response); + } }