Files
alfa-bank-sdk/src/Laravel/AlfaBankServiceProvider.php
Anatoliy Poloz b15d29f8e9 fix(formating)
2026-01-28 03:36:26 +03:00

129 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
/*
// MODULE_CONTRACT: Integrates the Alfa Bank SDK into the Laravel framework's Service Container (IoC).
// RESPONSIBILITY: To register and configure all necessary SDK services, connectors, and interfaces, making them available for dependency injection throughout a Laravel application.
// INPUTS: Laravel application container, configuration from `config/alfa-bank.php`.
// OUTPUTS: Fully configured and injectable Connector and Service instances.
// LINKS: config/alfa-bank.php, AlfaBankConnector.php, SbpConnector.php
*/
namespace Apoloz\AlfaBank\Laravel;
// <SECTION name="module_imports" intent="Import SDK core components and Laravel framework base classes required for service registration.">
use Apoloz\AlfaBank\Contracts\EncryptorInterface;
use Apoloz\AlfaBank\Contracts\SignerInterface;
use Apoloz\AlfaBank\Core\AlfaBankConnector;
use Apoloz\AlfaBank\Core\AlfaConfig;
use Apoloz\AlfaBank\Enums\Environment;
use Apoloz\AlfaBank\Laravel\Http\Controllers\AlfaWebhookController;
use Apoloz\AlfaBank\Laravel\Middleware\VerifyAlfaWebhook;
use Apoloz\AlfaBank\Sbp\SbpConnector;
use Apoloz\AlfaBank\Services\OpenSslCmsSigner;
use Apoloz\AlfaBank\Services\PhpseclibCardEncryptor;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
// </SECTION>
class AlfaBankServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/Config/alfa-bank.php', 'alfa-bank');
$this->app->singleton('alfa.baas.config', function ($app) {
$conf = $app['config']['alfa-bank.baas'];
return new AlfaConfig(
baseUrl: $conf['environment'] === 'production' ? Environment::PRODUCTION->value : Environment::SANDBOX->value,
certificatePath: $conf['certificate_path'],
privateKeyPath: $conf['private_key_path'],
caBundlePath: $conf['ca_bundle'] ?? null,
clientId: $conf['client_id'],
clientSecret: $conf['client_secret'],
redirectUri: $conf['redirect_uri'] ?? null,
signingCertificatePath: $conf['signing_certificate_path'] ?? null,
signingPrivateKeyPath: $conf['signing_private_key_path'] ?? null,
);
});
$this->app->bind(SignerInterface::class, function ($app) {
/** @var AlfaConfig $config */
$config = $app->make('alfa.baas.config');
$cert = $config->signingCertificatePath ?: $config->certificatePath;
$key = $config->signingPrivateKeyPath ?: $config->privateKeyPath;
return new OpenSslCmsSigner($cert, $key);
});
$this->app->bind(EncryptorInterface::class, function ($app) {
$key = $app['config']['alfa-bank.encryption.card_public_key'] ?? '';
return new PhpseclibCardEncryptor($key);
});
$this->app->singleton(AlfaBankConnector::class, function ($app) {
return new AlfaBankConnector(
alfaConfig: $app->make('alfa.baas.config'),
signer: $app->make(SignerInterface::class)
);
});
$this->app->alias(AlfaBankConnector::class, 'alfa.baas');
$this->app->singleton('alfa.sbp.config', function ($app) {
$conf = $app['config']['alfa-bank.sbp'];
return new AlfaConfig(
baseUrl: $conf['environment'] === 'production' ? Environment::SBP_PRODUCTION->value : Environment::SBP_SANDBOX->value,
certificatePath: $conf['certificate_path'],
privateKeyPath: $conf['private_key_path'],
caBundlePath: $conf['ca_bundle'] ?? null,
signingCertificatePath: $conf['signing_certificate_path'] ?? null,
signingPrivateKeyPath: $conf['signing_private_key_path'] ?? null,
);
});
$this->app->singleton(SbpConnector::class, function ($app) {
$config = $app['config']['alfa-bank.sbp'];
return new SbpConnector(
alfaConfig: $app->make('alfa.sbp.config'),
keyNameAlias: $config['key_name_alias'] ?? null
);
});
$this->app->alias(SbpConnector::class, 'alfa.sbp');
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/Config/alfa-bank.php' => config_path('alfa-bank.php'),
], 'alfa-bank-config');
$this->commands([
\Apoloz\AlfaBank\Laravel\Commands\CheckConnectionCommand::class,
]);
}
if ($this->app->bound('router')) {
$router = $this->app->make('router');
$router->aliasMiddleware('alfa.webhook', VerifyAlfaWebhook::class);
Route::macro('alfaWebhooks', function (string $url) {
return Route::post($url, AlfaWebhookController::class)
->name('alfa.webhooks')
->middleware('alfa.webhook');
});
}
}
}