Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transaction find refund endpoint #25

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Api/Requests/TransactionRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Przelewy24\Api\Requests\Items\Shipping;
use Przelewy24\Api\Responses\Transaction\FindTransactionResponse;
use Przelewy24\Api\Responses\Transaction\RegisterTransactionResponse;
use Przelewy24\Api\Responses\Transaction\TransactionFindRefundResponse;
use Przelewy24\Api\Responses\Transaction\TransactionRefundResponse;
use Przelewy24\Api\Responses\Transaction\VerifyTransactionResponse;
use Przelewy24\Enums\Country;
Expand Down Expand Up @@ -175,4 +176,15 @@ public function refund(
throw Przelewy24Exception::fromBadResponseException($exception);
}
}

public function findRefund(int $orderId): TransactionFindRefundResponse
{
try {
$response = $this->client()->get("api/v1/refund/by/orderId/{$orderId}");

return TransactionFindRefundResponse::fromResponse($response);
} catch (BadResponseException $exception) {
throw Przelewy24Exception::fromBadResponseException($exception);
}
}
}
53 changes: 53 additions & 0 deletions src/Api/Responses/Transaction/TransactionFindRefundItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Przelewy24\Api\Responses\Transaction;

use Przelewy24\Enums\TransactionFindRefundStatus;

class TransactionFindRefundItem
{
public function __construct(
private readonly int $batchId,
private readonly string $requestId,
private readonly string $date,
private readonly string $login,
private readonly string $description,
private readonly int $status,
private readonly int $amount,
) {}

public function batchId(): int
{
return $this->batchId;
}

public function requestId(): string
{
return $this->requestId;
}

public function date(): string
{
return $this->date;
}

public function login(): string
{
return $this->login;
}

public function description(): string
{
return $this->description;
}

public function status(): ?TransactionFindRefundStatus
{
return TransactionFindRefundStatus::tryFrom($this->status);
}

public function amount(): int
{
return $this->amount;
}
}
42 changes: 42 additions & 0 deletions src/Api/Responses/Transaction/TransactionFindRefundResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Przelewy24\Api\Responses\Transaction;

use Przelewy24\Api\Responses\AbstractResponse;
use Przelewy24\Enums\Currency;

class TransactionFindRefundResponse extends AbstractResponse
{
public function orderId(): int
{
return $this->parameters['data']['orderId'];
}

public function sessionId(): string
{
return $this->parameters['data']['sessionId'];
}

public function amount(): int
{
return $this->parameters['data']['amount'];
}

public function currency(): Currency
{
return Currency::from($this->parameters['data']['currency']);
}

public function refunds(): array
{
return array_map(fn (array $data): TransactionFindRefundItem => new TransactionFindRefundItem(
batchId: $data['batchId'],
requestId: $data['requestId'],
date: $data['date'],
login: $data['login'],
description: $data['description'],
status: $data['status'],
amount: $data['amount'],
), $this->parameters['data']['refunds']);
}
}
11 changes: 11 additions & 0 deletions src/Enums/TransactionFindRefundStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Przelewy24\Enums;

enum TransactionFindRefundStatus: int
{
case COMPLETED = 1;
case PENDING = 2;
case AWAITING_ACCEPTANCE = 3;
case REJECTED = 4;
}
31 changes: 31 additions & 0 deletions tests/Api/Responses/Transaction/TransactionFindRefundItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Przelewy24\Tests\Api\Responses\Transaction;

use PHPUnit\Framework\TestCase;
use Przelewy24\Api\Responses\Transaction\TransactionFindRefundItem;
use Przelewy24\Enums\TransactionFindRefundStatus;

class TransactionFindRefundItemTest extends TestCase
{
public function testGetters(): void
{
$response = new TransactionFindRefundItem(
batchId: 999999999,
requestId: 'ae0b12',
date: '2024-07-17 08:25:43',
login: 'login',
description: 'return',
status: 1,
amount: 1500,
);

$this->assertSame(999999999, $response->batchId());
$this->assertSame('ae0b12', $response->requestId());
$this->assertSame('2024-07-17 08:25:43', $response->date());
$this->assertSame('login', $response->login());
$this->assertSame('return', $response->description());
$this->assertSame(TransactionFindRefundStatus::COMPLETED, $response->status());
$this->assertSame(1500, $response->amount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Przelewy24\Tests\Api\Responses\Transaction;

use PHPUnit\Framework\TestCase;
use Przelewy24\Api\Responses\Transaction\TransactionFindRefundItem;
use Przelewy24\Api\Responses\Transaction\TransactionFindRefundResponse;
use Przelewy24\Enums\Currency;
use Przelewy24\Enums\TransactionFindRefundStatus;

class TransactionFindRefundResponseTest extends TestCase
{
public function testGetters(): void
{
$response = new TransactionFindRefundResponse([
'data' => [
'orderId' => 123456789,
'sessionId' => '0beec7',
'amount' => 1500,
'currency' => 'PLN',
'refunds' => [
[
'batchId' => 999999999,
'requestId' => 'ae0b12',
'date' => '2024-07-17 08:25:43',
'login' => 'login',
'description' => 'return',
'status' => 1,
'amount' => 1500,
],
],
],
'responseCode' => 0,
]);

$this->assertSame(123456789, $response->orderId());
$this->assertSame('0beec7', $response->sessionId());
$this->assertSame(1500, $response->amount());
$this->assertSame(Currency::PLN, $response->currency());

$refunds = $response->refunds();

$this->assertIsArray($refunds);
$this->assertCount(1, $refunds);
$this->assertInstanceOf(TransactionFindRefundItem::class, $refunds[0]);
$this->assertSame(999999999, $refunds[0]->batchId());
$this->assertSame('ae0b12', $refunds[0]->requestId());
$this->assertSame('2024-07-17 08:25:43', $refunds[0]->date());
$this->assertSame('login', $refunds[0]->login());
$this->assertSame('return', $refunds[0]->description());
$this->assertSame(TransactionFindRefundStatus::COMPLETED, $refunds[0]->status());
$this->assertSame(1500, $refunds[0]->amount());
}
}
Loading