From 30dd654f831823c111193908a6b055b62947bced Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Thu, 13 Feb 2025 15:17:22 +0100 Subject: [PATCH 01/12] Fix non wc gateway items null enabled key --- modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php index 5047b484d..772d4e19f 100644 --- a/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php +++ b/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php @@ -710,10 +710,10 @@ class PaymentRestEndpoint extends RestEndpoint { if ( ! isset( $all_gateways[ $key ] ) && isset( $request_data[ $key ] ) ) { switch ( $key ) { case 'venmo': - $this->settings->set_venmo_enabled( $request_data[ $key ]['enabled'] ); + $this->settings->set_venmo_enabled( $request_data[ $key ]['enabled'] ?? false ); break; case 'pay-later': - $this->settings->set_paylater_enabled( $request_data[ $key ]['enabled'] ); + $this->settings->set_paylater_enabled( $request_data[ $key ]['enabled'] ?? false ); break; } From 6814fd0a2e3e8d8c8d49e286e005f7892070e828 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 18:17:45 +0400 Subject: [PATCH 02/12] Map the "Brand Name" and "Soft Descriptor" --- modules/ppcp-compat/services.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 142bec40d..30d687583 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -161,7 +161,9 @@ return array( new SettingsMap( $container->get( 'settings.data.settings' ), array( - 'disable_cards' => 'disabled_cards', + 'disable_cards' => 'disabled_cards', + 'brand_name' => 'brand_name', + 'soft_descriptor' => 'soft_descriptor', ) ), new SettingsMap( From 480dd8d8aa8f19733440a9fadeace1e938ee9799 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 19:42:57 +0400 Subject: [PATCH 03/12] Create a helper to map the settings tab. This is necessary because we need to map the specific values too and not just the keys in some cases like for the 'subtotal_mismatch_behavior' --- .../src/Settings/SettingsTabMapHelper.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php new file mode 100644 index 000000000..62a93fb9a --- /dev/null +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -0,0 +1,73 @@ + + */ + public function map(): array { + return array( + 'disable_cards' => 'disabled_cards', + 'brand_name' => 'brand_name', + 'soft_descriptor' => 'soft_descriptor', + 'subtotal_mismatch_behavior' => 'subtotal_adjustment', + ); + } + + /** + * Retrieves the value of a mapped key from the new settings. + * + * @param string $old_key The key from the legacy settings. + * @param array $settings_model The new settings model data as an array. + * @return mixed The value of the mapped setting, (null if not found). + */ + public function mapped_value( string $old_key, array $settings_model ) { + $settings_map = $this->map(); + $new_key = $settings_map[ $old_key ] ?? false; + switch ( $old_key ) { + case 'subtotal_mismatch_behavior': + return $this->mapped_mismatch_behavior_value( $settings_model ); + + default: + return $settings_model[ $new_key ] ?? null; + } + } + + /** + * Retrieves the mapped mismatch_behavior value from the new settings. + * + * @param array $settings_model The new settings model data as an array. + * @return 'extra_line'|'ditch'|null The mapped mismatch_behavior value. + */ + protected function mapped_mismatch_behavior_value( array $settings_model ): ?string { + $subtotal_adjustment = $settings_model[ 'subtotal_adjustment' ] ?? false; + + if ( ! $subtotal_adjustment ) { + return null; + } + + return $subtotal_adjustment === 'correction' ? PurchaseUnitSanitizer::MODE_EXTRA_LINE : PurchaseUnitSanitizer::MODE_DITCH; + } +} From 619c3b14670a4d585efda443a74ba28382b86665 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 19:43:23 +0400 Subject: [PATCH 04/12] Use a helper to map the settings tab. --- modules/ppcp-compat/services.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 30d687583..b69d4fc0d 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Compat; use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets; use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap; use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsTabMapHelper; use WooCommerce\PayPalCommerce\Compat\Settings\StylingSettingsMapHelper; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; @@ -133,6 +134,9 @@ return array( $styling_settings_map_helper = $container->get( 'compat.settings.styling_map_helper' ); assert( $styling_settings_map_helper instanceof StylingSettingsMapHelper ); + $settings_tab_map_helper = $container->get( 'compat.settings.settings_tab_map_helper' ); + assert( $settings_tab_map_helper instanceof SettingsTabMapHelper ); + return array( new SettingsMap( $container->get( 'settings.data.general' ), @@ -160,11 +164,7 @@ return array( ), new SettingsMap( $container->get( 'settings.data.settings' ), - array( - 'disable_cards' => 'disabled_cards', - 'brand_name' => 'brand_name', - 'soft_descriptor' => 'soft_descriptor', - ) + $settings_tab_map_helper->map() ), new SettingsMap( $container->get( 'settings.data.styling' ), @@ -185,10 +185,14 @@ return array( 'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper { return new SettingsMapHelper( $container->get( 'compat.setting.new-to-old-map' ), - $container->get( 'compat.settings.styling_map_helper' ) + $container->get( 'compat.settings.styling_map_helper' ), + $container->get( 'compat.settings.settings_tab_map_helper' ) ); }, 'compat.settings.styling_map_helper' => static function() : StylingSettingsMapHelper { return new StylingSettingsMapHelper(); }, + 'compat.settings.settings_tab_map_helper' => static function() : SettingsTabMapHelper { + return new SettingsTabMapHelper(); + }, ); From 00486f4934bd2419191aa6b8acef50f75c53b020 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 19:43:36 +0400 Subject: [PATCH 05/12] Implement a helper to map the settings tab. --- .../src/Settings/SettingsMapHelper.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/Settings/SettingsMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsMapHelper.php index a18f2ceda..678644897 100644 --- a/modules/ppcp-compat/src/Settings/SettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsMapHelper.php @@ -10,6 +10,7 @@ declare( strict_types = 1 ); namespace WooCommerce\PayPalCommerce\Compat\Settings; use RuntimeException; +use WooCommerce\PayPalCommerce\Settings\Data\SettingsModel; use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings; /** @@ -49,17 +50,30 @@ class SettingsMapHelper { */ protected StylingSettingsMapHelper $styling_settings_map_helper; + /** + * A helper for mapping the old/new settings tab settings. + * + * @var SettingsTabMapHelper + */ + protected SettingsTabMapHelper $settings_tab_map_helper; + /** * Constructor. * * @param SettingsMap[] $settings_map A list of settings maps containing key definitions. * @param StylingSettingsMapHelper $styling_settings_map_helper A helper for mapping the old/new styling settings. + * @param SettingsTabMapHelper $settings_tab_map_helper A helper for mapping the old/new settings tab settings. * @throws RuntimeException When an old key has multiple mappings. */ - public function __construct( array $settings_map, StylingSettingsMapHelper $styling_settings_map_helper ) { + public function __construct( + array $settings_map, + StylingSettingsMapHelper $styling_settings_map_helper, + SettingsTabMapHelper $settings_tab_map_helper + ) { $this->validate_settings_map( $settings_map ); $this->settings_map = $settings_map; $this->styling_settings_map_helper = $styling_settings_map_helper; + $this->settings_tab_map_helper = $settings_tab_map_helper; } /** @@ -135,6 +149,10 @@ class SettingsMapHelper { switch ( true ) { case $model instanceof StylingSettings: return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] ); + + case $model instanceof SettingsModel: + return $this->settings_tab_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] ); + default: return $this->model_cache[ $model_id ][ $new_key ] ?? null; } From f27339de0b5bbd356e1e1dde68da40e1d92dd979 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 19:44:06 +0400 Subject: [PATCH 06/12] Fix the CS --- modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php index 62a93fb9a..7a2b6a10f 100644 --- a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -39,7 +39,7 @@ class SettingsTabMapHelper { /** * Retrieves the value of a mapped key from the new settings. * - * @param string $old_key The key from the legacy settings. + * @param string $old_key The key from the legacy settings. * @param array $settings_model The new settings model data as an array. * @return mixed The value of the mapped setting, (null if not found). */ From d25084aa00db336343aa0e88862893168b5f320d Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 19:44:57 +0400 Subject: [PATCH 07/12] Fix the CS --- modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php index 7a2b6a10f..db9ccb554 100644 --- a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -62,7 +62,7 @@ class SettingsTabMapHelper { * @return 'extra_line'|'ditch'|null The mapped mismatch_behavior value. */ protected function mapped_mismatch_behavior_value( array $settings_model ): ?string { - $subtotal_adjustment = $settings_model[ 'subtotal_adjustment' ] ?? false; + $subtotal_adjustment = $settings_model['subtotal_adjustment'] ?? false; if ( ! $subtotal_adjustment ) { return null; From 897106499c95880481f048fb2e90bc52fdbd84ce Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 13 Feb 2025 19:45:27 +0400 Subject: [PATCH 08/12] Fix the CS --- modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php index db9ccb554..03f863e5a 100644 --- a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -39,7 +39,7 @@ class SettingsTabMapHelper { /** * Retrieves the value of a mapped key from the new settings. * - * @param string $old_key The key from the legacy settings. + * @param string $old_key The key from the legacy settings. * @param array $settings_model The new settings model data as an array. * @return mixed The value of the mapped setting, (null if not found). */ From 47f61cb543fe2b4fc2aac732b3dfeba94b7128e8 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 14 Feb 2025 12:55:26 +0400 Subject: [PATCH 09/12] Map the "Instant payments only" & thee "Landing Page" option. Currently, the "Instant payments only" option is not saved in DB so probably will be needed to update the new key later --- .../src/Settings/SettingsTabMapHelper.php | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php index 03f863e5a..a37490e15 100644 --- a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Compat\Settings; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer; use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; @@ -32,7 +33,9 @@ class SettingsTabMapHelper { 'disable_cards' => 'disabled_cards', 'brand_name' => 'brand_name', 'soft_descriptor' => 'soft_descriptor', + 'payee_preferred' => 'instant_payments_only', 'subtotal_mismatch_behavior' => 'subtotal_adjustment', + 'landing_page' => 'landing_page', ); } @@ -50,16 +53,19 @@ class SettingsTabMapHelper { case 'subtotal_mismatch_behavior': return $this->mapped_mismatch_behavior_value( $settings_model ); + case 'landing_page': + return $this->mapped_landing_page_value( $settings_model ); + default: return $settings_model[ $new_key ] ?? null; } } /** - * Retrieves the mapped mismatch_behavior value from the new settings. + * Retrieves the mapped value for the 'mismatch_behavior' from the new settings. * * @param array $settings_model The new settings model data as an array. - * @return 'extra_line'|'ditch'|null The mapped mismatch_behavior value. + * @return 'extra_line'|'ditch'|null The mapped 'mismatch_behavior' setting value. */ protected function mapped_mismatch_behavior_value( array $settings_model ): ?string { $subtotal_adjustment = $settings_model['subtotal_adjustment'] ?? false; @@ -70,4 +76,25 @@ class SettingsTabMapHelper { return $subtotal_adjustment === 'correction' ? PurchaseUnitSanitizer::MODE_EXTRA_LINE : PurchaseUnitSanitizer::MODE_DITCH; } + + /** + * Retrieves the mapped value for the 'landing_page' from the new settings. + * + * @param array $settings_model The new settings model data as an array. + * @return 'LOGIN'|'BILLING'|'NO_PREFERENCE'|null The mapped 'landing_page' setting value. + */ + protected function mapped_landing_page_value( array $settings_model ): ?string { + $landing_page = $settings_model['landing_page'] ?? false; + + if ( ! $landing_page ) { + return null; + } + + return $landing_page === 'login' + ? ApplicationContext::LANDING_PAGE_LOGIN + : ( $landing_page === 'guest_checkout' + ? ApplicationContext::LANDING_PAGE_BILLING + : ApplicationContext::LANDING_PAGE_NO_PREFERENCE + ); + } } From aa4062784045b921d6f6b01d7273f0a7a179466c Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 14 Feb 2025 13:19:12 +0400 Subject: [PATCH 10/12] Store the language locale instead of the language code. --- .../Settings/Components/Settings/Blocks/PaypalSettings.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js b/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js index 62240b35b..86a0c81af 100644 --- a/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js +++ b/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js @@ -142,10 +142,10 @@ const PaypalSettings = () => { }; const languagesExample = [ - { value: 'en', label: 'English' }, - { value: 'de', label: 'German' }, - { value: 'es', label: 'Spanish' }, - { value: 'it', label: 'Italian' }, + { value: 'en_US', label: 'English' }, + { value: 'de_DE', label: 'German' }, + { value: 'es_ES', label: 'Spanish' }, + { value: 'it_IT', label: 'Italian' }, ]; const subtotalAdjustmentChoices = [ From 935f1a5a429d763038082bd93be2033f5c9111bd Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 14 Feb 2025 13:21:31 +0400 Subject: [PATCH 11/12] Adjust the comment about storing the button language --- modules/ppcp-settings/src/Data/SettingsModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-settings/src/Data/SettingsModel.php b/modules/ppcp-settings/src/Data/SettingsModel.php index b88a899d3..0e9e8e706 100644 --- a/modules/ppcp-settings/src/Data/SettingsModel.php +++ b/modules/ppcp-settings/src/Data/SettingsModel.php @@ -73,7 +73,7 @@ class SettingsModel extends AbstractDataModel { // Enum-type string values. 'subtotal_adjustment' => 'skip_details', // Options: [correction|no_details]. 'landing_page' => 'any', // Options: [any|login|guest_checkout]. - 'button_language' => '', // empty or a 2-letter language code. + 'button_language' => '', // empty or a language locale code. // Boolean flags. 'authorize_only' => false, From 9c58620ad6aef0d83e2b1b6c55c0630ca653a061 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 14 Feb 2025 13:21:46 +0400 Subject: [PATCH 12/12] Map the "Button Language" --- modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php index a37490e15..d3ce17290 100644 --- a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -36,6 +36,7 @@ class SettingsTabMapHelper { 'payee_preferred' => 'instant_payments_only', 'subtotal_mismatch_behavior' => 'subtotal_adjustment', 'landing_page' => 'landing_page', + 'smart_button_language' => 'button_language', ); }