-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppConfig.php
More file actions
391 lines (352 loc) · 14.8 KB
/
Copy pathAppConfig.php
File metadata and controls
391 lines (352 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* Plugin format 1.0
*/
#App\GP247\Plugins\PaypalExpress\AppConfig.php
namespace App\GP247\Plugins\PaypalExpress;
use App\GP247\Plugins\PaypalExpress\Models\ExtensionModel;
use GP247\Core\Models\AdminConfig;
use GP247\Core\Models\AdminHome;
use GP247\Core\ExtensionConfigDefault;
use GP247\Core\Models\AdminMenu;
use Illuminate\Support\Facades\DB;
class AppConfig extends ExtensionConfigDefault
{
/**
* Display order of the settings on the admin screen (admin_config.sort).
*
* WHY explicit: core orders the form by `sort` only; with every row at 0 the database
* returned them in index order, which differed between servers (3.1.2 and earlier).
*/
public const SORT = [
'sandbox' => 1,
'client_id_sandbox' => 2,
'client_secret_sandbox' => 3,
'client_id_live' => 4,
'client_secret_live' => 5,
'webhook_id' => 6,
'order_status_success' => 7,
'order_status_refunded' => 8,
'payment_status_success' => 9,
'payment_status_refunded' => 10,
];
public function __construct()
{
//Read config from gp247.json
$config = file_get_contents(__DIR__.'/gp247.json');
$config = json_decode($config, true);
$this->configGroup = $config['configGroup'];
$this->configKey = $config['configKey'];
$this->configCode = $config['configCode'] ?? $this->configKey;
$this->requireCore = $config['requireCore'] ?? [];
$this->requireComposerPackages = $config['requireComposerPackages'] ?? [];
$this->requireGp247Extensions = $config['requireGp247Extensions'] ?? [];
//Path
$this->appPath = $this->configGroup . '/' . $this->configKey;
//Language
$this->title = trans($this->appPath.'::lang.title');
//Image logo or thumb
$this->image = $this->appPath.'/'.$config['image'];
//
$this->version = $config['version'];
$this->auth = $config['auth'];
$this->link = $config['link'];
}
public function install()
{
$check = AdminConfig::where('key', $this->configKey)
->where('group', $this->configGroup)->first();
if ($check) {
//Check Plugin key exist
$return = ['error' => 1, 'msg' => gp247_language_render('admin.extension.plugin_exist')];
} else {
// Insert plugin to config. WHY: every row must carry the same column
// set — AdminConfig::insert() derives the column list from the first
// row, so the credential rows appended below (which add `security`)
// would otherwise mismatch. Keep `security => 0` on these base rows.
$dataInsert = [
[
'group' => $this->configGroup,
'code' => $this->configCode,
'key' => $this->configKey,
'sort' => 0,
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => self::ON, //Enable extension
'security' => 0,
'detail' => $this->appPath.'::lang.title',
],
[
'group' => $this->configGroup,
'code' => $this->configKey.'_config',
'key' => $this->configKey.'_order_status_success',
'sort' => self::SORT['order_status_success'],
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => 2, //Order sttus processing
'security' => 0,
'detail' => $this->appPath.'::lang.order_status_success',
],
[
'group' => $this->configGroup,
'code' => $this->configKey.'_config',
'key' => $this->configKey.'_order_status_refunded',
'sort' => self::SORT['order_status_refunded'],
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => 7, //Order sttus refunded
'security' => 0,
'detail' => $this->appPath.'::lang.order_status_refunded',
],
[
'group' => $this->configGroup,
'code' => $this->configKey.'_config',
'key' => $this->configKey.'_payment_status_success',
'sort' => self::SORT['payment_status_success'],
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => 3, //Order payment paid
'security' => 0,
'detail' => $this->appPath.'::lang.payment_status_success',
],
[
'group' => $this->configGroup,
'code' => $this->configKey.'_config',
'key' => $this->configKey.'_payment_status_refunded',
'sort' => self::SORT['payment_status_refunded'],
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => 4, //Order payment refunded
'security' => 0,
'detail' => $this->appPath.'::lang.payment_status_refunded',
],
];
// Connection credentials (GLOBAL, empty by default; secrets flagged for
// at-rest encryption). Site owner enters them in admin; the update() hook
// imports any existing .env values on upgrade (ADR paypal-express_per-store-credentials).
foreach ($this->credentialSeed() as $row) {
$dataInsert[] = $row;
}
try {
AdminConfig::insert(
$dataInsert
);
(new ExtensionModel)->installExtension();
$return = ['error' => 0, 'msg' => gp247_language_render('admin.extension.install_success')];
} catch (\Throwable $e) {
$return = ['error' => 1, 'msg' => $e->getMessage()];
}
// Insert menu (skip if already present - insertOrIgnore only dedupes
// on a DB unique constraint, which this table doesn't have on uri)
$checkIdBlockPayment = AdminMenu::where('key', 'ADMIN_SHOP_PAYMENT')->first();
if ($checkIdBlockPayment && !AdminMenu::where('uri', 'admin::paypal-express')->exists()) {
$menu = [
[
'parent_id' => $checkIdBlockPayment->id,
'sort' => 30,
'title' => 'Paypal Express',
'icon' => 'fas fa-user',
'uri' => 'admin::paypal-express',
'key' => null,
'type' => 0
]
];
AdminMenu::insertOrIgnore($menu);
}
}
return $return;
}
/**
* The credential rows to seed at GLOBAL (empty; secrets flagged security = 1).
*
* @return array<int, array<string, mixed>>
*
* @aidlc-unit plugin-paypal-express
* @aidlc-story US-paypal-express-per-store-credentials
* @aidlc-adr paypal-express_per-store-credentials
*/
private function credentialSeed(): array
{
$keys = [
'sandbox' => ['default' => '1', 'secret' => false],
'client_id_sandbox' => ['default' => '', 'secret' => false],
'client_secret_sandbox' => ['default' => '', 'secret' => true],
'client_id_live' => ['default' => '', 'secret' => false],
'client_secret_live' => ['default' => '', 'secret' => true],
'webhook_id' => ['default' => '', 'secret' => false],
];
$rows = [];
foreach ($keys as $key => $meta) {
$rows[] = [
'group' => $this->configGroup,
'code' => $this->configKey.'_config',
'key' => $this->configKey.'_'.$key,
'sort' => self::SORT[$key],
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => $meta['default'],
'security' => $meta['secret'] ? 1 : 0,
'detail' => $this->appPath.'::lang.'.$key,
];
}
return $rows;
}
/**
* Upgrade hook: on installs made before 3.1 the credentials lived only in .env.
* Seed the credential rows if missing, then import any real value still set in .env
* into the GLOBAL row (secrets encrypted at rest via setConfigValue), so an existing
* site keeps working without re-entering credentials. From 3.1 the runtime no longer
* reads .env (config.php dropped env()), so this reads env() DIRECTLY rather than
* through config(). Idempotent: only fills a missing/empty row, never overwrites a
* value the admin already saved, and never deletes .env.
*
* Best-effort: env() returns nothing when the site runs `config:cache` (Laravel skips
* loading .env then); such a site re-enters credentials in admin. .env is left intact.
*
* @param string|null $fromVersion Version installed before this update.
* @return array{error:int,msg:string}
*
* @aidlc-unit plugin-paypal-express
* @aidlc-story US-paypal-express-per-store-credentials
* @aidlc-adr paypal-express_per-store-credentials
*/
public function update(?string $fromVersion = null)
{
try {
foreach ($this->credentialSeed() as $row) {
$existing = AdminConfig::where('group', $this->configGroup)
->where('key', $row['key'])
->where('store_id', GP247_STORE_ID_GLOBAL)
->first();
// Seed the row if a pre-3.1 install never had it.
if ($existing === null) {
AdminConfig::insert($row);
$existing = AdminConfig::where('group', $this->configGroup)
->where('key', $row['key'])
->where('store_id', GP247_STORE_ID_GLOBAL)
->first();
}
// Import from .env only when the DB row is still empty (never clobber a saved value).
if ($existing !== null && (string) $existing->getRawOriginal('value') === '') {
$shortKey = substr($row['key'], strlen($this->configKey.'_'));
$envValue = env('PAYPAL_'.strtoupper($shortKey));
if ($envValue !== null && (string) $envValue !== '') {
AdminConfig::setConfigValue(
$this->configGroup,
$row['key'],
GP247_STORE_ID_GLOBAL,
(string) $envValue,
(int) $row['security']
);
}
}
}
self::convergeSort();
$return = ['error' => 0, 'msg' => ''];
} catch (\Throwable $e) {
$return = ['error' => 1, 'msg' => $e->getMessage()];
}
return $return;
}
/**
* Give every setting row (all stores) its display position from SORT. Idempotent and
* value-free: only `sort` is written, and only where it differs.
*
* @return void
*
* @aidlc-unit plugin-paypal-express
* @aidlc-story US-paypal-express-per-store-credentials
*/
public static function convergeSort(): void
{
foreach (self::SORT as $key => $sort) {
AdminConfig::where('group', 'Plugins')
->where('key', 'PaypalExpress_'.$key)
->where('sort', '!=', $sort)
->update(['sort' => $sort]);
}
}
public function uninstall()
{
//Please delete all values inserted in the installation step
try {
// WHY group + key prefix: per-store overrides saved from the admin screen carry
// `code = Plugins` (core's setConfigValue), so deleting by `code` alone left the
// store rows - encrypted secrets included - behind after uninstall.
(new AdminConfig)
->where('group', $this->configGroup)
->where(function ($query) {
$query->where('key', $this->configKey)
->orWhere('key', 'like', $this->configKey.'\_%');
})
->delete();
//Admin config home
AdminHome::where('extension', $this->appPath)->delete();
(new ExtensionModel)->uninstallExtension();
$return = ['error' => 0, 'msg' => gp247_language_render('admin.extension.uninstall_success')];
} catch (\Throwable $e) {
$return = ['error' => 1, 'msg' => $e->getMessage()];
}
//Delete menu
AdminMenu::where('uri', 'admin::paypal-express')->delete();
return $return;
}
public function enable()
{
$process = (new AdminConfig)
->where('group', $this->configGroup)
->where('key', $this->configKey)
->update(['value' => self::ON]);
//Admin config home
AdminHome::where('extension', $this->appPath)->update(['status' => 1]);
if (!$process) {
$return = ['error' => 1, 'msg' => gp247_language_render('admin.extension.action_error', ['action' => 'Enable'])];
}
$return = ['error' => 0, 'msg' => gp247_language_render('admin.extension.enable_success')];
return $return;
}
public function disable()
{
$return = ['error' => 0, 'msg' => ''];
$process = (new AdminConfig)
->where('group', $this->configGroup)
->where('key', $this->configKey)
->update(['value' => self::OFF]);
if (!$process) {
$return = ['error' => 1, 'msg' => 'Error disable'];
}
//Admin config home
AdminHome::where('extension', $this->appPath)->update(['status' => 0]);
return $return;
}
// Remove setup for store
public function removeStore($storeId = null)
{
// code here
}
// Setup for store
public function setupStore($storeId = null)
{
// code here
}
// Process when click button plugin in admin
public function clickApp()
{
return redirect(gp247_route_admin('admin_paypal-express.index'));
}
/**
* Get info plugin
*
* @return [type] [return description]
*/
public function getInfo()
{
$arrData = [
'title' => $this->title,
'key' => $this->configKey,
'code' => $this->configCode,
'image' => $this->image,
'permission' => self::ALLOW,
'version' => $this->version,
'auth' => $this->auth,
'link' => $this->link,
'value' => 0, // this return need for plugin shipping
'appPath' => $this->appPath
];
return $arrData;
}
}