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
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Auto detect text files and perform LF normalization
* text=auto
* text eol=lf

# Convert images to binary
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.tiff binary
*.ico binary
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "paynl/php-sdk",
"description": "Software Development Kit for implementing Pay.'s API version 3",
"version": "0.2.0",
"version": "0.2.1",
"type": "library",
"require": {
"php": "^8.1",
Expand Down
5 changes: 3 additions & 2 deletions config/config.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
'username' => '', # Use AT-Code or SL-Code. Use AT-code together with API-Token.
'password' => '', # Use API token or secret. Use Secret in combination with SL-Code.
],
'debug' => false
];
'debug' => false,
'useFileCaching' => true
];
1 change: 0 additions & 1 deletion samples/OrderCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
try {
$request->setReference('SDK0123456789');
$payOrder = $request->start();
echo get_class($payOrder);
} catch (PayException $e) {
echo '<pre>';
echo 'Technical message: ' . $e->getMessage() . PHP_EOL;
Expand Down
15 changes: 7 additions & 8 deletions samples/ServiceGetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
$config->setUsername($_REQUEST['username'] ?? '');
$config->setPassword($_REQUEST['password'] ?? '');


try {
$slCode = $_REQUEST['slcode'] ?? '';
$config = (new ServiceGetConfigRequest($slCode))->setConfig($config)->start();
$serviceConfig = (new ServiceGetConfigRequest($slCode))->setConfig($config)->start();
} catch (PayException $e) {
echo '<pre>';
echo 'Technical message: ' . $e->getMessage() . PHP_EOL;
Expand All @@ -28,18 +27,18 @@

echo '<pre>';

echo $config->getCode() . ' - ' . $config->getName() . PHP_EOL;
echo $serviceConfig->getCode() . ' - ' . $serviceConfig->getName() . PHP_EOL;

$banks = $config->getBanks();
$banks = $serviceConfig->getBanks();
print_r($banks);

$terminals = $config->getTerminals();
$terminals = $serviceConfig->getTerminals();
print_r($terminals);

$tguList = $config->getCores();
$tguList = $serviceConfig->getCores();
print_r($tguList);

$paymentMethods = $config->getPaymentMethods();
$paymentMethods = $serviceConfig->getPaymentMethods();
foreach ($paymentMethods as $method) {
echo $method->getId() . ' - ';
echo $method->getName() . ' - ';
Expand All @@ -51,6 +50,6 @@
echo PHP_EOL;
}

foreach ($config->getCheckoutOptions() as $checkoutOption) {
foreach ($serviceConfig->getCheckoutOptions() as $checkoutOption) {
echo '=> TAG: ' . $checkoutOption->getTag() . PHP_EOL;
}
18 changes: 18 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ public function isEmpty()
return false;
}

/**
* @return boolean
*/
public function isCacheEnabled()
{
return ($this->data['useFileCaching'] ?? 0) == 1;
}

/**
* @return string
*/
Expand All @@ -389,6 +397,16 @@ public function setPassword(string $password): self
return $this;
}

/**
* @param boolean $caching
* @return self
*/
public function setCaching(bool $caching): self
{
$this->data['useFileCaching'] = $caching;
return $this;
}

/**
* @return string
*/
Expand Down
63 changes: 63 additions & 0 deletions src/Helpers/StaticCacheTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace PayNL\Sdk\Helpers;

trait StaticCacheTrait
{
/**
* In-memory static cache array
*/
private static array $cache = [];

/**
* Get value from static cache, or execute callback and cache it.
*
* @param string $key
* @param callable $callback
* @return mixed
* @throws \Exception
*/
protected function staticCache(string $key, callable $callback): mixed
{
if (isset(self::$cache[$key])) {
if (self::$cache[$key] instanceof \Exception) {
throw self::$cache[$key];
}
return self::$cache[$key];
}

try {
return self::$cache[$key] = $callback();
} catch (\Exception $e) {
self::$cache[$key] = $e;
throw $e;
}
}

/**
* @param string $key
* @return boolean
*/
protected function hasStaticCache(string $key): bool
{
return isset(self::$cache[$key]);
}

/**
* @param string $key
* @return mixed
* @throws \Exception
*/
protected function getStaticCacheValue(string $key): mixed
{
if (!isset(self::$cache[$key])) {
return null;
}

if (self::$cache[$key] instanceof \Exception) {
throw self::$cache[$key];
}

return self::$cache[$key];
}
}
32 changes: 26 additions & 6 deletions src/Model/Request/OrderStatusRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PayNL\Sdk\Model\Pay\PayOrder;
use PayNL\Sdk\Request\RequestInterface;
use PayNL\Sdk\Config\Config;
use PayNL\Sdk\Helpers\StaticCacheTrait;
use PayNL\Sdk\Util\PayCache;

/**
* Class OrderStatusRequest
Expand All @@ -18,6 +20,8 @@
*/
class OrderStatusRequest extends RequestData
{
use StaticCacheTrait;

private string $orderId;

/**
Expand All @@ -35,7 +39,7 @@ public function __construct(string $orderId)
public function getPathParameters(): array
{
return [
'transactionId' => $this->orderId
'transactionId' => $this->orderId
];
}

Expand All @@ -53,9 +57,25 @@ public function getBodyParameters(): array
*/
public function start(): PayOrder
{
# Always use TGU-1 for orderStatus
$this->config->setCore(Config::TGU1);

return parent::start();
$cacheKey = 'order_status_' . md5(json_encode([$this->config->getUsername(), $this->orderId]));

if ($this->hasStaticCache($cacheKey)) {
return $this->getStaticCacheValue($cacheKey);
}

if ($this->config->isCacheEnabled()) {
$cache = new PayCache();
return $cache->get($cacheKey, function () use ($cacheKey) {
return $this->staticCache($cacheKey, function () {
$this->config->setCore(Config::TGU1);
return parent::start();
});
}, 3); # 3 seconds file caching
}

return $this->staticCache($cacheKey, function () {
$this->config->setCore(Config::TGU1);
return parent::start();
});
}
}
}
39 changes: 36 additions & 3 deletions src/Model/Request/ServiceGetConfigRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PayNL\Sdk\Request\RequestData;
use PayNL\Sdk\Model\Response\ServiceGetConfigResponse;
use PayNL\Sdk\Request\RequestInterface;
use PayNL\Sdk\Util\PayCache;
use PayNL\Sdk\Helpers\StaticCacheTrait;

