From b7ae093cc4dd0b95dd533ec41fd418c45139c267 Mon Sep 17 00:00:00 2001 From: Opheugene Date: Thu, 20 Jun 2024 14:03:20 +0200 Subject: [PATCH 1/2] Support for working with customer subscriptions --- src/Model/Entity/Customers/Subscription.php | 45 +++++++++++++ .../CustomersSubscriptionsRequest.php | 47 +++++++++++++ src/ResourceGroup/Customers.php | 66 +++++++++++++++++++ tests/src/ResourceGroup/CustomersTest.php | 32 +++++++++ 4 files changed, 190 insertions(+) create mode 100644 src/Model/Entity/Customers/Subscription.php create mode 100644 src/Model/Request/Customers/CustomersSubscriptionsRequest.php diff --git a/src/Model/Entity/Customers/Subscription.php b/src/Model/Entity/Customers/Subscription.php new file mode 100644 index 0000000..0067bad --- /dev/null +++ b/src/Model/Entity/Customers/Subscription.php @@ -0,0 +1,45 @@ +channel = 'waba'; + * $subscription->active = false; + * $subscription->unsubscribeMessageId = 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..79ee92c 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,34 @@ EOF; self::assertModelEqualsToResponse($json, $response); } + + public function testCustomersSubscriptions(): void + { + $json = <<<'EOF' +{ + "success": true +} +EOF; + + $subscription = new Subscription(); + $subscription->channel = 'waba'; + $subscription->active = false; + $subscription->unsubscribeMessageId = 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); + } } From 0298a1ad5859b500b915c0ee79a1cc3567048b6f Mon Sep 17 00:00:00 2001 From: Opheugene Date: Thu, 20 Jun 2024 16:47:02 +0200 Subject: [PATCH 2/2] new fields --- src/Model/Entity/Customers/Subscription.php | 12 ++++++++++-- src/ResourceGroup/Customers.php | 17 +++++++++-------- tests/src/ResourceGroup/CustomersTest.php | 3 ++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Model/Entity/Customers/Subscription.php b/src/Model/Entity/Customers/Subscription.php index 0067bad..e6beb38 100644 --- a/src/Model/Entity/Customers/Subscription.php +++ b/src/Model/Entity/Customers/Subscription.php @@ -27,6 +27,14 @@ class Subscription */ public $channel; + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("subscription") + */ + public $subscription; + /** * @var bool * @@ -39,7 +47,7 @@ class Subscription * @var int * * @JMS\Type("int") - * @JMS\SerializedName("unsubscribeMessageId") + * @JMS\SerializedName("messageId") */ - public $unsubscribeMessageId; + public $messageId; } diff --git a/src/ResourceGroup/Customers.php b/src/ResourceGroup/Customers.php index e47c275..4b68f1f 100644 --- a/src/ResourceGroup/Customers.php +++ b/src/ResourceGroup/Customers.php @@ -797,14 +797,15 @@ class Customers extends AbstractApiResourceGroup * * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); * - * $subscription = new Subscription(); - * $subscription->channel = 'waba'; - * $subscription->active = false; - * $subscription->unsubscribeMessageId = 123; - * $request = new CustomersSubscriptionsRequest(); - * $request->by = ByIdentifier::ID; - * $request->site = 'aliexpress'; - * $request->subscriptions = [$subscription]; + * $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]; * * try { * $response = $client->customers->subscriptions(4770, $request); diff --git a/tests/src/ResourceGroup/CustomersTest.php b/tests/src/ResourceGroup/CustomersTest.php index 79ee92c..2c26914 100644 --- a/tests/src/ResourceGroup/CustomersTest.php +++ b/tests/src/ResourceGroup/CustomersTest.php @@ -2786,8 +2786,9 @@ EOF; $subscription = new Subscription(); $subscription->channel = 'waba'; + $subscription->subscription = 'category'; $subscription->active = false; - $subscription->unsubscribeMessageId = 123; + $subscription->messageId = 123; $request = new CustomersSubscriptionsRequest(); $request->by = ByIdentifier::ID;