Skip to content

Commit

Permalink
Resolves #206: /api/v5/loyalty/account/{id}/bonus/charge method support
Browse files Browse the repository at this point in the history
  • Loading branch information
Neur0toxine authored Sep 4, 2024
2 parents 3a3d00a + 7b393e9 commit 4880ed9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Model/Request/Loyalty/LoyaltyBonusChargeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* PHP version 7.3
*
* @category LoyaltyBonusChargeRequest
* @package RetailCrm\Api\Model\Request\Loyalty
*/

namespace RetailCrm\Api\Model\Request\Loyalty;

use DateTime;
use RetailCrm\Api\Component\FormData\Mapping as Form;
use RetailCrm\Api\Interfaces\RequestInterface;

/**
* Class LoyaltyBonusChargeRequest
*
* @category LoyaltyBonusChargeRequest
* @package RetailCrm\Api\Model\Request\Loyalty
*/
class LoyaltyBonusChargeRequest implements RequestInterface
{
/**
* @var float
*
* @Form\Type("float")
* @Form\SerializedName("amount")
*/
public $amount;

/**
* @var string
*
* @Form\Type("string")
* @Form\SerializedName("comment")
*/
public $comment;
}
62 changes: 62 additions & 0 deletions src/ResourceGroup/Loyalty.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyAccountCreateRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyAccountEditRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyAccountsRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusChargeRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusCreditRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusOperationsRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyCalculateRequest;
Expand All @@ -30,6 +31,7 @@
use RetailCrm\Api\Model\Response\Loyalty\LoyaltyBonusOperationsResponse;
use RetailCrm\Api\Model\Response\Loyalty\LoyaltyCalculateResponse;
use RetailCrm\Api\Model\Response\Loyalty\LoyaltyResponse;
use RetailCrm\Api\Model\Response\SuccessResponse;

/**
* Class Loyalty
Expand Down Expand Up @@ -163,6 +165,66 @@ public function accountActivate(int $id): LoyaltyAccountActivateResponse
return $response;
}

/**
* Makes POST "/api/v5/loyalty/account/{id}/bonus/charge" request.
*
* Example:
* ```php
* use RetailCrm\Api\Factory\SimpleClientFactory;
* use RetailCrm\Api\Interfaces\ApiExceptionInterface;
* use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusChargeRequest;
*
* $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
*
* $request = new LoyaltyBonusChargeRequest();
* $request->amount = 100;
* $request->comment = 'Payment for the goods.';
*
* try {
* $response = $client->loyalty->accountBonusCharge(159, $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;
* }
*
* echo 'Status: ' . var_export($response->success, true);
* ```
*
* @param int $id
* @param \RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusChargeRequest $request
*
* @return \RetailCrm\Api\Model\Response\Loyalty\LoyaltyBonusCreditResponse
* @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
* @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface
* @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface
*/
public function accountBonusCharge(int $id, LoyaltyBonusChargeRequest $request): SuccessResponse
{
/** @var LoyaltyBonusCreditResponse $response */
$response = $this->sendRequest(
RequestMethod::POST,
'loyalty/account/' . $id . '/bonus/charge',
$request,
SuccessResponse::class
);
return $response;
}

/**
* Makes POST "/api/v5/loyalty/account/{id}/bonus/credit" request.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/src/ResourceGroup/LoyaltyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyAccountCreateRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyAccountEditRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyAccountsRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusChargeRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusCreditRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyBonusOperationsRequest;
use RetailCrm\Api\Model\Request\Loyalty\LoyaltyCalculateRequest;
Expand Down Expand Up @@ -121,6 +122,29 @@ public function testAccountActivate(): void
self::assertModelEqualsToResponse($json, $response);
}

public function testAccountBonusCharge(): void
{
$json = <<<EOF
{
"success": true
}
EOF;

$request = new LoyaltyBonusChargeRequest();
$request->amount = 100;
$request->comment = 'Monthly membership bonuses.';

$mock = static::createApiMockBuilder('loyalty/account/159/bonus/charge');
$mock->matchMethod(RequestMethod::POST)
->reply(200)
->withBody($json);

$client = TestClientFactory::createClient($mock->getClient());
$response = $client->loyalty->accountBonusCharge(159, $request);

self::assertModelEqualsToResponse($json, $response);
}

public function testAccountBonusCredit(): void
{
$activationDate = new DateTime();
Expand Down

0 comments on commit 4880ed9

Please sign in to comment.