mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Fix eligibility, move capabilities to definition
This commit is contained in:
parent
f18f9c3b50
commit
d3a813ef12
4 changed files with 72 additions and 44 deletions
|
@ -129,8 +129,7 @@ const OverviewTodos = () => {
|
||||||
|
|
||||||
const OverviewFeatures = () => {
|
const OverviewFeatures = () => {
|
||||||
const [ isRefreshing, setIsRefreshing ] = useState( false );
|
const [ isRefreshing, setIsRefreshing ] = useState( false );
|
||||||
const { merchant, features: eligibleFeatures } =
|
const { merchant } = CommonHooks.useMerchantInfo();
|
||||||
CommonHooks.useMerchantInfo();
|
|
||||||
const { refreshFeatureStatuses } = useDispatch( CommonStoreName );
|
const { refreshFeatureStatuses } = useDispatch( CommonStoreName );
|
||||||
const { setActiveModal, setActiveHighlight } =
|
const { setActiveModal, setActiveHighlight } =
|
||||||
useDispatch( CommonStoreName );
|
useDispatch( CommonStoreName );
|
||||||
|
@ -141,17 +140,6 @@ const OverviewFeatures = () => {
|
||||||
useEffect( () => {
|
useEffect( () => {
|
||||||
fetchFeatures();
|
fetchFeatures();
|
||||||
}, [ 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 () => {
|
const refreshHandler = async () => {
|
||||||
setIsRefreshing( true );
|
setIsRefreshing( true );
|
||||||
|
|
||||||
|
@ -204,7 +192,7 @@ const OverviewFeatures = () => {
|
||||||
contentContainer={ false }
|
contentContainer={ false }
|
||||||
>
|
>
|
||||||
<ContentWrapper>
|
<ContentWrapper>
|
||||||
{ featuresData.map( ( { id, ...feature } ) => (
|
{ features.map( ( { id, ...feature } ) => (
|
||||||
<OverviewFeatureItem
|
<OverviewFeatureItem
|
||||||
key={ id }
|
key={ id }
|
||||||
isBusy={ isRefreshing }
|
isBusy={ isRefreshing }
|
||||||
|
|
|
@ -10,6 +10,7 @@ declare( strict_types = 1 );
|
||||||
namespace WooCommerce\PayPalCommerce\Settings;
|
namespace WooCommerce\PayPalCommerce\Settings;
|
||||||
|
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
|
||||||
use WooCommerce\PayPalCommerce\Settings\Ajax\SwitchSettingsUiEndpoint;
|
use WooCommerce\PayPalCommerce\Settings\Ajax\SwitchSettingsUiEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\Settings\Data\Definition\FeaturesDefinition;
|
use WooCommerce\PayPalCommerce\Settings\Data\Definition\FeaturesDefinition;
|
||||||
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
|
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
|
||||||
|
@ -487,12 +488,6 @@ return array(
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'settings.data.definition.features' => static function ( ContainerInterface $container ) : FeaturesDefinition {
|
'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(
|
$features = apply_filters(
|
||||||
'woocommerce_paypal_payments_rest_common_merchant_features',
|
'woocommerce_paypal_payments_rest_common_merchant_features',
|
||||||
array()
|
array()
|
||||||
|
@ -505,8 +500,7 @@ return array(
|
||||||
$gateways = array(
|
$gateways = array(
|
||||||
'card-button' => $settings['data']['ppcp-card-button-gateway']['enabled'] ?? false,
|
'card-button' => $settings['data']['ppcp-card-button-gateway']['enabled'] ?? false,
|
||||||
);
|
);
|
||||||
|
// Merchant capabilities, serve to show active or inactive badge and buttons.
|
||||||
// Merchant eligibility.
|
|
||||||
$capabilities = array(
|
$capabilities = array(
|
||||||
'apple_pay' => $features['apple_pay']['enabled'] ?? false,
|
'apple_pay' => $features['apple_pay']['enabled'] ?? false,
|
||||||
'google_pay' => $features['google_pay']['enabled'] ?? false,
|
'google_pay' => $features['google_pay']['enabled'] ?? false,
|
||||||
|
@ -515,14 +509,37 @@ return array(
|
||||||
'apm' => $features['alternative_payment_methods']['enabled'] ?? false,
|
'apm' => $features['alternative_payment_methods']['enabled'] ?? false,
|
||||||
'paylater' => $features['pay_later_messaging']['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(
|
return new FeaturesEligibilityService(
|
||||||
$capabilities['save_paypal'], // Save PayPal and Venmo eligibility.
|
$container->get( 'save-payment-methods.eligible' ), // Save PayPal and Venmo eligibility.
|
||||||
$capabilities['acdc'] && ! $gateways['card-button'], // Advanced credit and debit cards eligibility.
|
$container->get( 'card-fields.eligible' ), // Advanced credit and debit cards eligibility.
|
||||||
$capabilities['apm'], // Alternative payment methods eligibility.
|
$apm_eligible, // Alternative payment methods eligibility.
|
||||||
$capabilities['acdc'] && ! $capabilities['google_pay'], // Google Pay eligibility.
|
$container->get( 'googlepay.eligible' ), // Google Pay eligibility.
|
||||||
$capabilities['acdc'] && ! $capabilities['apple_pay'], // Apple Pay eligibility.
|
$container->get( 'applepay.eligible' ), // Apple Pay eligibility.
|
||||||
$capabilities['paylater'], // Pay Later eligibility.
|
$pay_later_eligible, // Pay Later eligibility.
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -35,18 +35,28 @@ class FeaturesDefinition {
|
||||||
*/
|
*/
|
||||||
protected GeneralSettings $settings;
|
protected GeneralSettings $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The merchant capabilities.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected array $merchant_capabilities;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param FeaturesEligibilityService $eligibilities The features eligibility service.
|
* @param FeaturesEligibilityService $eligibilities The features eligibility service.
|
||||||
* @param GeneralSettings $settings The general settings service.
|
* @param GeneralSettings $settings The general settings service.
|
||||||
|
* @param array $merchant_capabilities The merchant capabilities.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
FeaturesEligibilityService $eligibilities,
|
FeaturesEligibilityService $eligibilities,
|
||||||
GeneralSettings $settings
|
GeneralSettings $settings,
|
||||||
|
array $merchant_capabilities
|
||||||
) {
|
) {
|
||||||
$this->eligibilities = $eligibilities;
|
$this->eligibilities = $eligibilities;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
|
$this->merchant_capabilities = $merchant_capabilities;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,7 +65,23 @@ class FeaturesDefinition {
|
||||||
* @return array The array of feature definitions.
|
* @return array The array of feature definitions.
|
||||||
*/
|
*/
|
||||||
public function get(): array {
|
public function get(): array {
|
||||||
|
$all_features = $this->all_available_features();
|
||||||
|
$eligible_features = array();
|
||||||
$eligibility_checks = $this->eligibilities->get_eligibility_checks();
|
$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(
|
$paylater_countries = array(
|
||||||
'UK',
|
'UK',
|
||||||
'ES',
|
'ES',
|
||||||
|
@ -72,7 +98,7 @@ class FeaturesDefinition {
|
||||||
'save_paypal_and_venmo' => array(
|
'save_paypal_and_venmo' => array(
|
||||||
'title' => __( 'Save PayPal and Venmo', 'woocommerce-paypal-payments' ),
|
'title' => __( 'Save PayPal and Venmo', 'woocommerce-paypal-payments' ),
|
||||||
'description' => __( 'Securely save PayPal and Venmo payment methods for subscriptions or return buyers.', '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(
|
'buttons' => array(
|
||||||
array(
|
array(
|
||||||
'type' => 'secondary',
|
'type' => 'secondary',
|
||||||
|
@ -105,7 +131,7 @@ class FeaturesDefinition {
|
||||||
'advanced_credit_and_debit_cards' => array(
|
'advanced_credit_and_debit_cards' => array(
|
||||||
'title' => __( 'Advanced Credit and Debit Cards', 'woocommerce-paypal-payments' ),
|
'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' ),
|
'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(
|
'buttons' => array(
|
||||||
array(
|
array(
|
||||||
'type' => 'secondary',
|
'type' => 'secondary',
|
||||||
|
@ -141,7 +167,7 @@ class FeaturesDefinition {
|
||||||
'alternative_payment_methods' => array(
|
'alternative_payment_methods' => array(
|
||||||
'title' => __( 'Alternative Payment Methods', 'woocommerce-paypal-payments' ),
|
'title' => __( 'Alternative Payment Methods', 'woocommerce-paypal-payments' ),
|
||||||
'description' => __( 'Offer global, country-specific payment options for your customers.', '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(
|
'buttons' => array(
|
||||||
array(
|
array(
|
||||||
'type' => 'secondary',
|
'type' => 'secondary',
|
||||||
|
@ -173,7 +199,7 @@ class FeaturesDefinition {
|
||||||
'google_pay' => array(
|
'google_pay' => array(
|
||||||
'title' => __( 'Google Pay', 'woocommerce-paypal-payments' ),
|
'title' => __( 'Google Pay', 'woocommerce-paypal-payments' ),
|
||||||
'description' => __( 'Let customers pay using their Google Pay wallet.', '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(
|
'buttons' => array(
|
||||||
array(
|
array(
|
||||||
'type' => 'secondary',
|
'type' => 'secondary',
|
||||||
|
@ -212,7 +238,7 @@ class FeaturesDefinition {
|
||||||
'apple_pay' => array(
|
'apple_pay' => array(
|
||||||
'title' => __( 'Apple Pay', 'woocommerce-paypal-payments' ),
|
'title' => __( 'Apple Pay', 'woocommerce-paypal-payments' ),
|
||||||
'description' => __( 'Let customers pay using their Apple Pay wallet.', '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(
|
'buttons' => array(
|
||||||
array(
|
array(
|
||||||
'type' => 'secondary',
|
'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.',
|
'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'
|
'woocommerce-paypal-payments'
|
||||||
),
|
),
|
||||||
'isEligible' => $eligibility_checks['pay_later'],
|
'enabled' => $this->merchant_capabilities['pay_later'],
|
||||||
'buttons' => array(
|
'buttons' => array(
|
||||||
array(
|
array(
|
||||||
'type' => 'secondary',
|
'type' => 'secondary',
|
||||||
|
|
|
@ -86,14 +86,11 @@ class FeaturesRestEndpoint extends RestEndpoint {
|
||||||
|
|
||||||
$features = array();
|
$features = array();
|
||||||
foreach ( $this->features_definition->get() as $id => $feature ) {
|
foreach ( $this->features_definition->get() as $id => $feature ) {
|
||||||
// Check eligibility and add to features if eligible.
|
|
||||||
if ( $feature['isEligible']() ) {
|
|
||||||
$features[] = array_merge(
|
$features[] = array_merge(
|
||||||
array( 'id' => $id ),
|
array( 'id' => $id ),
|
||||||
array_diff_key( $feature, array( 'isEligible' => true ) )
|
$feature
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $this->return_success(
|
return $this->return_success(
|
||||||
array(
|
array(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue