|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bmatovu\AirtelMoney\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Bmatovu\AirtelMoney\Traits\CommandUtils; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Console\ConfirmableTrait; |
|
8
|
|
|
|
|
9
|
|
|
class PinCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
use CommandUtils, ConfirmableTrait; |
|
12
|
|
|
|
|
13
|
|
|
protected $signature = "airtel-money:pin |
|
14
|
|
|
{--no-write : Don't write credentials to .env file.} |
|
15
|
|
|
{--f|force : Force the operation to run when in production.}"; |
|
16
|
|
|
|
|
17
|
|
|
public $description = 'Encrypt the disbursement PIN'; |
|
18
|
|
|
|
|
19
|
|
|
public function handle(): int |
|
20
|
|
|
{ |
|
21
|
|
|
if (! $this->confirmToProceed()) { |
|
22
|
|
|
return self::FAILURE; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$this->line('<options=bold>Set Disbursement PIN</>'); |
|
26
|
|
|
|
|
27
|
|
|
$pin = $this->ask('Enter 4 digit PIN'); |
|
28
|
|
|
|
|
29
|
|
|
$publicKeyPath = (string) $this->writeConfig('public_key'); |
|
30
|
|
|
|
|
31
|
|
|
$this->info("\nUsing: ".storage_path($publicKeyPath)); |
|
32
|
|
|
|
|
33
|
|
|
$encryptedPin = $this->encrypt($pin, storage_path($publicKeyPath)); |
|
34
|
|
|
|
|
35
|
|
|
$this->persistConfig('airtel-money.encrypted_pin', $encryptedPin); |
|
36
|
|
|
|
|
37
|
|
|
$this->info("\nEncrypted PIN written to .env file"); |
|
38
|
|
|
|
|
39
|
|
|
return self::SUCCESS; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function encrypt(string $data, string $publicKeyPath, int $padding = OPENSSL_PKCS1_OAEP_PADDING): string |
|
43
|
|
|
{ |
|
44
|
|
|
$publicKey = file_get_contents($publicKeyPath); |
|
45
|
|
|
|
|
46
|
|
|
if ($publicKey === false) { |
|
47
|
|
|
throw new \RuntimeException("Failed to read public key from {$publicKeyPath}"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (! openssl_public_encrypt($data, $encrypted, $publicKey, $padding)) { |
|
51
|
|
|
throw new \RuntimeException('Failed to encrypt data with the public key.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return base64_encode($encrypted); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|