Files
alfa-bank-sdk/src/AlfaBank.php
2026-06-09 16:11:15 +03:00

106 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Apoloz\AlfaBank;
// <SECTION name="module_imports" intent="Import all necessary connectors, services, and configuration objects.">
// <BELIEF_STATE "The factory needs to know about the concrete classes it will instantiate, including the connectors, config DTOs, and any default services like the CMS signer.">
use Apoloz\AlfaBank\Core\AlfaBankConnector;
use Apoloz\AlfaBank\Core\AlfaConfig;
use Apoloz\AlfaBank\Enums\Environment;
use Apoloz\AlfaBank\Requests\Accounts\GetAccountsRequest;
use Apoloz\AlfaBank\Sbp\SbpConnector;
use Apoloz\AlfaBank\Services\OpenSslCmsSigner;
use Psr\Log\LoggerInterface;
class AlfaBank
{
/**
* Performs a health check on the connection.
* Throws an exception if authentication or account listing fails.
*/
public static function check(
string $certificatePath,
string $privateKeyPath,
string $clientId,
string $clientSecret,
Environment $environment = Environment::SANDBOX,
?string $caBundlePath = null
): bool {
$baas = self::baas($certificatePath, $privateKeyPath, $environment, $caBundlePath);
$baas->login($clientId, $clientSecret);
$baas->send(new GetAccountsRequest);
return true;
}
public static function baas(
string $certificatePath,
string $privateKeyPath,
Environment $environment = Environment::SANDBOX,
?string $caBundlePath = null,
?string $clientId = null,
?string $clientSecret = null,
?string $redirectUri = null,
?string $signingCertificatePath = null,
?string $signingPrivateKeyPath = null,
?LoggerInterface $logger = null,
): AlfaBankConnector {
$config = new AlfaConfig(
baseUrl: $environment->value,
certificatePath: $certificatePath,
privateKeyPath: $privateKeyPath,
caBundlePath: $caBundlePath,
clientId: $clientId,
clientSecret: $clientSecret,
redirectUri: $redirectUri,
signingCertificatePath: $signingCertificatePath,
signingPrivateKeyPath: $signingPrivateKeyPath
);
$signerCert = $signingCertificatePath ?: $certificatePath;
$signerKey = $signingPrivateKeyPath ?: $privateKeyPath;
$signer = new OpenSslCmsSigner($signerCert, $signerKey);
$connector = new AlfaBankConnector($config, $signer);
if ($logger) {
$connector->withLogging($logger);
}
return $connector;
}
// </SECTION>
// <SECTION name="sbp_connector_factory" intent="Create a fully configured connector for the Faster Payments (SBP) API.">
// <BELIEF_STATE "This method encapsulates the logic for creating an SBP connector. It correctly resolves the distinct SBP base URL based on the environment and passes the required SBP-specific parameters like `keyNameAlias`. This separation from the `baas` factory is crucial due to the different configuration requirements.">
public static function sbp(
string $certificatePath,
string $privateKeyPath,
string $keyNameAlias,
Environment $environment = Environment::SANDBOX,
?string $caBundlePath = null,
?string $signingCertificatePath = null,
?string $signingPrivateKeyPath = null,
?LoggerInterface $logger = null,
): SbpConnector {
$baseUrl = $environment === Environment::PRODUCTION
? Environment::SBP_PRODUCTION->value
: Environment::SBP_SANDBOX->value;
$config = new AlfaConfig(
baseUrl: $baseUrl,
certificatePath: $certificatePath,
privateKeyPath: $privateKeyPath,
caBundlePath: $caBundlePath,
signingCertificatePath: $signingCertificatePath,
signingPrivateKeyPath: $signingPrivateKeyPath
);
return new SbpConnector($config, $keyNameAlias, logger: $logger);
}
// </SECTION>
}