forked from apoloz/alfa-bank-sdk
refactor(core): enhance SDK structure and documentation
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -1,46 +1,120 @@
|
||||
<?php
|
||||
/*
|
||||
// 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;
|
||||
|
||||
use Apoloz\AlfaBank\AlfaBankConnector;
|
||||
// <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\Services\OpenSslCmsSigner;
|
||||
use Apoloz\AlfaBank\Services\PhpseclibCardEncryptor;
|
||||
use Apoloz\AlfaBank\Sbp\SbpConnector;
|
||||
use Apoloz\AlfaBank\Laravel\Middleware\VerifyAlfaWebhook;
|
||||
use Apoloz\AlfaBank\Laravel\Http\Controllers\AlfaWebhookController;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
// </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(AlfaBankConnector::class, function ($app) {
|
||||
$config = $app['config']['alfa-bank.baas'];
|
||||
$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'],
|
||||
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(
|
||||
certificatePath: $config['certificate_path'],
|
||||
privateKeyPath: $config['private_key_path'],
|
||||
environment: $config['environment'] === 'production' ? Environment::PRODUCTION : Environment::SANDBOX
|
||||
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(
|
||||
certificatePath: $config['certificate_path'],
|
||||
privateKeyPath: $config['private_key_path'],
|
||||
keyNameAlias: $config['key_name_alias'],
|
||||
environment: $config['environment'] === 'production' ? Environment::PRODUCTION : Environment::SANDBOX
|
||||
alfaConfig: $app->make('alfa.sbp.config'),
|
||||
keyNameAlias: $config['key_name_alias']
|
||||
);
|
||||
});
|
||||
$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');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user