Skip to content

Commit

Permalink
Cached access token for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Wojciechowski committed Mar 19, 2024
1 parent ca48572 commit 46315d8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.dev-tools/vendor/
/.dev-tools/.php-cs-fixer.cache
.idea/
26 changes: 24 additions & 2 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tpay\Magento2\Model\ApiFacade;

use Magento\Framework\App\CacheInterface;
use Magento\Payment\Model\InfoInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Model\ApiFacade\Transaction\Dto\Channel;
Expand All @@ -12,11 +13,22 @@ class OpenApi
/** @var TpayApi */
private $tpayApi;

public function __construct(TpayConfigInterface $tpay)
private $cache;

const AUTH_TOKEN_CACHE_KEY = 'tpay_auth_token_%s';

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
{
$this->cache = $cache;
$this->tpayApi = new TpayApi($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode());
$this->tpayApi->authorization();
$token = $this->cache->load($this->getAuthTokenCacheKey($tpay));
if ($token) {
$this->tpayApi->setCustomToken(unserialize($token));
}
$this->tpayApi->authorization()->setClientName($tpay->buildMagentoInfo());
if(!$token){
$this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay));
}
}

public function create(array $data): array
Expand Down Expand Up @@ -199,4 +211,14 @@ private function waitForBlikAccept(array $result): array

return $result;
}

private function getAuthTokenCacheKey(TpayConfigInterface $tpay)
{
return sprintf(
self::AUTH_TOKEN_CACHE_KEY,
md5(
join('|', [$tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode()])
)
);
}
}
8 changes: 6 additions & 2 deletions Model/ApiFacade/Refund/RefundApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tpay\Magento2\Model\ApiFacade\Refund;

use Exception;
use Magento\Framework\App\CacheInterface;
use Magento\Payment\Model\InfoInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
Expand All @@ -22,9 +23,12 @@ class RefundApiFacade
/** @var bool */
private $useOpenApi;

public function __construct(TpayConfigInterface $tpay)
private $cache;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
{
$this->tpay = $tpay;
$this->cache = $cache;
}

public function makeRefund(InfoInterface $payment, float $amount)
Expand Down Expand Up @@ -66,7 +70,7 @@ private function createRefundOriginApiInstance(TpayConfigInterface $tpay)
private function createOpenApiInstance(TpayConfigInterface $tpay)
{
try {
$this->openApi = new OpenApi($tpay);
$this->openApi = new OpenApi($tpay, $this->cache);
$this->useOpenApi = true;
} catch (Exception $exception) {
$this->openApi = null;
Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function createOpenApiInstance(TpayConfigInterface $tpay)
}

try {
$this->openApi = new OpenApi($tpay);
$this->openApi = new OpenApi($tpay, $this->cache);
$this->useOpenApi = true;
} catch (Exception $exception) {
$this->openApi = null;
Expand Down
4 changes: 3 additions & 1 deletion Model/TpayPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Magento\Checkout\Model\Session;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use Magento\Framework\Event\ManagerInterface;
Expand Down Expand Up @@ -102,6 +103,7 @@ public function __construct(
string $code,
string $formBlockType,
string $infoBlockType,
CacheInterface $cache,
?CommandPoolInterface $commandPool = null,
?ValidatorPoolInterface $validatorPool = null,
?CommandManagerInterface $commandExecutor = null,
Expand Down Expand Up @@ -257,7 +259,7 @@ public function assignData(DataObject $data)
*/
public function refund(InfoInterface $payment, $amount)
{
$refundService = new RefundApiFacade($this->configurationProvider);
$refundService = new RefundApiFacade($this->configurationProvider, $this->cache);

$refundResult = $refundService->makeRefund($payment, (float) $amount);
try {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": ">=7.1",
"ext-json": "*",
"magento/framework": "^102.0 || ^103.0",
"tpay-com/tpay-openapi-php": "^1.6.6",
"tpay-com/tpay-openapi-php": "^1.7.1",
"tpay-com/tpay-php": "^2.4.3"
},
"autoload": {
Expand Down

0 comments on commit 46315d8

Please sign in to comment.