diff --git a/example/Transaction/example-authorize-retry.php b/example/Transaction/example-authorize-retry.php index c7112fc..67bff56 100644 --- a/example/Transaction/example-authorize-retry.php +++ b/example/Transaction/example-authorize-retry.php @@ -11,11 +11,12 @@ $token = 'xxx'; -// The request ID you received after the first authorize request fails with a Behavior of RETRY or RETRY_LATER +// The request ID which is generated by our merchant-system to retry the authorize request +// after the first attempt returned a Behavior of RETRY or RETRY_LATER $requestId = 'your_request_id'; -// retryIndicator is set to 1 to indicate that this is a retry (see SaferpayJson/Request/RequestConfig.php) +// retryIndicator is set to 1 or more to indicate that this is a retry (see SaferpayJson/Request/RequestConfig.php) $retryIndicator = 1; @@ -23,19 +24,21 @@ // Step 1: // Prepare the authorize request // See https://saferpay.github.io/jsonapi/#Payment_v1_Transaction_Authorize -// -// Note: The RequestConfig is created with a requestId and retryIndicator to indicate that this is a retry -// (see https://docs.saferpay.com/home/integration-guide/licences-and-interfaces/error-handling#the-requestid-and-retryindicator) $requestConfig = new RequestConfig( $apiKey, $apiSecret, $customerId, - true, - $requestId, - $retryIndicator + true ); +// Note: The RequestConfig contains the optional fields requestId and retryIndicator to indicate that this is a retry +// or for debugging purposes +// (see https://docs.saferpay.com/home/integration-guide/licences-and-interfaces/error-handling#the-requestid-and-retryindicator) + +$requestConfig->setRequestId($requestId); +$requestConfig->setRetryIndicator($retryIndicator); + // ----------------------------- // Step 2: // Create the request with required data diff --git a/lib/SaferpayJson/Request/RequestConfig.php b/lib/SaferpayJson/Request/RequestConfig.php index 1267312..3fa8849 100644 --- a/lib/SaferpayJson/Request/RequestConfig.php +++ b/lib/SaferpayJson/Request/RequestConfig.php @@ -17,33 +17,19 @@ final class RequestConfig private string $customerId; private bool $test; private ?Client $client = null; - private ?string $requestId; - private int $retryIndicator; + private ?string $requestId = null; + private int $retryIndicator = self::MIN_RETRY_INDICATOR; public function __construct( - string $apiKey, - string $apiSecret, - string $customerId, - bool $test = false, - ?string $requestId = null, - int $retryIndicator = 0 + string $apiKey, + string $apiSecret, + string $customerId, + bool $test = false ) { $this->apiKey = $apiKey; $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; } public function getApiKey(): string @@ -82,11 +68,30 @@ public function getClient(): Client return $this->client; } + public function setRequestId(?string $requestId): self + { + $this->requestId = $requestId; + + return $this; + } + public function getRequestId(): ?string { return $this->requestId; } + public function setRetryIndicator(int $retryIndicator): self + { + 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); + } + + $this->retryIndicator = $retryIndicator; + + return $this; + } + public function getRetryIndicator(): int { return $this->retryIndicator; diff --git a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php index 4539740..8b2d580 100644 --- a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php +++ b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php @@ -38,7 +38,6 @@ public function getRequestConfigValidationParams(): array '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], ]; } @@ -50,18 +49,20 @@ public function testRequestConfigValidation( int $retryIndicator, ?string $expectedException = null): void { - if ($expectedException !== null) { - $this->expectException($expectedException, $requestId, $retryIndicator); - } - - new RequestConfig( + $config = new RequestConfig( 'apiKey', 'apiSecret', 'customerId', - false, - $requestId, - $retryIndicator + false ); + + if ($expectedException !== null) { + $this->expectException($expectedException, $requestId, $retryIndicator); + } + + $config + ->setRequestId($requestId) + ->setRetryIndicator($retryIndicator); } public function doTestSuccessfulResponse(string $responseClass): void