Skip to content

Commit e657d40

Browse files
authored
Merge pull request #20 from paynl/fromPriv
From priv
2 parents 7ce4029 + d1de6b2 commit e657d40

15 files changed

Lines changed: 373 additions & 37 deletions

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
* text eol=lf
4+
5+
# Convert images to binary
6+
*.png binary
7+
*.jpg binary
8+
*.jpeg binary
9+
*.gif binary
10+
*.tiff binary
11+
*.ico binary

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "paynl/php-sdk",
33
"description": "Software Development Kit for implementing Pay.'s API version 3",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"type": "library",
66
"require": {
77
"php": "^8.1",

config/config.global.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
'username' => '', # Use AT-Code or SL-Code. Use AT-code together with API-Token.
88
'password' => '', # Use API token or secret. Use Secret in combination with SL-Code.
99
],
10-
'debug' => false
11-
];
10+
'debug' => false,
11+
'useFileCaching' => true
12+
];

samples/OrderCreate.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
try {
107107
$request->setReference('SDK0123456789');
108108
$payOrder = $request->start();
109-
echo get_class($payOrder);
110109
} catch (PayException $e) {
111110
echo '<pre>';
112111
echo 'Technical message: ' . $e->getMessage() . PHP_EOL;

samples/ServiceGetConfig.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
$config->setUsername($_REQUEST['username'] ?? '');
1414
$config->setPassword($_REQUEST['password'] ?? '');
1515

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

2928
echo '<pre>';
3029

31-
echo $config->getCode() . ' - ' . $config->getName() . PHP_EOL;
30+
echo $serviceConfig->getCode() . ' - ' . $serviceConfig->getName() . PHP_EOL;
3231

33-
$banks = $config->getBanks();
32+
$banks = $serviceConfig->getBanks();
3433
print_r($banks);
3534

36-
$terminals = $config->getTerminals();
35+
$terminals = $serviceConfig->getTerminals();
3736
print_r($terminals);
3837

39-
$tguList = $config->getCores();
38+
$tguList = $serviceConfig->getCores();
4039
print_r($tguList);
4140

42-
$paymentMethods = $config->getPaymentMethods();
41+
$paymentMethods = $serviceConfig->getPaymentMethods();
4342
foreach ($paymentMethods as $method) {
4443
echo $method->getId() . ' - ';
4544
echo $method->getName() . ' - ';
@@ -51,6 +50,6 @@
5150
echo PHP_EOL;
5251
}
5352

54-
foreach ($config->getCheckoutOptions() as $checkoutOption) {
53+
foreach ($serviceConfig->getCheckoutOptions() as $checkoutOption) {
5554
echo '=> TAG: ' . $checkoutOption->getTag() . PHP_EOL;
5655
}

src/Config/Config.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ public function isEmpty()
371371
return false;
372372
}
373373

374+
/**
375+
* @return boolean
376+
*/
377+
public function isCacheEnabled()
378+
{
379+
return ($this->data['useFileCaching'] ?? 0) == 1;
380+
}
381+
374382
/**
375383
* @return string
376384
*/
@@ -389,6 +397,16 @@ public function setPassword(string $password): self
389397
return $this;
390398
}
391399

400+
/**
401+
* @param boolean $caching
402+
* @return self
403+
*/
404+
public function setCaching(bool $caching): self
405+
{
406+
$this->data['useFileCaching'] = $caching;
407+
return $this;
408+
}
409+
392410
/**
393411
* @return string
394412
*/

