Authorization
Set up OAuth2 authentication with automatic token management.
The Authorization facade handles OAuth2 authentication with the Paysera API.
Basic Usage​
<?php
use Paysera\CheckoutSdk\SdkFacadeBuilder;
use Paysera\CheckoutSdk\Entity\PaymentApiCredentials;
$sdk = (new SdkFacadeBuilder())->build();
$authFacade = $sdk->getAuthorizationFacade();
// Create credentials
$credentials = new PaymentApiCredentials(
'your-client-id',
'your-client-secret'
);
// Authenticate
$authToken = $authFacade->authorize($credentials);
// Token is now available for API calls
echo "Expires at: " . $authToken->getExpiresAt() . "\n"; // Unix timestamp
How It Works​
The authorize() method:
- Sends credentials to Paysera auth endpoint
- Receives JWT access token
- Stores token in configured repository (or memory)
- Returns token entity for reference
Credentials Entity​
use Paysera\CheckoutSdk\Entity\PaymentApiCredentials;
// Create credentials
$credentials = new PaymentApiCredentials(
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
);
// Access values
$clientId = $credentials->getClientId();
$clientSecret = $credentials->getClientSecret();
Token Entity​
use Paysera\CheckoutSdk\Entity\PaymentApiAuthToken;
// After authorization
$token = $authFacade->authorize($credentials);
// Access token properties
$accessToken = $token->getAccessToken(); // JWT string
$tokenType = $token->getTokenType(); // e.g., 'Bearer'
$scope = $token->getScope(); // Token scope
$issuedAt = $token->getIssuedAt(); // int (Unix timestamp)
$expiresIn = $token->getExpiresIn(); // int (seconds)
$expiresAt = $token->getExpiresAt(); // int (Unix timestamp, computed)
$environment = $token->getEnvironment(); // PaymentApiEnvironment
Token Persistence​
By default, tokens are stored in memory and lost when the script ends. For persistent storage, implement PaymentApiAuthTokenRepositoryInterface and pass it to the builder. See Installation for details.
Error Handling​
use Paysera\CheckoutSdk\Exception\IntegrationException;
try {
$token = $authFacade->authorize($credentials);
} catch (IntegrationException $e) {
// Authentication failed
error_log('Auth failed: ' . $e->getMessage());
// Check for specific errors
if (str_contains($e->getMessage(), 'invalid_client')) {
// Invalid credentials
} elseif (str_contains($e->getMessage(), 'unauthorized')) {
// Credentials revoked or disabled
}
}