1
0
mirror of synced 2024-11-25 06:46:07 +03:00

Add Loyalty methods (#165)

Loyalty API
This commit is contained in:
curse89 2023-05-05 09:54:42 +03:00 committed by GitHub
parent 1901f1a81d
commit 2efa527817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1204 additions and 1 deletions

4
.gitignore vendored
View File

@ -12,3 +12,7 @@ phpunit.xml
.project
.swp
/nbproject
.docker
docker-compose*.yml
.php_cs*
.phpunit*

View File

@ -114,6 +114,82 @@ abstract class AbstractLoader
return true;
}
/**
* Check ID type
*
* @param int $id
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function checkId($id)
{
if (!is_int($id)) {
throw new \InvalidArgumentException(
'Parameter `id` must be integer'
);
}
}
/**
* Check pagination input parameters
*
* @param int $limit
* @param int $page
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function checkPaginationParameters($limit, $page)
{
$this->checkPaginationLimit($limit);
if (!is_int($page) || 1 > $page) {
throw new \InvalidArgumentException(
'Parameter `page` must be an integer and must be greater than 0'
);
}
}
/**
* Check pagination input parameter `limit`
*
* @param int $limit
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function checkPaginationLimit($limit)
{
$allowedLimits = [20, 50, 100];
if (!is_int($limit) || !in_array($limit, $allowedLimits, true)) {
throw new \InvalidArgumentException(
'Parameter `limit` must be an integer and contain one of the values [20, 50, 100]'
);
}
}
/**
* Check input parameter `site`
*
* @param string $site
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function checkSite($site)
{
if (!is_string($site) || '' === $site) {
throw new \InvalidArgumentException(
'Parameter `site` must contains a data'
);
}
}
/**
* Fill params by site value
*

View File

@ -56,4 +56,5 @@ class ApiVersion5 extends AbstractLoader
use V5\Telephony;
use V5\Users;
use V5\IntegrationPayments;
use V5\Loyalty;
}

View File

@ -0,0 +1,399 @@
<?php
namespace RetailCrm\Methods\V5;
use InvalidArgumentException;
use RetailCrm\Response\ApiResponse;
trait Loyalty
{
/**
* Create Account
*
* @param array $loyaltyAccount
* @param string $site
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountCreate(array $loyaltyAccount, $site)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkSite($site);
if (empty($loyaltyAccount['phoneNumber']) && empty($loyaltyAccount['cardNumber'])) {
throw new InvalidArgumentException(
'One of the parameters must be filled: `phoneNumber` or `cardNumber`'
);
}
if (empty($loyaltyAccount['customer'])) {
throw new InvalidArgumentException(
'Parameter `customer` must contains a data'
);
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
'/loyalty/account/create',
'POST',
$this->fillSite(
$site,
['loyaltyAccount' => json_encode($loyaltyAccount)]
)
);
}
/**
* Get Account info
*
* @param int $id
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountGet($id)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id",
'GET'
);
}
/**
* Activate Account
*
* @param int $id
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountActivate($id)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id/activate",
'POST'
);
}
/**
* Write off Bonuses for participation in the loyalty program
*
* @param int $id
* @param array $bonus
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function bonusCharge($id, array $bonus)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
if (empty($bonus['amount'])) {
throw new InvalidArgumentException(
'Parameter `amount` must contains a data or must be greater than 0'
);
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id/bonus/charge",
'POST',
$bonus
);
}
/**
* Accrue Bonuses for participation in the loyalty program
*
* @param int $id
* @param array $bonus
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function bonusCredit($id, array $bonus)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
if (empty($bonus['amount'])) {
throw new InvalidArgumentException(
'Parameter `amount` must contains a data or must be greater than 0'
);
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id/bonus/credit",
'POST',
$bonus
);
}
/**
* Get a Bonus account history to participate in the loyalty program
*
* @param int $id
* @param array $filter
* @param int $limit
* @param int $page
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountBonusOperationsGet($id, array $filter = [], $limit = 20, $page = 1)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
/* @noinspection PhpUndefinedMethodInspection */
$this->checkPaginationParameters($limit, $page);
$parameters = [
'limit' => $limit,
'page' => $page,
];
if (!empty($filter)) {
$parameters['filter'] = $filter;
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id/bonus/operations",
'GET',
$parameters
);
}
/**
* Get detailed information about Bonuses
*
* @param int $id
* @param string $status
* @param array $filter
* @param int $limit
* @param int $page
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountBonusDetailsGet($id, $status, array $filter = [], $limit = 20, $page = 1)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
/* @noinspection PhpUndefinedMethodInspection */
$this->checkPaginationParameters($limit, $page);
$allowedStatuses = [
'waiting_activation',
'burn_soon',
];
if (!in_array($status, $allowedStatuses, true)) {
throw new InvalidArgumentException(
'Parameter `status` must take one of the values [`waiting_activation`,`burn_soon`]'
);
}
$parameters = [
'limit' => $limit,
'page' => $page,
];
if (!empty($filter)) {
$parameters['filter'] = $filter;
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id/bonus/$status/details",
'GET',
$parameters
);
}
/**
* Editing participation in the loyalty program
*
* @param int $id
* @param array $loyaltyAccount
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountEdit($id, array $loyaltyAccount)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
if (empty($loyaltyAccount)) {
throw new InvalidArgumentException(
'Parameter `loyaltyAccount` must contains a data'
);
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/account/$id/edit",
'POST',
['loyaltyAccount' => \json_encode($loyaltyAccount)]
);
}
/**
* Get Accounts info
*
* @param array $filter
* @param int $limit
* @param int $page
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function accountsGet(array $filter = [], $limit = 20, $page = 1)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkPaginationParameters($limit, $page);
$parameters = [
'limit' => $limit,
'page' => $page,
];
if (!empty($filter)) {
$parameters['filter'] = $filter;
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
'/loyalty/accounts',
'GET',
$parameters
);
}
/**
* Get the Bonus account history for all participants in all loyalty programs
*
* @param array $filter
* @param int $limit
* @param string|null $cursor
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function bonusOperationsGet($cursor, array $filter = [], $limit = 20)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkPaginationLimit($limit);
$parameters = [
'limit' => $limit,
];
if (!empty($filter)) {
$parameters['filter'] = $filter;
}
if (!empty($cursor)) {
$parameters['cursor'] = $cursor;
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
'/loyalty/bonus/operations',
'GET',
$parameters
);
}
/**
* Calculate the maximum customer discount
*
* @param string $site
* @param array $order
* @param int|null $bonuses
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function calculate($site, array $order, $bonuses)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkSite($site);
if (empty($order)) {
throw new InvalidArgumentException(
'Parameter `order` must contains a data'
);
}
if (null !== $bonuses && !is_int($bonuses) && !is_float($bonuses)) {
throw new InvalidArgumentException(
'Parameter `bonuses` must be an integer or float'
);
}
$data = $this->fillSite($site, ['order' => \json_encode($order)]);
if (null !== $bonuses) {
$data['bonuses'] = $bonuses;
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
'/loyalty/calculate',
'POST',
$data
);
}
/**
* Get a list of loyalty programs
*
* @param array $filter
* @param int $limit
* @param int $page
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function loyaltiesGet(array $filter = [], $limit = 20, $page = 1)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkPaginationParameters($limit, $page);
$parameters = [
'limit' => $limit,
'page' => $page,
];
if (!empty($filter)) {
$parameters['filter'] = $filter;
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
'/loyalty/loyalties',
'GET',
$parameters
);
}
/**
* Get information about the loyalty program
*
* @param int $id
*
* @return ApiResponse
* @throws InvalidArgumentException
*/
public function loyaltyGet($id)
{
/* @noinspection PhpUndefinedMethodInspection */
$this->checkId($id);
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/loyalty/loyalties/$id",
'GET'
);
}
}

