Skip to content
Merged
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
14 changes: 8 additions & 6 deletions .dev-tools/.php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

require __DIR__.'/vendor/tpay-com/coding-standards/bootstrap.php';

return Tpay\CodingStandards\PhpCsFixerConfigFactory::createWithLegacyRules()
->setFinder(
PhpCsFixer\Finder::create()
->ignoreDotFiles(false)
->in(__DIR__.'/..')
);
$config = Tpay\CodingStandards\PhpCsFixerConfigFactory::createWithLegacyRules();
$config->setFinder(
PhpCsFixer\Finder::create()
->ignoreDotFiles(false)
->in(__DIR__.'/..')
);

return $config;
6 changes: 2 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
fail-fast: false
matrix:
include:
- php-version: '5.6'
- php-version: '7.0'
- php-version: '7.1'
- php-version: '7.2'
- php-version: '7.3'
Expand All @@ -28,7 +26,7 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: none, curl, dom, json, mbstring, tokenizer, xml, xmlwriter
extensions: none, curl, dom, json, mbstring, tokenizer, xml, xmlwriter, fileinfo
coverage: none

- id: composer-cache
Expand All @@ -40,7 +38,7 @@ jobs:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}

- run: composer update --no-progress --classmap-authoritative
- run: composer update --no-progress --no-cache --classmap-authoritative

- run: ./vendor/bin/parallel-lint . --exclude ./vendor

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/create_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: 5.6
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json, fileinfo
coverage: none

- run: composer install --no-dev
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:

- uses: shivammathur/setup-php@v2
with:
php-version: 5.6
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json
php-version: 7.1
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json, fileinfo
coverage: none

- run: composer install --no-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, fileinfo
coverage: none

- run: composer install --no-dev --no-progress --classmap-authoritative
Expand Down
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,8 @@ The [`src/Loader.php`](src/Loader.php) file handles all required class loading,

