-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppConfig.php
More file actions
260 lines (234 loc) · 8.85 KB
/
Copy pathAppConfig.php
File metadata and controls
260 lines (234 loc) · 8.85 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
<?php
/**
* Plugin format 2.1
*/
#App\GP247\Plugins\StripePayment\AppConfig.php
namespace App\GP247\Plugins\StripePayment;
use GP247\Core\Models\AdminConfig;
use GP247\Core\Models\AdminHome;
use GP247\Core\Models\AdminMenu;
use GP247\Core\ExtensionConfigDefault;
/**
* Stripe Payment plugin lifecycle.
*
* Every setting lives in admin_config (group "Plugins", code "StripePayment_config"), so a
* 1-click update — which replaces the plugin files — keeps the site owner's keys. The plugin
* owns no table: payments and refunds go to the shop's order ledger.
*
* @aidlc-unit plugin-stripe-payment
* @aidlc-story US-stripe-payment-per-store-credentials
*/
class AppConfig extends ExtensionConfigDefault
{
private const MENU_URI = 'admin::stripe-payment';
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) {
return ['error' => 1, 'msg' => gp247_language_render('admin.extension.plugin_exist')];
}
try {
AdminConfig::insert($this->seedRows());
$this->ensureMenu();
$return = ['error' => 0, 'msg' => gp247_language_render('admin.extension.install_success')];
} catch (\Throwable $e) {
$return = ['error' => 1, 'msg' => $e->getMessage()];
}
return $return;
}
/**
* Converge an existing install: add any setting row a later version introduced, never
* touch a value the site owner saved. Idempotent — safe on every update.
*
* @param string|null $fromVersion Version installed before this update.
* @return array{error:int,msg:string}
*/
public function update(?string $fromVersion = null)
{
try {
foreach ($this->seedRows() as $row) {
$exists = AdminConfig::where('group', $row['group'])
->where('key', $row['key'])
->where('store_id', (string) $row['store_id'])
->exists();
if (!$exists) {
AdminConfig::insert($row);
}
}
$this->ensureMenu();
return ['error' => 0, 'msg' => ''];
} catch (\Throwable $e) {
return ['error' => 1, 'msg' => $e->getMessage()];
}
}
public function uninstall()
{
// Only the plugin's own settings and menu. Payments recorded through Stripe stay in
// the shop's order ledger — they are the store's accounting, not plugin data.
try {
// By key prefix as well as by code: a store override written outside the
// settings screen can carry another `code`, and a surviving secret row would
// silently come back on the next install.
(new AdminConfig)
->where('group', $this->configGroup)
->where(function ($query) {
$query->where('key', $this->configKey)
->orWhere('key', 'like', $this->configKey.'\_%')
->orWhere('code', $this->configKey.'_config');
})
->delete();
AdminHome::where('extension', $this->appPath)->delete();
AdminMenu::where('uri', self::MENU_URI)->delete();
$return = ['error' => 0, 'msg' => gp247_language_render('admin.extension.uninstall_success')];
} catch (\Throwable $e) {
$return = ['error' => 1, 'msg' => $e->getMessage()];
}
return $return;
}
public function enable()
{
$process = (new AdminConfig)
->where('group', $this->configGroup)
->where('key', $this->configKey)
->update(['value' => self::ON]);
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')];
}
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' => gp247_language_render('admin.extension.action_error', ['action' => 'Disable'])];
}
AdminHome::where('extension', $this->appPath)->update(['status' => 0]);
return $return;
}
public function removeStore($storeId = null)
{
// Store overrides are plain admin_config rows handled by core.
}
public function setupStore($storeId = null)
{
// Nothing to provision: a new store inherits the GLOBAL Stripe settings.
}
public function clickApp()
{
return redirect(gp247_route_admin('admin_stripe-payment.index'));
}
/**
* Get info plugin
*
* @return array<string, mixed>
*/
public function getInfo()
{
return [
'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,
'appPath' => $this->appPath,
];
}
/**
* Rows seeded at GLOBAL on install (and re-added by update() when missing).
*
* WHY every row carries the same columns, `security` included: AdminConfig::insert()
* takes its column list from the first row, and a mismatch makes MySQL reject the whole
* batch. Secret rows are flagged security = 1 so core encrypts them at rest.
*
* There is deliberately no "payment status" setting: the payment status of an order is
* derived from the money in its ledger.
*
* @return array<int, array<string, mixed>>
*/
private function seedRows(): array
{
$rows = [[
'group' => $this->configGroup,
'code' => $this->configCode,
'key' => $this->configKey,
'sort' => 0,
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => self::ON,
'security' => 0,
'detail' => $this->appPath.'::lang.title',
]];
$settings = [
'test_mode' => ['1', 0],
'secret_key_test' => ['', 1],
'secret_key_live' => ['', 1],
'webhook_secret_test' => ['', 1],
'webhook_secret_live' => ['', 1],
'order_status_success' => ['2', 0],
'order_status_refunded' => ['7', 0],
];
$sort = 0;
foreach ($settings as $key => [$default, $security]) {
$rows[] = [
'group' => $this->configGroup,
'code' => $this->configKey.'_config',
'key' => $this->configKey.'_'.$key,
'sort' => ++$sort,
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => $default,
'security' => $security,
'detail' => $this->appPath.'::lang.'.$key,
];
}
return $rows;
}
/**
* Menu entry under the shop's Payment block (skipped when the block is absent).
*/
private function ensureMenu(): void
{
$paymentBlock = AdminMenu::where('key', 'ADMIN_SHOP_PAYMENT')->first();
if ($paymentBlock && !AdminMenu::where('uri', self::MENU_URI)->exists()) {
AdminMenu::insert([
'parent_id' => $paymentBlock->id,
'sort' => 40,
'title' => 'Stripe',
'icon' => 'fas fa-credit-card',
'uri' => self::MENU_URI,
'key' => null,
'type' => 0,
]);
}
}
}