Skip to content

Commit

Permalink
PHPSDK-94: Add support to list transactions (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
J0risS authored Jul 12, 2022
1 parent 41791fb commit 1ae502f
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 0 deletions.
16 changes: 16 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
18 changes: 18 additions & 0 deletions src/Api/Base/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace MultiSafepay\Api\Base;

use MultiSafepay\Api\Pager\Pager;
use MultiSafepay\Exception\ApiException;

/**
Expand All @@ -27,6 +28,11 @@ class Response
*/
private $raw;

/**
* @var Pager
*/
private $pager;

/**
* @param string $json
* @param array $context
Expand Down Expand Up @@ -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']);
}
}

/**
Expand Down Expand Up @@ -103,4 +113,12 @@ public function getRawData(): string
{
return $this->raw;
}

/**
* @return Pager
*/
public function getPager(): Pager
{
return $this->pager;
}
}
66 changes: 66 additions & 0 deletions src/Api/Pager/Cursor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);
/**
* Copyright © 2022 MultiSafepay, Inc. All rights reserved.
* See DISCLAIMER.md for disclaimer details.
*/

namespace MultiSafepay\Api\Pager;

/**
* Class Cursor
*
* @package MultiSafepay\Api\Pager
*/
class Cursor
{
public const AFTER_NAME = 'after';
public const BEFORE_NAME = 'before';

/**
* @var string|null
*/
private $after;

/**
* @var string|null
*/
private $before;

/**
* Pager constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->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;
}
}
98 changes: 98 additions & 0 deletions src/Api/Pager/Pager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php declare(strict_types=1);
/**
* Copyright © 2022 MultiSafepay, Inc. All rights reserved.
* See DISCLAIMER.md for disclaimer details.
*/

namespace MultiSafepay\Api\Pager;

/**
* Class Pager
*
* @package MultiSafepay\Api\Pager
*/
class Pager
{
public const AFTER_NAME = 'after';
public const BEFORE_NAME = 'before';
public const LIMIT_NAME = 'limit';
public const CURSOR_NAME = 'cursor';

/**
* @var string|null
*/
private $after;

/**
* @var string|null
*/
private $before;

/**
* @var int|null
*/
private $limit;

/**
* @var Cursor
*/
private $cursor;

/**
* Pager constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->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;
}
}
30 changes: 30 additions & 0 deletions src/Api/TransactionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions src/Api/Transactions/TransactionListing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/**
* Copyright © 2019 MultiSafepay, Inc. All rights reserved.
* See DISCLAIMER.md for disclaimer details.
*/

namespace MultiSafepay\Api\Transactions;

use MultiSafepay\Api\Pager\Pager;

class TransactionListing
{
/** @var array */
private $transactions;

/** @var Pager */
private $pager;

/**
* TransactionListing constructor.
* @param array $data
* @param Pager|null $pager
*/
public function __construct(array $data, Pager $pager = null)
{
$transactions = [];
if (!empty($data)) {
foreach ($data as $transactionData) {
$transactions[] = new TransactionResponse($transactionData, json_encode($transactionData));
}
}
$this->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;
}
}
31 changes: 31 additions & 0 deletions tests/Unit/Api/Pager/CursorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/**
* Copyright © 2022 MultiSafepay, Inc. All rights reserved.
* See DISCLAIMER.md for disclaimer details.
*/

namespace MultiSafepay\Tests\Unit\Api\Pager;

use MultiSafepay\Api\Pager\Cursor;
use PHPUnit\Framework\TestCase;

class CursorTest extends TestCase
{
/**
* Test whether the after string could be fetched
*/
public function testGetAfter(): void
{
$testCursor = new Cursor(['after' => '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());
}
}
Loading

0 comments on commit 1ae502f

Please sign in to comment.