diff --git a/USAGE.md b/USAGE.md index 4f2a29a..f5a580f 100644 --- a/USAGE.md +++ b/USAGE.md @@ -328,6 +328,22 @@ $refundRequest->getCheckoutData()->refundByMerchantItemId($merchantItemId , -2); $refundRequest->getCheckoutData()->refundByMerchantItemId($merchantItemId, -1); ``` +### List transactions +Get a list of all transactions for your API Key. +It is possible to paginate this request using the 'limit' option and the Pager object. +Find all allowed options and more information at our [Documentation Center](https://docs.multisafepay.com/reference/listtransactions/). +```php +$yourApiKey = 'your-api-key'; +$isProduction = false; +$multiSafepaySdk = new \MultiSafepay\Sdk($yourApiKey, $isProduction); + +$options = []; // For all allowed options please check the link to our Documentation Center above. +$transactionListing = $multiSafepaySdk->getTransactionManager()->getTransactions($options); +$transactions = $transactionListing->getTransactions(); + +$pager = $transactionListing->getPager(); // If pagination is needed, this Pager object can be used +``` + ## Tokenization Before working on Tokenization be sure to check out our [Documentation Center](https://docs.multisafepay.com/tools/tokenization/tokenization-api-level/) and that Tokenization is activated on your MultiSafepay account. diff --git a/src/Api/Base/Response.php b/src/Api/Base/Response.php index 7223687..c57c8fa 100644 --- a/src/Api/Base/Response.php +++ b/src/Api/Base/Response.php @@ -6,6 +6,7 @@ namespace MultiSafepay\Api\Base; +use MultiSafepay\Api\Pager\Pager; use MultiSafepay\Exception\ApiException; /** @@ -27,6 +28,11 @@ class Response */ private $raw; + /** + * @var Pager + */ + private $pager; + /** * @param string $json * @param array $context @@ -61,6 +67,10 @@ public function __construct(array $data, array $context = [], string $raw = '') $this->validate($data, $context); $this->data = $data['data']; $this->raw = $raw; + + if (isset($data['pager'])) { + $this->pager = new Pager($data['pager']); + } } /** @@ -103,4 +113,12 @@ public function getRawData(): string { return $this->raw; } + + /** + * @return Pager + */ + public function getPager(): Pager + { + return $this->pager; + } } diff --git a/src/Api/Pager/Cursor.php b/src/Api/Pager/Cursor.php new file mode 100644 index 0000000..8510acd --- /dev/null +++ b/src/Api/Pager/Cursor.php @@ -0,0 +1,66 @@ +after = $data[self::AFTER_NAME] ?? null; + $this->before = $data[self::BEFORE_NAME] ?? null; + } + + /** + * @return string[] + */ + public function getData(): array + { + return [ + self::AFTER_NAME => $this->getAfter(), + self::BEFORE_NAME => $this->getBefore(), + ]; + } + + /** + * @return string|null + */ + public function getAfter() + { + return $this->after; + } + + /** + * @return string|null + */ + public function getBefore() + { + return $this->before; + } +} diff --git a/src/Api/Pager/Pager.php b/src/Api/Pager/Pager.php new file mode 100644 index 0000000..ebb0f6b --- /dev/null +++ b/src/Api/Pager/Pager.php @@ -0,0 +1,98 @@ +after = $data[self::AFTER_NAME] ?? null; + $this->before = $data[self::BEFORE_NAME] ?? null; + $this->limit = $data[self::LIMIT_NAME] ?? null; + $this->cursor = new Cursor($data[self::CURSOR_NAME] ?? []); + } + + /** + * @return string[] + */ + public function getData(): array + { + return [ + self::AFTER_NAME => $this->getAfter(), + self::BEFORE_NAME => $this->getBefore(), + self::LIMIT_NAME => $this->getLimit(), + self::CURSOR_NAME => $this->getCursor()->getData(), + ]; + } + + /** + * @return string|null + */ + public function getAfter() + { + return $this->after; + } + + /** + * @return string|null + */ + public function getBefore() + { + return $this->before; + } + + /** + * @return int|null + */ + public function getLimit() + { + return $this->limit; + } + + /** + * @return Cursor + */ + public function getCursor(): Cursor + { + return $this->cursor; + } +} diff --git a/src/Api/TransactionManager.php b/src/Api/TransactionManager.php index 08ae3e0..328dd71 100644 --- a/src/Api/TransactionManager.php +++ b/src/Api/TransactionManager.php @@ -7,8 +7,10 @@ namespace MultiSafepay\Api; use MultiSafepay\Api\Base\Response; +use MultiSafepay\Api\Pager\Pager; use MultiSafepay\Api\Transactions\OrderRequestInterface; use MultiSafepay\Api\Transactions\RefundRequest; +use MultiSafepay\Api\Transactions\TransactionListing; use MultiSafepay\Api\Transactions\TransactionResponse as Transaction; use MultiSafepay\Api\Transactions\RefundRequest\Arguments\CheckoutData; use MultiSafepay\Api\Transactions\UpdateRequest; @@ -23,6 +25,22 @@ */ class TransactionManager extends AbstractManager { + private const ALLOWED_OPTIONS = [ + 'site_id' => '', + 'financial_status' => '', + 'status' => '', + 'payment_method' => '', + 'type' => '', + 'created_until' => '', + 'created_from' => '', + 'completed_until' => '', + 'completed_from' => '', + 'debit_credit' => '', + 'after' => '', + 'before' => '', + 'limit' => '' + ]; + /** * @param OrderRequestInterface $requestOrder * @return Transaction @@ -52,6 +70,18 @@ public function get(string $orderId): Transaction return new Transaction($response->getResponseData()); } + /** + * @return TransactionListing + * @throws ClientExceptionInterface + */ + public function getTransactions(array $options = []): TransactionListing + { + $options = array_intersect_key($options, self::ALLOWED_OPTIONS); + + $response = $this->client->createGetRequest('json/transactions', $options); + return new TransactionListing($response->getResponseData(), $response->getPager()); + } + /** * @param string $orderId * @param UpdateRequest $updateRequest diff --git a/src/Api/Transactions/TransactionListing.php b/src/Api/Transactions/TransactionListing.php new file mode 100644 index 0000000..710b54f --- /dev/null +++ b/src/Api/Transactions/TransactionListing.php @@ -0,0 +1,54 @@ +transactions = $transactions; + + if (isset($pager)) { + $this->pager = $pager; + } + } + + /** + * @return TransactionResponse[] + */ + public function getTransactions(): array + { + return $this->transactions; + } + + /** + * @return Pager|null + */ + public function getPager(): ?Pager + { + return $this->pager ?? null; + } +} diff --git a/tests/Unit/Api/Pager/CursorTest.php b/tests/Unit/Api/Pager/CursorTest.php new file mode 100644 index 0000000..23ad6e4 --- /dev/null +++ b/tests/Unit/Api/Pager/CursorTest.php @@ -0,0 +1,31 @@ + '12345', 'before' => '54321']); + $this->assertEquals('12345', $testCursor->getAfter()); + } + + /** + * Test whether the before string could be fetched + */ + public function testGetBefore(): void + { + $testCursor = new Cursor(['after' => '12345', 'before' => '54321']); + $this->assertEquals('54321', $testCursor->getBefore()); + } +} diff --git a/tests/Unit/Api/Pager/PagerTest.php b/tests/Unit/Api/Pager/PagerTest.php new file mode 100644 index 0000000..c109a91 --- /dev/null +++ b/tests/Unit/Api/Pager/PagerTest.php @@ -0,0 +1,72 @@ + 'https://example.com/12345', + 'before' => 'https://example.com/54321', + 'limit' => 100, + 'cursor' => [ + 'after' => '12345', + 'before' => '54321', + ], + ]; + + /** + * Test whether the after string could be fetched + */ + public function testGetAfter(): void + { + $testPager = new Pager($this->testData); + $this->assertEquals('https://example.com/12345', $testPager->getAfter()); + } + + /** + * Test whether the before string could be fetched + */ + public function testGetBefore(): void + { + $testPager = new Pager($this->testData); + $this->assertEquals('https://example.com/54321', $testPager->getBefore()); + } + + /** + * Test whether the limit could be fetched + */ + public function testGetLimit(): void + { + $testPager = new Pager($this->testData); + $this->assertEquals(100, $testPager->getLimit()); + } + + /** + * Test whether the cursor is actually a Cursor object + */ + public function testGetCursor(): void + { + $testPager = new Pager($this->testData); + $this->assertInstanceOf(Cursor::class, $testPager->getCursor()); + } + + /** + * Test missing data when initializing a new Pager object + */ + public function testMissingData(): void + { + $pager = new Pager([]); + $this->assertEquals(null, $pager->getLimit()); + $this->assertEquals(null, $pager->getBefore()); + $this->assertEquals(null, $pager->getAfter()); + } +} diff --git a/tests/Unit/Api/Transactions/TransactionListingTest.php b/tests/Unit/Api/Transactions/TransactionListingTest.php new file mode 100644 index 0000000..eab50f8 --- /dev/null +++ b/tests/Unit/Api/Transactions/TransactionListingTest.php @@ -0,0 +1,27 @@ + '999']]); + $transactions = $transactionListing->getTransactions(); + $this->assertCount(1, $transactions); + + $transaction = array_shift($transactions); + $this->assertEquals('999', $transaction->getTransactionId()); + } +}