All methods described in [Tpay OpenAPI documentation](https://openapi.tpay.com) can be easily executed by running one of the library methods like:
```php
$cache = /*Your Cache Implementation. Preferably PSR\Cache implementation, because it will be used in future by this library. */;
$tpayApiKey = $cache->get('example_YOUR_TPAY_API_TOKEN_KEY');

$tpayApi = new TpayApi($clientId, $clientSecret, true, 'read');
//if your cache returned a token, set it to TpayApi
if($tpayApiKey !== null){
$tpayApiKey->setCustomToken(unserialize($tpayApiKey));
} else {
//if not, get new token and cache it for given time
//as you can notice, we slightly shorten token validity time to avoid using almost invalid token
$tpayApi->authorization();
$cache->set('example_YOUR_TPAY_API_TOKEN_KEY', serialize($tpayApi->getToken()), $tpayApiKey->getToken()->expires_in->getValue() - 10);
}
$cache = new Cache(/** here you can inject your PSR6 or PSR16 cache storage object */);
$tpayApi = new TpayApi($cache,'12345-132123', '456');
$transactions = $tpayApi->Transactions->getTransactions();
```
##### Credentials
Expand Down
4 changes: 3 additions & 1 deletion README_PL.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ Plik [`src/Loader.php`](src/Loader.php) obsługuje ładowanie wszystkich wymagan

Wszystkie metody opisane w [dokumentacji Tpay OpenAPI](https://openapi.tpay.com) można łatwo wykonać, uruchamiając jedną z metod z tej biblioteki, takie jak:
```php
$tpayApi = new TpayApi($clientId, $clientSecret, true, 'read');
$cache = new Cache(/** Tutaj powinieneś przekazać w argumencie swoją implementację cache zgodną z PSR6 lub PSR16 */);
$tpayApi = new TpayApi($cache,'12345-132123', '456');
$transactions = $tpayApi->Transactions->getTransactions();
```


Wszystkie obecnie dostępne metody API mają przykładowe użycie w katalogu [`Examples`](examples).

### Przykładowe dane uwierzytelniające
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
"require": {
"php": ">=5.6.0",
"ext-curl": "*",
"ext-fileinfo": "*",
"ext-json": "*",
"phpseclib/phpseclib": "^2 || ^3",
"psr/cache": "^1 || ^2 || ^3",
"psr/log": "^1 || ^2 || ^3"
"psr/log": "^1 || ^2 || ^3",
"psr/simple-cache": "^1 || ^2 || ^3"
},
"require-dev": {
"ext-openssl": "*",
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"phpunit/phpunit": "^5.7.27 || ^9.6.10"
"phpunit/phpunit": "^5.7.27 || ^9.6.10",
"psx/cache": "^v1.0.2"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 1 addition & 5 deletions src/Api/ApiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ class ApiAction

private $clientName;

/**
* @param Token $Token
* @param bool $productionMode
*/
public function __construct($Token, $productionMode)
public function __construct(Token $Token, bool $productionMode)
{
$this->productionMode = $productionMode;
$this->Token = $Token;
Expand Down
85 changes: 12 additions & 73 deletions src/Api/TpayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@

namespace Tpay\OpenApi\Api;

use Psr\Cache\CacheItemPoolInterface;
use RuntimeException;
use Tpay\OpenApi\Api\Accounts\AccountsApi;
use Tpay\OpenApi\Api\Authorization\AuthorizationApi;
use Tpay\OpenApi\Api\Refunds\RefundsApi;
use Tpay\OpenApi\Api\Reports\ReportsApi;
use Tpay\OpenApi\Api\Transactions\TransactionsApi;
use Tpay\OpenApi\Model\Objects\Authorization\Token;
use Tpay\OpenApi\Utilities\Cache;
use Tpay\OpenApi\Utilities\TpayException;

class TpayApi
{
/** @deprecated will be removed in 2.0.0 */
const TPAY_API = [
'Accounts' => AccountsApi::class,
'Authorization' => AuthorizationApi::class,
'Transactions' => TransactionsApi::class,
'Refunds' => RefundsApi::class,
'Reports' => ReportsApi::class,
];

/** @var null|AccountsApi */
private $accounts;

Expand Down Expand Up @@ -59,29 +50,21 @@ class TpayApi
/** @var null|string */
private $clientName;

/** @var CacheItemPoolInterface */
/** @var Cache */
private $cache;

/**
* @param string $clientId
* @param string $clientSecret
* @param bool $productionMode
* @param string $scope
* @param null|string $apiUrlOverride
* @param null|string $clientName
*/
public function __construct(
$clientId,
$clientSecret,
$productionMode = false,
$scope = 'read',
$apiUrlOverride = null,
$clientName = null
Cache $cache,
string $clientId,
string $clientSecret,
bool $productionMode = false,
?string $apiUrlOverride = null,
?string $clientName = null
) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->productionMode = $productionMode;
$this->scope = $scope;
$this->scope = 'read';
$this->apiUrl = true === $this->productionMode
? ApiAction::TPAY_API_URL_PRODUCTION
: ApiAction::TPAY_API_URL_SANDBOX;
Expand All @@ -92,36 +75,7 @@ public function __construct(
}
$this->apiUrl = $apiUrlOverride;
}
}

/**
* @param string $propertyName
*
* @return AccountsApi|AuthorizationApi|RefundsApi|TransactionsApi
*/
public function __get($propertyName)
{
@trigger_error(
sprintf(
'Using property "%s" is deprecated and will be removed in 2.0.0. Call the method "%s()".',
$propertyName,
lcfirst($propertyName)
),
E_USER_DEPRECATED
);

switch ($propertyName) {
case 'Accounts':
return $this->accounts();
case 'Authorization':
return $this->authorization();
case 'Refunds':
return $this->refunds();
case 'Transactions':
return $this->transactions();
}

throw new RuntimeException(sprintf('Property %s::%s does not exist!', __CLASS__, $propertyName));
$this->cache = $cache;
}

/** @param Token $token */
Expand All @@ -130,11 +84,6 @@ public function setCustomToken($token)
$this->token = $token;
}

public function setCache(CacheItemPoolInterface $cacheItemPool)
{
$this->cache = $cacheItemPool;
}

public function getToken()
{
return $this->token;
Expand Down Expand Up @@ -229,12 +178,7 @@ private function authorize()
];
$cacheKey = sha1(json_encode($fields).$this->apiUrl);

if ($this->cache) {
$cacheItem = $this->cache->getItem($cacheKey);
if ($cacheItem->isHit()) {
$this->token = $cacheItem->get();
}
}
$this->token = $this->cache->get($cacheKey);

if (
$this->token instanceof Token
Expand Down Expand Up @@ -263,11 +207,6 @@ private function authorize()

$this->token = new Token();
$this->token->setObjectValues($this->token, $authApi->getRequestResult());
if ($this->cache) {
$item = $this->cache->getItem($cacheKey);
$item->set($this->token);
$item->expiresAfter(7100);
$this->cache->save($item);
}
$this->cache->set($cacheKey, $this->token, 7100);
}
}
15 changes: 10 additions & 5 deletions src/Api/Transactions/TransactionsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ public function createPaymentByTransactionId($fields, $transactionId)
*/
public function createInstantPaymentByTransactionId($fields, $transactionId)
{
return $this->run(static::POST, sprintf('/transactions/%s/pay', $transactionId), $fields, new PayWithInstantRedirection());
return $this->run(
static::POST,
sprintf('/transactions/%s/pay', $transactionId),
$fields,
new PayWithInstantRedirection()
);
}

/**
Expand All @@ -117,13 +122,13 @@ public function createRefundByTransactionId($fields, $transactionId)
return $this->run(static::POST, sprintf('/transactions/%s/refunds', $transactionId), $fields, new Refund());
}

/** @param array $fields */
public function cancelTransaction($fields)
/** @param string $transactionId */
public function cancelTransaction($transactionId)
{
return $this->run(static::POST, '/transactions/%s/pay', $fields);
return $this->run(static::POST, sprintf('/transactions/%s/pay', $transactionId));
}

/** @param array $fields */
/** @param array $fields */
public function initApplePay($fields)
{
return $this->run(static::POST, '/wallet/applepay/init', $fields, new InitApplePay());
Expand Down
62 changes: 62 additions & 0 deletions src/Utilities/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Tpay\OpenApi\Utilities;

use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

class Cache
{
private $cacheItemPool;
private $cache;

public function __construct(?CacheItemPoolInterface $cacheItemPool = null, ?CacheInterface $cache = null)
{
$this->cacheItemPool = $cacheItemPool;
$this->cache = $cache;

if (null === $cacheItemPool && null === $cache) {
throw new TpayException('Both of cache implementation cannot be null!');
}
}

public function set($key, $value, $ttl)
{
if ($this->cache) {
$this->cache->set($key, $value, $ttl);

return;
}

if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem($key);
$item->set($value);
$item->expiresAfter(7100);
$this->cacheItemPool->save($item);
}
}

public function get($key)
{
if ($this->cache) {
return $this->cache->get($key, null);
}

if ($this->cacheItemPool) {
$cacheItem = $this->cacheItemPool->getItem($key);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
}
}

public function delete($key)
{
if ($this->cache) {
$this->cache->delete($key);
}
if ($this->cacheItemPool) {
$this->cacheItemPool->deleteItem($key);
}
}
}
Loading