/**
* Class ServiceGetConfigRequest
Expand All @@ -18,12 +20,17 @@
*/
class ServiceGetConfigRequest extends RequestData
{
use StaticCacheTrait;

/**
* @var string|mixed
*/
private string $serviceId;

/**
* @param $serviceId
* @param string $serviceId
*/
public function __construct($serviceId = '')
public function __construct(string $serviceId = '')
{
$this->serviceId = $serviceId;
parent::__construct('GetConfig', '/services/config', RequestInterface::METHOD_GET);
Expand Down Expand Up @@ -53,9 +60,35 @@ public function getBodyParameters(): array
* @throws PayException
*/
public function start(): ServiceGetConfigResponse
{
$cacheKey = 'service_getconfig_' . md5(json_encode([$this->config->getUsername(), $this->config->getPassword(), $this->serviceId]));

if ($this->hasStaticCache($cacheKey)) {
return $this->getStaticCacheValue($cacheKey);
}

if ($this->config->isCacheEnabled()) {
$cache = new PayCache();
return $cache->get($cacheKey, function () use ($cacheKey) {
return $this->staticCache($cacheKey, function () {
return $this->startAPI();
});
}, 5);
}

return $this->staticCache($cacheKey, function () {
return $this->startAPI();
});
}

/**
* @return ServiceGetConfigResponse
* @throws PayException
*/
private function startAPI(): ServiceGetConfigResponse
{
$this->config->setCore('https://rest.pay.nl');
$this->config->setVersion(2);
return parent::start();
}
}
}
36 changes: 29 additions & 7 deletions src/Model/Request/TransactionStatusRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace PayNL\Sdk\Model\Request;

use PayNL\Sdk\Config\Config;
use PayNL\Sdk\Exception\PayException;
use PayNL\Sdk\Request\RequestData;
use PayNL\Sdk\Model\Pay\PayOrder;
use PayNL\Sdk\Request\RequestInterface;
use PayNL\Sdk\Helpers\StaticCacheTrait;
use PayNL\Sdk\Util\PayCache;

/**
* Class TransactionStatusRequest
Expand All @@ -17,12 +20,14 @@
*/
class TransactionStatusRequest extends RequestData
{
use StaticCacheTrait;

private string $orderId;

/**
* @param $orderid
* @param string $orderId
*/
public function __construct($orderId)
public function __construct(string $orderId)
{
$this->orderId = $orderId;
parent::__construct('TransactionStatus', '/transactions/%transactionId%/status', RequestInterface::METHOD_GET);
Expand All @@ -48,12 +53,29 @@ public function getBodyParameters(): array

/**
* @return PayOrder
* @throws PayException
* @throws \Exception
*/
public function start(): PayOrder
{
# Always use rest.pay.nl for this status request
$this->config->setCore('https://rest.pay.nl');
return parent::start();
$cacheKey = 'transaction_status_' . md5(json_encode([$this->config->getUsername(), $this->orderId]));

if ($this->hasStaticCache($cacheKey)) {
return $this->getStaticCacheValue($cacheKey);
}

if ($this->config->isCacheEnabled()) {
$cache = new PayCache();
return $cache->get($cacheKey, function () use ($cacheKey) {
return $this->staticCache($cacheKey, function () {
$this->config->setCore('https://rest.pay.nl');
return parent::start();
});
}, 3); # 3 seconds file caching
}

return $this->staticCache($cacheKey, function () {
$this->config->setCore('https://rest.pay.nl');
return parent::start();
});
}
}
}
Loading