From b9a1516375d048e384189bcbeebcaba57364e69e Mon Sep 17 00:00:00 2001 From: Mateusz Nastalski Date: Wed, 17 Jul 2024 17:54:34 +0200 Subject: [PATCH] Add find refund --- src/Api/Requests/TransactionRequests.php | 12 +++++ .../Transaction/TransactionFindRefundItem.php | 53 ++++++++++++++++++ .../TransactionFindRefundResponse.php | 42 +++++++++++++++ src/Enums/TransactionFindRefundStatus.php | 11 ++++ .../TransactionFindRefundItemTest.php | 31 +++++++++++ .../TransactionFindRefundResponseTest.php | 54 +++++++++++++++++++ 6 files changed, 203 insertions(+) create mode 100644 src/Api/Responses/Transaction/TransactionFindRefundItem.php create mode 100644 src/Api/Responses/Transaction/TransactionFindRefundResponse.php create mode 100644 src/Enums/TransactionFindRefundStatus.php create mode 100644 tests/Api/Responses/Transaction/TransactionFindRefundItemTest.php create mode 100644 tests/Api/Responses/Transaction/TransactionFindRefundResponseTest.php diff --git a/src/Api/Requests/TransactionRequests.php b/src/Api/Requests/TransactionRequests.php index b1d108c..ef34aed 100644 --- a/src/Api/Requests/TransactionRequests.php +++ b/src/Api/Requests/TransactionRequests.php @@ -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; @@ -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); + } + } } diff --git a/src/Api/Responses/Transaction/TransactionFindRefundItem.php b/src/Api/Responses/Transaction/TransactionFindRefundItem.php new file mode 100644 index 0000000..2973691 --- /dev/null +++ b/src/Api/Responses/Transaction/TransactionFindRefundItem.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/src/Api/Responses/Transaction/TransactionFindRefundResponse.php b/src/Api/Responses/Transaction/TransactionFindRefundResponse.php new file mode 100644 index 0000000..34b4a62 --- /dev/null +++ b/src/Api/Responses/Transaction/TransactionFindRefundResponse.php @@ -0,0 +1,42 @@ +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']); + } +} diff --git a/src/Enums/TransactionFindRefundStatus.php b/src/Enums/TransactionFindRefundStatus.php new file mode 100644 index 0000000..ddf13c7 --- /dev/null +++ b/src/Enums/TransactionFindRefundStatus.php @@ -0,0 +1,11 @@ +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()); + } +} diff --git a/tests/Api/Responses/Transaction/TransactionFindRefundResponseTest.php b/tests/Api/Responses/Transaction/TransactionFindRefundResponseTest.php new file mode 100644 index 0000000..82556e7 --- /dev/null +++ b/tests/Api/Responses/Transaction/TransactionFindRefundResponseTest.php @@ -0,0 +1,54 @@ + [ + '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()); + } +}