From a445c3d908ad65428e30dd2505c9f26664d7487d Mon Sep 17 00:00:00 2001 From: Kocmonavtik <61938582+Kocmonavtik@users.noreply.github.com> Date: Thu, 4 Apr 2024 09:10:34 +0300 Subject: [PATCH] ref #71905 Add API methods for loyalty program (#324) --- .../api/class-wc-retailcrm-client-v5.php | 403 ++++++++++-------- tests/loyalty/test-wc-retailcrm-client-v5.php | 135 ++++++ 2 files changed, 352 insertions(+), 186 deletions(-) create mode 100644 tests/loyalty/test-wc-retailcrm-client-v5.php diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index 449125f..540172a 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -169,17 +169,7 @@ class WC_Retailcrm_Client_V5 */ public function customersCorporateList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/customers-corporate', @@ -273,17 +263,7 @@ class WC_Retailcrm_Client_V5 */ public function customersCorporateNotesList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/customers-corporate/notes', @@ -421,17 +401,8 @@ class WC_Retailcrm_Client_V5 ) { $this->checkIdParameter($by); - $parameters = ['by' => $by]; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); + $parameters['by'] = $by; return $this->client->makeRequest( "/customers-corporate/$id/addresses", @@ -537,17 +508,8 @@ class WC_Retailcrm_Client_V5 ) { $this->checkIdParameter($by); - $parameters = ['by' => $by]; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); + $parameters['by'] = $by; return $this->client->makeRequest( "/customers-corporate/$id/companies", @@ -640,17 +602,8 @@ class WC_Retailcrm_Client_V5 ) { $this->checkIdParameter($by); - $parameters = ['by' => $by]; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); + $parameters['by'] = $by; return $this->client->makeRequest( "/customers-corporate/$id/contacts", @@ -769,17 +722,7 @@ class WC_Retailcrm_Client_V5 */ public function usersList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/users', @@ -840,17 +783,7 @@ class WC_Retailcrm_Client_V5 */ public function segmentsList(array $filter = [], $limit = null, $page = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/segments', @@ -870,17 +803,7 @@ class WC_Retailcrm_Client_V5 */ public function customFieldsList(array $filter = [], $limit = null, $page = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/custom-fields', @@ -994,17 +917,7 @@ class WC_Retailcrm_Client_V5 */ public function customDictionariesList(array $filter = [], $limit = null, $page = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/custom-fields/dictionaries', @@ -1101,17 +1014,7 @@ class WC_Retailcrm_Client_V5 */ public function ordersList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/orders', @@ -1445,17 +1348,7 @@ class WC_Retailcrm_Client_V5 */ public function customersList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/customers', @@ -1666,16 +1559,8 @@ class WC_Retailcrm_Client_V5 */ public function customersNotesList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); + return $this->client->makeRequest( '/customers/notes', WC_Retailcrm_Request::METHOD_GET, @@ -1748,17 +1633,7 @@ class WC_Retailcrm_Client_V5 */ public function ordersPacksList(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/orders/packs', @@ -1899,17 +1774,7 @@ class WC_Retailcrm_Client_V5 */ public function tasksList(array $filter = [], $limit = null, $page = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/tasks', @@ -2008,17 +1873,7 @@ class WC_Retailcrm_Client_V5 */ public function storeProductsGroups(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/store/product-groups', @@ -2042,17 +1897,7 @@ class WC_Retailcrm_Client_V5 */ public function storeInventories(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/store/inventories', @@ -2180,17 +2025,7 @@ class WC_Retailcrm_Client_V5 */ public function storeProducts(array $filter = [], $page = null, $limit = null) { - $parameters = []; - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $parameters = $this->buildParameters($filter, $page, $limit); return $this->client->makeRequest( '/store/products', @@ -3008,6 +2843,183 @@ class WC_Retailcrm_Client_V5 ); } + /** Loyalty program */ + + /** Customer registration in the loyalty program */ + public function createLoyaltyAccount(array $parameters, $site = null) + { + if ([] === $parameters) { + throw new InvalidArgumentException( + 'Parameter `parameters` must contains a data' + ); + } + + return $this->client->makeRequest( + '/loyalty/account/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, ['loyaltyAccount' => json_encode($parameters)]) + ); + } + + /** Receiving information about participation in the loyalty program */ + public function getLoyaltyClientInfo(int $clientIdLoyalty) + { + return $this->client->makeRequest( + "/loyalty/account/$clientIdLoyalty", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** Activation of participation in the loyalty program */ + public function activateLoyaltyAccount(int $clientIdLoyalty) + { + return $this->client->makeRequest( + "/loyalty/account/$clientIdLoyalty/activate", + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** Editing participation in the loyalty program */ + public function editLoyaltyAccount(int $clientIdLoyalty, array $parameters) + { + return $this->client->makeRequest( + "/loyalty/account/$clientIdLoyalty/edit", + WC_Retailcrm_Request::METHOD_POST, + ['id' => $clientIdLoyalty, 'loyaltyAccount' => json_encode($parameters)] + ); + } + + /** List of participation in the loyalty program */ + public function getLoyaltyAccountList(array $filter = [], $limit = null, $page = null) + { + $parameters = $this->buildParameters($filter, $limit, $page); + + return $this->client->makeRequest( + '/loyalty/accounts', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** List of loyalty programs */ + public function getListLoyalty(array $filter = [], $limit = null, $page = null) + { + $parameters = $this->buildParameters($filter, $limit, $page); + + return $this->client->makeRequest( + '/loyalty/loyalties', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** Receiving information about the loyalty program */ + public function getLoyalty(int $idLoyalty) + { + return $this->client->makeRequest( + "/loyalty/loyalties/$idLoyalty", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** Charge bonus */ + public function chargeBonusLoyalty(int $clientIdLoyalty, float $amount, string $comment = '') + { + return $this->client->makeRequest( + "/loyalty/account/$clientIdLoyalty/bonus/charge", + WC_Retailcrm_Request::METHOD_POST, + ['amount' => $amount, 'comment' => $comment] + ); + } + + public function creditBonusLoyalty(int $clientIdLoyalty, array $parameters) + { + if (!isset($parameters['amount'])) { + throw new InvalidArgumentException( + 'Parameter `amount` does not exist' + ); + } + + return $this->client->makeRequest( + "/loyalty/account/$clientIdLoyalty/bonus/credit", + WC_Retailcrm_Request::METHOD_POST, + $parameters + ); + } + + /** History of the client's bonus account */ + public function getClientBonusHistory(int $clientIdLoyalty, array $filter = [], $limit = null, $page = null) + { + $parameters = $this->buildParameters($filter, $limit, $page); + $parameters['id'] = $clientIdLoyalty; + + return $this->client->makeRequest( + "/loyalty/account/$clientIdLoyalty/bonus/operations", + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + public function getDetailClientBonus( + int $clientIdLoyalty, + string $status, + array $filter = [], + $limit = null, + $page = null + ) { + $parameters = $this->buildParameters($filter, $limit, $page); + $parameters['id'] = $clientIdLoyalty; + $parameters['status'] = $status; + + return $this->client->makeRequest( + "/api/v5/loyalty/account/$clientIdLoyalty/bonus/$status/details", + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** Bonus account history for all participants */ + public function getBonusHistory(string $cursor, array $filter = [], $limit = null) + { + $parameters = $this->buildParameters($filter, $limit); + $parameters['cursor'] = $cursor; + + return $this->client->makeRequest( + "/loyalty/bonus/operations", + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** Maximum discount calculation */ + public function calculateDiscountLoyalty(string $site, array $order, float $bonuses) + { + return $this->client->makeRequest( + "/loyalty/calculate", + WC_Retailcrm_Request::METHOD_POST, + ['site' => $site, 'order' => json_encode($order), 'bonuses' => $bonuses] + ); + } + + /** Application of bonuses under the loyalty program */ + public function applyBonusToOrder(string $site, array $order, float $bonuses) + { + return $this->client->makeRequest( + "/orders/loyalty/apply", + WC_Retailcrm_Request::METHOD_POST, + ['site' => $site, 'order' => json_encode($order), 'bonuses' => $bonuses] + ); + } + + public function cancelBonusOrder(string $site, array $order) + { + return $this->client->makeRequest( + "/orders/loyalty/cancel-bonus-operations", + WC_Retailcrm_Request::METHOD_POST, + ['site' => $site, 'order' => json_encode($order)] + ); + } + /** * Update CRM basic statistic * @@ -3121,4 +3133,23 @@ class WC_Retailcrm_Client_V5 return $params; } + + protected function buildParameters(array $filter = [], $limit = null, $page = null): array + { + $parameters = []; + + if (count($filter)) { + $parameters['filter'] = $filter; + } + + if (isset($page)) { + $parameters['page'] = (int) $page; + } + + if (isset($limit)) { + $parameters['limit'] = (int) $limit; + } + + return $parameters; + } } diff --git a/tests/loyalty/test-wc-retailcrm-client-v5.php b/tests/loyalty/test-wc-retailcrm-client-v5.php new file mode 100644 index 0000000..01e128a --- /dev/null +++ b/tests/loyalty/test-wc-retailcrm-client-v5.php @@ -0,0 +1,135 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ + +class WC_Retailcrm_Loyalty_Client_Test extends WC_Retailcrm_Test_Case_Helper +{ + protected $responseMock; + protected $apiMock; + + /** @var \WC_Retailcrm_Client_V5 */ + protected $clientMock; + + public function setUp() + { + $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(['isSuccessful']) + ->getMock() + ; + + $this->responseMock->setResponse(['success' => true]); + $this->setMockResponse($this->responseMock, 'isSuccessful', true); + + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Request') + ->disableOriginalConstructor() + ->setMethods(['makeRequest']) + ->getMock() + ; + + $this->setMockResponse($this->apiMock, 'makeRequest', $this->responseMock); + + $this->clientMock = new \WC_Retailcrm_Client_V5('https://test@retailcrm.ru', 'test', 'test'); + /* $this->clientMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5') + ->disableOriginalConstructor() + ->getMock() + ;*/ + + $reflection = new ReflectionClass($this->clientMock); + $reflection_property = $reflection->getProperty('client'); + $reflection_property->setAccessible(true); + + $reflection_property->setValue($this->clientMock, $this->apiMock); + } + + /** + * @dataProvider requestLoyaltyData + */ + public function testLoyaltyRequest($method, $parameters) + { + /** @var WC_Retailcrm_Response $test */ + $test = call_user_func([$this->clientMock, $method], ...$parameters); + + $this->assertTrue($test->isSuccessful()); + } + + public function requestLoyaltyData() + { + return [ + [ + 'method' => 'createLoyaltyAccount', + 'parameters' => [['test'], 'testSite'] + ], + [ + 'method' => 'getLoyaltyClientInfo', + 'parameters' => [1] + ], + [ + 'method' => 'activateLoyaltyAccount', + 'parameters' => [1] + ], + [ + 'method' => 'editLoyaltyAccount', + 'parameters' => [1, ['test']] + ], + [ + 'method' => 'getLoyaltyAccountList', + 'parameters' => [['filter'], 20, 1] + ], + [ + 'method' => 'getListLoyalty', + 'parameters' => [['filter'], 20, 1] + ], + [ + 'method' => 'getLoyalty', + 'parameters' => [1] + ], + [ + 'method' => 'chargeBonusLoyalty', + 'parameters' => [1, 100, 'test'] + ], + [ + 'method' => 'creditBonusLoyalty', + 'parameters' => [1, ['amount' => 100]] + ], + [ + 'method' => 'getClientBonusHistory', + 'parameters' => [1, ['filter'], 20, 1] + ], + [ + 'method' => 'getDetailClientBonus', + 'parameters' => [1, 'status', ['filter'], 20, 1] + ], + [ + 'method' => 'getBonusHistory', + 'parameters' => ['cursor', ['filter'], 20] + ], + [ + 'method' => 'calculateDiscountLoyalty', + 'parameters' => ['site', ['order'], 100] + ], + [ + 'method' => 'applyBonusToOrder', + 'parameters' => ['site', ['order'], 100] + ], + [ + 'method' => 'cancelBonusOrder', + 'parameters' => ['site', ['order']] + ], + ]; + } +}