diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 53af6033e..5956619fb 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -23,6 +23,7 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData; use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint; +use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Session\SessionHandler; @@ -41,7 +42,7 @@ */ class SmartButton implements SmartButtonInterface { - use FreeTrialHandlerTrait; + use FreeTrialHandlerTrait, ContextTrait; /** * The Settings status helper. @@ -1067,48 +1068,6 @@ private function load_button_component() : bool { } } - /** - * The current context. - * - * @return string - */ - private function context(): string { - $context = 'mini-cart'; - if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) { - $context = 'product'; - } - if ( is_cart() ) { - $context = 'cart'; - } - if ( is_checkout() && ! $this->is_paypal_continuation() ) { - $context = 'checkout'; - } - if ( is_checkout_pay_page() ) { - $context = 'pay-now'; - } - return $context; - } - - /** - * Checks if PayPal payment was already initiated (on the product or cart pages). - * - * @return bool - */ - private function is_paypal_continuation(): bool { - $order = $this->session_handler->order(); - if ( ! $order ) { - return false; - } - $source = $order->payment_source(); - if ( $source && $source->card() ) { - return false; // Ignore for DCC. - } - if ( 'card' === $this->session_handler->funding_source() ) { - return false; // Ignore for card buttons. - } - return true; - } - /** * Whether DCC is enabled or not. * diff --git a/modules/ppcp-button/src/Helper/ContextTrait.php b/modules/ppcp-button/src/Helper/ContextTrait.php new file mode 100644 index 000000000..79abfa181 --- /dev/null +++ b/modules/ppcp-button/src/Helper/ContextTrait.php @@ -0,0 +1,62 @@ +is_paypal_continuation() ) { + $context = 'checkout'; + } + + if ( is_checkout_pay_page() ) { + $context = 'pay-now'; + } + + return $context; + } + + /** + * Checks if PayPal payment was already initiated (on the product or cart pages). + * + * @return bool + */ + private function is_paypal_continuation(): bool { + $order = $this->session_handler->order(); + if ( ! $order ) { + return false; + } + + $source = $order->payment_source(); + if ( $source && $source->card() ) { + return false; // Ignore for DCC. + } + + if ( 'card' === $this->session_handler->funding_source() ) { + return false; // Ignore for card buttons. + } + + return true; + } +} diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index f5be3163f..d4c433404 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1333,39 +1333,6 @@ static function ( ContainerInterface $container ): AuthorizeOrderActionNotice { return $enabled_ppcp_gateways; }, - 'wcgateway.is-paypal-continuation' => static function ( ContainerInterface $container ): bool { - $session_handler = $container->get( 'session.handler' ); - assert( $session_handler instanceof SessionHandler ); - - $order = $session_handler->order(); - if ( ! $order ) { - return false; - } - $source = $order->payment_source(); - if ( $source && $source->card() ) { - return false; // Ignore for DCC. - } - if ( 'card' === $session_handler->funding_source() ) { - return false; // Ignore for card buttons. - } - return true; - }, - 'wcgateway.current-context' => static function ( ContainerInterface $container ): string { - $context = 'mini-cart'; - if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) { - $context = 'product'; - } - if ( is_cart() ) { - $context = 'cart'; - } - if ( is_checkout() && ! $container->get( 'wcgateway.is-paypal-continuation' ) ) { - $context = 'checkout'; - } - if ( is_checkout_pay_page() ) { - $context = 'pay-now'; - } - return $context; - }, 'wcgateway.is-fraudnet-enabled' => static function ( ContainerInterface $container ): bool { $settings = $container->get( 'wcgateway.settings' ); assert( $settings instanceof Settings ); @@ -1380,7 +1347,7 @@ static function ( ContainerInterface $container ): AuthorizeOrderActionNotice { $container->get( 'onboarding.environment' ), $container->get( 'wcgateway.settings' ), $container->get( 'wcgateway.enabled-ppcp-gateways' ), - $container->get( 'wcgateway.current-context' ), + $container->get( 'session.handler' ), $container->get( 'wcgateway.is-fraudnet-enabled' ) ); }, diff --git a/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php b/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php index 6ab3f7d98..63f41e8c3 100644 --- a/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php @@ -9,7 +9,10 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Assets; +use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; use WooCommerce\PayPalCommerce\Onboarding\Environment; +use WooCommerce\PayPalCommerce\Session\SessionHandler; +use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway; @@ -20,6 +23,8 @@ */ class FraudNetAssets { + use ContextTrait; + /** * The URL of this module. * @@ -63,11 +68,11 @@ class FraudNetAssets { protected $enabled_ppcp_gateways; /** - * The current context. + * The session handler * - * @var string + * @var SessionHandler */ - protected $context; + protected $session_handler; /** * True if FraudNet support is enabled in settings, otherwise false. @@ -79,14 +84,14 @@ class FraudNetAssets { /** * Assets constructor. * - * @param string $module_url The url of this module. - * @param string $version The assets version. - * @param FraudNet $fraud_net The FraudNet entity. - * @param Environment $environment The environment. - * @param Settings $settings The Settings. - * @param string[] $enabled_ppcp_gateways The list of enabled PayPal gateways. - * @param string $context The current context. - * @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false. + * @param string $module_url The url of this module. + * @param string $version The assets version. + * @param FraudNet $fraud_net The FraudNet entity. + * @param Environment $environment The environment. + * @param Settings $settings The Settings. + * @param string[] $enabled_ppcp_gateways The list of enabled PayPal gateways. + * @param SessionHandler $session_handler The session handler. + * @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false. */ public function __construct( string $module_url, @@ -95,7 +100,7 @@ public function __construct( Environment $environment, Settings $settings, array $enabled_ppcp_gateways, - string $context, + SessionHandler $session_handler, bool $is_fraudnet_enabled ) { $this->module_url = $module_url; @@ -104,7 +109,7 @@ public function __construct( $this->environment = $environment; $this->settings = $settings; $this->enabled_ppcp_gateways = $enabled_ppcp_gateways; - $this->context = $context; + $this->session_handler = $session_handler; $this->is_fraudnet_enabled = $is_fraudnet_enabled; } @@ -151,12 +156,11 @@ protected function should_load_fraudnet_script(): bool { $is_pui_gateway_enabled = in_array( PayUponInvoiceGateway::ID, $this->enabled_ppcp_gateways, true ); $is_only_standard_gateway_enabled = $this->enabled_ppcp_gateways === array( PayPalGateway::ID ); - if ( $this->context !== 'checkout' || $is_only_standard_gateway_enabled ) { + if ( $this->context() !== 'checkout' || $is_only_standard_gateway_enabled ) { return $this->is_fraudnet_enabled && $this->are_buttons_enabled_for_context(); } return $is_pui_gateway_enabled ? true : $this->is_fraudnet_enabled; - } /** @@ -168,19 +172,20 @@ protected function are_buttons_enabled_for_context() : bool { if ( ! in_array( PayPalGateway::ID, $this->enabled_ppcp_gateways, true ) ) { return false; } - - $location_prefix = $this->context === 'checkout' ? '' : "{$this->context}_"; - $setting_name = "button_{$location_prefix}enabled"; - $buttons_enabled_for_context = $this->settings->has( $setting_name ) && $this->settings->get( $setting_name ); - - if ( $this->context === 'product' ) { - return $buttons_enabled_for_context || $this->settings->has( 'mini-cart' ) && $this->settings->get( 'mini-cart' ); + try { + $button_locations = $this->settings->get( 'smart_button_locations' ); + } catch ( NotFoundException $exception ) { + return false; } - if ( $this->context === 'pay-now' ) { + if ( $this->context() === 'pay-now' ) { return true; } - return $buttons_enabled_for_context; + if ( $this->context() === 'product' ) { + return in_array( 'product', $button_locations, true ) || in_array( 'mini-cart', $button_locations, true ); + } + + return in_array( $this->context(), $button_locations, true ); } }