mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
🔀 Merge branch 'trunk'
# Conflicts: # modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php
This commit is contained in:
commit
f958696c5f
5 changed files with 135 additions and 10 deletions
|
@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Compat;
|
||||||
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
|
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
|
||||||
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap;
|
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap;
|
||||||
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper;
|
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper;
|
||||||
|
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsTabMapHelper;
|
||||||
use WooCommerce\PayPalCommerce\Compat\Settings\StylingSettingsMapHelper;
|
use WooCommerce\PayPalCommerce\Compat\Settings\StylingSettingsMapHelper;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
|
@ -133,6 +134,9 @@ return array(
|
||||||
$styling_settings_map_helper = $container->get( 'compat.settings.styling_map_helper' );
|
$styling_settings_map_helper = $container->get( 'compat.settings.styling_map_helper' );
|
||||||
assert( $styling_settings_map_helper instanceof StylingSettingsMapHelper );
|
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(
|
return array(
|
||||||
new SettingsMap(
|
new SettingsMap(
|
||||||
$container->get( 'settings.data.general' ),
|
$container->get( 'settings.data.general' ),
|
||||||
|
@ -160,9 +164,7 @@ return array(
|
||||||
),
|
),
|
||||||
new SettingsMap(
|
new SettingsMap(
|
||||||
$container->get( 'settings.data.settings' ),
|
$container->get( 'settings.data.settings' ),
|
||||||
array(
|
$settings_tab_map_helper->map()
|
||||||
'disable_cards' => 'disabled_cards',
|
|
||||||
)
|
|
||||||
),
|
),
|
||||||
new SettingsMap(
|
new SettingsMap(
|
||||||
$container->get( 'settings.data.styling' ),
|
$container->get( 'settings.data.styling' ),
|
||||||
|
@ -183,10 +185,14 @@ return array(
|
||||||
'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper {
|
'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper {
|
||||||
return new SettingsMapHelper(
|
return new SettingsMapHelper(
|
||||||
$container->get( 'compat.setting.new-to-old-map' ),
|
$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 {
|
'compat.settings.styling_map_helper' => static function() : StylingSettingsMapHelper {
|
||||||
return new StylingSettingsMapHelper();
|
return new StylingSettingsMapHelper();
|
||||||
},
|
},
|
||||||
|
'compat.settings.settings_tab_map_helper' => static function() : SettingsTabMapHelper {
|
||||||
|
return new SettingsTabMapHelper();
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -10,6 +10,7 @@ declare( strict_types = 1 );
|
||||||
namespace WooCommerce\PayPalCommerce\Compat\Settings;
|
namespace WooCommerce\PayPalCommerce\Compat\Settings;
|
||||||
|
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
use WooCommerce\PayPalCommerce\Settings\Data\SettingsModel;
|
||||||
use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings;
|
use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,17 +50,30 @@ class SettingsMapHelper {
|
||||||
*/
|
*/
|
||||||
protected StylingSettingsMapHelper $styling_settings_map_helper;
|
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.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param SettingsMap[] $settings_map A list of settings maps containing key definitions.
|
* @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 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.
|
* @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->validate_settings_map( $settings_map );
|
||||||
$this->settings_map = $settings_map;
|
$this->settings_map = $settings_map;
|
||||||
$this->styling_settings_map_helper = $styling_settings_map_helper;
|
$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 ) {
|
switch ( true ) {
|
||||||
case $model instanceof StylingSettings:
|
case $model instanceof StylingSettings:
|
||||||
return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] );
|
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:
|
default:
|
||||||
return $this->model_cache[ $model_id ][ $new_key ] ?? null;
|
return $this->model_cache[ $model_id ][ $new_key ] ?? null;
|
||||||
}
|
}
|
||||||
|
|
101
modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php
Normal file
101
modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* A helper for mapping the old/new settings tab settings.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\Compat\Settings
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A map of old to new styling settings.
|
||||||
|
*
|
||||||
|
* @psalm-import-type newSettingsKey from SettingsMap
|
||||||
|
* @psalm-import-type oldSettingsKey from SettingsMap
|
||||||
|
*/
|
||||||
|
class SettingsTabMapHelper {
|
||||||
|
|
||||||
|
use ContextTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps old setting keys to new setting keys.
|
||||||
|
*
|
||||||
|
* @psalm-return array<oldSettingsKey, newSettingsKey>
|
||||||
|
*/
|
||||||
|
public function map(): array {
|
||||||
|
return array(
|
||||||
|
'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',
|
||||||
|
'smart_button_language' => 'button_language',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the value of a mapped key from the new settings.
|
||||||
|
*
|
||||||
|
* @param string $old_key The key from the legacy settings.
|
||||||
|
* @param array<string, scalar|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 );
|
||||||
|
|
||||||
|
case 'landing_page':
|
||||||
|
return $this->mapped_landing_page_value( $settings_model );
|
||||||
|
|
||||||
|
default:
|
||||||
|
return $settings_model[ $new_key ] ?? null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the mapped value for the 'mismatch_behavior' from the new settings.
|
||||||
|
*
|
||||||
|
* @param array<string, scalar|array> $settings_model The new settings model data as an array.
|
||||||
|
* @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;
|
||||||
|
|
||||||
|
if ( ! $subtotal_adjustment ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<string, scalar|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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -142,10 +142,10 @@ const PaypalSettings = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const languagesExample = [
|
const languagesExample = [
|
||||||
{ value: 'en', label: 'English' },
|
{ value: 'en_US', label: 'English' },
|
||||||
{ value: 'de', label: 'German' },
|
{ value: 'de_DE', label: 'German' },
|
||||||
{ value: 'es', label: 'Spanish' },
|
{ value: 'es_ES', label: 'Spanish' },
|
||||||
{ value: 'it', label: 'Italian' },
|
{ value: 'it_IT', label: 'Italian' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const subtotalAdjustmentChoices = [
|
const subtotalAdjustmentChoices = [
|
||||||
|
|
|
@ -73,7 +73,7 @@ class SettingsModel extends AbstractDataModel {
|
||||||
// Enum-type string values.
|
// Enum-type string values.
|
||||||
'subtotal_adjustment' => 'skip_details', // Options: [correction|no_details].
|
'subtotal_adjustment' => 'skip_details', // Options: [correction|no_details].
|
||||||
'landing_page' => 'any', // Options: [any|login|guest_checkout].
|
'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.
|
// Boolean flags.
|
||||||
'authorize_only' => false,
|
'authorize_only' => false,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue