Skip to content

Commit

Permalink
Moved variable Parts in RequestConfig.php constructor to dedicated se…
Browse files Browse the repository at this point in the history
…tter
  • Loading branch information
DF-Dave committed Jul 26, 2024
1 parent 2c0d1f4 commit da9b4e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
19 changes: 11 additions & 8 deletions example/Transaction/example-authorize-retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@

$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;

// -----------------------------
// 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
Expand Down
45 changes: 25 additions & 20 deletions lib/SaferpayJson/Request/RequestConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions tests/SaferpayJson/Tests/Request/CommonRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}

Expand All @@ -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
Expand Down

0 comments on commit da9b4e6

Please sign in to comment.