From 300013d9e87065df4c23b36b0234085a0ad2ff97 Mon Sep 17 00:00:00 2001 From: David Funke Date: Mon, 22 Jul 2024 14:31:09 +0200 Subject: [PATCH 1/6] Added retry fields in RequestConfig.php --- .../Request/Container/RequestHeader.php | 16 +++++++++++++- lib/SaferpayJson/Request/Request.php | 4 +++- lib/SaferpayJson/Request/RequestConfig.php | 22 ++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/SaferpayJson/Request/Container/RequestHeader.php b/lib/SaferpayJson/Request/Container/RequestHeader.php index ec40baf..5071c2c 100644 --- a/lib/SaferpayJson/Request/Container/RequestHeader.php +++ b/lib/SaferpayJson/Request/Container/RequestHeader.php @@ -4,10 +4,14 @@ namespace Ticketpark\SaferpayJson\Request\Container; +use InvalidArgumentException; use JMS\Serializer\Annotation\SerializedName; final class RequestHeader { + private const MIN_RETRY_INDICATOR = 0; + private const MAX_RETRY_INDICATOR = 9; + /** * @SerializedName("SpecVersion") */ @@ -33,9 +37,19 @@ final class RequestHeader */ private ?ClientInfo $clientInfo = null; - public function __construct(string $customerId, string $requestId = null, int $retryIndicator = 0) + public function __construct(string $customerId, string $requestId = null, int $retryIndicator = self::MIN_RETRY_INDICATOR) { $this->customerId = $customerId; + + if ($retryIndicator < self::MIN_RETRY_INDICATOR || $retryIndicator > self::MAX_RETRY_INDICATOR) { + throw new InvalidArgumentException('Retry indicator range: inclusive between ' + . self::MIN_RETRY_INDICATOR . ' and ' . self::MAX_RETRY_INDICATOR); + } + + if ($retryIndicator > self::MIN_RETRY_INDICATOR && $requestId === null) { + throw new InvalidArgumentException('Request id must be set if retry indicator is greater than 0'); + } + $this->requestId = $requestId; $this->retryIndicator = $retryIndicator; diff --git a/lib/SaferpayJson/Request/Request.php b/lib/SaferpayJson/Request/Request.php index d336945..b0a9064 100644 --- a/lib/SaferpayJson/Request/Request.php +++ b/lib/SaferpayJson/Request/Request.php @@ -50,7 +50,9 @@ public function __construct(RequestConfig $requestConfig) public function getRequestHeader(): RequestHeader { return new RequestHeader( - $this->requestConfig->getCustomerId() + $this->requestConfig->getCustomerId(), + $this->requestConfig->getRequestId(), + $this->requestConfig->getRetryIndicator() ); } diff --git a/lib/SaferpayJson/Request/RequestConfig.php b/lib/SaferpayJson/Request/RequestConfig.php index e482ee5..8ba2e0c 100644 --- a/lib/SaferpayJson/Request/RequestConfig.php +++ b/lib/SaferpayJson/Request/RequestConfig.php @@ -13,13 +13,23 @@ final class RequestConfig private string $customerId; private bool $test; private ?Client $client = null; + private ?string $requestId; + private int $retryIndicator; - public function __construct(string $apiKey, string $apiSecret, string $customerId, bool $test = false) + public function __construct( + string $apiKey, + string $apiSecret, + string $customerId, + bool $test = false, + ?string $requestId = null, + int $retryIndicator = 0) { $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; $this->customerId = $customerId; $this->test = $test; + $this->requestId = $requestId; + $this->retryIndicator = $retryIndicator; } public function getApiKey(): string @@ -57,4 +67,14 @@ public function getClient(): Client return $this->client; } + + public function getRequestId(): ?string + { + return $this->requestId; + } + + public function getRetryIndicator(): int + { + return $this->retryIndicator; + } } From 514bb0c0f04376f712ceff25e24c4bb92a271870 Mon Sep 17 00:00:00 2001 From: David Funke Date: Mon, 22 Jul 2024 19:08:39 +0200 Subject: [PATCH 2/6] Moved retry fields validation in RequestConfig.php --- .../Request/Container/RequestHeader.php | 20 +++++-------------- lib/SaferpayJson/Request/RequestConfig.php | 14 +++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/SaferpayJson/Request/Container/RequestHeader.php b/lib/SaferpayJson/Request/Container/RequestHeader.php index 5071c2c..21819ae 100644 --- a/lib/SaferpayJson/Request/Container/RequestHeader.php +++ b/lib/SaferpayJson/Request/Container/RequestHeader.php @@ -4,14 +4,11 @@ namespace Ticketpark\SaferpayJson\Request\Container; -use InvalidArgumentException; use JMS\Serializer\Annotation\SerializedName; +use Ticketpark\SaferpayJson\Request\RequestConfig; final class RequestHeader { - private const MIN_RETRY_INDICATOR = 0; - private const MAX_RETRY_INDICATOR = 9; - /** * @SerializedName("SpecVersion") */ @@ -37,19 +34,12 @@ final class RequestHeader */ private ?ClientInfo $clientInfo = null; - public function __construct(string $customerId, string $requestId = null, int $retryIndicator = self::MIN_RETRY_INDICATOR) + public function __construct( + string $customerId, + string $requestId = null, + int $retryIndicator = RequestConfig::MIN_RETRY_INDICATOR) { $this->customerId = $customerId; - - if ($retryIndicator < self::MIN_RETRY_INDICATOR || $retryIndicator > self::MAX_RETRY_INDICATOR) { - throw new InvalidArgumentException('Retry indicator range: inclusive between ' - . self::MIN_RETRY_INDICATOR . ' and ' . self::MAX_RETRY_INDICATOR); - } - - if ($retryIndicator > self::MIN_RETRY_INDICATOR && $requestId === null) { - throw new InvalidArgumentException('Request id must be set if retry indicator is greater than 0'); - } - $this->requestId = $requestId; $this->retryIndicator = $retryIndicator; diff --git a/lib/SaferpayJson/Request/RequestConfig.php b/lib/SaferpayJson/Request/RequestConfig.php index 8ba2e0c..736d313 100644 --- a/lib/SaferpayJson/Request/RequestConfig.php +++ b/lib/SaferpayJson/Request/RequestConfig.php @@ -5,9 +5,13 @@ namespace Ticketpark\SaferpayJson\Request; use GuzzleHttp\Client; +use InvalidArgumentException; final class RequestConfig { + public const MIN_RETRY_INDICATOR = 0; + public const MAX_RETRY_INDICATOR = 9; + private string $apiKey; private string $apiSecret; private string $customerId; @@ -28,6 +32,16 @@ public function __construct( $this->apiSecret = $apiSecret; $this->customerId = $customerId; $this->test = $test; + + if ($retryIndicator < self::MIN_RETRY_INDICATOR || $retryIndicator > self::MAX_RETRY_INDICATOR) { + throw new InvalidArgumentException('Retry indicator range: inclusive between ' + . self::MIN_RETRY_INDICATOR . ' and ' . self::MAX_RETRY_INDICATOR); + } + + if ($retryIndicator > self::MIN_RETRY_INDICATOR && $requestId === null) { + throw new InvalidArgumentException('Request id must be set if retry indicator is greater than 0'); + } + $this->requestId = $requestId; $this->retryIndicator = $retryIndicator; } From 2f9f281dfffe2356b2a1e5a70b7e6957465d8dcd Mon Sep 17 00:00:00 2001 From: David Funke Date: Mon, 22 Jul 2024 19:17:44 +0200 Subject: [PATCH 3/6] Added unit tests for RequestConfig --- .../Tests/Request/CommonRequestTest.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php index 1937577..27750fb 100644 --- a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php +++ b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response as GuzzleResponse; use GuzzleHttp\Psr7\Utils as GuzzleUtils; +use InvalidArgumentException; use JMS\Serializer\SerializerBuilder; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -30,6 +31,36 @@ public function testErrorResponse(): void $this->executeRequest(); } + public function getRequestConfigValidationParams(): array + { + return [ + 'first try' => [null, RequestConfig::MIN_RETRY_INDICATOR], + 'second try' => [uniqid(), RequestConfig::MIN_RETRY_INDICATOR + 1], + 'last try' => [uniqid(), RequestConfig::MAX_RETRY_INDICATOR], + 'try after all retries exceeded' => [uniqid(), RequestConfig::MAX_RETRY_INDICATOR + 1, InvalidArgumentException::class], + 'retry without previous request id' => [null, RequestConfig::MAX_RETRY_INDICATOR, InvalidArgumentException::class], + ]; + } + + public function testRequestConfigValidation( + ?string $requestId, + int $retryIndicator, + ?string $expectedException = null): void + { + if ($expectedException !== null) { + $this->expectException($expectedException, $requestId, $retryIndicator); + } + + new RequestConfig( + 'apiKey', + 'apiSecret', + 'customerId', + false, + $requestId, + $retryIndicator + ); + } + public function doTestSuccessfulResponse(string $responseClass): void { $this->successful = true; @@ -87,7 +118,7 @@ private function getResponseMock(): MockObject $response->expects($this->any()) ->method('getStatusCode') - ->will($this->returnValue($this->successful ? 200: 404)); + ->will($this->returnValue($this->successful ? 200 : 404)); if ($this->successful) { $content = $this->getFakedApiResponse($this->successfulResponseClass); From 67e7a551feec42514231d87145940e1b8b8fb318 Mon Sep 17 00:00:00 2001 From: David Funke Date: Mon, 22 Jul 2024 19:43:29 +0200 Subject: [PATCH 4/6] Fixed missing dataProvider --- tests/SaferpayJson/Tests/Request/CommonRequestTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php index 27750fb..4539740 100644 --- a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php +++ b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php @@ -42,6 +42,9 @@ public function getRequestConfigValidationParams(): array ]; } + /** + * @dataProvider getRequestConfigValidationParams + */ public function testRequestConfigValidation( ?string $requestId, int $retryIndicator, From fd74519d871833286f37f2d35bd86c7e973a488b Mon Sep 17 00:00:00 2001 From: David Funke Date: Mon, 22 Jul 2024 19:51:37 +0200 Subject: [PATCH 5/6] Fixed static code analysis --- lib/SaferpayJson/Request/Container/RequestHeader.php | 4 ++-- lib/SaferpayJson/Request/RequestConfig.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/SaferpayJson/Request/Container/RequestHeader.php b/lib/SaferpayJson/Request/Container/RequestHeader.php index 21819ae..6b943ca 100644 --- a/lib/SaferpayJson/Request/Container/RequestHeader.php +++ b/lib/SaferpayJson/Request/Container/RequestHeader.php @@ -37,8 +37,8 @@ final class RequestHeader public function __construct( string $customerId, string $requestId = null, - int $retryIndicator = RequestConfig::MIN_RETRY_INDICATOR) - { + int $retryIndicator = RequestConfig::MIN_RETRY_INDICATOR + ) { $this->customerId = $customerId; $this->requestId = $requestId; $this->retryIndicator = $retryIndicator; diff --git a/lib/SaferpayJson/Request/RequestConfig.php b/lib/SaferpayJson/Request/RequestConfig.php index 736d313..1267312 100644 --- a/lib/SaferpayJson/Request/RequestConfig.php +++ b/lib/SaferpayJson/Request/RequestConfig.php @@ -26,8 +26,8 @@ public function __construct( string $customerId, bool $test = false, ?string $requestId = null, - int $retryIndicator = 0) - { + int $retryIndicator = 0 + ) { $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; $this->customerId = $customerId; From 2c0d1f4edae72375cf70a56c4e8a220f0e641b86 Mon Sep 17 00:00:00 2001 From: David Funke Date: Tue, 23 Jul 2024 09:41:19 +0200 Subject: [PATCH 6/6] Added retry example --- .../Transaction/example-authorize-retry.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 example/Transaction/example-authorize-retry.php diff --git a/example/Transaction/example-authorize-retry.php b/example/Transaction/example-authorize-retry.php new file mode 100644 index 0000000..c7112fc --- /dev/null +++ b/example/Transaction/example-authorize-retry.php @@ -0,0 +1,67 @@ +setCondition() +// See Saferpay documentation for available options. + +// ----------------------------- +// Step 3: +// Execute and check for successful response + +try { + $response = $authorizeRequest->execute(); +} catch (SaferpayErrorException $e) { + die ($e->getErrorResponse()->getErrorMessage()); +} + +echo 'The transaction has been successful! Transaction id: ' . $response->getTransaction()->getId() . "\n"; + +// ----------------------------- +// Step 4: +// Capture the transaction to get the cash flowing. +// see: example-capture.php