Fix eligibility, move capabilities to definition

This commit is contained in:
carmenmaymo 2025-02-19 13:56:58 +01:00
parent f18f9c3b50
commit d3a813ef12
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
4 changed files with 72 additions and 44 deletions

View file

@ -129,8 +129,7 @@ const OverviewTodos = () => {
const OverviewFeatures = () => {
const [ isRefreshing, setIsRefreshing ] = useState( false );
const { merchant, features: eligibleFeatures } =
CommonHooks.useMerchantInfo();
const { merchant } = CommonHooks.useMerchantInfo();
const { refreshFeatureStatuses } = useDispatch( CommonStoreName );
const { setActiveModal, setActiveHighlight } =
useDispatch( CommonStoreName );
@ -141,17 +140,6 @@ const OverviewFeatures = () => {
useEffect( () => {
fetchFeatures();
}, [ fetchFeatures ] );
// Map merchant features status to the config
const featuresData = useMemo( () => {
return features.map( ( feature ) => {
const eligibleFeature = eligibleFeatures?.[ feature.id ];
return {
...feature,
enabled: eligibleFeature?.enabled ?? false,
};
} );
}, [ features, eligibleFeatures ] );
const refreshHandler = async () => {
setIsRefreshing( true );
@ -204,7 +192,7 @@ const OverviewFeatures = () => {
contentContainer={ false }
>
<ContentWrapper>
{ featuresData.map( ( { id, ...feature } ) => (
{ features.map( ( { id, ...feature } ) => (
<OverviewFeatureItem
key={ id }
isBusy={ isRefreshing }

View file

@ -10,6 +10,7 @@ declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Settings\Ajax\SwitchSettingsUiEndpoint;
use WooCommerce\PayPalCommerce\Settings\Data\Definition\FeaturesDefinition;
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
@ -487,12 +488,6 @@ return array(
);
},
'settings.data.definition.features' => static function ( ContainerInterface $container ) : FeaturesDefinition {
return new FeaturesDefinition(
$container->get( 'settings.service.features_eligibilities' ),
$container->get( 'settings.data.general' )
);
},
'settings.service.features_eligibilities' => static function( ContainerInterface $container ): FeaturesEligibilityService {
$features = apply_filters(
'woocommerce_paypal_payments_rest_common_merchant_features',
array()
@ -505,8 +500,7 @@ return array(
$gateways = array(
'card-button' => $settings['data']['ppcp-card-button-gateway']['enabled'] ?? false,
);
// Merchant eligibility.
// Merchant capabilities, serve to show active or inactive badge and buttons.
$capabilities = array(
'apple_pay' => $features['apple_pay']['enabled'] ?? false,
'google_pay' => $features['google_pay']['enabled'] ?? false,
@ -515,14 +509,37 @@ return array(
'apm' => $features['alternative_payment_methods']['enabled'] ?? false,
'paylater' => $features['pay_later_messaging']['enabled'] ?? false,
);
$merchant_capabilities = array(
'save_paypal' => $capabilities['save_paypal'], // Save PayPal and Venmo eligibility.
'acdc' => $capabilities['acdc'] && ! $gateways['card-button'], // Advanced credit and debit cards eligibility.
'apm' => $capabilities['apm'], // Alternative payment methods eligibility.
'google_pay' => $capabilities['acdc'] && ! $capabilities['google_pay'], // Google Pay eligibility.
'apple_pay' => $capabilities['acdc'] && ! $capabilities['apple_pay'], // Apple Pay eligibility.
'pay_later' => $capabilities['paylater'],
);
return new FeaturesDefinition(
$container->get( 'settings.service.features_eligibilities' ),
$container->get( 'settings.data.general' ),
$merchant_capabilities
);
},
'settings.service.features_eligibilities' => static function( ContainerInterface $container ): FeaturesEligibilityService {
$messages_apply = $container->get( 'button.helper.messages-apply' );
assert( $messages_apply instanceof MessagesApply );
$pay_later_eligible = $messages_apply->for_country();
$merchant_country = $container->get( 'api.shop.country' );
$ineligible_countries = array( 'RU', 'BR', 'JP' );
$apm_eligible = ! in_array( $merchant_country, $ineligible_countries, true );
return new FeaturesEligibilityService(
$capabilities['save_paypal'], // Save PayPal and Venmo eligibility.
$capabilities['acdc'] && ! $gateways['card-button'], // Advanced credit and debit cards eligibility.
$capabilities['apm'], // Alternative payment methods eligibility.
$capabilities['acdc'] && ! $capabilities['google_pay'], // Google Pay eligibility.
$capabilities['acdc'] && ! $capabilities['apple_pay'], // Apple Pay eligibility.
$capabilities['paylater'], // Pay Later eligibility.
$container->get( 'save-payment-methods.eligible' ), // Save PayPal and Venmo eligibility.
$container->get( 'card-fields.eligible' ), // Advanced credit and debit cards eligibility.
$apm_eligible, // Alternative payment methods eligibility.
$container->get( 'googlepay.eligible' ), // Google Pay eligibility.
$container->get( 'applepay.eligible' ), // Apple Pay eligibility.
$pay_later_eligible, // Pay Later eligibility.
);
},
);

View file

@ -35,18 +35,28 @@ class FeaturesDefinition {
*/
protected GeneralSettings $settings;
/**
* The merchant capabilities.
*
* @var array
*/
protected array $merchant_capabilities;
/**
* Constructor.
*
* @param FeaturesEligibilityService $eligibilities The features eligibility service.
* @param GeneralSettings $settings The general settings service.
* @param array $merchant_capabilities The merchant capabilities.
*/
public function __construct(
FeaturesEligibilityService $eligibilities,
GeneralSettings $settings
GeneralSettings $settings,
array $merchant_capabilities
) {
$this->eligibilities = $eligibilities;
$this->settings = $settings;
$this->eligibilities = $eligibilities;
$this->settings = $settings;
$this->merchant_capabilities = $merchant_capabilities;
}
/**
@ -55,7 +65,23 @@ class FeaturesDefinition {
* @return array The array of feature definitions.
*/
public function get(): array {
$all_features = $this->all_available_features();
$eligible_features = array();
$eligibility_checks = $this->eligibilities->get_eligibility_checks();
foreach ( $all_features as $feature_key => $feature ) {
if ( $eligibility_checks[ $feature_key ]() ) {
$eligible_features[ $feature_key ] = $feature;
}
}
return $eligible_features;
}
/**
* Returns all available features.
*
* @return array[] The array of all available features.
*/
public function all_available_features(): array {
$paylater_countries = array(
'UK',
'ES',
@ -72,7 +98,7 @@ class FeaturesDefinition {
'save_paypal_and_venmo' => array(
'title' => __( 'Save PayPal and Venmo', 'woocommerce-paypal-payments' ),
'description' => __( 'Securely save PayPal and Venmo payment methods for subscriptions or return buyers.', 'woocommerce-paypal-payments' ),
'isEligible' => $eligibility_checks['save_paypal_and_venmo'],
'enabled' => $this->merchant_capabilities['save_paypal'],
'buttons' => array(
array(
'type' => 'secondary',
@ -105,7 +131,7 @@ class FeaturesDefinition {
'advanced_credit_and_debit_cards' => array(
'title' => __( 'Advanced Credit and Debit Cards', 'woocommerce-paypal-payments' ),
'description' => __( 'Process major credit and debit cards including Visa, Mastercard, American Express and Discover.', 'woocommerce-paypal-payments' ),
'isEligible' => $eligibility_checks['advanced_credit_and_debit_cards'],
'enabled' => $this->merchant_capabilities['acdc'],
'buttons' => array(
array(
'type' => 'secondary',
@ -141,7 +167,7 @@ class FeaturesDefinition {
'alternative_payment_methods' => array(
'title' => __( 'Alternative Payment Methods', 'woocommerce-paypal-payments' ),
'description' => __( 'Offer global, country-specific payment options for your customers.', 'woocommerce-paypal-payments' ),
'isEligible' => $eligibility_checks['alternative_payment_methods'],
'enabled' => $this->merchant_capabilities['apm'],
'buttons' => array(
array(
'type' => 'secondary',
@ -173,7 +199,7 @@ class FeaturesDefinition {
'google_pay' => array(
'title' => __( 'Google Pay', 'woocommerce-paypal-payments' ),
'description' => __( 'Let customers pay using their Google Pay wallet.', 'woocommerce-paypal-payments' ),
'isEligible' => $eligibility_checks['google_pay'],
'enabled' => $this->merchant_capabilities['google_pay'],
'buttons' => array(
array(
'type' => 'secondary',
@ -212,7 +238,7 @@ class FeaturesDefinition {
'apple_pay' => array(
'title' => __( 'Apple Pay', 'woocommerce-paypal-payments' ),
'description' => __( 'Let customers pay using their Apple Pay wallet.', 'woocommerce-paypal-payments' ),
'isEligible' => $eligibility_checks['apple_pay'],
'enabled' => $this->merchant_capabilities['apple_pay'],
'buttons' => array(
array(
'type' => 'secondary',
@ -261,7 +287,7 @@ class FeaturesDefinition {
'Let customers know they can buy now and pay later with PayPal. Adding this messaging can boost conversion rates and increase cart sizes by 39%¹, with no extra cost to you—plus, you get paid up front.',
'woocommerce-paypal-payments'
),
'isEligible' => $eligibility_checks['pay_later'],
'enabled' => $this->merchant_capabilities['pay_later'],
'buttons' => array(
array(
'type' => 'secondary',

View file

@ -86,13 +86,10 @@ class FeaturesRestEndpoint extends RestEndpoint {
$features = array();
foreach ( $this->features_definition->get() as $id => $feature ) {
// Check eligibility and add to features if eligible.
if ( $feature['isEligible']() ) {
$features[] = array_merge(
array( 'id' => $id ),
array_diff_key( $feature, array( 'isEligible' => true ) )
);
}
$features[] = array_merge(
array( 'id' => $id ),
$feature
);
}
return $this->return_success(