View File

@ -0,0 +1,723 @@
<?php
namespace RetailCrm\Tests\Methods\Version5;
use RetailCrm\Test\TestCase;
class ApiClientLoyaltyTest extends TestCase
{
/**
* @dataProvider accountCreateProvider
*
* @group loyalty_v5
*
* @param array $account
* @param string $site
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountCreate(array $account, $site, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountCreate($account, $site);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountCreateProvider()
{
return [
'success' => [
[
'phoneNumber' => 111111111,
'customer' => ['id' => 6218],
],
'gray_sale_3',
null,
],
'error_data' => [
[
'customer' => ['id' => 6218],
],
'gray_sale_3',
'InvalidArgumentException',
],
'error_site' => [
[
'phoneNumber' => 111111111,
'customer' => ['id' => 6218],
],
'',
'InvalidArgumentException',
],
];
}
/**
* @dataProvider accountGetProvider
*
* @group loyalty_v5
*
* @param int $id
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountGet($id, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountGet($id);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountGetProvider()
{
return [
'success' => [
246,
null,
],
'error' => [
null,
'InvalidArgumentException'
],
];
}
/**
* @dataProvider accountActivateProvider
*
* @group loyalty_v5
*
* @param int $id
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountActivate($id, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountActivate($id);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountActivateProvider()
{
return [
'success' => [
246,
null,
],
'error' => [
null,
'InvalidArgumentException'
],
];
}
/**
* @dataProvider bonusChargeProvider
*
* @group loyalty_v5
*
* @param int $id
* @param array $bonus
* @param string|null $exceptionClass
*
* @return void
*/
public function testBonusCharge($id, array $bonus, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->bonusCharge($id, $bonus);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function bonusChargeProvider()
{
return [
'success' => [
246,
['amount' => 50],
null,
],
'error_bonus' => [
246,
['comment' => 'test'],
'InvalidArgumentException'
],
'error_id' => [
null,
['amount' => 50],
'InvalidArgumentException'
],
];
}
/**
* @dataProvider bonusCreditProvider
*
* @group loyalty_v5
*
* @param int $id
* @param array $bonus
* @param string|null $exceptionClass
*
* @return void
*/
public function testBonusCredit($id, array $bonus, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->bonusCredit($id, $bonus);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function bonusCreditProvider()
{
return [
'success' => [
246,
['amount' => 50],
null,
],
'error_bonus' => [
246,
['comment' => 'test'],
'InvalidArgumentException'
],
'error_id' => [
null,
['amount' => 50],
'InvalidArgumentException'
],
];
}
/**
* @dataProvider accountBonusOperationsGetProvider
*
* @group loyalty_v5
*
* @param int $id
* @param array $filter
* @param int $limit
* @param int $page
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountBonusOperationsGet($id, array $filter, $limit, $page, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountBonusOperationsGet($id, $filter, $limit, $page);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountBonusOperationsGetProvider()
{
return [
'success_1' => [
246,
['createdAtFrom' => '2023-05-02 17:29:03'],
100,
1,
null,
],
'success_2' => [
246,
[],
50,
1,
null,
],
'error_limit' => [
246,
[],
35,
2,
'InvalidArgumentException',
],
'error_page' => [
246,
[],
50,
0,
'InvalidArgumentException',
],
'error_id' => [
null,
[],
50,
0,
'InvalidArgumentException',
],
];
}
/**
* @dataProvider accountBonusDetailsGetProvider
*
* @group loyalty_v5
*
* @param int $id
* @param string $status
* @param array $filter
* @param int $limit
* @param int $page
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountBonusDetailsGet($id, $status, array $filter, $limit, $page, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountBonusDetailsGet($id, $status, $filter, $limit, $page);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountBonusDetailsGetProvider()
{
return [
'success_1' => [
246,
'waiting_activation',
['date' => '2023-11-11 18:29:03'],
100,
1,
null,
],
'success_2' => [
246,
'burn_soon',
[],
50,
1,
null,
],
'error_status' => [
246,
'something',
[],
100,
1,
'InvalidArgumentException',
],
'error_limit' => [
246,
'waiting_activation',
[],
35,
2,
'InvalidArgumentException',
],
'error_page' => [
246,
'waiting_activation',
[],
50,
0,
'InvalidArgumentException',
],
'error_id' => [
null,
'waiting_activation',
[],
50,
0,
'InvalidArgumentException',
],
];
}
/**
* @dataProvider accountEditProvider
*
* @group loyalty_v5
*
* @param int $id
* @param array $account
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountEdit($id, array $account, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountEdit($id, $account);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountEditProvider()
{
return [
'success' => [
246,
['cardNumber' => 'xxx-001'],
null,
],
'error_data' => [
246,
[],
'InvalidArgumentException',
],
'error_id' => [
null,
['cardNumber' => 'xxx-001'],
'InvalidArgumentException',
],
];
}
/**
* @dataProvider accountsGetProvider
*
* @group loyalty_v5
*
* @param array $filter
* @param int $limit
* @param int $page
* @param string|null $exceptionClass
*
* @return void
*/
public function testAccountsGet(array $filter, $limit, $page, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->accountsGet($filter, $limit, $page);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function accountsGetProvider()
{
return [
'success_1' => [
['sites' => ['gray_sale_3', 'lp-prerelize-demo']],
100,
1,
null,
],
'success_2' => [
[],
100,
1,
null,
],
'error_limit' => [
[],
11,
1,
'InvalidArgumentException',
],
'error_page' => [
[],
50,
0,
'InvalidArgumentException',
],
];
}
/**
* @dataProvider bonusOperationsGetProvider
*
* @group loyalty_v5
*
* @param string|null $cursor
* @param array $filter
* @param int $limit
* @param string|null $exceptionClass
*
* @return void
*/
public function testBonusOperationsGet($cursor, array $filter, $limit, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->bonusOperationsGet($cursor, $filter, $limit);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function bonusOperationsGetProvider()
{
return [
'success' => [
null,
['loyalties' => [44, 6]],
20,
null,
],
'error_limit' => [
null,
[],
11,
'InvalidArgumentException',
],
];
}
/**
* @dataProvider calculateProvider
*
* @group loyalty_v5
*
* @param string|null $site
* @param array $order
* @param int $bonuses
* @param string|null $exceptionClass
*
* @return void
*/
public function testCalculate($site, array $order, $bonuses, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->calculate($site, $order, $bonuses);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function calculateProvider()
{
return [
'success' => [
'gray_sale_3',
self::getOrderForCalculate(),
0,
null,
],
'error_bonuses' => [
'gray_sale_3',
self::getOrderForCalculate(),
'test',
'InvalidArgumentException',
],
'error_site' => [
null,
self::getOrderForCalculate(),
0,
'InvalidArgumentException',
],
];
}
/**
* @return array
*/
private static function getOrderForCalculate()
{
return [
"customer" => [
"id" => 6260,
],
"privilegeType" => "loyalty_level",
"discountManualPercent" => 10,
"applyRound" => true,
"items" => [
[
"initialPrice" => 1000,
"discountManualPercent" => 10,
"quantity" => 1,
"offer" => [
"id" => 2539665,
"externalId" => "9339",
"xmlId" => "9339",
],
],
],
];
}
/**
* @dataProvider loyaltiesGetProvider
*
* @group loyalty_v5
*
* @param array $filter
* @param int $limit
* @param int $page
* @param string|null $exceptionClass
*
* @return void
*/
public function testLoyaltiesGet(array $filter, $limit, $page, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->loyaltiesGet($filter, $limit, $page);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function loyaltiesGetProvider()
{
return [
'success_1' => [
['active' => true],
20,
1,
null,
],
'success_2' => [
[],
20,
1,
null,
],
'error_limit' => [
['active' => true],
11,
1,
'InvalidArgumentException',
],
'error_page' => [
['active' => true],
11,
0,
'InvalidArgumentException',
],
];
}
/**
* @dataProvider loyaltyGetProvider
*
* @group loyalty_v5
*
* @param int|null $id
* @param string|null $exceptionClass
*
* @return void
*/
public function testLoyaltyGet($id, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->loyaltyGet($id);
if (empty($exceptionClass)) {
static::assertTrue(in_array($response->getStatusCode(), [200, 201]));
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function loyaltyGetProvider()
{
return [
'success' => [
44,
null,
],
'error_id' => [
null,
'InvalidArgumentException',
],
];
}
}

View File

@ -65,7 +65,7 @@ class ApiClientNotificationsTest extends TestCase
],
'error_users_filled' => [
static function () {
return self::getErrorNotification();;
return self::getErrorNotification();
},
'InvalidArgumentException',
],