components->info('Der Klartext-Key wird nur flüchtig zur Hash-Erzeugung verwendet und nirgends gespeichert oder geloggt.');
$plainKey = trim((string) password(
label: 'PAYONE-Portal-Key (Klartext, verdeckte Eingabe)',
required: true,
));
if ($plainKey === '') {
$this->components->error('Es wurde kein Key eingegeben.');
return self::FAILURE;
}
$hash = hash('md5', $plainKey);
$this->newLine();
$this->components->twoColumnDetail('PAYONE_KEY_HASH', $hash);
$this->newLine();
if (! $this->option('write')) {
$this->line(' Ohne --write: Wert manuell als PAYONE_KEY_HASH in die .env übernehmen.');
return self::SUCCESS;
}
if (! confirm("PAYONE_KEY_HASH in {$this->envPath()} schreiben/aktualisieren?", default: true)) {
$this->components->warn('Abgebrochen – .env wurde nicht verändert.');
return self::SUCCESS;
}
if (! $this->writeHashToEnv($hash)) {
return self::FAILURE;
}
$this->components->info('PAYONE_KEY_HASH in die .env geschrieben.');
$this->line(' Danach php artisan config:clear ausführen (bzw. config neu cachen).');
return self::SUCCESS;
}
private function envPath(): string
{
return base_path('.env');
}
private function writeHashToEnv(string $hash): bool
{
$path = $this->envPath();
if (! is_file($path) || ! is_writable($path)) {
$this->components->error(".env nicht gefunden oder nicht beschreibbar: {$path}");
return false;
}
$contents = (string) file_get_contents($path);
$line = 'PAYONE_KEY_HASH='.$hash;
if (preg_match('/^PAYONE_KEY_HASH=.*$/m', $contents)) {
$contents = preg_replace('/^PAYONE_KEY_HASH=.*$/m', $line, $contents);
} else {
$contents = rtrim($contents, "\n")."\n".$line."\n";
}
return file_put_contents($path, $contents) !== false;
}
}