1
0
Fork 0
mirror of synced 2025-04-03 22:03:34 +03:00
Adding API methods for working with the loyalty program
This commit is contained in:
Ivan Chaplygin 2024-04-03 17:24:50 +03:00
parent 498646c10a
commit 642e088898
2 changed files with 352 additions and 186 deletions

View file

@ -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 createPLAccount(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 getPLClientInfo(int $clientIdPL)
{
return $this->client->makeRequest(
"/loyalty/account/$clientIdPL",
WC_Retailcrm_Request::METHOD_GET
);
}
/** Activation of participation in the loyalty program */
public function activatePLAccount(int $clientIdPL)
{
return $this->client->makeRequest(
"/loyalty/account/$clientIdPL/activate",
WC_Retailcrm_Request::METHOD_POST
);
}
/** Editing participation in the loyalty program */
public function editPLAccount(int $clientIdPL, array $parameters)
{
return $this->client->makeRequest(
"/loyalty/account/$clientIdPL/edit",
WC_Retailcrm_Request::METHOD_POST,
['id' => $clientIdPL, 'loyaltyAccount' => json_encode($parameters)]
);
}
/** List of participation in the loyalty program */
public function getPLAccountList(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 getListPL(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 getPL(int $idPL)
{
return $this->client->makeRequest(
"/loyalty/loyalties/$idPL",
WC_Retailcrm_Request::METHOD_GET
);
}
/** Charge bonus */
public function chargeBonusPL(int $clientIdPL, float $amount, string $comment = '')
{
return $this->client->makeRequest(
"/loyalty/account/$clientIdPL/bonus/charge",
WC_Retailcrm_Request::METHOD_POST,
['amount' => $amount, 'comment' => $comment]
);
}
public function creditBonusPL(int $clientIdPL, array $parameters)
{
if (!isset($parameters['amount'])) {
throw new InvalidArgumentException(
'Parameter `amount` does not exist'
);
}
return $this->client->makeRequest(
"/loyalty/account/$clientIdPL/bonus/credit",
WC_Retailcrm_Request::METHOD_POST,
$parameters
);
}
/** History of the client's bonus account */
public function getClientBonusHistory(int $clientIdPL, array $filter = [], $limit = null, $page = null)
{
$parameters = $this->buildParameters($filter, $limit, $page);
$parameters['id'] = $clientIdPL;
return $this->client->makeRequest(
"/loyalty/account/$clientIdPL/bonus/operations",
WC_Retailcrm_Request::METHOD_GET,
$parameters
);
}
public function getDetailClientBonus(
int $clientIdPL,
string $status,
array $filter = [],
$limit = null,
$page = null
) {
$parameters = $this->buildParameters($filter, $limit, $page);
$parameters['id'] = $clientIdPL;
$parameters['status'] = $status;
return $this->client->makeRequest(
"/api/v5/loyalty/account/$clientIdPL/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 calculateDiscountPL(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;
}
}

View file

@ -0,0 +1,135 @@
<?php
if (!class_exists('WC_Retailcrm_Client_V5')) {
include_once(WC_Integration_Retailcrm::checkCustomFile('include/api/class-wc-retailcrm-client-v5.php'));
}
/**
* PHP version 7.0
*
* Class WC_Retailcrm_Loyalty_Client_Test
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @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 requestPLData
*/
public function testPLRequest($method, $parameters)
{
/** @var WC_Retailcrm_Response $test */
$test = call_user_func([$this->clientMock, $method], ...$parameters);
$this->assertTrue($test->isSuccessful());
}
public function requestPLData()
{
return [
[
'method' => 'createPLAccount',
'parameters' => [['test'], 'testSite']
],
[
'method' => 'getPLClientInfo',
'parameters' => [1]
],
[
'method' => 'activatePLAccount',
'parameters' => [1]
],
[
'method' => 'editPLAccount',
'parameters' => [1, ['test']]
],
[
'method' => 'getPLAccountList',
'parameters' => [['filter'], 20, 1]
],
[
'method' => 'getListPL',
'parameters' => [['filter'], 20, 1]
],
[
'method' => 'getPL',
'parameters' => [1]
],
[
'method' => 'chargeBonusPL',
'parameters' => [1, 100, 'test']
],
[
'method' => 'creditBonusPL',
'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' => 'calculateDiscountPL',
'parameters' => ['site', ['order'], 100]
],
[
'method' => 'applyBonusToOrder',
'parameters' => ['site', ['order'], 100]
],
[
'method' => 'cancelBonusOrder',
'parameters' => ['site', ['order']]
],
];
}
}