diff --git a/modules/ppcp-button/resources/js/modules/Renderer/Renderer.js b/modules/ppcp-button/resources/js/modules/Renderer/Renderer.js index c8df1a393..357225a6c 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/Renderer.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/Renderer.js @@ -11,7 +11,7 @@ class Renderer { } renderButtons(wrapper, contextConfig) { - if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper)) { + if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper) || 'undefined' === typeof paypal.Buttons ) { return; } diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index d7fa3d916..9e2482268 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -60,8 +60,7 @@ return array( } $settings = $container->get( 'wcgateway.settings' ); $paypal_disabled = ! $settings->has( 'enabled' ) || ! $settings->get( 'enabled' ); - $credit_card_disabled = ! $settings->has( 'dcc_gateway_enabled' ) || ! $settings->get( 'dcc_gateway_enabled' ); - if ( $paypal_disabled && $credit_card_disabled ) { + if ( $paypal_disabled ) { return new DisabledSmartButton(); } $payee_repository = $container->get( 'api.repository.payee' ); diff --git a/modules/ppcp-button/src/Assets/class-smartbutton.php b/modules/ppcp-button/src/Assets/class-smartbutton.php index 0c156b1ec..a3bc2fcdf 100644 --- a/modules/ppcp-button/src/Assets/class-smartbutton.php +++ b/modules/ppcp-button/src/Assets/class-smartbutton.php @@ -165,8 +165,8 @@ class SmartButton implements SmartButtonInterface { } if ( - $this->settings->has( 'dcc_gateway_enabled' ) - && $this->settings->get( 'dcc_gateway_enabled' ) + $this->settings->has( 'dcc_enabled' ) + && $this->settings->get( 'dcc_enabled' ) && ! $this->session_handler->order() ) { add_action( @@ -312,25 +312,39 @@ class SmartButton implements SmartButtonInterface { if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) { return false; } - wp_enqueue_style( - 'ppcp-hosted-fields', - $this->module_url . '/assets/css/hosted-fields.css', - array(), - 1 - ); - wp_enqueue_script( - 'ppcp-smart-button', - $this->module_url . '/assets/js/button.js', - array( 'jquery' ), - 1, - true - ); - wp_localize_script( - 'ppcp-smart-button', - 'PayPalCommerceGateway', - $this->localize_script() - ); + if ( $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) { + wp_enqueue_style( + 'ppcp-hosted-fields', + $this->module_url . '/assets/css/hosted-fields.css', + array(), + 1 + ); + } + + $load_script = false; + if ( is_checkout() && $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) { + $load_script = true; + } + if ( $this->load_button_component() ) { + $load_script = true; + } + + if ( $load_script ) { + wp_enqueue_script( + 'ppcp-smart-button', + $this->module_url . '/assets/js/button.js', + array( 'jquery' ), + 1, + true + ); + + wp_localize_script( + 'ppcp-smart-button', + 'PayPalCommerceGateway', + $this->localize_script() + ); + } return true; } @@ -732,7 +746,11 @@ class SmartButton implements SmartButtonInterface { * @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found. */ private function components(): array { - $components = array( 'buttons' ); + $components = array(); + + if ( $this->load_button_component() ) { + $components[] = 'buttons'; + } if ( $this->messages_apply->for_country() ) { $components[] = 'messages'; } @@ -742,6 +760,45 @@ class SmartButton implements SmartButtonInterface { return $components; } + /** + * Determines whether the button component should be loaded. + * + * @return bool + * @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found. + */ + private function load_button_component() : bool { + + $load_buttons = false; + if ( + $this->context() === 'checkout' + && $this->settings->has( 'button_enabled' ) + && $this->settings->get( 'button_enabled' ) + ) { + $load_buttons = true; + } + if ( + $this->context() === 'product' + && $this->settings->has( 'button_product_enabled' ) + && $this->settings->get( 'button_product_enabled' ) + ) { + $load_buttons = true; + } + if ( + $this->settings->has( 'button_mini-cart_enabled' ) + && $this->settings->get( 'button_mini-cart_enabled' ) + ) { + $load_buttons = true; + } + if ( + $this->context() === 'cart' + && $this->settings->has( 'button_cart_enabled' ) + && $this->settings->get( 'button_cart_enabled' ) + ) { + $load_buttons = true; + } + return $load_buttons; + } + /** * The current context. * @@ -772,7 +829,7 @@ class SmartButton implements SmartButtonInterface { return false; } $keys = array( - 'dcc_gateway_enabled' => 'is_checkout', + 'dcc_enabled' => 'is_checkout', ); foreach ( $keys as $key => $callback ) { if ( $this->settings->has( $key ) && $this->settings->get( $key ) && $callback() ) { @@ -811,6 +868,6 @@ class SmartButton implements SmartButtonInterface { if ( is_bool( $value ) ) { $value = $value ? 'true' : 'false'; } - return $value; + return (string) $value; } } diff --git a/modules/ppcp-onboarding/assets/js/settings.js b/modules/ppcp-onboarding/assets/js/settings.js index f240f224f..b38a5369c 100644 --- a/modules/ppcp-onboarding/assets/js/settings.js +++ b/modules/ppcp-onboarding/assets/js/settings.js @@ -12,7 +12,6 @@ const groupToggle = (selector, group) => { 'change', (event) => { - if (! event.target.checked) { group.forEach( (elementToHide) => { document.querySelector(elementToHide).style.display = 'none'; @@ -20,8 +19,8 @@ const groupToggle = (selector, group) => { return; } - group.forEach( (elementToHide) => { - document.querySelector(elementToHide).style.display = 'table-row'; + group.forEach( (elementToShow) => { + document.querySelector(elementToShow).style.display = 'table-row'; }) } ); @@ -34,23 +33,27 @@ const groupToggleSelect = (selector, group) => { return; } const value = toggleElement.value; - group.forEach( (elementToHide) => { - if (value === elementToHide.value) { - document.querySelector(elementToHide.selector).style.display = 'table-row'; + group.forEach( (elementToToggle) => { + const domElement = document.querySelector(elementToToggle.selector); + if (! domElement) { return; } - document.querySelector(elementToHide.selector).style.display = 'none'; + if (value === elementToToggle.value && domElement.style.display !== 'none') { + domElement.style.display = 'table-row'; + return; + } + domElement.style.display = 'none'; }) toggleElement.addEventListener( 'change', (event) => { const value = event.target.value; - group.forEach( (elementToHide) => { - if (value === elementToHide.value) { - document.querySelector(elementToHide.selector).style.display = 'table-row'; + group.forEach( (elementToToggle) => { + if (value === elementToToggle.value) { + document.querySelector(elementToToggle.selector).style.display = 'table-row'; return; } - document.querySelector(elementToHide.selector).style.display = 'none'; + document.querySelector(elementToToggle.selector).style.display = 'none'; }) } ); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index ad334a12b..8ca46f73e 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -23,6 +23,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor; +use Inpsyde\PayPalCommerce\WcGateway\Settings\SectionsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsListener; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; @@ -81,6 +82,9 @@ return array( static function ( ContainerInterface $container ): AuthorizeOrderActionNotice { return new AuthorizeOrderActionNotice(); }, + 'wcgateway.settings.sections-renderer' => static function ( ContainerInterface $container ): SectionsRenderer { + return new SectionsRenderer(); + }, 'wcgateway.settings.render' => static function ( ContainerInterface $container ): SettingsRenderer { $settings = $container->get( 'wcgateway.settings' ); $state = $container->get( 'onboarding.state' ); @@ -178,7 +182,7 @@ return array( State::STATE_PROGRESSIVE, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'sandbox_on' => array( 'title' => __( 'Sandbox', 'paypal-for-woocommerce' ), @@ -189,7 +193,7 @@ return array( State::STATE_START, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'sandbox_on_info' => array( 'title' => __( 'Sandbox', 'paypal-for-woocommerce' ), @@ -201,7 +205,7 @@ return array( ), 'hidden' => 'sandbox_on', 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'merchant_email' => array( 'title' => __( 'Email address', 'paypal-for-woocommerce' ), @@ -214,7 +218,7 @@ return array( State::STATE_START, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'merchant_email_info' => array( 'title' => __( 'Email address', 'paypal-for-woocommerce' ), @@ -226,7 +230,7 @@ return array( ), 'hidden' => 'merchant_email', 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'toggle_manual_input' => array( 'type' => 'ppcp-text', @@ -238,7 +242,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'client_id' => array( 'title' => __( 'Client Id', 'paypal-for-woocommerce' ), @@ -252,7 +256,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'client_secret' => array( 'title' => __( 'Secret Key', 'paypal-for-woocommerce' ), @@ -266,7 +270,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'title' => array( 'title' => __( 'Title', 'paypal-for-woocommerce' ), @@ -284,6 +288,21 @@ return array( 'requirements' => array(), 'gateway' => 'paypal', ), + 'dcc_enabled' => array( + 'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ), + 'desc_tip' => true, + 'description' => __( 'Once enabled, the Credit Card option will show up in the checkout.', 'paypal-for-woocommerce' ), + 'label' => __( 'Enable PayPal Card Processing', 'paypal-for-woocommerce' ), + 'type' => 'checkbox', + 'default' => false, + 'gateway' => 'dcc', + 'requirements' => array( + 'dcc', + ), + 'screens' => array( + State::STATE_ONBOARDED, + ), + ), 'dcc_gateway_title' => array( 'title' => __( 'Title', 'paypal-for-woocommerce' ), 'type' => 'text', @@ -294,10 +313,11 @@ return array( 'default' => __( 'Credit Cards', 'paypal-for-woocommerce' ), 'desc_tip' => true, 'screens' => array( - State::STATE_PROGRESSIVE, State::STATE_ONBOARDED, ), - 'requirements' => array(), + 'requirements' => array( + 'dcc', + ), 'gateway' => 'dcc', ), 'description' => array( @@ -332,10 +352,11 @@ return array( 'paypal-for-woocommerce' ), 'screens' => array( - State::STATE_PROGRESSIVE, State::STATE_ONBOARDED, ), - 'requirements' => array(), + 'requirements' => array( + 'dcc', + ), 'gateway' => 'dcc', ), 'intent' => array( @@ -356,7 +377,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'capture_for_virtual_only' => array( 'title' => __( 'Capture Virtual-Only Orders ', 'paypal-for-woocommerce' ), @@ -372,7 +393,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'payee_preferred' => array( 'title' => __( 'Instant Payments ', 'paypal-for-woocommerce' ), @@ -484,7 +505,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'prefix' => array( 'title' => __( 'Installation prefix', 'paypal-for-woocommerce' ), @@ -498,7 +519,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), // General button styles. @@ -512,6 +533,18 @@ return array( 'requirements' => array(), 'gateway' => 'paypal', ), + 'button_enabled' => array( + 'title' => __( 'Enable buttons on Checkout', 'paypal-for-woocommerce' ), + 'type' => 'checkbox', + 'label' => __( 'Enable on Checkout', 'paypal-for-woocommerce' ), + 'default' => true, + 'screens' => array( + State::STATE_PROGRESSIVE, + State::STATE_ONBOARDED, + ), + 'requirements' => array(), + 'gateway' => 'paypal', + ), 'button_layout' => array( 'title' => __( 'Button Layout', 'paypal-for-woocommerce' ), 'type' => 'select', @@ -1337,9 +1370,9 @@ return array( 'gateway' => 'paypal', ), 'message_cart_enabled' => array( - 'title' => __( 'Enable message on Single Product', 'paypal-for-woocommerce' ), + 'title' => __( 'Enable message on Cart', 'paypal-for-woocommerce' ), 'type' => 'checkbox', - 'label' => __( 'Enable on Single Product', 'paypal-for-woocommerce' ), + 'label' => __( 'Enable on Cart', 'paypal-for-woocommerce' ), 'default' => true, 'screens' => array( State::STATE_PROGRESSIVE, diff --git a/modules/ppcp-wc-gateway/src/Checkout/class-disablegateways.php b/modules/ppcp-wc-gateway/src/Checkout/class-disablegateways.php index d9d0503f5..df4f08faa 100644 --- a/modules/ppcp-wc-gateway/src/Checkout/class-disablegateways.php +++ b/modules/ppcp-wc-gateway/src/Checkout/class-disablegateways.php @@ -73,6 +73,10 @@ class DisableGateways { unset( $methods[ CreditCardGateway::ID ] ); } + if ( $this->settings->has('button_enabled') && ! $this->settings->get('button_enabled') && ! $this->session_handler->order()) { + unset( $methods[ PayPalGateway::ID] ); + } + if ( ! $this->needs_to_disable_gateways() ) { return $methods; } diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index 90aa5205f..15daaf853 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php @@ -80,11 +80,11 @@ class CreditCardGateway extends PayPalGateway { } $this->method_title = __( - 'PayPal Credit Card Processing', + 'PayPal Card Processing', 'paypal-for-woocommerce' ); $this->method_description = __( - 'Provide your customers with the option to pay with credit card.', + 'Accept debit and credit cards, and local payment methods with PayPal’s latest solution.', 'paypal-for-woocommerce' ); $this->title = $this->config->has( 'dcc_gateway_title' ) ? @@ -123,20 +123,6 @@ class CreditCardGateway extends PayPalGateway { ); } - /** - * Renders the settings. - * - * @return string - */ - public function generate_ppcp_html(): string { - - ob_start(); - $this->settings_renderer->render( true ); - $content = ob_get_contents(); - ob_end_clean(); - return $content; - } - /** * Returns the title of the gateway. * @@ -211,4 +197,13 @@ class CreditCardGateway extends PayPalGateway { ), ); } + + /** + * Whether the gateway is available or not. + * + * @return bool + */ + public function is_available() : bool { + return $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' ); + } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index dbbab5252..5782922af 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -14,6 +14,7 @@ use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor; +use Inpsyde\PayPalCommerce\WcGateway\Settings\SectionsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Psr\Container\ContainerInterface; @@ -116,11 +117,8 @@ class PayPalGateway extends \WC_Payment_Gateway { ); } - $this->method_title = __( 'PayPal Checkout', 'paypal-for-woocommerce' ); - $this->method_description = __( - 'Accept PayPal, PayPal Credit and alternative payment types with PayPal’s latest solution.', - 'paypal-for-woocommerce' - ); + $this->method_title = $this->define_method_title(); + $this->method_description = $this->define_method_description(); $this->title = $this->config->has( 'title' ) ? $this->config->get( 'title' ) : $this->method_title; $this->description = $this->config->has( 'description' ) ? @@ -154,15 +152,20 @@ class PayPalGateway extends \WC_Payment_Gateway { public function init_form_fields() { $this->form_fields = array( 'enabled' => array( - 'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ), - 'type' => 'checkbox', - 'label' => __( 'Enable PayPal Payments', 'paypal-for-woocommerce' ), - 'default' => 'no', + 'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ), + 'type' => 'checkbox', + 'desc_tip' => true, + 'description' => __( 'In order to use PayPal or PayPal Card Processing, you need to enable the Gateway.', 'paypal-for-woocommerce' ), + 'label' => __( 'Enable the PayPal Gateway', 'paypal-for-woocommerce' ), + 'default' => 'no', ), 'ppcp' => array( 'type' => 'ppcp', ), ); + if ( $this->is_credit_card_tab() ) { + unset( $this->form_fields['enabled'] ); + } } /** @@ -299,4 +302,65 @@ class PayPalGateway extends \WC_Payment_Gateway { ob_end_clean(); return $content; } + + /** + * Defines the method title. If we are on the credit card tab in the settings, we want to change this. + * + * @return string + */ + private function define_method_title(): string { + if ( $this->is_credit_card_tab() ) { + return __( 'PayPal Card Processing', 'paypal-for-woocommerce' ); + } + if ( $this->is_paypal_tab() ) { + return __( 'PayPal Checkout', 'paypal-for-woocommerce' ); + } + return __( 'PayPal', 'paypal-for-woocommerce' ); + } + + /** + * Defines the method description. If we are on the credit card tab in the settings, we want to change this. + * + * @return string + */ + private function define_method_description(): string { + if ( $this->is_credit_card_tab() ) { + return __( + 'Accept debit and credit cards, and local payment methods with PayPal’s latest solution.', + 'paypal-for-woocommerce' + ); + } + + return __( + 'Accept PayPal, PayPal Credit and alternative payment types with PayPal’s latest solution.', + 'paypal-for-woocommerce' + ); + } + + // phpcs:disable WordPress.Security.NonceVerification.Recommended + + /** + * Determines, whether the current session is on the credit card tab in the admin settings. + * + * @return bool + */ + private function is_credit_card_tab() : bool { + return is_admin() + && isset( $_GET[ SectionsRenderer::KEY ] ) + && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ); + + } + + /** + * Whether we are on the PayPal settings tab. + * + * @return bool + */ + private function is_paypal_tab() : bool { + return ! $this->is_credit_card_tab() + && is_admin() + && isset( $_GET['section'] ) + && self::ID === sanitize_text_field( wp_unslash( $_GET['section'] ) ); + } + // phpcs:enable WordPress.Security.NonceVerification.Recommended } diff --git a/modules/ppcp-wc-gateway/src/Notice/class-connectadminnotice.php b/modules/ppcp-wc-gateway/src/Notice/class-connectadminnotice.php index ed2763e16..3d83c479f 100644 --- a/modules/ppcp-wc-gateway/src/Notice/class-connectadminnotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/class-connectadminnotice.php @@ -57,7 +57,7 @@ class ConnectAdminNotice { $message = sprintf( /* translators: %1$s the gateway name. */ __( - 'PayPal Payments is almost ready. To get started, connect your account.', + 'PayPal Checkout is almost ready. To get started, connect your account.', 'paypal-for-woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway' ) diff --git a/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php new file mode 100644 index 000000000..c7e95d416 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php @@ -0,0 +1,59 @@ +should_render() ) { + return; + } + + //phpcs:ignore WordPress.Security.NonceVerification.Recommended + $current = ! isset( $_GET[ self::KEY ] ) ? PayPalGateway::ID : sanitize_text_field( wp_unslash( $_GET[ self::KEY ] ) ); + $sections = array( + PayPalGateway::ID => __( 'PayPal Checkout', 'paypal-for-woocommerce' ), + CreditCardGateway::ID => __( 'PayPal Card Processing', 'paypal-for-woocommerce' ), + ); + + echo '
- ', - '' - ) - ); - ?> -
-+ ', + '' + ) + ); + ?> +
++ + + + + +
++ + + + + +
+