From e7cbd23155357d44b5d8f3d2fff361f3736f4a30 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 09:55:20 +0300 Subject: [PATCH 01/31] do not show dcc gateway on gateway edit screen --- modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php index 61417ff67..e05ed60c5 100644 --- a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php +++ b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php @@ -158,12 +158,14 @@ class WcGatewayModule implements ModuleInterface { static function ( $methods ) use ( $container ): array { $methods[] = $container->get( 'wcgateway.paypal-gateway' ); $dcc_applies = $container->get( 'api.helpers.dccapplies' ); + + $screen = get_current_screen(); /** * The DCC Applies object. * * @var DccApplies $dcc_applies */ - if ( $dcc_applies->for_country_currency() ) { + if ( $screen->id !== 'woocommerce_page_wc-settings' && $dcc_applies->for_country_currency() ) { $methods[] = $container->get( 'wcgateway.credit-card-gateway' ); } return (array) $methods; From df7e22006bee1f5e143fc81ff387fb7f97960eea Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 09:56:04 +0300 Subject: [PATCH 02/31] move dcc settings in extra section --- modules/ppcp-wc-gateway/services.php | 4 ++ .../src/Gateway/class-creditcardgateway.php | 14 ------- .../src/Settings/class-sectionrenderer.php | 41 +++++++++++++++++++ .../src/Settings/class-settingsrenderer.php | 5 +-- .../src/class-wcgatewaymodule.php | 14 +++++++ 5 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index f8c8e5abe..2ead04fc2 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' ); diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index 90aa5205f..9d8e735f6 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php @@ -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. * diff --git a/modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php new file mode 100644 index 000000000..489622e72 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php @@ -0,0 +1,41 @@ +should_render()) { + return; + } + + $current = ! isset($_GET[self::KEY]) ? 'paypal' : sanitize_text_field(wp_unslash($_GET[self::KEY])); + $sections = [ + 'paypal' => __( 'PayPal', 'paypal-for-woocommerce' ), + 'dcc' => __( 'Credit Card', 'paypal-for-woocommerce' ), + ]; + + echo '
'; + return; + } +} \ No newline at end of file diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php index 510f448d9..53625e725 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php @@ -209,11 +209,10 @@ class SettingsRenderer { /** * Renders the settings. - * - * @param bool $is_dcc Whether it is the DCC gateway or not. */ - public function render( bool $is_dcc ) { + public function render() { + $is_dcc = isset($_GET[SectionsRenderer::KEY]) && 'dcc' === sanitize_text_field(wp_unslash($_GET[SectionsRenderer::KEY])); $nonce = wp_create_nonce( SettingsListener::NONCE ); ?> diff --git a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php index e05ed60c5..1a55da933 100644 --- a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php +++ b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php @@ -22,6 +22,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; +use Inpsyde\PayPalCommerce\WcGateway\Settings\SectionsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Interop\Container\ServiceProviderInterface; @@ -56,6 +57,19 @@ class WcGatewayModule implements ModuleInterface { $this->register_checkout_paypal_address_preset( $container ); $this->ajax_gateway_enabler( $container ); + add_action( + 'woocommerce_sections_checkout', + function() use ($container) { + $section_renderer = $container->get('wcgateway.settings.sections-renderer'); + /** + * The Section Renderer. + * + * @var SectionsRenderer $section_renderer + */ + $section_renderer->render(); + } + ); + add_filter( Repository::NOTICES_FILTER, static function ( $notices ) use ( $container ): array { From cb37119a994ba1d0de103f80c50b05630a4a2773 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 10:47:35 +0300 Subject: [PATCH 03/31] make sure get_current_screen method exists --- modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php index 1a55da933..2c0bb1f09 100644 --- a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php +++ b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php @@ -173,7 +173,7 @@ class WcGatewayModule implements ModuleInterface { $methods[] = $container->get( 'wcgateway.paypal-gateway' ); $dcc_applies = $container->get( 'api.helpers.dccapplies' ); - $screen = get_current_screen(); + $screen = ! function_exists('get_current_screen') ? (object) ['id' => 'front'] : get_current_screen(); /** * The DCC Applies object. * From 4daca6dde078ae448e5896dd18cb1841e0a19e8a Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 10:56:26 +0300 Subject: [PATCH 04/31] phpcs --- .../src/Settings/class-sectionrenderer.php | 41 ------------- .../src/Settings/class-sectionsrenderer.php | 58 +++++++++++++++++++ .../src/Settings/class-settingsrenderer.php | 5 +- .../src/class-wcgatewaymodule.php | 8 +-- 4 files changed, 65 insertions(+), 47 deletions(-) delete mode 100644 modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php create mode 100644 modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php diff --git a/modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php deleted file mode 100644 index 489622e72..000000000 --- a/modules/ppcp-wc-gateway/src/Settings/class-sectionrenderer.php +++ /dev/null @@ -1,41 +0,0 @@ -should_render()) { - return; - } - - $current = ! isset($_GET[self::KEY]) ? 'paypal' : sanitize_text_field(wp_unslash($_GET[self::KEY])); - $sections = [ - 'paypal' => __( 'PayPal', 'paypal-for-woocommerce' ), - 'dcc' => __( 'Credit Card', 'paypal-for-woocommerce' ), - ]; - - echo '
    '; - - $array_keys = array_keys( $sections ); - - foreach ( $sections as $id => $label ) { - $url = admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway&' . self::KEY . '=' . $id ); - echo '
  • ' . esc_html($label) . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . '
  • '; - } - - echo '

'; - return; - } -} \ No newline at end of file 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..b0c1e64b4 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php @@ -0,0 +1,58 @@ +should_render() ) { + return; + } + + //phpcs:ignore WordPress.Security.NonceVerification.Recommended + $current = ! isset( $_GET[ self::KEY ] ) ? 'paypal' : sanitize_text_field( wp_unslash( $_GET[ self::KEY ] ) ); + $sections = array( + 'paypal' => __( 'PayPal', 'paypal-for-woocommerce' ), + 'dcc' => __( 'Credit Card', 'paypal-for-woocommerce' ), + ); + + echo '
    '; + + $array_keys = array_keys( $sections ); + + foreach ( $sections as $id => $label ) { + $url = admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway&' . self::KEY . '=' . $id ); + echo '
  • ' . esc_html( $label ) . ' ' . ( end( $array_keys ) === $id ? '' : '|' ) . '
  • '; + } + + echo '

'; + } +} diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php index 53625e725..fb0654a3c 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php @@ -212,8 +212,9 @@ class SettingsRenderer { */ public function render() { - $is_dcc = isset($_GET[SectionsRenderer::KEY]) && 'dcc' === sanitize_text_field(wp_unslash($_GET[SectionsRenderer::KEY])); - $nonce = wp_create_nonce( SettingsListener::NONCE ); + //phpcs:ignore WordPress.Security.NonceVerification.Recommended + $is_dcc = isset( $_GET[ SectionsRenderer::KEY ] ) && 'dcc' === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ); + $nonce = wp_create_nonce( SettingsListener::NONCE ); ?> get('wcgateway.settings.sections-renderer'); + function() use ( $container ) { + $section_renderer = $container->get( 'wcgateway.settings.sections-renderer' ); /** * The Section Renderer. * @@ -173,13 +173,13 @@ class WcGatewayModule implements ModuleInterface { $methods[] = $container->get( 'wcgateway.paypal-gateway' ); $dcc_applies = $container->get( 'api.helpers.dccapplies' ); - $screen = ! function_exists('get_current_screen') ? (object) ['id' => 'front'] : get_current_screen(); + $screen = ! function_exists( 'get_current_screen' ) ? (object) array( 'id' => 'front' ) : get_current_screen(); /** * The DCC Applies object. * * @var DccApplies $dcc_applies */ - if ( $screen->id !== 'woocommerce_page_wc-settings' && $dcc_applies->for_country_currency() ) { + if ( 'woocommerce_page_wc-settings' !== $screen->id && $dcc_applies->for_country_currency() ) { $methods[] = $container->get( 'wcgateway.credit-card-gateway' ); } return (array) $methods; From bf7eb4cd0410cbb28c0db93d48c7a7323a6b4fcb Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:20:47 +0300 Subject: [PATCH 05/31] remove options from the credit card settings screen --- modules/ppcp-wc-gateway/services.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 2ead04fc2..4eac2c071 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -182,7 +182,7 @@ return array( State::STATE_PROGRESSIVE, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'sandbox_on' => array( 'title' => __( 'Sandbox', 'paypal-for-woocommerce' ), @@ -193,7 +193,7 @@ return array( State::STATE_START, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'sandbox_on_info' => array( 'title' => __( 'Sandbox', 'paypal-for-woocommerce' ), @@ -205,7 +205,7 @@ return array( ), 'hidden' => 'sandbox_on', 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'merchant_email' => array( 'title' => __( 'Email address', 'paypal-for-woocommerce' ), @@ -218,7 +218,7 @@ return array( State::STATE_START, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'merchant_email_info' => array( 'title' => __( 'Email address', 'paypal-for-woocommerce' ), @@ -230,7 +230,7 @@ return array( ), 'hidden' => 'merchant_email', 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'toggle_manual_input' => array( 'type' => 'ppcp-text', @@ -242,7 +242,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'client_id' => array( 'title' => __( 'Client Id', 'paypal-for-woocommerce' ), @@ -256,7 +256,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'client_secret' => array( 'title' => __( 'Secret Key', 'paypal-for-woocommerce' ), @@ -270,7 +270,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'title' => array( 'title' => __( 'Title', 'paypal-for-woocommerce' ), @@ -360,7 +360,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' ), @@ -376,7 +376,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'payee_preferred' => array( 'title' => __( 'Instant Payments ', 'paypal-for-woocommerce' ), @@ -488,7 +488,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), 'prefix' => array( 'title' => __( 'Installation prefix', 'paypal-for-woocommerce' ), @@ -502,7 +502,7 @@ return array( State::STATE_ONBOARDED, ), 'requirements' => array(), - 'gateway' => 'all', + 'gateway' => 'paypal', ), // General button styles. From 238f4a9207e555019097967448ae2b19c7a36217 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:21:13 +0300 Subject: [PATCH 06/31] alter title and description on credit card tab --- .../src/Gateway/class-paypalgateway.php | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 0db2c5498..c5deda52e 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' ) ? @@ -299,4 +297,35 @@ 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 (is_admin() && isset($_GET[SectionsRenderer::KEY]) && CreditCardGateway::ID === sanitize_text_field(wp_unslash($_GET[SectionsRenderer::KEY]))) { + return __('PayPal Card Processing', 'paypal-for-woocommerce'); + } + return __( 'PayPal Checkout', '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 (is_admin() && isset($_GET[SectionsRenderer::KEY]) && CreditCardGateway::ID === sanitize_text_field(wp_unslash($_GET[SectionsRenderer::KEY]))) { + 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' + ); + } } From 5428e0b513341b8a97baf4ab5c9ea0e63e87d629 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:21:37 +0300 Subject: [PATCH 07/31] change key values for tabs --- .../src/Gateway/class-creditcardgateway.php | 4 ++-- .../src/Settings/class-sectionsrenderer.php | 7 ++++--- .../src/Settings/class-settingslistener.php | 4 ++-- .../src/Settings/class-settingsrenderer.php | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index 9d8e735f6..9a0ecd293 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' ) ? diff --git a/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php index b0c1e64b4..145e49042 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php @@ -9,6 +9,7 @@ declare( strict_types=1 ); namespace Inpsyde\PayPalCommerce\WcGateway\Settings; +use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; /** @@ -38,10 +39,10 @@ class SectionsRenderer { } //phpcs:ignore WordPress.Security.NonceVerification.Recommended - $current = ! isset( $_GET[ self::KEY ] ) ? 'paypal' : sanitize_text_field( wp_unslash( $_GET[ self::KEY ] ) ); + $current = ! isset( $_GET[ self::KEY ] ) ? PayPalGateway::ID : sanitize_text_field( wp_unslash( $_GET[ self::KEY ] ) ); $sections = array( - 'paypal' => __( 'PayPal', 'paypal-for-woocommerce' ), - 'dcc' => __( 'Credit Card', 'paypal-for-woocommerce' ), + PayPalGateway::ID => __( 'PayPal', 'paypal-for-woocommerce' ), + CreditCardGateway::ID => __( 'Credit Card', 'paypal-for-woocommerce' ), ); echo '
    '; diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index 09dc562cf..b86288077 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -119,11 +119,11 @@ class SettingsListener { $raw_data = ( isset( $_POST['ppcp'] ) ) ? (array) wp_unslash( $_POST['ppcp'] ) : array(); // phpcs:enable phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = $this->retrieve_settings_from_raw_data( $raw_data ); - if ( isset( $_GET['section'] ) && PayPalGateway::ID === $_GET['section'] ) { + if ( ! isset( $_GET[SectionsRenderer::KEY] ) || PayPalGateway::ID === $_GET[SectionsRenderer::KEY] ) { $settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] ) && 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] ); } - if ( isset( $_GET['section'] ) && CreditCardGateway::ID === $_GET['section'] ) { + if ( isset( $_GET[SectionsRenderer::KEY] ) && CreditCardGateway::ID === $_GET[SectionsRenderer::KEY] ) { $dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled'; $settings['dcc_gateway_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] ) && 1 === absint( $_POST[ $dcc_enabled_post_key ] ); diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php index fb0654a3c..c7f76e112 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php @@ -12,6 +12,7 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Settings; use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies; use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply; use Inpsyde\PayPalCommerce\Onboarding\State; +use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Psr\Container\ContainerInterface; /** @@ -213,7 +214,7 @@ class SettingsRenderer { public function render() { //phpcs:ignore WordPress.Security.NonceVerification.Recommended - $is_dcc = isset( $_GET[ SectionsRenderer::KEY ] ) && 'dcc' === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ); + $is_dcc = isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ); $nonce = wp_create_nonce( SettingsListener::NONCE ); ?> From 3b0a10e7563e5fc5e6648657a8367170a178ca19 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:38:24 +0300 Subject: [PATCH 08/31] make sure 3d secure text only appears when fully onboarded --- modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php index c7f76e112..7dc13446c 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php @@ -277,7 +277,7 @@ class SettingsRenderer { state->current_state() ) : ?> From c269f35d615ff6bd632e025859a37061f2fcee9f Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:38:42 +0300 Subject: [PATCH 09/31] wording --- modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php | 2 +- modules/ppcp-wc-gateway/src/Notice/class-connectadminnotice.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index c5deda52e..b8f2405c0 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -154,7 +154,7 @@ class PayPalGateway extends \WC_Payment_Gateway { 'enabled' => array( 'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ), 'type' => 'checkbox', - 'label' => __( 'Enable PayPal Payments', 'paypal-for-woocommerce' ), + 'label' => __( 'Enable PayPal Checkout', 'paypal-for-woocommerce' ), 'default' => 'no', ), 'ppcp' => array( 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' ) From 0e45cc5cb6a6b1b4c4b7579f6afe998500887bfa Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:41:10 +0300 Subject: [PATCH 10/31] phcps --- .../ppcp-wc-gateway/src/Gateway/class-paypalgateway.php | 8 +++++--- .../src/Settings/class-sectionsrenderer.php | 4 ++-- .../src/Settings/class-settingslistener.php | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index b8f2405c0..b26429ce2 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -298,14 +298,15 @@ class PayPalGateway extends \WC_Payment_Gateway { return $content; } + // phpcs:disable WordPress.Security.NonceVerification.Recommended /** * 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 (is_admin() && isset($_GET[SectionsRenderer::KEY]) && CreditCardGateway::ID === sanitize_text_field(wp_unslash($_GET[SectionsRenderer::KEY]))) { - return __('PayPal Card Processing', 'paypal-for-woocommerce'); + if ( is_admin() && isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) ) { + return __( 'PayPal Card Processing', 'paypal-for-woocommerce' ); } return __( 'PayPal Checkout', 'paypal-for-woocommerce' ); } @@ -316,7 +317,7 @@ class PayPalGateway extends \WC_Payment_Gateway { * @return string */ private function define_method_description(): string { - if (is_admin() && isset($_GET[SectionsRenderer::KEY]) && CreditCardGateway::ID === sanitize_text_field(wp_unslash($_GET[SectionsRenderer::KEY]))) { + if ( is_admin() && isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) ) { return __( 'Accept debit and credit cards, and local payment methods with PayPal’s latest solution.', 'paypal-for-woocommerce' @@ -328,4 +329,5 @@ class PayPalGateway extends \WC_Payment_Gateway { 'paypal-for-woocommerce' ); } + // phpcs:enable WordPress.Security.NonceVerification.Recommended } diff --git a/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php index 145e49042..0ce2d9628 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php @@ -41,8 +41,8 @@ class SectionsRenderer { //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', 'paypal-for-woocommerce' ), - CreditCardGateway::ID => __( 'Credit Card', 'paypal-for-woocommerce' ), + PayPalGateway::ID => __( 'PayPal', 'paypal-for-woocommerce' ), + CreditCardGateway::ID => __( 'Credit Card', 'paypal-for-woocommerce' ), ); echo '
      '; diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index b86288077..34fee853d 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -119,11 +119,11 @@ class SettingsListener { $raw_data = ( isset( $_POST['ppcp'] ) ) ? (array) wp_unslash( $_POST['ppcp'] ) : array(); // phpcs:enable phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = $this->retrieve_settings_from_raw_data( $raw_data ); - if ( ! isset( $_GET[SectionsRenderer::KEY] ) || PayPalGateway::ID === $_GET[SectionsRenderer::KEY] ) { + if ( ! isset( $_GET[ SectionsRenderer::KEY ] ) || PayPalGateway::ID === $_GET[ SectionsRenderer::KEY ] ) { $settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] ) && 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] ); } - if ( isset( $_GET[SectionsRenderer::KEY] ) && CreditCardGateway::ID === $_GET[SectionsRenderer::KEY] ) { + if ( isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === $_GET[ SectionsRenderer::KEY ] ) { $dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled'; $settings['dcc_gateway_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] ) && 1 === absint( $_POST[ $dcc_enabled_post_key ] ); From 70d866d53e248bae4ed7c2d2b09f1742bb770e4e Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:47:53 +0300 Subject: [PATCH 11/31] make sure return value is string --- modules/ppcp-button/src/Assets/class-smartbutton.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-button/src/Assets/class-smartbutton.php b/modules/ppcp-button/src/Assets/class-smartbutton.php index 0c156b1ec..a0422a0dc 100644 --- a/modules/ppcp-button/src/Assets/class-smartbutton.php +++ b/modules/ppcp-button/src/Assets/class-smartbutton.php @@ -811,6 +811,6 @@ class SmartButton implements SmartButtonInterface { if ( is_bool( $value ) ) { $value = $value ? 'true' : 'false'; } - return $value; + return (string) $value; } } From 430ffe457ede27077652a53d6e7801056d482c08 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:48:10 +0300 Subject: [PATCH 12/31] fix wording --- modules/ppcp-wc-gateway/services.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 4eac2c071..82b724740 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1341,9 +1341,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, From 8aef527108cfde65ee5f74229755e19392f22fd9 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 12:58:06 +0300 Subject: [PATCH 13/31] fix some logic in toggling settings --- modules/ppcp-onboarding/assets/js/settings.js | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) 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'; }) } ); From 2eea01effefc66db71af5b8140659f415dab2f04 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 13:02:00 +0300 Subject: [PATCH 14/31] fix toggle for which options to update and listen to --- .../ppcp-wc-gateway/src/Settings/class-settingslistener.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index 34fee853d..da7aa2d6f 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -189,13 +189,13 @@ class SettingsListener { } if ( 'dcc' === $config['gateway'] - && sanitize_text_field( wp_unslash( $_GET['section'] ) ) !== 'ppcp-credit-card-gateway' + && sanitize_text_field( wp_unslash( $_GET[SectionsRenderer::KEY] ) ) !== CreditCardGateway::ID ) { continue; } if ( 'paypal' === $config['gateway'] - && sanitize_text_field( wp_unslash( $_GET['section'] ) ) !== 'ppcp-gateway' + && sanitize_text_field( wp_unslash( $_GET[SectionsRenderer::KEY] ) ) !== PayPalGateway::ID ) { continue; } From f9e64abe141d744844c3c68050793c5900de0c94 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 13:02:31 +0300 Subject: [PATCH 15/31] do not show general enable checkbox on credit card --- .../src/Gateway/class-paypalgateway.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index b26429ce2..1b9f3bd6f 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -161,6 +161,9 @@ class PayPalGateway extends \WC_Payment_Gateway { 'type' => 'ppcp', ), ); + if ( $this->is_credit_card_tab() ) { + unset( $this->form_fields['enabled'] ); + } } /** @@ -298,14 +301,13 @@ class PayPalGateway extends \WC_Payment_Gateway { return $content; } - // phpcs:disable WordPress.Security.NonceVerification.Recommended /** * 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 ( is_admin() && isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) ) { + if ( $this->is_credit_card_tab() ) { return __( 'PayPal Card Processing', 'paypal-for-woocommerce' ); } return __( 'PayPal Checkout', 'paypal-for-woocommerce' ); @@ -317,7 +319,7 @@ class PayPalGateway extends \WC_Payment_Gateway { * @return string */ private function define_method_description(): string { - if ( is_admin() && isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) ) { + if ( $this->is_credit_card_tab() ) { return __( 'Accept debit and credit cards, and local payment methods with PayPal’s latest solution.', 'paypal-for-woocommerce' @@ -329,5 +331,12 @@ class PayPalGateway extends \WC_Payment_Gateway { 'paypal-for-woocommerce' ); } + + // phpcs:disable WordPress.Security.NonceVerification.Recommended + private function is_credit_card_tab() : bool + { + return is_admin() && isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ); + + } // phpcs:enable WordPress.Security.NonceVerification.Recommended } From 57a6f8b85cb26eeb69534d9eca56ac61c542cfec Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 13:52:40 +0300 Subject: [PATCH 16/31] add setting and manage availability of dcc gateway --- modules/ppcp-button/services.php | 2 +- modules/ppcp-button/src/Assets/class-smartbutton.php | 6 +++--- modules/ppcp-wc-gateway/services.php | 12 ++++++++++-- .../src/Gateway/class-creditcardgateway.php | 4 ++++ .../ppcp-wc-gateway/src/Settings/class-settings.php | 2 +- .../src/Settings/class-settingslistener.php | 4 ++-- .../ppcp-wc-gateway/src/class-wcgatewaymodule.php | 2 +- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index d7fa3d916..e50046955 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -60,7 +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' ); + $credit_card_disabled = ! $settings->has( 'dcc_enabled' ) || ! $settings->get( 'dcc_enabled' ); if ( $paypal_disabled && $credit_card_disabled ) { return new DisabledSmartButton(); } diff --git a/modules/ppcp-button/src/Assets/class-smartbutton.php b/modules/ppcp-button/src/Assets/class-smartbutton.php index a0422a0dc..38e7c228a 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( @@ -772,7 +772,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() ) { diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 82b724740..080a14085 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -288,6 +288,16 @@ return array( 'requirements' => array(), 'gateway' => 'paypal', ), + 'dcc_enabled' => array( + 'title' => __( 'Enable/Disable' ), + 'type' => 'checkbox', + 'default' => false, + 'gateway' => 'dcc', + 'requirements' => array(), + 'screens' => array( + State::STATE_ONBOARDED, + ), + ), 'dcc_gateway_title' => array( 'title' => __( 'Title', 'paypal-for-woocommerce' ), 'type' => 'text', @@ -298,7 +308,6 @@ return array( 'default' => __( 'Credit Cards', 'paypal-for-woocommerce' ), 'desc_tip' => true, 'screens' => array( - State::STATE_PROGRESSIVE, State::STATE_ONBOARDED, ), 'requirements' => array(), @@ -336,7 +345,6 @@ return array( 'paypal-for-woocommerce' ), 'screens' => array( - State::STATE_PROGRESSIVE, State::STATE_ONBOARDED, ), 'requirements' => array(), diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index 9a0ecd293..3244f2018 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php @@ -197,4 +197,8 @@ class CreditCardGateway extends PayPalGateway { ), ); } + + public function is_available() : bool { + return $this->config->has('dcc_enabled') && $this->config->get('dcc_enabled'); + } } diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settings.php b/modules/ppcp-wc-gateway/src/Settings/class-settings.php index 3abb9bac9..f0c6a5ea2 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settings.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settings.php @@ -81,7 +81,7 @@ class Settings implements ContainerInterface { $this->load(); $fields_to_reset = array( 'enabled', - 'dcc_gateway_enabled', + 'dcc_enabled', 'intent', 'client_id', 'client_secret', diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index da7aa2d6f..410355786 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -122,13 +122,13 @@ class SettingsListener { if ( ! isset( $_GET[ SectionsRenderer::KEY ] ) || PayPalGateway::ID === $_GET[ SectionsRenderer::KEY ] ) { $settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] ) && 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] ); + $this->maybe_register_webhooks( $settings ); } if ( isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === $_GET[ SectionsRenderer::KEY ] ) { $dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled'; - $settings['dcc_gateway_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] ) + $settings['dcc_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] ) && 1 === absint( $_POST[ $dcc_enabled_post_key ] ); } - $this->maybe_register_webhooks( $settings ); foreach ( $settings as $id => $value ) { $this->settings->set( $id, $value ); diff --git a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php index accc32bbe..baf862dbb 100644 --- a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php +++ b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php @@ -144,7 +144,7 @@ class WcGatewayModule implements ModuleInterface { $settings = $container->get( 'wcgateway.settings' ); $key = PayPalGateway::ID === $_POST['gateway_id'] ? 'enabled' : ''; if ( CreditCardGateway::ID === $_POST['gateway_id'] ) { - $key = 'dcc_gateway_enabled'; + $key = 'dcc_enabled'; } if ( ! $key ) { return; From ccda439569a0956028f6a4bbd8a59d01df14e76a Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 13:54:53 +0300 Subject: [PATCH 17/31] fix tests --- tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index a1260a248..c03fafd55 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -21,6 +21,7 @@ class WcGatewayTest extends TestCase public function testProcessPaymentSuccess() { + expect('is_admin')->andReturn(false); $orderId = 1; $wcOrder = Mockery::mock(\WC_Order::class); @@ -64,6 +65,7 @@ class WcGatewayTest extends TestCase } public function testProcessPaymentOrderNotFound() { + expect('is_admin')->andReturn(false); $orderId = 1; $settingsRenderer = Mockery::mock(SettingsRenderer::class); @@ -95,6 +97,7 @@ class WcGatewayTest extends TestCase public function testProcessPaymentFails() { + expect('is_admin')->andReturn(false); $orderId = 1; $wcOrder = Mockery::mock(\WC_Order::class); @@ -136,6 +139,7 @@ class WcGatewayTest extends TestCase } public function testCaptureAuthorizedPayment() { + expect('is_admin')->andReturn(false); $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder @@ -181,6 +185,7 @@ class WcGatewayTest extends TestCase public function testCaptureAuthorizedPaymentHasAlreadyBeenCaptured() { + expect('is_admin')->andReturn(false); $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder ->expects('get_status') @@ -233,6 +238,7 @@ class WcGatewayTest extends TestCase */ public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $expectedMessage) { + expect('is_admin')->andReturn(false); $wcOrder = Mockery::mock(\WC_Order::class); $settingsRenderer = Mockery::mock(SettingsRenderer::class); $orderProcessor = Mockery::mock(OrderProcessor::class); From 5fc8dde271f0700c0ee9106dad1662d9f5f1f4df Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Sep 2020 14:53:30 +0300 Subject: [PATCH 18/31] codestyle --- modules/ppcp-wc-gateway/services.php | 2 +- .../src/Gateway/class-creditcardgateway.php | 7 ++++++- .../src/Gateway/class-paypalgateway.php | 9 +++++++-- .../src/Settings/class-settingslistener.php | 10 +++++++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 080a14085..86d675be2 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -289,7 +289,7 @@ return array( 'gateway' => 'paypal', ), 'dcc_enabled' => array( - 'title' => __( 'Enable/Disable' ), + 'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ), 'type' => 'checkbox', 'default' => false, 'gateway' => 'dcc', diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index 3244f2018..15daaf853 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php @@ -198,7 +198,12 @@ 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'); + 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 1b9f3bd6f..6388da31d 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -333,8 +333,13 @@ class PayPalGateway extends \WC_Payment_Gateway { } // phpcs:disable WordPress.Security.NonceVerification.Recommended - private function is_credit_card_tab() : bool - { + + /** + * 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 ] ) ); } diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index 410355786..458199b4b 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -125,7 +125,7 @@ class SettingsListener { $this->maybe_register_webhooks( $settings ); } if ( isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === $_GET[ SectionsRenderer::KEY ] ) { - $dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled'; + $dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled'; $settings['dcc_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] ) && 1 === absint( $_POST[ $dcc_enabled_post_key ] ); } @@ -189,13 +189,17 @@ class SettingsListener { } if ( 'dcc' === $config['gateway'] - && sanitize_text_field( wp_unslash( $_GET[SectionsRenderer::KEY] ) ) !== CreditCardGateway::ID + && ( + ! isset( $_GET[ SectionsRenderer::KEY ] ) + || sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) !== CreditCardGateway::ID + ) ) { continue; } if ( 'paypal' === $config['gateway'] - && sanitize_text_field( wp_unslash( $_GET[SectionsRenderer::KEY] ) ) !== PayPalGateway::ID + && isset( $_GET[ SectionsRenderer::KEY ] ) + && sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) !== PayPalGateway::ID ) { continue; } From 7069925a30689cacd91e75ccad5ccec7c6242284 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 06:58:47 +0300 Subject: [PATCH 19/31] default name of gateway is 'PayPal' --- modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 6388da31d..63efd764b 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -310,7 +310,7 @@ class PayPalGateway extends \WC_Payment_Gateway { if ( $this->is_credit_card_tab() ) { return __( 'PayPal Card Processing', 'paypal-for-woocommerce' ); } - return __( 'PayPal Checkout', 'paypal-for-woocommerce' ); + return __( 'PayPal', 'paypal-for-woocommerce' ); } /** From ce9e9d13084534bf97f5c1b9caaebea255791cca Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 07:16:52 +0300 Subject: [PATCH 20/31] pcp-48 / set correct title for setting tabs --- .../src/Gateway/class-paypalgateway.php | 19 ++++++++++++++++++- .../src/Settings/class-sectionsrenderer.php | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 63efd764b..cf8ce1853 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -310,6 +310,9 @@ class PayPalGateway extends \WC_Payment_Gateway { 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' ); } @@ -340,8 +343,22 @@ class PayPalGateway extends \WC_Payment_Gateway { * @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 ] ) ); + 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/Settings/class-sectionsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php index 0ce2d9628..c7e95d416 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-sectionsrenderer.php @@ -41,8 +41,8 @@ class SectionsRenderer { //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', 'paypal-for-woocommerce' ), - CreditCardGateway::ID => __( 'Credit Card', 'paypal-for-woocommerce' ), + PayPalGateway::ID => __( 'PayPal Checkout', 'paypal-for-woocommerce' ), + CreditCardGateway::ID => __( 'PayPal Card Processing', 'paypal-for-woocommerce' ), ); echo '
        '; From c647dbdb4ee50e77738ae3d7e98ff3e68baba9a7 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 07:19:24 +0300 Subject: [PATCH 21/31] correct text domain in plugin header --- woocommerce-paypal-commerce-gateway.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/woocommerce-paypal-commerce-gateway.php b/woocommerce-paypal-commerce-gateway.php index 6facc9cdb..42f6f4763 100644 --- a/woocommerce-paypal-commerce-gateway.php +++ b/woocommerce-paypal-commerce-gateway.php @@ -9,9 +9,7 @@ declare( strict_types = 1 ); * Author: Inpsyde GmbH * Author URI: https://inpsyde.com/ * License: GPL-2.0 - * Text Domain: woocommerce-paypal-commerce-gateway - * Domain Path: /languages - * Network: TODO: Specify value 'true' or remove line + * Text Domain: paypal-for-woocommerce */ From 85061fa785ebb3095c8e76915c1b528ed6da7402 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 08:10:29 +0300 Subject: [PATCH 22/31] when performing a reset, delete all existing settings --- .../ppcp-wc-gateway/src/Settings/class-settings.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settings.php b/modules/ppcp-wc-gateway/src/Settings/class-settings.php index f0c6a5ea2..04b91b1ed 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settings.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settings.php @@ -79,17 +79,7 @@ class Settings implements ContainerInterface { */ public function reset(): bool { $this->load(); - $fields_to_reset = array( - 'enabled', - 'dcc_enabled', - 'intent', - 'client_id', - 'client_secret', - 'merchant_email', - ); - foreach ( $fields_to_reset as $id ) { - $this->settings[ $id ] = null; - } + $this->settings = []; return true; } From 2678d33d9cdfff5bf4dc4ae767a271c54a3844f8 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 08:17:45 +0300 Subject: [PATCH 23/31] Better description what you enable with the Enable/Disable option on PayPal. --- .../src/Gateway/class-paypalgateway.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index cf8ce1853..c4c92c221 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -152,10 +152,12 @@ 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 Checkout', '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', From 4163af8a47aa445ee3408e56d0d7709f1baa895c Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 08:43:03 +0300 Subject: [PATCH 24/31] add dcc requirement to dcc fields --- modules/ppcp-wc-gateway/services.php | 12 +++++++++--- .../ppcp-wc-gateway/src/Settings/class-settings.php | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 86d675be2..c557b1858 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -293,7 +293,9 @@ return array( 'type' => 'checkbox', 'default' => false, 'gateway' => 'dcc', - 'requirements' => array(), + 'requirements' => array( + 'dcc', + ), 'screens' => array( State::STATE_ONBOARDED, ), @@ -310,7 +312,9 @@ return array( 'screens' => array( State::STATE_ONBOARDED, ), - 'requirements' => array(), + 'requirements' => array( + 'dcc', + ), 'gateway' => 'dcc', ), 'description' => array( @@ -347,7 +351,9 @@ return array( 'screens' => array( State::STATE_ONBOARDED, ), - 'requirements' => array(), + 'requirements' => array( + 'dcc', + ), 'gateway' => 'dcc', ), 'intent' => array( diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settings.php b/modules/ppcp-wc-gateway/src/Settings/class-settings.php index 04b91b1ed..ea0ad4517 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settings.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settings.php @@ -79,7 +79,7 @@ class Settings implements ContainerInterface { */ public function reset(): bool { $this->load(); - $this->settings = []; + $this->settings = array(); return true; } From c8ecdeb3300cd043acf7ee9cde2190a088efe8e5 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 08:43:25 +0300 Subject: [PATCH 25/31] add dcc notices depending on state and dcc availability --- .../src/Settings/class-settingsrenderer.php | 141 ++++++++++++++---- 1 file changed, 109 insertions(+), 32 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php index 7dc13446c..2f8ea4f1a 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingsrenderer.php @@ -277,38 +277,18 @@ class SettingsRenderer { state->current_state() ) : - ?> - - - -

        - ', - '' - ) - ); - ?> -

        - - - dcc_applies->for_country_currency() ) { + if ( State::STATE_ONBOARDED > $this->state->current_state() ) { + $this->render_dcc_onboarding_info(); + } + if ( State::STATE_ONBOARDED === $this->state->current_state() ) { + $this->render_3d_secure_info(); + } + } else { + $this->render_dcc_does_not_apply_info(); + } + } } /** @@ -329,4 +309,101 @@ class SettingsRenderer { > '; } } + + /** + * Renders the 3d secure info text. + */ + private function render_3d_secure_info() { + ?> + + + +

        + ', + '' + ) + ); + ?> +

        + + + + + + +

        + + + + + +

        + + + + + + +

        + + + + + +

        + + + Date: Thu, 3 Sep 2020 08:48:50 +0300 Subject: [PATCH 26/31] better description for dcc enable option --- modules/ppcp-wc-gateway/services.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index c557b1858..0b3f71cce 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -290,6 +290,9 @@ return array( ), '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', From 116837eadb5036990bd618ad57544588abc0a7e0 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 11:23:11 +0300 Subject: [PATCH 27/31] add option to enable paypal in checkout --- modules/ppcp-wc-gateway/services.php | 12 ++++++++++++ .../src/Checkout/class-disablegateways.php | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 0b3f71cce..eebc53107 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -533,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', 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; } From d75c29c420ff68df75f68a06b0a3f585a8ce370b Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 11:23:26 +0300 Subject: [PATCH 28/31] remove outdated dcc_enable overwrite --- .../ppcp-wc-gateway/src/Settings/class-settingslistener.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index 458199b4b..bf74553b9 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -124,11 +124,6 @@ class SettingsListener { && 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] ); $this->maybe_register_webhooks( $settings ); } - if ( isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === $_GET[ SectionsRenderer::KEY ] ) { - $dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled'; - $settings['dcc_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] ) - && 1 === absint( $_POST[ $dcc_enabled_post_key ] ); - } foreach ( $settings as $id => $value ) { $this->settings->set( $id, $value ); From a4aea5330232ee7eaf066c7eaad0d870150697c2 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 11:25:02 +0300 Subject: [PATCH 29/31] update logic on when to return the DisabledSmartButton object --- modules/ppcp-button/services.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index e50046955..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_enabled' ) || ! $settings->get( 'dcc_enabled' ); - if ( $paypal_disabled && $credit_card_disabled ) { + if ( $paypal_disabled ) { return new DisabledSmartButton(); } $payee_repository = $container->get( 'api.repository.payee' ); From cc4777273a42df1960080331a06a853d31908464 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 11:40:44 +0300 Subject: [PATCH 30/31] load button component in JS sdk only when needed --- .../resources/js/modules/Renderer/Renderer.js | 2 +- .../src/Assets/class-smartbutton.php | 50 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) 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/src/Assets/class-smartbutton.php b/modules/ppcp-button/src/Assets/class-smartbutton.php index 38e7c228a..f658c8776 100644 --- a/modules/ppcp-button/src/Assets/class-smartbutton.php +++ b/modules/ppcp-button/src/Assets/class-smartbutton.php @@ -312,12 +312,15 @@ 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 - ); + + 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 + ); + } wp_enqueue_script( 'ppcp-smart-button', $this->module_url . '/assets/js/button.js', @@ -732,7 +735,40 @@ 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(); + + $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->context() === 'mini-cart' + && $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; + } + if ( $load_buttons ) { + $components[] = 'buttons'; + } if ( $this->messages_apply->for_country() ) { $components[] = 'messages'; } From 89b97f0535cd38e1108a33eefd7ce97a8653ff0a Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 3 Sep 2020 11:48:25 +0300 Subject: [PATCH 31/31] load paypal script only when needed. --- .../src/Assets/class-smartbutton.php | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/modules/ppcp-button/src/Assets/class-smartbutton.php b/modules/ppcp-button/src/Assets/class-smartbutton.php index f658c8776..a3bc2fcdf 100644 --- a/modules/ppcp-button/src/Assets/class-smartbutton.php +++ b/modules/ppcp-button/src/Assets/class-smartbutton.php @@ -321,19 +321,30 @@ class SmartButton implements SmartButtonInterface { 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() - ); + $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; } @@ -737,6 +748,26 @@ class SmartButton implements SmartButtonInterface { private function components(): array { $components = array(); + if ( $this->load_button_component() ) { + $components[] = 'buttons'; + } + if ( $this->messages_apply->for_country() ) { + $components[] = 'messages'; + } + if ( $this->dcc_is_enabled() ) { + $components[] = 'hosted-fields'; + } + 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' @@ -753,8 +784,7 @@ class SmartButton implements SmartButtonInterface { $load_buttons = true; } if ( - $this->context() === 'mini-cart' - && $this->settings->has( 'button_mini-cart_enabled' ) + $this->settings->has( 'button_mini-cart_enabled' ) && $this->settings->get( 'button_mini-cart_enabled' ) ) { $load_buttons = true; @@ -766,16 +796,7 @@ class SmartButton implements SmartButtonInterface { ) { $load_buttons = true; } - if ( $load_buttons ) { - $components[] = 'buttons'; - } - if ( $this->messages_apply->for_country() ) { - $components[] = 'messages'; - } - if ( $this->dcc_is_enabled() ) { - $components[] = 'hosted-fields'; - } - return $components; + return $load_buttons; } /**