Skip to content

Commit

Permalink
Introduce PaymentService
Browse files Browse the repository at this point in the history
  • Loading branch information
odolbeau committed Apr 11, 2024
1 parent 47c847d commit f7a3862
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/HelloassoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
use Helloasso\Models\Statistics\PaymentDetail;
use Helloasso\Service\CheckoutIntentService;
use Helloasso\Service\DirectoryService;
use Helloasso\Service\PaymentService;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

class HelloassoClient
{
public CheckoutIntentService $checkout;
public PaymentService $payment;
public DirectoryService $directory;

public function __construct(
Expand All @@ -27,6 +29,7 @@ public function __construct(
) {
$this->checkout = new CheckoutIntentService($apiCaller, $organizationSlug);
$this->directory = new DirectoryService();
$this->payment = new PaymentService($apiCaller, $organizationSlug);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Http/ApiCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function post(string $url, array|HelloassoObject|null $body, string $resp
*
* @return T
*/
public function get(string $url, string $responseClassType): HelloassoObject
public function get(string $url, string $responseClassType, array|HelloassoObject|null $request = null): HelloassoObject
{
$response = $this->httpClient->request(Request::METHOD_GET, $url, [
'auth_bearer' => $this->tokenManager->getAccessToken(),
'body' => $request,
]);

return $this->responseHandler->deserializeResponse($response, $responseClassType);
Expand Down
55 changes: 55 additions & 0 deletions src/Models/Common/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Helloasso\Models\Common;

use Helloasso\Models\HelloassoObject;

/**
* @template T of HelloassoObject
*/
abstract class Collection implements HelloassoObject
{
/**
* @var T[]
*/
protected array $data = [];

protected PaginationModel $pagination;

public function isEmpty(): bool
{
return 0 === \count($this->data);
}

/**
* @return T[]
*/
public function getData(): array
{
return $this->data;
}

/**
* @param T[] $data
*/
public function setData(array $data): self
{
$this->data = $data;

return $this;
}

public function getPagination(): PaginationModel
{
return $this->pagination;
}

public function setPagination(PaginationModel $pagination): self
{
$this->pagination = $pagination;

return $this;
}
}
21 changes: 21 additions & 0 deletions src/Models/Statistics/PaymentCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Helloasso\Models\Statistics;

use Helloasso\Models\Common\Collection;

/**
* @extends Collection<Payment>
*/
class PaymentCollection extends Collection
{
/**
* Overriding the property with an @var annotation is needed for the
* serializer component until generics are not supported.
*
* @var Payment[]
*/
protected array $data = [];
}
35 changes: 35 additions & 0 deletions src/Service/PaymentService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Helloasso\Service;

use Helloasso\Exception\HelloassoApiException;
use Helloasso\Http\ApiCaller;
use Helloasso\Models\Statistics\Payment;
use Helloasso\Models\Statistics\PaymentCollection;

class PaymentService
{
public function __construct(
private readonly ApiCaller $apiCaller,
private readonly string $organizationSlug,
) {
}

/**
* @throws HelloassoApiException
*/
public function retrieve(int $id): Payment
{
return $this->apiCaller->get("/v5/payments/$id", Payment::class);
}

/**
* @throws HelloassoApiException
*/
public function all(array $params = []): PaymentCollection
{
return $this->apiCaller->get("/v5/organizations/{$this->organizationSlug}/payments", PaymentCollection::class, $params);
}
}
28 changes: 28 additions & 0 deletions tests/Functional/Service/PaymentServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Helloasso\Tests\Functional\Service;

use Helloasso\Models\Statistics\Payment;
use Helloasso\Models\Statistics\PaymentCollection;
use Helloasso\Tests\Functional\FunctionalTestCase;

final class PaymentServiceTest extends FunctionalTestCase
{
public function testAll(): void
{
$paymentCollection = $this->getClient()->payment->all();
$this->assertInstanceOf(PaymentCollection::class, $paymentCollection);

if ($paymentCollection->isEmpty()) {
$this->markTestSkipped();
}

$paymentId = $paymentCollection->getData()[0]->getId();

$payment = $this->getClient()->payment->retrieve($paymentId);
$this->assertInstanceOf(Payment::class, $payment);
$this->assertSame($paymentId, $payment->getId());
}
}

0 comments on commit f7a3862

Please sign in to comment.