feat: Migrate Extensions from old to new settings system

This commit is contained in:
IceToast 2023-03-04 20:28:27 +01:00 committed by IceToast
parent 3cba1c60f8
commit d7a36c61b2
7 changed files with 57 additions and 17 deletions

View file

@ -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;
}

View file

@ -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",
];
}

View file

@ -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

View file

@ -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();
}
}
}
}

View file

@ -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');

View file

@ -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)) {

View file

@ -71,10 +71,12 @@
<form action="{{ route('admin.settings.update') }}" method="POST">
@csrf
@method('POST')
<input type="hidden" name="settings_class"
value="{{ $options['settings_class'] }}">
<input type="hidden" name="category" value="{{ $category }}">
@foreach ($options as $key => $value)
@if ($key == 'category_icon')
@if ($key == 'category_icon' || $key == 'settings_class')
@continue
@endif
<div class="row">