From 43f4d15cea9937195500191446186ff60636f383 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 28 Jun 2024 14:47:23 +0200 Subject: [PATCH] Extract disabled funding sources logic into a new class --- modules/ppcp-button/services.php | 13 ++- .../ppcp-button/src/Assets/SmartButton.php | 61 ++++-------- .../src/Helper/DisabledFundingSources.php | 97 +++++++++++++++++++ 3 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 modules/ppcp-button/src/Helper/DisabledFundingSources.php diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index aa518f13f..069355da6 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper; use WooCommerce\PayPalCommerce\Button\Helper\CheckoutFormSaver; use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint; use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; +use WooCommerce\PayPalCommerce\Button\Helper\DisabledFundingSources; use WooCommerce\PayPalCommerce\Button\Helper\WooCommerceOrderCreator; use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator; use WooCommerce\PayPalCommerce\Button\Endpoint\ValidateCheckoutEndpoint; @@ -149,7 +150,8 @@ return array( $container->get( 'vaulting.vault-v3-enabled' ), $container->get( 'api.endpoint.payment-tokens' ), $container->get( 'woocommerce.logger.woocommerce' ), - $container->get( 'button.handle-shipping-in-paypal' ) + $container->get( 'button.handle-shipping-in-paypal' ), + $container->get( 'button.helper.disabled-funding-sources' ) ); }, 'button.url' => static function ( ContainerInterface $container ): string { @@ -306,12 +308,10 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, - 'button.helper.cart-products' => static function ( ContainerInterface $container ): CartProductsHelper { $data_store = \WC_Data_Store::load( 'product' ); return new CartProductsHelper( $data_store ); }, - 'button.helper.three-d-secure' => static function ( ContainerInterface $container ): ThreeDSecure { return new ThreeDSecure( $container->get( 'api.factory.card-authentication-result-factory' ), @@ -323,7 +323,12 @@ return array( $container->get( 'api.shop.country' ) ); }, - + 'button.helper.disabled-funding-sources' => static function ( ContainerInterface $container ): DisabledFundingSources { + return new DisabledFundingSources( + $container->get( 'wcgateway.settings' ), + $container->get( 'wcgateway.all-funding-sources' ) + ); + }, 'button.is-logged-in' => static function ( ContainerInterface $container ): bool { return is_user_logged_in(); }, diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 202693929..bf4b3d3a0 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -32,6 +32,7 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\SimulateCartEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\ValidateCheckoutEndpoint; use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; +use WooCommerce\PayPalCommerce\Button\Helper\DisabledFundingSources; use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\PayLaterBlock\PayLaterBlockModule; @@ -225,6 +226,13 @@ class SmartButton implements SmartButtonInterface { */ private $should_handle_shipping_in_paypal; + /** + * List of funding sources to be disabled. + * + * @var DisabledFundingSources + */ + private $disabled_funding_sources; + /** * SmartButton constructor. * @@ -251,6 +259,7 @@ class SmartButton implements SmartButtonInterface { * @param PaymentTokensEndpoint $payment_tokens_endpoint Payment tokens endpoint. * @param LoggerInterface $logger The logger. * @param bool $should_handle_shipping_in_paypal Whether the shipping should be handled in PayPal. + * @param DisabledFundingSources $disabled_funding_sources List of funding sources to be disabled. */ public function __construct( string $module_url, @@ -275,7 +284,8 @@ class SmartButton implements SmartButtonInterface { bool $vault_v3_enabled, PaymentTokensEndpoint $payment_tokens_endpoint, LoggerInterface $logger, - bool $should_handle_shipping_in_paypal + bool $should_handle_shipping_in_paypal, + DisabledFundingSources $disabled_funding_sources ) { $this->module_url = $module_url; @@ -301,6 +311,7 @@ class SmartButton implements SmartButtonInterface { $this->logger = $logger; $this->payment_tokens_endpoint = $payment_tokens_endpoint; $this->should_handle_shipping_in_paypal = $should_handle_shipping_in_paypal; + $this->disabled_funding_sources = $disabled_funding_sources; } /** @@ -1400,54 +1411,18 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages } } - $disable_funding = $this->settings->has( 'disable_funding' ) - ? $this->settings->get( 'disable_funding' ) - : array(); - - if ( ! is_checkout() ) { - $disable_funding[] = 'card'; - } - - $is_dcc_enabled = $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ); - - $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); - $is_separate_card_enabled = isset( $available_gateways[ CardButtonGateway::ID ] ); - - if ( is_checkout() && ( $is_dcc_enabled || $is_separate_card_enabled ) ) { - $key = array_search( 'card', $disable_funding, true ); - if ( false !== $key ) { - unset( $disable_funding[ $key ] ); - } - } - - if ( in_array( $context, array( 'checkout-block', 'cart-block' ), true ) ) { - $disable_funding = array_merge( - $disable_funding, - array_diff( - array_keys( $this->all_funding_sources ), - array( 'venmo', 'paylater', 'paypal', 'card' ) - ) - ); - } - - if ( $this->is_free_trial_cart() ) { - $all_sources = array_keys( $this->all_funding_sources ); - if ( $is_dcc_enabled || $is_separate_card_enabled ) { - $all_sources = array_diff( $all_sources, array( 'card' ) ); - } - $disable_funding = $all_sources; - } + $disabled_funding_sources = $this->disabled_funding_sources->sources( $context ); $enable_funding = array( 'venmo' ); if ( $this->is_pay_later_button_enabled_for_location( $context ) ) { $enable_funding[] = 'paylater'; } else { - $disable_funding[] = 'paylater'; + $disabled_funding_sources[] = 'paylater'; } - $disable_funding = array_filter( - $disable_funding, + $disabled_funding_sources = array_filter( + $disabled_funding_sources, /** * Make sure paypal is not sent in disable funding. * @@ -1460,8 +1435,8 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages } ); - if ( count( $disable_funding ) > 0 ) { - $params['disable-funding'] = implode( ',', array_unique( $disable_funding ) ); + if ( count( $disabled_funding_sources ) > 0 ) { + $params['disable-funding'] = implode( ',', array_unique( $disabled_funding_sources ) ); } if ( $this->is_free_trial_cart() ) { diff --git a/modules/ppcp-button/src/Helper/DisabledFundingSources.php b/modules/ppcp-button/src/Helper/DisabledFundingSources.php new file mode 100644 index 000000000..acab7d5f7 --- /dev/null +++ b/modules/ppcp-button/src/Helper/DisabledFundingSources.php @@ -0,0 +1,97 @@ +settings = $settings; + $this->all_funding_sources = $all_funding_sources; + } + + /** + * Returns the list of funding sources to be disabled. + * + * @param string $context The context. + * @return array|int[]|mixed|string[] + * @throws NotFoundException When the setting is not found. + */ + public function sources( $context ) { + $disable_funding = $this->settings->has( 'disable_funding' ) + ? $this->settings->get( 'disable_funding' ) + : array(); + + if ( ! is_checkout() ) { + $disable_funding[] = 'card'; + } + + $is_dcc_enabled = $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ); + + $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); + $is_separate_card_enabled = isset( $available_gateways[ CardButtonGateway::ID ] ); + + if ( is_checkout() && ( $is_dcc_enabled || $is_separate_card_enabled ) ) { + $key = array_search( 'card', $disable_funding, true ); + if ( false !== $key ) { + unset( $disable_funding[ $key ] ); + } + } + + if ( in_array( $context, array( 'checkout-block', 'cart-block' ), true ) ) { + $disable_funding = array_merge( + $disable_funding, + array_diff( + array_keys( $this->all_funding_sources ), + array( 'venmo', 'paylater', 'paypal', 'card' ) + ) + ); + } + + if ( $this->is_free_trial_cart() ) { + $all_sources = array_keys( $this->all_funding_sources ); + if ( $is_dcc_enabled || $is_separate_card_enabled ) { + $all_sources = array_diff( $all_sources, array( 'card' ) ); + } + $disable_funding = $all_sources; + } + + return $disable_funding; + } +}