src/Helpers/StaticCacheTrait.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace PayNL\Sdk\Helpers;
4+
5+
trait StaticCacheTrait
6+
{
7+
/**
8+
* In-memory static cache array
9+
*/
10+
private static array $cache = [];
11+
12+
/**
13+
* Get value from static cache, or execute callback and cache it.
14+
*
15+
* @param string $key
16+
* @param callable $callback
17+
* @return mixed
18+
* @throws \Exception
19+
*/
20+
protected function staticCache(string $key, callable $callback): mixed
21+
{
22+
if (isset(self::$cache[$key])) {
23+
if (self::$cache[$key] instanceof \Exception) {
24+
throw self::$cache[$key];
25+
}
26+
return self::$cache[$key];
27+
}
28+
29+
try {
30+
return self::$cache[$key] = $callback();
31+
} catch (\Exception $e) {
32+
self::$cache[$key] = $e;
33+
throw $e;
34+
}
35+
}
36+
37+
/**
38+
* @param string $key
39+
* @return boolean
40+
*/
41+
protected function hasStaticCache(string $key): bool
42+
{
43+
return isset(self::$cache[$key]);
44+
}
45+
46+
/**
47+
* @param string $key
48+
* @return mixed
49+
* @throws \Exception
50+
*/
51+
protected function getStaticCacheValue(string $key): mixed
52+
{
53+
if (!isset(self::$cache[$key])) {
54+
return null;
55+
}
56+
57+
if (self::$cache[$key] instanceof \Exception) {
58+
throw self::$cache[$key];
59+
}
60+
61+
return self::$cache[$key];
62+
}
63+
}

src/Model/Request/OrderStatusRequest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PayNL\Sdk\Model\Pay\PayOrder;
1010
use PayNL\Sdk\Request\RequestInterface;
1111
use PayNL\Sdk\Config\Config;
12+
use PayNL\Sdk\Helpers\StaticCacheTrait;
13+
use PayNL\Sdk\Util\PayCache;
1214

1315
/**
1416
* Class OrderStatusRequest
@@ -18,6 +20,8 @@
1820
*/
1921
class OrderStatusRequest extends RequestData
2022
{
23+
use StaticCacheTrait;
24+
2125
private string $orderId;
2226

2327
/**
@@ -35,7 +39,7 @@ public function __construct(string $orderId)
3539
public function getPathParameters(): array
3640
{
3741
return [
38-
'transactionId' => $this->orderId
42+
'transactionId' => $this->orderId
3943
];
4044
}
4145

@@ -53,9 +57,25 @@ public function getBodyParameters(): array
5357
*/
5458
public function start(): PayOrder
5559
{
56-
# Always use TGU-1 for orderStatus
57-
$this->config->setCore(Config::TGU1);
58-
59-
return parent::start();
60+
$cacheKey = 'order_status_' . md5(json_encode([$this->config->getUsername(), $this->orderId]));
61+
62+
if ($this->hasStaticCache($cacheKey)) {
63+
return $this->getStaticCacheValue($cacheKey);
64+
}
65+
66+
if ($this->config->isCacheEnabled()) {
67+
$cache = new PayCache();
68+
return $cache->get($cacheKey, function () use ($cacheKey) {
69+
return $this->staticCache($cacheKey, function () {
70+
$this->config->setCore(Config::TGU1);
71+
return parent::start();
72+
});
73+
}, 3); # 3 seconds file caching
74+
}
75+
76+
return $this->staticCache($cacheKey, function () {
77+
$this->config->setCore(Config::TGU1);
78+
return parent::start();
79+
});
6080
}
61-
}
81+
}

src/Model/Request/ServiceGetConfigRequest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PayNL\Sdk\Request\RequestData;
99
use PayNL\Sdk\Model\Response\ServiceGetConfigResponse;
1010
use PayNL\Sdk\Request\RequestInterface;
11+
use PayNL\Sdk\Util\PayCache;
12+
use PayNL\Sdk\Helpers\StaticCacheTrait;
1113

