Files
alfa-bank-sdk/src/AlfaBank.php
2026-01-18 23:53:31 +03:00

89 lines
3.5 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\Services\OpenSslCmsSigner;
use Apoloz\AlfaBank\Sbp\SbpConnector;
use Apoloz\AlfaBank\Requests\Accounts\GetAccountsRequest;
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 $signingCertificatePath = null,
?string $signingPrivateKeyPath = null
): AlfaBankConnector {
$config = new AlfaConfig(
baseUrl: $environment->value,
certificatePath: $certificatePath,
privateKeyPath: $privateKeyPath,
caBundlePath: $caBundlePath,
signingCertificatePath: $signingCertificatePath,
signingPrivateKeyPath: $signingPrivateKeyPath
);
$signerCert = $signingCertificatePath ?: $certificatePath;
$signerKey = $signingPrivateKeyPath ?: $privateKeyPath;
$signer = new OpenSslCmsSigner($signerCert, $signerKey);
return new AlfaBankConnector($config, $signer);
}
// </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
): 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);
}
// </SECTION>
}