1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created By: Henry Ejemuta |
4
|
|
|
* Project: laravel-monnify |
5
|
|
|
* Class Name: InstallLaravelMonnify.php |
6
|
|
|
* Date Created: 7/13/20 |
7
|
|
|
* Time Created: 7:26 PM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace HenryEjemuta\LaravelMonnify\Console; |
11
|
|
|
|
12
|
|
|
use Illuminate\Console\Command; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
class InstallLaravelMonnify extends Command |
16
|
|
|
{ |
17
|
|
|
protected $signature = 'monnify:init'; |
18
|
|
|
|
19
|
|
|
protected $description = 'Install Laravel Monnify package'; |
20
|
|
|
|
21
|
|
|
public function handle() |
22
|
|
|
{ |
23
|
|
|
$this->info('Installing Laravel Monnify by Henry Ejemuta...'); |
24
|
|
|
|
25
|
|
|
$this->info('Publishing configuration...'); |
26
|
|
|
|
27
|
|
|
$this->call('vendor:publish', [ |
28
|
|
|
'--provider' => "HenryEjemuta\LaravelMonnify\LaravelMonnifyServiceProvider", |
29
|
|
|
'--tag' => "config" |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$this->info('Configuration Published!'); |
33
|
|
|
|
34
|
|
|
$this->info('Checking for environmental variable file (.env)'); |
35
|
|
|
if (file_exists($path = $this->envPath()) === false) { |
36
|
|
|
$this->info('Environmental variable file (.env) not found!'); |
37
|
|
|
} else { |
38
|
|
|
if ($this->isConfirmed() === false) { |
39
|
|
|
$this->comment('Phew... No changes were made to your .env file'); |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
$this->info('Now writing .env file with Monnify Sandbox credential for you to modify...'); |
43
|
|
|
|
44
|
|
|
$this->writeChanges($path, "MONNIFY_BASE_URL", "base_url", 'https://sandbox.monnify.com'); |
45
|
|
|
$this->writeChanges($path, "MONNIFY_API_KEY", "api_key", "MK_TEST_SAF7HR5F3F"); |
46
|
|
|
$this->writeChanges($path, "MONNIFY_SECRET_KEY", "secret_key", "4SY6TNL8CK3VPRSBTHTRG2N8XXEGC6NL"); |
47
|
|
|
$this->writeChanges($path, "MONNIFY_CONTRACT_CODE", "contract_code", "4934121686"); |
48
|
|
|
$this->writeChanges($path, "MONNIFY_WALLET_ID", "wallet_id", "2A47114E88904626955A6BD333A6B164"); |
49
|
|
|
$this->writeChanges($path, "MONNIFY_DEFAULT_SPLIT_PERCENTAGE", "default_split_percentage", 20); |
50
|
|
|
$this->writeChanges($path, "MONNIFY_DEFAULT_CURRENCY_CODE", "default_currency_code", 'NGN'); |
51
|
|
|
$this->writeChanges($path, "MONNIFY_DEFAULT_PAYMENT_REDIRECT_URL", "redirect_url", '"${APP_URL}/transaction/confirm"'); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->info('Laravel Monnify Package Installation Complete!'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function writeChanges($path, string $key, string $configKey, $value){ |
59
|
|
|
if (Str::contains(file_get_contents($path), "$key") === false) { |
60
|
|
|
$this->info("Now writing .env with $key=$value ..."); |
61
|
|
|
file_put_contents($path, PHP_EOL."$key=$value".PHP_EOL, FILE_APPEND); |
62
|
|
|
}else{ |
63
|
|
|
$this->info("Now updating $key value in your .env to $value ..."); |
64
|
|
|
// update existing entry |
65
|
|
|
file_put_contents($path, str_replace( |
66
|
|
|
"$key=".$this->laravel['config']["monnify.$configKey"], |
67
|
|
|
"$key=$value", file_get_contents($path) |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the .env file path. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
protected function envPath() |
79
|
|
|
{ |
80
|
|
|
if (method_exists($this->laravel, 'environmentFilePath')) { |
81
|
|
|
return $this->laravel->environmentFilePath(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// check if laravel version Less than 5.4.17 |
85
|
|
|
if (version_compare($this->laravel->version(), '5.4.17', '<')) { |
86
|
|
|
return $this->laravel->basePath() . DIRECTORY_SEPARATOR . '.env'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->laravel->basePath('.env'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check if the modification is confirmed. |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
protected function isConfirmed() |
98
|
|
|
{ |
99
|
|
|
return $this->confirm( |
100
|
|
|
'If your Monnify details are set within your .env file they would be overridden. Are you sure you want to override them if exists?' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|