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\PaymentEvent;
use App\Events\UserUpdateCreditsEvent; use App\Events\UserUpdateCreditsEvent;
use App\Extensions\PaymentGateways\PayPal\PayPalSettings;
use App\Models\PartnerDiscount; use App\Models\PartnerDiscount;
use App\Models\Payment; use App\Models\Payment;
use App\Models\ShopProduct; use App\Models\ShopProduct;
@ -25,6 +26,8 @@ use PayPalHttp\HttpException;
*/ */
function PaypalPay(Request $request) function PaypalPay(Request $request)
{ {
$settings = new PayPalSettings();
/** @var User $user */ /** @var User $user */
$user = Auth::user(); $user = Auth::user();
$shopProduct = ShopProduct::findOrFail($request->shopProduct); $shopProduct = ShopProduct::findOrFail($request->shopProduct);
@ -111,6 +114,8 @@ function PaypalPay(Request $request)
*/ */
function PaypalSuccess(Request $laravelRequest) function PaypalSuccess(Request $laravelRequest)
{ {
$settings = new PayPalSettings();
$user = Auth::user(); $user = Auth::user();
$user = User::findOrFail($user->id); $user = User::findOrFail($user->id);
@ -165,6 +170,8 @@ function PaypalSuccess(Request $laravelRequest)
*/ */
function getPayPalClient() function getPayPalClient()
{ {
$settings = new PayPalSettings();
$environment = env('APP_ENV') == 'local' $environment = env('APP_ENV') == 'local'
? new SandboxEnvironment(getPaypalClientId(), getPaypalClientSecret()) ? new SandboxEnvironment(getPaypalClientId(), getPaypalClientSecret())
: new ProductionEnvironment(getPaypalClientId(), getPaypalClientSecret()); : new ProductionEnvironment(getPaypalClientId(), getPaypalClientSecret());
@ -175,12 +182,14 @@ function getPayPalClient()
*/ */
function getPaypalClientId() 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 * @return string
*/ */
function getPaypalClientSecret() 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 [ return [
"name" => "Stripe", "name" => "Stripe",
"description" => "Stripe payment gateway",
"RoutesIgnoreCsrf" => [ "RoutesIgnoreCsrf" => [
"payment/StripeWebhooks", "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\PaymentEvent;
use App\Events\UserUpdateCreditsEvent; use App\Events\UserUpdateCreditsEvent;
use App\Extensions\PaymentGateways\Stripe\StripeSettings;
use App\Models\PartnerDiscount; use App\Models\PartnerDiscount;
use App\Models\Payment; use App\Models\Payment;
use App\Models\ShopProduct; use App\Models\ShopProduct;
@ -79,7 +80,6 @@ function StripePay(Request $request)
], ],
'mode' => 'payment', '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}', 'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id]) . '&session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('payment.Cancel'), 'cancel_url' => route('payment.Cancel'),
'payment_intent_data' => [ 'payment_intent_data' => [
@ -244,9 +244,11 @@ function getStripeClient()
*/ */
function getStripeSecret() function getStripeSecret()
{ {
$settings = new StripeSettings();
return env('APP_ENV') == 'local' return env('APP_ENV') == 'local'
? config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET') ? $settings->test_secret_key
: config('SETTINGS::PAYMENTS:STRIPE:SECRET'); : $settings->secret_key;
} }
/** /**
@ -254,9 +256,10 @@ function getStripeSecret()
*/ */
function getStripeEndpointSecret() function getStripeEndpointSecret()
{ {
$settings = new StripeSettings();
return env('APP_ENV') == 'local' return env('APP_ENV') == 'local'
? config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET') ? $settings->test_endpoint_secret
: config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET'); : $settings->endpoint_secret;
} }
/** /**
* @param $amount * @param $amount

View file

@ -107,7 +107,7 @@ class ExtensionHelper
* Summary of getAllExtensionSettings * Summary of getAllExtensionSettings
* @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings * @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
*/ */
public static function getAllExtensionSettings() public static function getAllExtensionSettingsClasses()
{ {
$extensions = ExtensionHelper::getAllExtensions(); $extensions = ExtensionHelper::getAllExtensions();
@ -132,4 +132,28 @@ class ExtensionHelper
return $settings; 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 // 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) { foreach ($extensions as $extension) {
$extensionName = basename($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 = new \stdClass();
$payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name'); $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); $settings_classes[] = 'App\\Settings\\' . str_replace('.php', '', $app_setting);
} }
// get all extension settings // 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) { foreach ($settings_files as $file) {
@ -70,6 +70,7 @@ class SettingsController extends Controller
if (isset($optionInputData['category_icon'])) { if (isset($optionInputData['category_icon'])) {
$optionsData['category_icon'] = $optionInputData['category_icon']; $optionsData['category_icon'] = $optionInputData['category_icon'];
} }
$optionsData['settings_class'] = $className;
$settings[str_replace('Settings', '', class_basename($className))] = $optionsData; $settings[str_replace('Settings', '', class_basename($className))] = $optionsData;
} }
@ -93,10 +94,10 @@ class SettingsController extends Controller
public function update(Request $request) public function update(Request $request)
{ {
$category = request()->get('category'); $category = request()->get('category');
$settings_class = request()->get('settings_class');
$className = 'App\\Settings\\' . $category . 'Settings'; if (method_exists($settings_class, 'getValidations')) {
if (method_exists($className, 'getValidations')) { $validations = $settings_class::getValidations();
$validations = $className::getValidations();
} else { } else {
$validations = []; $validations = [];
} }
@ -107,7 +108,7 @@ class SettingsController extends Controller
return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput(); return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput();
} }
$settingsClass = new $className(); $settingsClass = new $settings_class();
foreach ($settingsClass->toArray() as $key => $value) { foreach ($settingsClass->toArray() as $key => $value) {
switch (gettype($value)) { switch (gettype($value)) {

View file

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