diff --git a/changelog.txt b/changelog.txt index d4eb0c4af..8e57b59dd 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 8.9.0 - xxxx-xx-xx = +* Fix - Hide express checkout when credit card payments are not enabled. * Fix - Fix issues when detaching payment methods on staging sites (with the new checkout experience enabled). * Fix - Display a notice if taxes vary by customer's billing address when checking out using the Stripe Express Checkout Element. * Tweak - Makes the new Stripe Express Checkout Element enabled by default. diff --git a/client/blocks/express-checkout/index.js b/client/blocks/express-checkout/index.js index 27222319d..3f3607bed 100644 --- a/client/blocks/express-checkout/index.js +++ b/client/blocks/express-checkout/index.js @@ -25,6 +25,10 @@ const expressCheckoutElementsGooglePay = ( api ) => ( { ), edit: , canMakePayment: ( { cart } ) => { + if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) { + return false; + } + // eslint-disable-next-line camelcase if ( typeof wc_stripe_express_checkout_params === 'undefined' ) { return false; @@ -53,6 +57,10 @@ const expressCheckoutElementsApplePay = ( api ) => ( { ), edit: , canMakePayment: ( { cart } ) => { + if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) { + return false; + } + // eslint-disable-next-line camelcase if ( typeof wc_stripe_express_checkout_params === 'undefined' ) { return false; @@ -80,6 +88,10 @@ const expressCheckoutElementsStripeLink = ( api ) => ( { ), edit: , canMakePayment: ( { cart } ) => { + if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) { + return false; + } + // eslint-disable-next-line camelcase if ( typeof wc_stripe_express_checkout_params === 'undefined' ) { return false; diff --git a/client/settings/payment-request-section/index.js b/client/settings/payment-request-section/index.js index 99f4b2ab1..7cde86b09 100644 --- a/client/settings/payment-request-section/index.js +++ b/client/settings/payment-request-section/index.js @@ -40,6 +40,7 @@ const PaymentRequestSection = () => { } }; + const displayExpressPaymentMethods = enabledMethodIds.includes( 'card' ); const displayLinkPaymentMethod = enabledMethodIds.includes( 'card' ) && availablePaymentMethodIds.includes( linkMethodID ); @@ -53,70 +54,83 @@ const PaymentRequestSection = () => {
    -
  • -
    - -
    -
    - -
    -
    -
    - { __( - 'Apple Pay / Google Pay', - 'woocommerce-gateway-stripe' - ) } + { ! displayExpressPaymentMethods && + ! displayLinkPaymentMethod && ( +
  • +
    + { __( + 'Credit card / debit card must be enabled as a payment method in order to use Express Checkout.', + 'woocommerce-gateway-stripe' + ) } +
    +
  • + ) } + { displayExpressPaymentMethods && ( +
  • +
    +
    -
    - { - /* eslint-disable jsx-a11y/anchor-has-content */ - interpolateComponents( { - mixedString: __( - 'Boost sales by offering a fast, simple, and secure checkout experience.' + - 'By enabling this feature, you agree to {{stripeLink}}Stripe{{/stripeLink}}, ' + - "{{appleLink}}Apple{{/appleLink}}, and {{googleLink}}Google{{/googleLink}}'s terms of use.", - 'woocommerce-gateway-stripe' - ), - components: { - stripeLink: ( - - ), - appleLink: ( - - ), - googleLink: ( - +
    + +
    +
    -
    - -
  • + + + ) } { displayLinkPaymentMethod && (
  • diff --git a/includes/class-wc-stripe-blocks-support.php b/includes/class-wc-stripe-blocks-support.php index fa93dcdda..0fd0a49bb 100644 --- a/includes/class-wc-stripe-blocks-support.php +++ b/includes/class-wc-stripe-blocks-support.php @@ -188,13 +188,14 @@ public function get_payment_method_data() { $js_params, // Blocks-specific options [ - 'icons' => $this->get_icons(), - 'supports' => $this->get_supported_features(), - 'showSavedCards' => $this->get_show_saved_cards(), - 'showSaveOption' => $this->get_show_save_option(), - 'isAdmin' => is_admin(), - 'shouldShowPaymentRequestButton' => $this->should_show_payment_request_button(), - 'button' => [ + 'icons' => $this->get_icons(), + 'supports' => $this->get_supported_features(), + 'showSavedCards' => $this->get_show_saved_cards(), + 'showSaveOption' => $this->get_show_save_option(), + 'isAdmin' => is_admin(), + 'shouldShowPaymentRequestButton' => $this->should_show_payment_request_button(), + 'shouldShowExpressCheckoutButton' => $this->should_show_express_checkout_button(), + 'button' => [ 'customLabel' => $this->payment_request_configuration->get_button_label(), ], ] @@ -255,10 +256,15 @@ private function should_show_payment_request_button() { * @return boolean True if ECEs should be displayed, false otherwise. */ private function should_show_express_checkout_button() { + // Don't show if ECEs are turned off in settings. + if ( ! $this->express_checkout_configuration->express_checkout_helper->is_express_checkout_enabled() ) { + return false; + } + // Don't show if ECEs are supposed to be hidden on the cart page. if ( has_block( 'woocommerce/cart' ) - && ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_cart_page()() + && ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_cart_page() ) { return false; } diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php index e178c6da3..7b1fc1bfa 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php @@ -542,6 +542,11 @@ public function should_show_express_checkout_button() { return false; } + $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); + if ( ! isset( $available_gateways['stripe'] ) ) { + return false; + } + // Don't show if on the cart or checkout page, or if page contains the cart or checkout // shortcodes, with items in the cart that aren't supported. if ( @@ -561,7 +566,7 @@ public function should_show_express_checkout_button() { return false; } - // Don't show if product page PRB is disabled. + // Don't show if product page ECE is disabled. if ( $this->is_product() && ! $this->should_show_ece_on_product_pages() ) { return false; } diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 660e6ea40..7bb83d579 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -967,6 +967,11 @@ public function should_show_payment_request_button() { return false; } + $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); + if ( ! isset( $available_gateways['stripe'] ) ) { + return false; + } + // Don't show if on the cart or checkout page, or if page contains the cart or checkout // shortcodes, with items in the cart that aren't supported. if ( diff --git a/readme.txt b/readme.txt index 054fe0343..3671a1562 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 8.9.0 - xxxx-xx-xx = +* Fix - Hide express checkout when credit card payments are not enabled. * Fix - Fix issues when detaching payment methods on staging sites (with the new checkout experience enabled). * Fix - Display a notice if taxes vary by customer's billing address when checking out using the Stripe Express Checkout Element. * Tweak - Makes the new Stripe Express Checkout Element enabled by default. diff --git a/tests/phpunit/test-wc-stripe-express-checkout-helper.php b/tests/phpunit/test-wc-stripe-express-checkout-helper.php index a416a4b25..201097806 100644 --- a/tests/phpunit/test-wc-stripe-express-checkout-helper.php +++ b/tests/phpunit/test-wc-stripe-express-checkout-helper.php @@ -3,11 +3,11 @@ /** * These tests make assertions against class WC_Stripe_Express_Checkout_Helper. * - * @package WooCommerce_Stripe/Tests/WC_Stripe_Express_Checkout_Helper + * @package WooCommerce_Stripe/Tests/WC_Stripe_Express_Checkout_Helper_Test */ /** - * WC_Stripe_Express_Checkout_Helper class. + * WC_Stripe_Express_Checkout_Helper_Test class. */ class WC_Stripe_Express_Checkout_Helper_Test extends WP_UnitTestCase { public function set_up() { @@ -54,6 +54,10 @@ public function test_hides_ece_if_cannot_compute_taxes() { if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { define( 'WOOCOMMERCE_CHECKOUT', true ); } + $original_gateways = WC()->payment_gateways()->payment_gateways; + WC()->payment_gateways()->payment_gateways = [ + 'stripe' => new WC_Gateway_Stripe(), + ]; // Create virtual product and add to cart. $virtual_product = WC_Helper_Product::create_simple_product(); @@ -85,6 +89,47 @@ public function test_hides_ece_if_cannot_compute_taxes() { $shippable_product = WC_Helper_Product::create_simple_product(); WC()->cart->add_to_cart( $shippable_product->get_id(), 1 ); $this->assertTrue( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() ); + + // Restore original gateways. + WC()->payment_gateways()->payment_gateways = $original_gateways; + } + + /** + * Test should_show_express_checkout_button, gateway logic. + */ + public function test_hides_ece_if_stripe_gateway_unavailable() { + $wc_stripe_ece_helper_mock = $this->createPartialMock( + WC_Stripe_Express_Checkout_Helper::class, + [ + 'is_product', + 'allowed_items_in_cart', + 'should_show_ece_on_cart_page', + 'should_show_ece_on_checkout_page', + ] + ); + $wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'is_product' )->willReturn( false ); + $wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'allowed_items_in_cart' )->willReturn( true ); + $wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'should_show_ece_on_cart_page' )->willReturn( true ); + $wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'should_show_ece_on_checkout_page' )->willReturn( true ); + $wc_stripe_ece_helper_mock->testmode = true; + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { + define( 'WOOCOMMERCE_CHECKOUT', true ); + } + $original_gateways = WC()->payment_gateways()->payment_gateways; + + // Hide if 'stripe' gateway is unavailable. + update_option( 'woocommerce_calc_taxes', 'no' ); + WC()->payment_gateways()->payment_gateways = [ + 'stripe' => new WC_Gateway_Stripe(), + 'stripe_alipay' => new WC_Gateway_Stripe_Alipay(), + ]; + $this->assertTrue( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() ); + + unset( WC()->payment_gateways()->payment_gateways['stripe'] ); + $this->assertFalse( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() ); + + // Restore original gateways. + WC()->payment_gateways()->payment_gateways = $original_gateways; } /**