Skip to content

Commit

Permalink
Implement payment refund
Browse files Browse the repository at this point in the history
  • Loading branch information
odolbeau committed Apr 11, 2024
1 parent bedd466 commit ab617e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Http/ApiCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ public function __construct(
/**
* @template T of HelloassoObject
*
* @param class-string<T> $responseClassType
* @param class-string<T> $responseClassType
* @param array<string, mixed> $options HttpClient request options
*
* @return T
*/
public function post(string $url, array|HelloassoObject|null $body, string $responseClassType): HelloassoObject
public function post(string $url, array|HelloassoObject|null $body, string $responseClassType, ?array $options = []): HelloassoObject
{
$response = $this->httpClient->request(Request::METHOD_POST, $url, [
$response = $this->httpClient->request(Request::METHOD_POST, $url, array_merge([
'auth_bearer' => $this->tokenManager->getAccessToken(),
'body' => $this->serializer->serialize($body, 'json'),
]);
], $options));

return $this->responseHandler->deserializeResponse($response, $responseClassType);
}
Expand All @@ -43,12 +44,11 @@ public function post(string $url, array|HelloassoObject|null $body, string $resp
*
* @return T
*/
public function get(string $url, string $responseClassType, array|HelloassoObject|null $request = null): HelloassoObject
public function get(string $url, string $responseClassType, ?array $options = []): HelloassoObject
{
$response = $this->httpClient->request(Request::METHOD_GET, $url, [
$response = $this->httpClient->request(Request::METHOD_GET, $url, array_merge([
'auth_bearer' => $this->tokenManager->getAccessToken(),
'body' => $request,
]);
], $options));

return $this->responseHandler->deserializeResponse($response, $responseClassType);
}
Expand Down
14 changes: 13 additions & 1 deletion src/Service/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public function retrieve(int $id): Payment
*/
public function all(array $params = []): PaymentCollection
{
return $this->apiCaller->get("/v5/organizations/{$this->organizationSlug}/payments", PaymentCollection::class, $params);
return $this->apiCaller->get("/v5/organizations/{$this->organizationSlug}/payments", PaymentCollection::class, [
'query' => $params,
]);
}

/**
* @throws HelloassoApiException
*/
public function refund(int $id, array $params = []): Payment
{
return $this->apiCaller->post("/v5/payments/$id/refund", null, Payment::class, [
'query' => $params,
]);
}
}
25 changes: 24 additions & 1 deletion tests/Functional/Service/PaymentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Helloasso\Tests\Functional\Service;

use Helloasso\Enums\PaymentState;
use Helloasso\Models\Statistics\Payment;
use Helloasso\Models\Statistics\PaymentCollection;
use Helloasso\Tests\Functional\FunctionalTestCase;
Expand All @@ -16,13 +17,35 @@ public function testAll(): void
$this->assertInstanceOf(PaymentCollection::class, $paymentCollection);

if ($paymentCollection->isEmpty()) {
$this->markTestSkipped();
$this->markTestSkipped('No payment fund in collection');
}

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

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

$this->markTestSkipped('Refund test is disable for now as it requires special permissions, even on sandbox environment.');

/* @phpstan-ignore-next-line Ignored due do skipped test */
if (null === $refundablePayment = $this->getRefundablePayment($paymentCollection)) {
$this->markTestSkipped('No refundable payment found in collection');
}

$payment = $this->getClient()->payment->refund($refundablePayment->getId(), ['comment' => 'Refunded from functional test']);
$this->assertSame(PaymentState::Refunded, $payment->getState());
}

/* @phpstan-ignore-next-line Ignored due do skipped test */
private function getRefundablePayment(PaymentCollection $paymentCollection): ?Payment
{
foreach ($paymentCollection->getData() as $payment) {
if (PaymentState::Authorized === $payment->getState()) {
return $payment;
}
}

return null;
}
}

0 comments on commit ab617e2

Please sign in to comment.