diff --git a/app/Extensions/PaymentGateways/PayPal/PayPalSettings.php b/app/Extensions/PaymentGateways/PayPal/PayPalSettings.php index 3413a451..1688c011 100644 --- a/app/Extensions/PaymentGateways/PayPal/PayPalSettings.php +++ b/app/Extensions/PaymentGateways/PayPal/PayPalSettings.php @@ -6,9 +6,11 @@ use Spatie\LaravelSettings\Settings; class PayPalSettings extends Settings { + public bool $enabled = false; public ?string $client_id; public ?string $client_secret; - + public ?string $sandbox_client_id; + public ?string $sandbox_client_secret; public static function group(): string { @@ -20,7 +22,9 @@ class PayPalSettings extends Settings { return [ 'client_id', - 'client_secret' + 'client_secret', + 'sandbox_client_id', + 'sandbox_client_secret' ]; } @@ -34,15 +38,30 @@ class PayPalSettings extends Settings return [ 'category_icon' => 'fas fa-dollar-sign', 'client_id' => [ - 'type' => 'text', + 'type' => 'string', 'label' => 'Client ID', 'description' => 'The Client ID of your PayPal App', ], 'client_secret' => [ - 'type' => 'text', + 'type' => 'string', 'label' => 'Client Secret', 'description' => 'The Client Secret of your PayPal App', - ] + ], + 'enabled' => [ + 'type' => 'boolean', + 'label' => 'Enabled', + 'description' => 'Enable this payment gateway', + ], + 'sandbox_client_id' => [ + 'type' => 'string', + 'label' => 'Sandbox Client ID', + 'description' => 'The Sandbox Client ID used when app_env = local', + ], + 'sandbox_client_secret' => [ + 'type' => 'string', + 'label' => 'Sandbox Client Secret', + 'description' => 'The Sandbox Client Secret used when app_env = local', + ], ]; } } diff --git a/app/Extensions/PaymentGateways/PayPal/config.php b/app/Extensions/PaymentGateways/PayPal/config.php index 6fa98a13..f47cccfa 100644 --- a/app/Extensions/PaymentGateways/PayPal/config.php +++ b/app/Extensions/PaymentGateways/PayPal/config.php @@ -6,8 +6,6 @@ function getConfig() { return [ "name" => "PayPal", - "description" => "PayPal payment gateway", "RoutesIgnoreCsrf" => [], - "enabled" => (config('SETTINGS::PAYMENTS:PAYPAL:SECRET') && config('SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID')) || (config('SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET') && config('SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID') && env("APP_ENV") === "local"), ]; } diff --git a/app/Extensions/PaymentGateways/PayPal/migrations/2023_03_04_135248_create_pay_pal_settings.php b/app/Extensions/PaymentGateways/PayPal/migrations/2023_03_04_135248_create_pay_pal_settings.php index 67d85524..5792abec 100644 --- a/app/Extensions/PaymentGateways/PayPal/migrations/2023_03_04_135248_create_pay_pal_settings.php +++ b/app/Extensions/PaymentGateways/PayPal/migrations/2023_03_04_135248_create_pay_pal_settings.php @@ -1,19 +1,100 @@ exists(); - $this->migrator->addEncrypted('paypal.client_id', "1234"); - $this->migrator->addEncrypted('paypal.client_secret', "123456"); + + $this->migrator->addEncrypted('paypal.client_id', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID') : null); + $this->migrator->addEncrypted('paypal.client_secret', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:PAYPAL:SECRET') : null); + $this->migrator->addEncrypted('paypal.sandbox_client_id', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID') : null); + $this->migrator->addEncrypted('paypal.sandbox_client_secret', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET') : null); + $this->migrator->add('paypal.enabled', false); } public function down(): void { + DB::table('settings_old')->insert([ + [ + 'key' => 'SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID', + 'value' => $this->getNewValue('client_id'), + 'type' => 'string', + 'description' => 'The Client ID of your PayPal App' + ], + [ + 'key' => 'SETTINGS::PAYMENTS:PAYPAL:SECRET', + 'value' => $this->getNewValue('client_secret'), + 'type' => 'string', + 'description' => 'The Client Secret of your PayPal App' + ], + [ + 'key' => 'SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID', + 'value' => $this->getNewValue('sandbox_client_id'), + 'type' => 'string', + 'description' => 'The Sandbox Client ID of your PayPal App' + ], + [ + 'key' => 'SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET', + 'value' => $this->getNewValue('sandbox_client_secret'), + 'type' => 'string', + 'description' => 'The Sandbox Client Secret of your PayPal App' + ] + ]); + + $this->migrator->delete('paypal.client_id'); $this->migrator->delete('paypal.client_secret'); + $this->migrator->delete('paypal.enabled'); + $this->migrator->delete('paypal.sandbox_client_id'); + $this->migrator->delete('paypal.sandbox_client_secret'); + } + + public function getNewValue(string $name) + { + $new_value = DB::table('settings')->where([['group', '=', 'paypal'], ['name', '=', $name]])->get(['payload'])->first(); + + // Some keys returns '""' as a value. + if ($new_value->payload === '""') { + return null; + } + + // remove the quotes from the string + if (substr($new_value->payload, 0, 1) === '"' && substr($new_value->payload, -1) === '"') { + return substr($new_value->payload, 1, -1); + } + + return $new_value->payload; + } + + public function getOldValue(string $key) + { + // Always get the first value of the key. + $old_value = DB::table('settings_old')->where('key', '=', $key)->get(['value', 'type'])->first(); + + // Handle the old values to return without it being a string in all cases. + if ($old_value->type === "string" || $old_value->type === "text") { + if (is_null($old_value->value)) { + return ''; + } + + // Some values have the type string, but their values are boolean. + if ($old_value->value === "false" || $old_value->value === "true") { + return filter_var($old_value->value, FILTER_VALIDATE_BOOL); + } + + return $old_value->value; + } + + if ($old_value->type === "boolean") { + return filter_var($old_value->value, FILTER_VALIDATE_BOOL); + } + + return filter_var($old_value->value, FILTER_VALIDATE_INT); } } diff --git a/app/Extensions/PaymentGateways/Stripe/StripeSettings.php b/app/Extensions/PaymentGateways/Stripe/StripeSettings.php new file mode 100644 index 00000000..c7a2d7bd --- /dev/null +++ b/app/Extensions/PaymentGateways/Stripe/StripeSettings.php @@ -0,0 +1,63 @@ + 'fas fa-dollar-sign', + 'secret_key' => [ + 'type' => 'string', + 'label' => 'Secret Key', + 'description' => 'The Secret Key of your Stripe App', + ], + 'endpoint_secret' => [ + 'type' => 'string', + 'label' => 'Endpoint Secret', + 'description' => 'The Endpoint Secret of your Stripe App', + ], + 'test_secret_key' => [ + 'type' => 'string', + 'label' => 'Test Secret Key', + 'description' => 'The Test Secret Key used when app_env = local', + ], + 'test_endpoint_secret' => [ + 'type' => 'string', + 'label' => 'Test Endpoint Secret', + 'description' => 'The Test Endpoint Secret used when app_env = local', + ], + 'enabled' => [ + 'type' => 'boolean', + 'label' => 'Enabled', + 'description' => 'Enable this payment gateway', + ] + ]; + } +} diff --git a/app/Extensions/PaymentGateways/Stripe/migrations/2023_03_04_181917_create_stripe_settings.php b/app/Extensions/PaymentGateways/Stripe/migrations/2023_03_04_181917_create_stripe_settings.php new file mode 100644 index 00000000..a8145a7c --- /dev/null +++ b/app/Extensions/PaymentGateways/Stripe/migrations/2023_03_04_181917_create_stripe_settings.php @@ -0,0 +1,98 @@ +exists(); + + $this->migrator->addEncrypted('stripe.secret_key', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:STRIPE:SECRET') : null); + $this->migrator->addEncrypted('stripe.endpoint_secret', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET') : null); + $this->migrator->addEncrypted('stripe.test_secret_key', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET') : null); + $this->migrator->addEncrypted('stripe.test_endpoint_secret', $table_exists ? $this->getOldValue('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET') : null); + $this->migrator->add('stripe.enabled', false); + } + + public function down(): void + { + DB::table('settings_old')->insert([ + [ + 'key' => 'SETTINGS::PAYMENTS:STRIPE:SECRET', + 'value' => $this->getNewValue('secret_key'), + 'type' => 'string', + 'description' => 'The Secret Key of your Stripe App' + ], + [ + 'key' => 'SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET', + 'value' => $this->getNewValue('endpoint_secret'), + 'type' => 'string', + 'description' => 'The Endpoint Secret of your Stripe App' + + ], + [ + 'key' => 'SETTINGS::PAYMENTS:STRIPE:TEST_SECRET', + 'value' => $this->getNewValue('test_secret_key'), + 'type' => 'string', + 'description' => 'The Test Secret Key of your Stripe App' + ], + [ + 'key' => 'SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET', + 'value' => $this->getNewValue('test_endpoint_secret'), + 'type' => 'string', + 'description' => 'The Test Endpoint Secret of your Stripe App' + ] + ]); + + $this->migrator->delete('stripe.secret_key'); + $this->migrator->delete('stripe.endpoint_secret'); + $this->migrator->delete('stripe.enabled'); + $this->migrator->delete('stripe.test_secret_key'); + $this->migrator->delete('stripe.test_endpoint_secret'); + } + + public function getNewValue(string $name) + { + $new_value = DB::table('settings')->where([['group', '=', 'stripe'], ['name', '=', $name]])->get(['payload'])->first(); + + // Some keys returns '""' as a value. + if ($new_value->payload === '""') { + return null; + } + + // remove the quotes from the string + if (substr($new_value->payload, 0, 1) === '"' && substr($new_value->payload, -1) === '"') { + return substr($new_value->payload, 1, -1); + } + + return $new_value->payload; + } + + public function getOldValue(string $key) + { + // Always get the first value of the key. + $old_value = DB::table('settings_old')->where('key', '=', $key)->get(['value', 'type'])->first(); + + // Handle the old values to return without it being a string in all cases. + if ($old_value->type === "string" || $old_value->type === "text") { + if (is_null($old_value->value)) { + return ''; + } + + // Some values have the type string, but their values are boolean. + if ($old_value->value === "false" || $old_value->value === "true") { + return filter_var($old_value->value, FILTER_VALIDATE_BOOL); + } + + return $old_value->value; + } + + if ($old_value->type === "boolean") { + return filter_var($old_value->value, FILTER_VALIDATE_BOOL); + } + + return filter_var($old_value->value, FILTER_VALIDATE_INT); + } +} diff --git a/database/migrations/2023_02_13_150022_delete_old_settings_table.php b/database/migrations/2023_03_15_150022_delete_old_settings_table.php similarity index 95% rename from database/migrations/2023_02_13_150022_delete_old_settings_table.php rename to database/migrations/2023_03_15_150022_delete_old_settings_table.php index 81c069c3..11f107cc 100644 --- a/database/migrations/2023_02_13_150022_delete_old_settings_table.php +++ b/database/migrations/2023_03_15_150022_delete_old_settings_table.php @@ -1,34 +1,34 @@ -string('key', 191)->primary(); - $table->text('value')->nullable(); - $table->string('type'); - $table->longText('description')->nullable(); - $table->timestamps(); - }); - } -}; +string('key', 191)->primary(); + $table->text('value')->nullable(); + $table->string('type'); + $table->longText('description')->nullable(); + $table->timestamps(); + }); + } +};