Add feature detection to MerchantDetails class

This commit is contained in:
Philipp Stracker 2025-06-11 17:43:46 +02:00
parent 66fa7db02b
commit 4344aae3a7
No known key found for this signature in database
4 changed files with 96 additions and 8 deletions

View file

@ -104,9 +104,10 @@ return array(
return $state->get_environment();
},
'settings.merchant-details' => static function ( ContainerInterface $container ) : MerchantDetails {
$woo_country = $container->get( 'api.shop.country' );
$woo_country = $container->get( 'api.shop.country' );
$eligibility_checks = $container->get( 'wcgateway.feature-eligibility.list' );
return new MerchantDetails( $woo_country, $woo_country );
return new MerchantDetails( $woo_country, $woo_country, $eligibility_checks );
},
'onboarding.assets' => function( ContainerInterface $container ) : OnboardingAssets {
$state = $container->get( 'onboarding.state' );

View file

@ -672,6 +672,8 @@ return array(
$merchant_country = $data->get_merchant_country();
$woo_data = $data->get_woo_settings();
return new MerchantDetails( $merchant_country, $woo_data['country'] );
$eligibility_checks = $container->get( 'wcgateway.feature-eligibility.list' );
return new MerchantDetails( $merchant_country, $woo_data['country'], $eligibility_checks );
},
);

View file

@ -2126,6 +2126,23 @@ return array(
);
};
},
/**
* Returns a centralized list of feature eligibility checks.
*
* This is a helper service which is used by the `MerchantDetails` class and
* should not be directly accessed.
*/
'wcgateway.feature-eligibility.list' => static function( ContainerInterface $container ): array {
return array(
MerchantDetails::FEATURE_SAVE_PAYPAL_VENMO => $container->get( 'save-payment-methods.eligibility.check' ),
MerchantDetails::FEATURE_ADVANCED_CARD_PROCESSING => $container->get( 'card-fields.eligibility.check' ),
MerchantDetails::FEATURE_GOOGLE_PAY => $container->get( 'googlepay.eligibility.check' ),
MerchantDetails::FEATURE_APPLE_PAY => $container->get( 'applepay.eligibility.check' ),
MerchantDetails::FEATURE_CONTACT_MODULE => $container->get( 'wcgateway.contact-module.eligibility.check' ),
);
},
/**
* Returns a prefix for the site, ensuring the same site always gets the same prefix (unless the URL changes).
*/

View file

@ -13,6 +13,34 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
* Main information source about merchant details.
*/
class MerchantDetails {
/**
* Save tokenized PayPal and Venmo payment details, required for subscriptions and saving
* payment methods in user account.
*/
public const FEATURE_SAVE_PAYPAL_VENMO = 'save_paypal_venmo';
/**
* Advanced card processing eligibility. Required for credit- and debit-card processing.
*/
public const FEATURE_ADVANCED_CARD_PROCESSING = 'acdc';
/**
* Merchant eligibility to use Google Pay.
*/
public const FEATURE_GOOGLE_PAY = 'googlepay';
/**
* Whether Apple Pay can be used by the merchant. Apple Pay requires an Apple device (like
* iPhone) to be used by customers.
*/
public const FEATURE_APPLE_PAY = 'applepay';
/**
* Contact module allows the merchant to unlock the "Custom Shipping Contact" toggle.
*/
public const FEATURE_CONTACT_MODULE = 'contact_module';
/**
* The merchant's country according to PayPal, which might be different from
* the WooCommerce country.
@ -30,15 +58,27 @@ class MerchantDetails {
*/
private string $store_country;
/**
* A collection of feature eligibility checks. The value can be either a
* boolean (static eligibility) or a callback that returns a boolean (lazy check).
*
* @var array
*/
private array $eligibility_checks;
/**
* Constructor.
*
* @param string $merchant_country Initial merchant country.
* @param string $store_country Initial store country.
* @param string $merchant_country Merchant country provided by PayPal's API. Not editable.
* @param string $store_country WooCommerce store country, can be changed by the site
* admin via the WooCommerce settings.
* @param array $eligibility_checks Array of eligibility checks. Default service:
* 'wcgateway.feature-eligibility.list'.
*/
public function __construct( string $merchant_country, string $store_country ) {
$this->merchant_country = $merchant_country;
$this->store_country = $store_country;
public function __construct( string $merchant_country, string $store_country, array $eligibility_checks ) {
$this->merchant_country = $merchant_country;
$this->store_country = $store_country;
$this->eligibility_checks = $eligibility_checks;
}
/**
@ -63,4 +103,32 @@ class MerchantDetails {
public function get_shop_country() : string {
return $this->store_country;
}
/**
* Tests, if the merchant is eligible to use a certain feature.
*
* Note:
* To register features for detection by this method, the features must be
* present in the service `wcgateway.contact-module.eligibility.check`, and
* also define a public FEATURE_* const in the class header.
* Adding all features is an ongoing task.
*
* @param string $feature One of the public self::FEATURE_* values.
* @return bool Whether the merchant can use the relevant feature.
*/
public function is_eligible_for( string $feature ) : bool {
if ( ! in_array( $feature, $this->eligibility_checks, true ) ) {
return false;
}
$check = $this->eligibility_checks[ $feature ];
if ( is_bool( $check ) ) {
return $check;
}
if ( is_callable( $check ) ) {
return (bool) $check();
}
return false;
}
}