diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 6a8b2ff5d..258b0379e 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway; +use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint; @@ -28,6 +29,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer; use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction; +use WooCommerce\PayPalCommerce\WcGateway\Assets\FraudNetAssets; use WooCommerce\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use WooCommerce\PayPalCommerce\WcGateway\Checkout\DisableGateways; use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; @@ -168,7 +170,7 @@ return array( CreditCardGateway::ID, PayUponInvoiceGateway::ID, CardButtonGateway::ID, - OXXOGateway::ID . + OXXOGateway::ID, Settings::PAY_LATER_TAB_ID, ), true @@ -1923,4 +1925,71 @@ return array( return $button_locations; }, + 'wcgateway.ppcp-gateways' => static function ( ContainerInterface $container ): array { + return array( + PayPalGateway::ID, + CreditCardGateway::ID, + PayUponInvoiceGateway::ID, + CardButtonGateway::ID, + OXXOGateway::ID, + ); + }, + 'wcgateway.enabled-ppcp-gateways' => static function ( ContainerInterface $container ): array { + $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); + $ppcp_gateways = $container->get( 'wcgateway.ppcp-gateways' ); + + foreach ( $ppcp_gateways as $gateway ) { + if ( ! isset( $available_gateways[ $gateway ] ) ) { + continue; + } + $enabled_ppcp_gateways[] = $gateway; + } + + return $enabled_ppcp_gateways ?? array(); + }, + '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.fraudnet-assets' => function( ContainerInterface $container ) : FraudNetAssets { + return new FraudNetAssets( + $container->get( 'wcgateway.url' ), + $container->get( 'ppcp.asset-version' ), + $container->get( 'wcgateway.pay-upon-invoice-fraudnet' ), + $container->get( 'onboarding.environment' ), + $container->get( 'wcgateway.settings' ), + $container->get( 'wcgateway.enabled-ppcp-gateways' ), + $container->get( 'wcgateway.current-context' ) + ); + }, ); diff --git a/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php b/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php index 05f90c9be..df7d2653c 100644 --- a/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php @@ -1,6 +1,6 @@ module_url = $module_url; - $this->version = $version; - $this->fraud_net = $fraud_net; - $this->session_handler = $session_handler; - $this->environment = $environment; + $this->module_url = $module_url; + $this->version = $version; + $this->fraud_net = $fraud_net; + $this->environment = $environment; + $this->settings = $settings; + $this->enabled_ppcp_gateways = $enabled_ppcp_gateways; + $this->context = $context; } /** - * Register assets provided by this module. + * Registers FraudNet assets. */ public function register_assets() { add_action( 'wp_enqueue_scripts', function() { - $gateway_settings = get_option( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' ); - $gateway_enabled = $gateway_settings['enabled'] ?? ''; - if ( $gateway_enabled === 'yes' && ! $this->session_handler->order() && ( is_checkout() || is_checkout_pay_page() ) ) { + if ( $this->should_load_fraudnet_script() ) { wp_enqueue_script( - 'ppcp-pay-upon-invoice', - trailingslashit( $this->module_url ) . 'assets/js/pay-upon-invoice.js', + 'ppcp-fraudnet', + trailingslashit( $this->module_url ) . 'assets/js/fraudnet.js', array(), $this->version, true ); wp_localize_script( - 'ppcp-pay-upon-invoice', + 'ppcp-fraudnet', 'FraudNetConfig', array( 'f' => $this->fraud_net->session_id(), @@ -106,6 +126,52 @@ class FraudNetAssets { } } ); + } + + /** + * Checks if FraudNet script should be loaded. + * + * @return bool true if FraudNet script should be loaded, otherwise false. + */ + protected function should_load_fraudnet_script(): bool { + if ( empty( $this->enabled_ppcp_gateways ) ) { + return false; + } + + $is_fraudnet_enabled = $this->settings->has( 'fraudnet_enabled' ) && $this->settings->get( 'fraudnet_enabled' ); + $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 ) { + return $is_fraudnet_enabled && $this->are_buttons_enabled_for_context(); + } + + return $is_pui_gateway_enabled ? true : $is_fraudnet_enabled; } + + /** + * Checks if buttons are enabled for current context. + * + * @return bool true if enabled, otherwise false. + */ + 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' ); + } + + if ( $this->context === 'pay-now' ) { + return true; + } + + return $buttons_enabled_for_context; + } } diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 07645ccd5..3de5529bb 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -22,6 +22,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer; use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction; +use WooCommerce\PayPalCommerce\WcGateway\Assets\FraudNetAssets; use WooCommerce\PayPalCommerce\WcGateway\Assets\SettingsPageAssets; use WooCommerce\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use WooCommerce\PayPalCommerce\WcGateway\Checkout\DisableGateways; @@ -258,6 +259,10 @@ class WCGatewayModule implements ModuleInterface { } ( $c->get( 'wcgateway.oxxo' ) )->init(); + + $fraudnet_assets = $c->get( 'wcgateway.fraudnet-assets' ); + assert( $fraudnet_assets instanceof FraudNetAssets ); + $fraudnet_assets->register_assets(); } ); diff --git a/modules/ppcp-wc-gateway/webpack.config.js b/modules/ppcp-wc-gateway/webpack.config.js index 009952fa6..bd4a22fdc 100644 --- a/modules/ppcp-wc-gateway/webpack.config.js +++ b/modules/ppcp-wc-gateway/webpack.config.js @@ -7,7 +7,7 @@ module.exports = { target: 'web', entry: { 'gateway-settings': path.resolve('./resources/js/gateway-settings.js'), - 'pay-upon-invoice': path.resolve('./resources/js/pay-upon-invoice.js'), + 'fraudnet': path.resolve('./resources/js/fraudnet.js'), 'oxxo': path.resolve('./resources/js/oxxo.js'), 'gateway-settings-style': path.resolve('./resources/css/gateway-settings.scss'), },