From 4dadd6143f3bc30ff3fd043156b602d38287a26f Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 2 Jul 2020 09:37:07 +0300 Subject: [PATCH] replace wc gateway settings with independent settings system --- modules.local/ppcp-button/services.php | 5 +- .../ppcp-button/src/Assets/SmartButton.php | 16 +- .../ppcp-onboarding/src/Environment.php | 2 +- modules.local/ppcp-onboarding/src/State.php | 2 +- modules.local/ppcp-wc-gateway/services.php | 292 ++++++++++++++++-- .../src/Gateway/ResetGateway.php | 92 ------ .../ppcp-wc-gateway/src/Gateway/WcGateway.php | 57 ++-- .../src/Settings/FullyOnboardedSettings.php | 111 ------- .../src/Settings/ProgressiveSettings.php | 36 --- .../ppcp-wc-gateway/src/Settings/Settings.php | 33 +- .../src/Settings/SettingsFields.php | 13 - .../src/Settings/SettingsListener.php | 83 +++++ .../src/Settings/SettingsRenderer.php | 84 +++++ .../src/Settings/SettingsTrait.php | 158 ---------- .../src/Settings/StartSettings.php | 35 --- .../ppcp-wc-gateway/src/WcGatewayModule.php | 30 +- 16 files changed, 514 insertions(+), 535 deletions(-) delete mode 100644 modules.local/ppcp-wc-gateway/src/Gateway/ResetGateway.php delete mode 100644 modules.local/ppcp-wc-gateway/src/Settings/FullyOnboardedSettings.php delete mode 100644 modules.local/ppcp-wc-gateway/src/Settings/ProgressiveSettings.php delete mode 100644 modules.local/ppcp-wc-gateway/src/Settings/SettingsFields.php create mode 100644 modules.local/ppcp-wc-gateway/src/Settings/SettingsListener.php create mode 100644 modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php delete mode 100644 modules.local/ppcp-wc-gateway/src/Settings/SettingsTrait.php delete mode 100644 modules.local/ppcp-wc-gateway/src/Settings/StartSettings.php diff --git a/modules.local/ppcp-button/services.php b/modules.local/ppcp-button/services.php index 492eb2391..35c63cda3 100644 --- a/modules.local/ppcp-button/services.php +++ b/modules.local/ppcp-button/services.php @@ -32,10 +32,9 @@ return [ /** * ToDo: Add production platform client Id. - * ToDo: We do not correctly detect sandbox mode when we use the current sandbox field switch in Settings. Bugfix needs to be fixed asap. */ return $env->currentEnvironmentIs(Environment::SANDBOX) ? - 'AQB97CzMsd58-It1vxbcDAGvMuXNCXRD9le_XUaMlHB_U7XsU9IiItBwGQOtZv9sEeD6xs2vlIrL4NiD' : 'AQB97CzMsd58-It1vxbcDAGvMuXNCXRD9le_XUaMlHB_U7XsU9IiItBwGQOtZv9sEeD6xs2vlIrL4NiD'; + 'AQB97CzMsd58-It1vxbcDAGvMuXNCXRD9le_XUaMlHB_U7XsU9IiItBwGQOtZv9sEeD6xs2vlIrL4NiD' : ''; }, 'button.smart-button' => static function (ContainerInterface $container): SmartButtonInterface { @@ -47,7 +46,7 @@ return [ return new DisabledSmartButton(); } $settings = $container->get('wcgateway.settings'); - if (!$settings->has('enabled') || ! wc_string_to_bool($settings->get('enabled'))) { + if (!$settings->has('enabled') || ! $settings->get('enabled')) { return new DisabledSmartButton(); } $payeeRepository = $container->get('api.repository.payee'); diff --git a/modules.local/ppcp-button/src/Assets/SmartButton.php b/modules.local/ppcp-button/src/Assets/SmartButton.php index 1ffd7622b..ba313d2a8 100644 --- a/modules.local/ppcp-button/src/Assets/SmartButton.php +++ b/modules.local/ppcp-button/src/Assets/SmartButton.php @@ -77,7 +77,7 @@ class SmartButton implements SmartButtonInterface }; $notEnabledOnCart = $this->settings->has('button_cart_enabled') && - !wc_string_to_bool($this->settings->get('button_cart_enabled')); + !$this->settings->get('button_cart_enabled'); if ( is_cart() && !$notEnabledOnCart @@ -91,7 +91,7 @@ class SmartButton implements SmartButtonInterface if ( is_cart() && $this->settings->has('dcc_cart_enabled') - && wc_string_to_bool($this->settings->get('dcc_cart_enabled')) + && $this->settings->get('dcc_cart_enabled') ) { add_action( 'woocommerce_proceed_to_checkout', @@ -101,7 +101,7 @@ class SmartButton implements SmartButtonInterface } $notEnabledOnProductPage = $this->settings->has('button_single_product_enabled') && - !wc_string_to_bool($this->settings->get('button_single_product_enabled')); + !$this->settings->get('button_single_product_enabled'); if ( is_product() && !$notEnabledOnProductPage @@ -115,7 +115,7 @@ class SmartButton implements SmartButtonInterface if ( is_product() && $this->settings->has('dcc_single_product_enabled') - && wc_string_to_bool($this->settings->get('dcc_single_product_enabled')) + && $this->settings->get('dcc_single_product_enabled') ) { add_action( 'woocommerce_single_product_summary', @@ -124,7 +124,7 @@ class SmartButton implements SmartButtonInterface ); } $notEnabledOnMiniCart = $this->settings->has('button_mini_cart_enabled') && - !wc_string_to_bool($this->settings->get('button_mini_cart_enabled')); + !$this->settings->get('button_mini_cart_enabled'); if ( ! $notEnabledOnMiniCart ) { @@ -138,7 +138,7 @@ class SmartButton implements SmartButtonInterface } if ( $this->settings->has('dcc_mini_cart_enabled') - && wc_string_to_bool($this->settings->get('dcc_mini_cart_enabled')) + && $this->settings->get('dcc_mini_cart_enabled') ) { add_action( 'woocommerce_widget_shopping_cart_after_buttons', @@ -155,7 +155,7 @@ class SmartButton implements SmartButtonInterface ); if ( $this->settings->has('dcc_checkout_enabled') - && wc_string_to_bool($this->settings->get('dcc_checkout_enabled')) + && $this->settings->get('dcc_checkout_enabled') ) { add_action( 'woocommerce_review_order_after_submit', @@ -316,7 +316,7 @@ class SmartButton implements SmartButtonInterface 'dcc_single_product_enabled', ]; foreach ($keys as $key) { - if ($this->settings->has($key) && wc_string_to_bool($this->settings->get($key))) { + if ($this->settings->has($key) && $this->settings->get($key)) { return true; } } diff --git a/modules.local/ppcp-onboarding/src/Environment.php b/modules.local/ppcp-onboarding/src/Environment.php index 8210590ff..f60c8be47 100644 --- a/modules.local/ppcp-onboarding/src/Environment.php +++ b/modules.local/ppcp-onboarding/src/Environment.php @@ -27,7 +27,7 @@ class Environment public function currentEnvironment() : string { return ( - $this->settings->has('sandbox_on') && wc_string_to_bool($this->settings->get('sandbox_on')) + $this->settings->has('sandbox_on') && $this->settings->get('sandbox_on') ) ? self::SANDBOX : self::PRODUCTION; } diff --git a/modules.local/ppcp-onboarding/src/State.php b/modules.local/ppcp-onboarding/src/State.php index 3980d8438..f30385f43 100644 --- a/modules.local/ppcp-onboarding/src/State.php +++ b/modules.local/ppcp-onboarding/src/State.php @@ -35,7 +35,7 @@ class State /** * Once we can fetch credentials we are completely onboarded. */ - if ($this->settings->has('client_id')) { + if ($this->settings->has('client_id') && $this->settings->get('client_id')) { $value = self::STATE_ONBOARDED; } return $value; diff --git a/modules.local/ppcp-wc-gateway/services.php b/modules.local/ppcp-wc-gateway/services.php index 625f630ae..a1f5fa014 100644 --- a/modules.local/ppcp-wc-gateway/services.php +++ b/modules.local/ppcp-wc-gateway/services.php @@ -20,32 +20,27 @@ use Inpsyde\PayPalCommerce\WcGateway\Settings\FullyOnboardedSettings; use Inpsyde\PayPalCommerce\WcGateway\Settings\ProgressiveSettings; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields; +use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsListener; +use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\StartSettings; return [ 'wcgateway.gateway.base' => static function (ContainerInterface $container): WcGatewayBase { return new WcGatewayBase(); }, - 'wcgateway.gateway.reset' => static function (ContainerInterface $container): ResetGateway { - return new ResetGateway( - $container->get('wcgateway.settings') - ); - }, 'wcgateway.gateway' => static function (ContainerInterface $container): WcGateway { $orderProcessor = $container->get('wcgateway.order-processor'); - $settingsFields = $container->get('wcgateway.settings.fields'); + $settingsRenderer = $container->get('wcgateway.settings.render'); $authorizedPayments = $container->get('wcgateway.processor.authorized-payments'); $notice = $container->get('wcgateway.notice.authorize-order-action'); $onboardingRender = $container->get('onboarding.render'); - $reset = $container->get('wcgateway.gateway.reset'); return new WcGateway( - $settingsFields, + $settingsRenderer, $orderProcessor, $authorizedPayments, $notice, - $onboardingRender, - $reset + $onboardingRender ); }, 'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways { @@ -65,19 +60,16 @@ return [ static function (ContainerInterface $container): AuthorizeOrderActionNotice { return new AuthorizeOrderActionNotice(); }, - 'wcgateway.settings.fields' => static function (ContainerInterface $container): SettingsFields { + 'wcgateway.settings.render' => static function (ContainerInterface $container): SettingsRenderer { + $settings = $container->get('wcgateway.settings'); $state = $container->get('onboarding.state'); - /** - * @var State $state - */ - if ($state->currentState() === State::STATE_START) { - return new StartSettings(); - } - $environment = $container->get('onboarding.environment'); - if ($state->currentState() === State::STATE_PROGRESSIVE) { - return new ProgressiveSettings($environment); - } - return new FullyOnboardedSettings($environment); + $fields = $container->get('wcgateway.settings.fields'); + return new SettingsRenderer($settings, $state, $fields); + }, + 'wcgateway.settings.listener' => static function (ContainerInterface $container): SettingsListener { + $settings = $container->get('wcgateway.settings'); + $fields = $container->get('wcgateway.settings.fields'); + return new SettingsListener($settings, $fields); }, 'wcgateway.order-processor' => static function (ContainerInterface $container): OrderProcessor { @@ -106,4 +98,260 @@ return [ $settings = $container->get('wcgateway.settings'); return new OrderTablePaymentStatusColumn($settings); }, + + 'wcgateway.settings.fields' => function(ContainerInterface $container) : array { + $settings = $container->get('wcgateway.settings'); + $sandboxText = $settings->has('sandbox_on') && $settings->get('sandbox_on') ? + __( + 'You are currently in the sandbox mode to test your installation. You can switch this, by clicking ', + 'woocommerce-paypal-commerce-gateway' + ) : __( + 'You are in live mode. This means, you can receive money into your account. You can switch this, by clicking ', + 'woocommerce-paypal-commerce-gateway' + ); + $sandboxText = sprintf( + $sandboxText, + 'save', + 'reset' + ); + + $merchantEmailText = sprintf( + __( + 'You are connected with your email address %1$s. + If you want to change this settings, please click ', + 'woocommerce-paypal-commerce-gateway' + ), + $settings->has('merchant_email') ? $settings->get('merchant_email') : '', + 'save', + 'reset' + ); + return [ + 'sandbox_on' => [ + 'title' => __('Sandbox', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('To test your Woocommerce installation, you can use the sandbox mode.', 'woocommerce-paypal-gateway'), + 'default' => 0, + 'screens' => [ + State::STATE_START, + ], + ], + 'sandbox_on_info' => [ + 'title' => __('Sandbox', 'woocommerce-paypal-gateway'), + 'type' => 'ppcp-text', + 'text' => $sandboxText, + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + 'hidden' => 'sandbox_on', + ], + 'merchant_email' => [ + 'title' => __('Email address', 'woocommerce-paypal-gateway'), + 'type' => 'text', + 'label' => __('The email address of your PayPal account.', 'woocommerce-paypal-gateway'), + 'default' => '', + 'screens' => [ + State::STATE_START, + ], + ], + 'merchant_email_info' => [ + 'title' => __('Email address', 'woocommerce-paypal-gateway'), + 'type' => 'ppcp-text', + 'text' => $merchantEmailText, + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + 'hidden' => 'merchant_email', + ], + 'title' => [ + 'title' => __('Title', 'woocommerce-paypal-gateway'), + 'type' => 'text', + 'description' => __( + 'This controls the title which the user sees during checkout.', + 'woocommerce-paypal-gateway' + ), + 'default' => __('PayPal', 'woocommerce-paypal-gateway'), + 'desc_tip' => true, + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'description' => [ + 'title' => __('Description', 'woocommerce-paypal-gateway'), + 'type' => 'text', + 'desc_tip' => true, + 'description' => __( + 'This controls the description which the user sees during checkout.', + 'woocommerce-paypal-gateway' + ), + 'default' => __( + 'Pay via PayPal; you can pay with your credit card if you don\'t have a PayPal account.', + 'woocommerce-paypal-gateway' + ), + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'button_single_product_enabled' => [ + 'title' => __('Buttons on Single Product', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Enable on Single Product', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'button_mini_cart_enabled' => [ + 'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Enable on Mini Cart', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'button_cart_enabled' => [ + 'title' => __('Buttons on Cart', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Enable on Cart', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'button_color' => [ + 'title' => __('Color', 'woocommerce-paypal-gateway'), + 'type' => 'select', + 'class' => ['wc-enhanced-select'], + 'default' => 'gold', + 'desc_tip' => true, + 'description' => __( + 'Controls the background color of the primary button. Use "Gold" to leverage PayPal\'s recognition and preference, or change it to match your site design or aesthetic.', + 'woocommerce-paypal-gateway' + ), + 'options' => [ + 'gold' => __('Gold (Recommended)', 'woocommerce-paypal-gateway'), + 'blue' => __('Blue', 'woocommerce-paypal-gateway'), + 'silver' => __('Silver', 'woocommerce-paypal-gateway'), + 'black' => __('Black', 'woocommerce-paypal-gateway'), + ], + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'button_shape' => [ + 'title' => __('Shape', 'woocommerce-paypal-gateway'), + 'type' => 'select', + 'class' => ['wc-enhanced-select'], + 'default' => 'rect', + 'desc_tip' => true, + 'description' => __( + 'The pill-shaped button\'s unique and powerful shape signifies PayPal in people\'s minds. Use the rectangular button as an alternative when pill-shaped buttons might pose design challenges.', + 'woocommerce-paypal-gateway' + ), + 'options' => [ + 'pill' => __('Pill', 'woocommerce-paypal-gateway'), + 'rect' => __('Rectangle', 'woocommerce-paypal-gateway'), + ], + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'disable_funding' => [ + 'title' => __('Disable funding sources', 'woocommerce-paypal-gateway'), + 'type' => 'ppcp-multiselect', + 'class' => ['wc-enhanced-select'], + 'default' => [], + 'desc_tip' => true, + 'description' => __( + 'By default all possible funding sources will be shown. You can disable some sources, if you wish.', + 'woocommerce-paypal-gateway' + ), + 'options' => [ + 'card' => _x('Credit or debit cards', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'credit' => _x('PayPal Credit', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'venmo' => _x('Venmo', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'sepa' => _x('SEPA-Lastschrift', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'bancontact' => _x('Bancontact', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'eps' => _x('eps', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'giropay' => _x('giropay', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'ideal' => _x('iDEAL', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'mybank' => _x('MyBank', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'p24' => _x('Przelewy24', 'Name of payment method', 'woocommerce-paypal-gateway'), + 'sofort' => _x('Sofort', 'Name of payment method', 'woocommerce-paypal-gateway'), + ], + 'screens' => [ + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ], + ], + 'dcc_cart_enabled' => [ + 'title' => __('Enable credit card on cart', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Allow your customers to pay with credit card directly in your cart.', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_ONBOARDED, + ], + ], + 'dcc_mini_cart_enabled' => [ + 'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Allow your customers to pay with credit card directly in your mini cart.', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_ONBOARDED, + ], + ], + 'dcc_checkout_enabled' => [ + 'title' => __('Enable credit card on checkout', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Allow your customers to pay with credit card in the checkout.', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_ONBOARDED, + ], + ], + 'dcc_single_product_enabled' => [ + 'title' => __('Enable credit card on products', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Allow your customers to pay with credit card instantly on the product page.', 'woocommerce-paypal-gateway'), + 'default' => 1, + 'screens' => [ + State::STATE_ONBOARDED, + ], + ], + 'disable_cards' => [ + 'title' => __('Disable specific credid cards', 'woocommerce-paypal-gateway'), + 'type' => 'ppcp-multiselect', + 'class' => ['wc-enhanced-select'], + 'default' => [], + 'desc_tip' => true, + 'description' => __( + 'By default all possible credit cards will be shown. You can disable some cards, if you wish.', + 'woocommerce-paypal-gateway' + ), + 'options' => [ + 'visa' => _x('Visa', 'Name of credit card', 'woocommerce-paypal-gateway'), + 'mastercard' => _x('Mastercard', 'Name of credit card', 'woocommerce-paypal-gateway'), + 'amex' => _x('American Express', 'Name of credit card', 'woocommerce-paypal-gateway'), + 'discover' => _x('Discover', 'Name of credit card', 'woocommerce-paypal-gateway'), + 'jcb' => _x('JCB', 'Name of credit card', 'woocommerce-paypal-gateway'), + 'elo' => _x('Elo', 'Name of credit card', 'woocommerce-paypal-gateway'), + 'hiper' => _x('Hiper', 'Name of credit card', 'woocommerce-paypal-gateway'), + ], + 'screens' => [ + State::STATE_ONBOARDED, + ], + ], + ]; + } ]; diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/ResetGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/ResetGateway.php deleted file mode 100644 index 19a89f05a..000000000 --- a/modules.local/ppcp-wc-gateway/src/Gateway/ResetGateway.php +++ /dev/null @@ -1,92 +0,0 @@ -settings = $settings; - } - - public function listen() : bool { - if (isset($_GET['ppcp-reset'])) { - return $this->reset(); - } - if (isset($_GET['ppcp-resetted'])) { - return $this->resetted(); - } - return false; - } - private function resetted() : bool { - add_filter( - Repository::NOTICES_FILTER, - function(array $notices) : array { - $notices[] = new Message( - __('Your PayPal settings have been resetted.', 'woocommerce-paypal-commerce-gateway'), - 'success' - ); - return $notices; - } - ); - - return true; - } - - private function reset() : bool { - - if ( - ! isset($_GET['nonce'] ) - || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['nonce'])), self::NONCE) - || ! current_user_can('manage_options') - ) { - return false; - } - - $this->settings->reset(); - $url = remove_query_arg([ - 'nonce', - 'ppcp-reset', - ]); - $url = add_query_arg([ - 'ppcp-resetted' => 1, - ], - $url - ); - - wp_redirect($url, 302); - exit; - } - - public function render() { - - $url = add_query_arg([ - 'ppcp-reset' => 1, - 'nonce' => wp_create_nonce(self::NONCE), - ]); - ?> - - - - - - - - - orderProcessor = $orderProcessor; $this->authorizedPayments = $authorizedPayments; $this->notice = $notice; - $this->settingsFields = $settingsFields; + $this->settingsRenderer = $settingsRenderer; $this->onboardingRenderer = $onboardingRenderer; - $this->resetGateway = $resetGateway; $this->method_title = __('PayPal Payments', 'woocommerce-paypal-gateway'); $this->method_description = __( @@ -74,7 +72,17 @@ class WcGateway extends WcGatewayBase public function init_form_fields() { - $this->form_fields = $this->settingsFields->fields(); + $this->form_fields = [ + 'enabled' => [ + 'title' => __('Enable/Disable', 'woocommerce-paypal-gateway'), + 'type' => 'checkbox', + 'label' => __('Enable PayPal Payments', 'woocommerce-paypal-gateway'), + 'default' => 'yes', + ], + 'ppcp' => [ + 'type' => 'ppcp', + ], + ]; } public function process_payment($orderId): ?array @@ -140,40 +148,11 @@ class WcGateway extends WcGatewayBase $this->notice->displayMessage($displayMessage); } - public function generate_ppcp_onboarding_html() : string + public function generate_ppcp_html() : string { ob_start(); - $this->onboardingRenderer->render(); - $content = ob_get_contents(); - ob_end_clean(); - return $content; - } - - public function generate_ppcp_reset_html() : string - { - - ob_start(); - $this->resetGateway->render(); - $content = ob_get_contents(); - ob_end_clean(); - return $content; - } - - public function generate_ppcp_info_html($type, $data) : string - { - - ob_start(); - ?> - - - - - - - - - settingsRenderer->render(); $content = ob_get_contents(); ob_end_clean(); return $content; diff --git a/modules.local/ppcp-wc-gateway/src/Settings/FullyOnboardedSettings.php b/modules.local/ppcp-wc-gateway/src/Settings/FullyOnboardedSettings.php deleted file mode 100644 index b14362f0a..000000000 --- a/modules.local/ppcp-wc-gateway/src/Settings/FullyOnboardedSettings.php +++ /dev/null @@ -1,111 +0,0 @@ -environment = $environment; - } - - public function fields(): array - { - return array_merge( - $this->gateway(), - $this->buttons(), - $this->creditCards() - ); - } - - private function gateway(): array - { - return array_merge( - $this->defaultFields(), - [ - 'intent' => [ - 'title' => __('Intent', 'woocommerce-paypal-gateway'), - 'type' => 'select', - 'class' => 'wc-enhanced-select', - 'default' => 'capture', - 'desc_tip' => true, - 'description' => __( - 'The intent to either capture payment immediately or authorize a payment for an order after order creation.', - 'woocommerce-paypal-gateway' - ), - 'options' => [ - 'capture' => __('Capture', 'woocommerce-paypal-gateway'), - 'authorize' => __('Authorize', 'woocommerce-paypal-gateway'), - ], - ], - ] - ); - } - - - private function creditCards(): array - { - - return [ - - 'credit_card_settings' => [ - 'title' => __('Credit Card Settings', 'woocommerce-paypal-gateway'), - 'type' => 'title', - 'description' => __( - 'Customize the appearance of Credit Card Payments on your site.', - 'woocommerce-paypal-gateway' - ), - ], - 'dcc_cart_enabled' => [ - 'title' => __('Enable credit card on cart', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card directly in your cart.', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'dcc_mini_cart_enabled' => [ - 'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card directly in your mini cart.', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'dcc_checkout_enabled' => [ - 'title' => __('Enable credit card on checkout', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card in the checkout.', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'dcc_single_product_enabled' => [ - 'title' => __('Enable credit card on products', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card instantly on the product page.', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'disable_cards' => [ - 'title' => __('Disable specific credid cards', 'woocommerce-paypal-gateway'), - 'type' => 'multiselect', - 'class' => 'wc-enhanced-select', - 'default' => [], - 'desc_tip' => true, - 'description' => __( - 'By default all possible credit cards will be shown. You can disable some cards, if you wish.', - 'woocommerce-paypal-gateway' - ), - 'options' => [ - 'visa' => _x('Visa', 'Name of credit card', 'woocommerce-paypal-gateway'), - 'mastercard' => _x('Mastercard', 'Name of credit card', 'woocommerce-paypal-gateway'), - 'amex' => _x('American Express', 'Name of credit card', 'woocommerce-paypal-gateway'), - 'discover' => _x('Discover', 'Name of credit card', 'woocommerce-paypal-gateway'), - 'jcb' => _x('JCB', 'Name of credit card', 'woocommerce-paypal-gateway'), - 'elo' => _x('Elo', 'Name of credit card', 'woocommerce-paypal-gateway'), - 'hiper' => _x('Hiper', 'Name of credit card', 'woocommerce-paypal-gateway'), - ], - ], - ]; - } -} diff --git a/modules.local/ppcp-wc-gateway/src/Settings/ProgressiveSettings.php b/modules.local/ppcp-wc-gateway/src/Settings/ProgressiveSettings.php deleted file mode 100644 index 3d59f1e97..000000000 --- a/modules.local/ppcp-wc-gateway/src/Settings/ProgressiveSettings.php +++ /dev/null @@ -1,36 +0,0 @@ -environment = $environment; - } - - public function fields(): array - { - $fields = array_merge( - [ - 'onboarding' => [ - 'type' => 'ppcp_onboarding', - ], - ], - $this->defaultFields(), - [ - 'reset' => [ - 'type' => 'ppcp_reset', - ], - ] - ); - return $fields; - } -} diff --git a/modules.local/ppcp-wc-gateway/src/Settings/Settings.php b/modules.local/ppcp-wc-gateway/src/Settings/Settings.php index ef9dc21de..8446c922f 100644 --- a/modules.local/ppcp-wc-gateway/src/Settings/Settings.php +++ b/modules.local/ppcp-wc-gateway/src/Settings/Settings.php @@ -10,11 +10,11 @@ use Psr\Container\ContainerInterface; class Settings implements ContainerInterface { - private $gateway; + public const KEY = 'woocommerce-ppcp-settings'; + private $settings = []; - public function __construct(\WC_Payment_Gateway $gateway) + public function __construct() { - $this->gateway = $gateway; } // phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType @@ -24,18 +24,28 @@ class Settings implements ContainerInterface if (!$this->has($id)) { throw new NotFoundException(); } - return $this->gateway->get_option($id); + return $this->settings[$id]; } public function has($id) { - return !!$this->gateway->get_option($id); + $this->load(); + return array_key_exists($id, $this->settings); } + public function set($id, $value) + { + $this->load(); + $this->settings[$id] = $value; + } + public function persist() { + update_option(self::KEY, $this->settings); + } public function reset() : bool { + $this->load(); $fieldsToReset = [ 'enabled', 'intent', @@ -43,11 +53,18 @@ class Settings implements ContainerInterface 'client_secret', 'merchant_email', ]; - - foreach ($fieldsToReset as $key) { - $this->gateway->update_option($key, ''); + foreach ($fieldsToReset as $id) { + $this->settings[$id] = null; } return true; } + + private function load() : bool { + if ($this->settings) { + return false; + } + $this->settings = get_option(self::KEY, []); + return true; + } } diff --git a/modules.local/ppcp-wc-gateway/src/Settings/SettingsFields.php b/modules.local/ppcp-wc-gateway/src/Settings/SettingsFields.php deleted file mode 100644 index ba53a9714..000000000 --- a/modules.local/ppcp-wc-gateway/src/Settings/SettingsFields.php +++ /dev/null @@ -1,13 +0,0 @@ -settings = $settings; + $this->settingFields = $settingFields; + } + + public function listen() { + if (! $this->isValidUpdateRequest()) { + return; + } + + if (isset($_POST['save']) && sanitize_text_field(wp_unslash($_POST['save'])) === 'reset') { + $this->settings->reset(); + $this->settings->persist(); + return; + } + + $settings = [ + 'enabled' => isset($_POST['woocommerce_ppcp-gateway_enabled']) && absint($_POST['woocommerce_ppcp-gateway_enabled']) === 1, + ]; + $rawData = (isset($_POST['ppcp'])) ? (array) $_POST['ppcp'] : []; + foreach ($this->settingFields as $key => $config) { + switch ($config['type']) { + case 'checkbox': + $settings[$key] = isset($rawData[$key]); + break; + case 'text': + $settings[$key] = isset($rawData[$key]) ? sanitize_text_field($rawData[$key]) : ''; + break; + case 'multiselect': + $values = isset($rawData[$key]) ? (array) $rawData[$key] : []; + $valuesToSave = []; + foreach ($values as $index => $rawValue) { + $value = sanitize_text_field($rawValue); + if (! in_array($value, $config['options'], true)) { + continue; + } + $valuesToSave[] = $value; + } + $settings[$key] = $valuesToSave; + break; + case 'select': + $settings[$key] = isset($rawData[$key]) && in_array(sanitize_text_field($rawData[$key]), $config['options'], true) ? sanitize_text_field($rawData[$key]) : null; + break; + } + } + foreach ($settings as $id => $value) { + $this->settings->set($id, $value); + } + $this->settings->persist(); + return; + } + + private function isValidUpdateRequest() : bool + { + + if (! isset($_REQUEST['section']) || sanitize_text_field(wp_unslash($_REQUEST['section'])) !== 'ppcp-gateway') { + return false; + } + + if (! current_user_can('manage_options')) { + return false; + } + + if (! isset($_POST['ppcp-nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ppcp-nonce'])), self::NONCE)) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php b/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php new file mode 100644 index 000000000..6bac2d2d5 --- /dev/null +++ b/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php @@ -0,0 +1,84 @@ +settings = $settings; + $this->state = $state; + $this->fields = $fields; + } + + public function renderMultiSelect($field, $key, $config, $value) : string { + if ($config['type'] !== 'ppcp-multiselect') { + return $field; + } + + $options = []; + foreach ($config['options'] as $optionKey => $optionValue) { + $selected = ''; + + $options[] = ''; + } + + $html = sprintf( + '', + implode(' ', $config['class']), + $key, + implode('', $options) + ); + + return $html; + } + + public function render() { + + $nonce = wp_create_nonce(SettingsListener::NONCE); + ?> + + fields as $field => $config) : + if (! in_array($this->state->currentState(), $config['screens'], true)) { + continue; + } + $value = $this->settings->has($field) ? $this->settings->get($field) : null; + $id = 'ppcp[' . $field . ']'; + ?> + + + + + settings->has($config['hidden']) ? (string) $this->settings->get($config['hidden']) : ''; + echo ''; + } + } else { + woocommerce_form_field($id, $config, $value); + } ?> + + environment) ? $this->environment->currentEnvironmentIs(Environment::SANDBOX) : false; - $sandbox = [ - 'type' => 'ppcp_info', - 'title' => __('Sandbox'), - 'text' => ($isSandbox) ? __('You are currently in the sandbox mode. Click Reset if you want to change your mode.', 'woocommerce-paypal-commerce-gateway') : __('You are in production mode. Click Reset if you want to change your mode.', 'woocommerce-paypal-commerce-gateway'), - ]; - return array_merge( - [ - 'enabled' => [ - 'title' => __('Enable/Disable', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Enable PayPal Payments', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'title' => [ - 'title' => __('Title', 'woocommerce-paypal-gateway'), - 'type' => 'text', - 'description' => __( - 'This controls the title which the user sees during checkout.', - 'woocommerce-paypal-gateway' - ), - 'default' => __('PayPal', 'woocommerce-paypal-gateway'), - 'desc_tip' => true, - ], - 'description' => [ - 'title' => __('Description', 'woocommerce-paypal-gateway'), - 'type' => 'text', - 'desc_tip' => true, - 'description' => __( - 'This controls the description which the user sees during checkout.', - 'woocommerce-paypal-gateway' - ), - 'default' => __( - 'Pay via PayPal; you can pay with your credit card if you don\'t have a PayPal account.', - 'woocommerce-paypal-gateway' - ), - ], - - 'account_settings' => [ - 'title' => __('Account Settings', 'woocommerce-paypal-gateway'), - 'type' => 'title', - 'description' => '', - ], - 'sandbox_on' => $sandbox, - 'reset' => [ - 'type' => 'ppcp_reset', - ], - ], - $this->buttons() - ); - } - - - private function buttons(): array - { - return [ - 'button_settings' => [ - 'title' => __('SmartButton Settings', 'woocommerce-paypal-gateway'), - 'type' => 'title', - 'description' => __( - 'Customize the appearance of PayPal Payments on your site.', - 'woocommerce-paypal-gateway' - ), - ], - 'button_single_product_enabled' => [ - 'title' => __('Buttons on Single Product', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Enable on Single Product', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'button_mini_cart_enabled' => [ - 'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Enable on Mini Cart', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'button_cart_enabled' => [ - 'title' => __('Buttons on Cart', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __('Enable on Cart', 'woocommerce-paypal-gateway'), - 'default' => 'yes', - ], - 'button_color' => [ - 'title' => __('Color', 'woocommerce-paypal-gateway'), - 'type' => 'select', - 'class' => 'wc-enhanced-select', - 'default' => 'gold', - 'desc_tip' => true, - 'description' => __( - 'Controls the background color of the primary button. Use "Gold" to leverage PayPal\'s recognition and preference, or change it to match your site design or aesthetic.', - 'woocommerce-paypal-gateway' - ), - 'options' => [ - 'gold' => __('Gold (Recommended)', 'woocommerce-paypal-gateway'), - 'blue' => __('Blue', 'woocommerce-paypal-gateway'), - 'silver' => __('Silver', 'woocommerce-paypal-gateway'), - 'black' => __('Black', 'woocommerce-paypal-gateway'), - ], - ], - 'button_shape' => [ - 'title' => __('Shape', 'woocommerce-paypal-gateway'), - 'type' => 'select', - 'class' => 'wc-enhanced-select', - 'default' => 'rect', - 'desc_tip' => true, - 'description' => __( - 'The pill-shaped button\'s unique and powerful shape signifies PayPal in people\'s minds. Use the rectangular button as an alternative when pill-shaped buttons might pose design challenges.', - 'woocommerce-paypal-gateway' - ), - 'options' => [ - 'pill' => __('Pill', 'woocommerce-paypal-gateway'), - 'rect' => __('Rectangle', 'woocommerce-paypal-gateway'), - ], - ], - 'disable_funding' => [ - 'title' => __('Disable funding sources', 'woocommerce-paypal-gateway'), - 'type' => 'multiselect', - 'class' => 'wc-enhanced-select', - 'default' => [], - 'desc_tip' => true, - 'description' => __( - 'By default all possible funding sources will be shown. You can disable some sources, if you wish.', - 'woocommerce-paypal-gateway' - ), - 'options' => [ - 'card' => _x('Credit or debit cards', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'credit' => _x('PayPal Credit', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'venmo' => _x('Venmo', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'sepa' => _x('SEPA-Lastschrift', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'bancontact' => _x('Bancontact', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'eps' => _x('eps', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'giropay' => _x('giropay', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'ideal' => _x('iDEAL', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'mybank' => _x('MyBank', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'p24' => _x('Przelewy24', 'Name of payment method', 'woocommerce-paypal-gateway'), - 'sofort' => _x('Sofort', 'Name of payment method', 'woocommerce-paypal-gateway'), - ], - ], - ]; - } -} diff --git a/modules.local/ppcp-wc-gateway/src/Settings/StartSettings.php b/modules.local/ppcp-wc-gateway/src/Settings/StartSettings.php deleted file mode 100644 index 8f8c7631d..000000000 --- a/modules.local/ppcp-wc-gateway/src/Settings/StartSettings.php +++ /dev/null @@ -1,35 +0,0 @@ - [ - 'title' => __('PayPal Email', 'woocommerce-paypal-gateway'), - 'type' => 'email', - 'description' => __( - 'Please enter the email address with which you want to receive payments.', - 'woocommerce-paypal-gateway' - ), - 'default' => '', - 'desc_tip' => true, - ], - 'sandbox_on' => [ - 'title' => __('Enable Sandbox', 'woocommerce-paypal-gateway'), - 'type' => 'checkbox', - 'label' => __( - 'For testing your integration, you can enable the sandbox.', - 'woocommerce-paypal-gateway' - ), - 'default' => 'no', - ], - ]; - } -} diff --git a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php index e382fbfc7..fc8d6e3b5 100644 --- a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php +++ b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php @@ -14,6 +14,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways; use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; +use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Interop\Container\ServiceProviderInterface; use Psr\Container\ContainerInterface; @@ -37,6 +38,27 @@ class WcGatewayModule implements ModuleInterface } ); + add_action( + 'woocommerce_settings_save_checkout', + static function () use ($container) { + $listener = $container->get('wcgateway.settings.listener'); + $listener->listen(); + } + ); + + add_filter( + 'woocommerce_form_field', + static function ($field, $key, $args, $value) use ($container) { + $renderer = $container->get('wcgateway.settings.render'); + /** + * @var SettingsRenderer $renderer + */ + return $renderer->renderMultiSelect($field, $key, $args, $value); + }, + 10, + 4 + ); + add_filter( 'woocommerce_available_payment_gateways', static function ($methods) use ($container): array { @@ -48,14 +70,6 @@ class WcGatewayModule implements ModuleInterface } ); - add_action( - 'admin_init', - function() use ($container) { - $resetGateway = $container->get('wcgateway.gateway.reset'); - $resetGateway->listen(); - } - ); - add_filter( Repository::NOTICES_FILTER, static function ($notices) use ($container): array {