From d7a36c61b2ba64804c9667745813011fca0d4739 Mon Sep 17 00:00:00 2001 From: IceToast Date: Sat, 4 Mar 2023 20:28:27 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Migrate=20Extensions=20from?= =?UTF-8?q?=20old=20to=20new=20settings=20system?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PaymentGateways/PayPal/index.php | 13 ++++++++-- .../PaymentGateways/Stripe/config.php | 2 -- .../PaymentGateways/Stripe/index.php | 13 ++++++---- app/Helpers/ExtensionHelper.php | 26 ++++++++++++++++++- .../Controllers/Admin/PaymentController.php | 5 +++- .../Controllers/Admin/SettingsController.php | 11 ++++---- .../views/admin/settings/index.blade.php | 4 ++- 7 files changed, 57 insertions(+), 17 deletions(-) diff --git a/app/Extensions/PaymentGateways/PayPal/index.php b/app/Extensions/PaymentGateways/PayPal/index.php index 648913ae..181ad252 100644 --- a/app/Extensions/PaymentGateways/PayPal/index.php +++ b/app/Extensions/PaymentGateways/PayPal/index.php @@ -2,6 +2,7 @@ use App\Events\PaymentEvent; use App\Events\UserUpdateCreditsEvent; +use App\Extensions\PaymentGateways\PayPal\PayPalSettings; use App\Models\PartnerDiscount; use App\Models\Payment; use App\Models\ShopProduct; @@ -25,6 +26,8 @@ use PayPalHttp\HttpException; */ function PaypalPay(Request $request) { + $settings = new PayPalSettings(); + /** @var User $user */ $user = Auth::user(); $shopProduct = ShopProduct::findOrFail($request->shopProduct); @@ -111,6 +114,8 @@ function PaypalPay(Request $request) */ function PaypalSuccess(Request $laravelRequest) { + $settings = new PayPalSettings(); + $user = Auth::user(); $user = User::findOrFail($user->id); @@ -165,6 +170,8 @@ function PaypalSuccess(Request $laravelRequest) */ function getPayPalClient() { + $settings = new PayPalSettings(); + $environment = env('APP_ENV') == 'local' ? new SandboxEnvironment(getPaypalClientId(), getPaypalClientSecret()) : new ProductionEnvironment(getPaypalClientId(), getPaypalClientSecret()); @@ -175,12 +182,14 @@ function getPayPalClient() */ function getPaypalClientId() { - return env('APP_ENV') == 'local' ? config("SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID") : config("SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID"); + $settings = new PayPalSettings(); + return env('APP_ENV') == 'local' ? $settings->sandbox_client_id : $settings->client_id; } /** * @return string */ function getPaypalClientSecret() { - return env('APP_ENV') == 'local' ? config("SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET") : config("SETTINGS::PAYMENTS:PAYPAL:SECRET"); + $settings = new PayPalSettings(); + return env('APP_ENV') == 'local' ? $settings->sandbox_client_secret : $settings->client_secret; } diff --git a/app/Extensions/PaymentGateways/Stripe/config.php b/app/Extensions/PaymentGateways/Stripe/config.php index cac2547a..92a43b8e 100644 --- a/app/Extensions/PaymentGateways/Stripe/config.php +++ b/app/Extensions/PaymentGateways/Stripe/config.php @@ -6,10 +6,8 @@ function getConfig() { return [ "name" => "Stripe", - "description" => "Stripe payment gateway", "RoutesIgnoreCsrf" => [ "payment/StripeWebhooks", ], - "enabled" => config('SETTINGS::PAYMENTS:STRIPE:SECRET') && config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET') || config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET') && config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET') && env("APP_ENV") === "local", ]; } diff --git a/app/Extensions/PaymentGateways/Stripe/index.php b/app/Extensions/PaymentGateways/Stripe/index.php index 56e898c2..aaf67f3a 100644 --- a/app/Extensions/PaymentGateways/Stripe/index.php +++ b/app/Extensions/PaymentGateways/Stripe/index.php @@ -2,6 +2,7 @@ use App\Events\PaymentEvent; use App\Events\UserUpdateCreditsEvent; +use App\Extensions\PaymentGateways\Stripe\StripeSettings; use App\Models\PartnerDiscount; use App\Models\Payment; use App\Models\ShopProduct; @@ -79,7 +80,6 @@ function StripePay(Request $request) ], 'mode' => 'payment', - 'payment_method_types' => str_getcsv(config('SETTINGS::PAYMENTS:STRIPE:METHODS')), 'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id]) . '&session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => route('payment.Cancel'), 'payment_intent_data' => [ @@ -244,9 +244,11 @@ function getStripeClient() */ function getStripeSecret() { + $settings = new StripeSettings(); + return env('APP_ENV') == 'local' - ? config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET') - : config('SETTINGS::PAYMENTS:STRIPE:SECRET'); + ? $settings->test_secret_key + : $settings->secret_key; } /** @@ -254,9 +256,10 @@ function getStripeSecret() */ function getStripeEndpointSecret() { + $settings = new StripeSettings(); return env('APP_ENV') == 'local' - ? config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET') - : config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET'); + ? $settings->test_endpoint_secret + : $settings->endpoint_secret; } /** * @param $amount diff --git a/app/Helpers/ExtensionHelper.php b/app/Helpers/ExtensionHelper.php index bce70f99..6844268b 100644 --- a/app/Helpers/ExtensionHelper.php +++ b/app/Helpers/ExtensionHelper.php @@ -107,7 +107,7 @@ class ExtensionHelper * Summary of getAllExtensionSettings * @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings */ - public static function getAllExtensionSettings() + public static function getAllExtensionSettingsClasses() { $extensions = ExtensionHelper::getAllExtensions(); @@ -132,4 +132,28 @@ class ExtensionHelper return $settings; } + + public static function getExtensionSettings(string $extensionName) + { + $extensions = ExtensionHelper::getAllExtensions(); + + // find the setting file of the extension and return an instance of it + foreach ($extensions as $extension) { + if (!(basename($extension) == $extensionName)) { + continue; + } + + $extensionName = basename($extension); + $settingFile = $extension . '/' . $extensionName . 'Settings.php'; + if (file_exists($settingFile)) { + // remove the base path from the setting file path to get the namespace + + $settingFile = str_replace(app_path() . '/', '', $settingFile); + $settingFile = str_replace('.php', '', $settingFile); + $settingFile = str_replace('/', '\\', $settingFile); + $settingFile = 'App\\' . $settingFile; + return new $settingFile(); + } + } + } } diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index 65b534f8..25d2c9ce 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -51,7 +51,10 @@ class PaymentController extends Controller // build a paymentgateways array that contains the routes for the payment gateways and the image path for the payment gateway which lays in public/images/Extensions/PaymentGateways with the extensionname in lowercase foreach ($extensions as $extension) { $extensionName = basename($extension); - if (!ExtensionHelper::getExtensionConfig($extensionName, 'enabled')) continue; // skip if not enabled + + $extensionSettings = ExtensionHelper::getExtensionSettings($extensionName); + if ($extensionSettings->enabled == false) continue; + $payment = new \stdClass(); $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name'); diff --git a/app/Http/Controllers/Admin/SettingsController.php b/app/Http/Controllers/Admin/SettingsController.php index d746d9de..456166bd 100644 --- a/app/Http/Controllers/Admin/SettingsController.php +++ b/app/Http/Controllers/Admin/SettingsController.php @@ -36,7 +36,7 @@ class SettingsController extends Controller $settings_classes[] = 'App\\Settings\\' . str_replace('.php', '', $app_setting); } // get all extension settings - $settings_files = array_merge($settings_classes, ExtensionHelper::getAllExtensionSettings()); + $settings_files = array_merge($settings_classes, ExtensionHelper::getAllExtensionSettingsClasses()); foreach ($settings_files as $file) { @@ -70,6 +70,7 @@ class SettingsController extends Controller if (isset($optionInputData['category_icon'])) { $optionsData['category_icon'] = $optionInputData['category_icon']; } + $optionsData['settings_class'] = $className; $settings[str_replace('Settings', '', class_basename($className))] = $optionsData; } @@ -93,10 +94,10 @@ class SettingsController extends Controller public function update(Request $request) { $category = request()->get('category'); + $settings_class = request()->get('settings_class'); - $className = 'App\\Settings\\' . $category . 'Settings'; - if (method_exists($className, 'getValidations')) { - $validations = $className::getValidations(); + if (method_exists($settings_class, 'getValidations')) { + $validations = $settings_class::getValidations(); } else { $validations = []; } @@ -107,7 +108,7 @@ class SettingsController extends Controller return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput(); } - $settingsClass = new $className(); + $settingsClass = new $settings_class(); foreach ($settingsClass->toArray() as $key => $value) { switch (gettype($value)) { diff --git a/themes/default/views/admin/settings/index.blade.php b/themes/default/views/admin/settings/index.blade.php index 12af0aab..8b0c3612 100644 --- a/themes/default/views/admin/settings/index.blade.php +++ b/themes/default/views/admin/settings/index.blade.php @@ -71,10 +71,12 @@
@csrf @method('POST') + @foreach ($options as $key => $value) - @if ($key == 'category_icon') + @if ($key == 'category_icon' || $key == 'settings_class') @continue @endif