1214
/**
1315
* Class ServiceGetConfigRequest
@@ -18,12 +20,17 @@
1820
*/
1921
class ServiceGetConfigRequest extends RequestData
2022
{
23+
use StaticCacheTrait;
24+
25+
/**
26+
* @var string|mixed
27+
*/
2128
private string $serviceId;
2229

2330
/**
24-
* @param $serviceId
31+
* @param string $serviceId
2532
*/
26-
public function __construct($serviceId = '')
33+
public function __construct(string $serviceId = '')
2734
{
2835
$this->serviceId = $serviceId;
2936
parent::__construct('GetConfig', '/services/config', RequestInterface::METHOD_GET);
@@ -53,9 +60,35 @@ public function getBodyParameters(): array
5360
* @throws PayException
5461
*/
5562
public function start(): ServiceGetConfigResponse
63+
{
64+
$cacheKey = 'service_getconfig_' . md5(json_encode([$this->config->getUsername(), $this->config->getPassword(), $this->serviceId]));
65+
66+
if ($this->hasStaticCache($cacheKey)) {
67+
return $this->getStaticCacheValue($cacheKey);
68+
}
69+
70+
if ($this->config->isCacheEnabled()) {
71+
$cache = new PayCache();
72+
return $cache->get($cacheKey, function () use ($cacheKey) {
73+
return $this->staticCache($cacheKey, function () {
74+
return $this->startAPI();
75+
});
76+
}, 5);
77+
}
78+
79+
return $this->staticCache($cacheKey, function () {
80+
return $this->startAPI();
81+
});
82+
}
83+
84+
/**
85+
* @return ServiceGetConfigResponse
86+
* @throws PayException
87+
*/
88+
private function startAPI(): ServiceGetConfigResponse
5689
{
5790
$this->config->setCore('https://rest.pay.nl');
5891
$this->config->setVersion(2);
5992
return parent::start();
6093
}
61-
}
94+
}

src/Model/Request/TransactionStatusRequest.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace PayNL\Sdk\Model\Request;
66

7+
use PayNL\Sdk\Config\Config;
78
use PayNL\Sdk\Exception\PayException;
89
use PayNL\Sdk\Request\RequestData;
910
use PayNL\Sdk\Model\Pay\PayOrder;
1011
use PayNL\Sdk\Request\RequestInterface;
12+
use PayNL\Sdk\Helpers\StaticCacheTrait;
13+
use PayNL\Sdk\Util\PayCache;
1114

1215
/**
1316
* Class TransactionStatusRequest
@@ -17,12 +20,14 @@
1720
*/
1821
class TransactionStatusRequest extends RequestData
1922
{
23+
use StaticCacheTrait;
24+
2025
private string $orderId;
2126

2227
/**
23-
* @param $orderid
28+
* @param string $orderId
2429
*/
25-
public function __construct($orderId)
30+
public function __construct(string $orderId)
2631
{
2732
$this->orderId = $orderId;
2833
parent::__construct('TransactionStatus', '/transactions/%transactionId%/status', RequestInterface::METHOD_GET);
@@ -48,12 +53,29 @@ public function getBodyParameters(): array
4853

4954
/**
5055
* @return PayOrder
51-
* @throws PayException
56+
* @throws \Exception
5257
*/
5358
public function start(): PayOrder
5459
{
55-
# Always use rest.pay.nl for this status request
56-
$this->config->setCore('https://rest.pay.nl');
57-
return parent::start();
60+
$cacheKey = 'transaction_status_' . md5(json_encode([$this->config->getUsername(), $this->orderId]));
61+
62+
if ($this->hasStaticCache($cacheKey)) {
63+
return $this->getStaticCacheValue($cacheKey);
64+
}
65+
66+
if ($this->config->isCacheEnabled()) {
67+
$cache = new PayCache();
68+
return $cache->get($cacheKey, function () use ($cacheKey) {
69+
return $this->staticCache($cacheKey, function () {
70+
$this->config->setCore('https://rest.pay.nl');
71+
return parent::start();
72+
});
73+
}, 3); # 3 seconds file caching
74+
}
75+
76+
return $this->staticCache($cacheKey, function () {
77+
$this->config->setCore('https://rest.pay.nl');
78+
return parent::start();
79+
});
5880
}
59-
}
81+
}

0 commit comments

Comments
 (0)