Skip to content

Commit eb0f1f8

Browse files
authored
[ECE] Hide express checkout if cards are disabled (#3589)
* Hide ECE if cards are disabled * Hide PRBs if cards are disabled * Add changelog and readme entries * Hide Express checkout section in wp-admin if cards are disabled * Add unit test
1 parent c97f908 commit eb0f1f8

File tree

8 files changed

+161
-72
lines changed

8 files changed

+161
-72
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 8.9.0 - xxxx-xx-xx =
4+
* Fix - Hide express checkout when credit card payments are not enabled.
45
* Fix - Fix issues when detaching payment methods on staging sites (with the new checkout experience enabled).
56
* Fix - Display a notice if taxes vary by customer's billing address when checking out using the Stripe Express Checkout Element.
67
* Tweak - Makes the new Stripe Express Checkout Element enabled by default.

client/blocks/express-checkout/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const expressCheckoutElementsGooglePay = ( api ) => ( {
2525
),
2626
edit: <GooglePayPreview />,
2727
canMakePayment: ( { cart } ) => {
28+
if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) {
29+
return false;
30+
}
31+
2832
// eslint-disable-next-line camelcase
2933
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
3034
return false;
@@ -53,6 +57,10 @@ const expressCheckoutElementsApplePay = ( api ) => ( {
5357
),
5458
edit: <ApplePayPreview />,
5559
canMakePayment: ( { cart } ) => {
60+
if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) {
61+
return false;
62+
}
63+
5664
// eslint-disable-next-line camelcase
5765
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
5866
return false;
@@ -80,6 +88,10 @@ const expressCheckoutElementsStripeLink = ( api ) => ( {
8088
),
8189
edit: <StripeLinkPreview />,
8290
canMakePayment: ( { cart } ) => {
91+
if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) {
92+
return false;
93+
}
94+
8395
// eslint-disable-next-line camelcase
8496
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
8597
return false;

client/settings/payment-request-section/index.js

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const PaymentRequestSection = () => {
4040
}
4141
};
4242

43+
const displayExpressPaymentMethods = enabledMethodIds.includes( 'card' );
4344
const displayLinkPaymentMethod =
4445
enabledMethodIds.includes( 'card' ) &&
4546
availablePaymentMethodIds.includes( linkMethodID );
@@ -53,70 +54,83 @@ const PaymentRequestSection = () => {
5354
<Card className="express-checkouts">
5455
<CardBody size={ 0 }>
5556
<ul className="express-checkouts-list">
56-
<li className="express-checkout has-icon-border">
57-
<div className="express-checkout__checkbox">
58-
<CheckboxControl
59-
checked={ isPaymentRequestEnabled }
60-
onChange={ updateIsPaymentRequestEnabled }
61-
/>
62-
</div>
63-
<div className="express-checkout__icon">
64-
<PaymentRequestIcon size="medium" />
65-
</div>
66-
<div className="express-checkout__label-container">
67-
<div className="express-checkout__label">
68-
{ __(
69-
'Apple Pay / Google Pay',
70-
'woocommerce-gateway-stripe'
71-
) }
57+
{ ! displayExpressPaymentMethods &&
58+
! displayLinkPaymentMethod && (
59+
<li className="express-checkout">
60+
<div>
61+
{ __(
62+
'Credit card / debit card must be enabled as a payment method in order to use Express Checkout.',
63+
'woocommerce-gateway-stripe'
64+
) }
65+
</div>
66+
</li>
67+
) }
68+
{ displayExpressPaymentMethods && (
69+
<li className="express-checkout has-icon-border">
70+
<div className="express-checkout__checkbox">
71+
<CheckboxControl
72+
checked={ isPaymentRequestEnabled }
73+
onChange={ updateIsPaymentRequestEnabled }
74+
/>
7275
</div>
73-
<div className="express-checkout__description">
74-
{
75-
/* eslint-disable jsx-a11y/anchor-has-content */
76-
interpolateComponents( {
77-
mixedString: __(
78-
'Boost sales by offering a fast, simple, and secure checkout experience.' +
79-
'By enabling this feature, you agree to {{stripeLink}}Stripe{{/stripeLink}}, ' +
80-
"{{appleLink}}Apple{{/appleLink}}, and {{googleLink}}Google{{/googleLink}}'s terms of use.",
81-
'woocommerce-gateway-stripe'
82-
),
83-
components: {
84-
stripeLink: (
85-
<a
86-
target="_blank"
87-
rel="noreferrer"
88-
href="https://stripe.com/apple-pay/legal"
89-
/>
90-
),
91-
appleLink: (
92-
<a
93-
target="_blank"
94-
rel="noreferrer"
95-
href="https://developer.apple.com/apple-pay/acceptable-use-guidelines-for-websites/"
96-
/>
97-
),
98-
googleLink: (
99-
<a
100-
target="_blank"
101-
rel="noreferrer"
102-
href="https://androidpay.developers.google.com/terms/sellertos"
103-
/>
76+
<div className="express-checkout__icon">
77+
<PaymentRequestIcon size="medium" />
78+
</div>
79+
<div className="express-checkout__label-container">
80+
<div className="express-checkout__label">
81+
{ __(
82+
'Apple Pay / Google Pay',
83+
'woocommerce-gateway-stripe'
84+
) }
85+
</div>
86+
<div className="express-checkout__description">
87+
{
88+
/* eslint-disable jsx-a11y/anchor-has-content */
89+
interpolateComponents( {
90+
mixedString: __(
91+
'Boost sales by offering a fast, simple, and secure checkout experience.' +
92+
'By enabling this feature, you agree to {{stripeLink}}Stripe{{/stripeLink}}, ' +
93+
"{{appleLink}}Apple{{/appleLink}}, and {{googleLink}}Google{{/googleLink}}'s terms of use.",
94+
'woocommerce-gateway-stripe'
10495
),
105-
},
106-
} )
107-
/* eslint-enable jsx-a11y/anchor-has-content */
108-
}
96+
components: {
97+
stripeLink: (
98+
<a
99+
target="_blank"
100+
rel="noreferrer"
101+
href="https://stripe.com/apple-pay/legal"
102+
/>
103+
),
104+
appleLink: (
105+
<a
106+
target="_blank"
107+
rel="noreferrer"
108+
href="https://developer.apple.com/apple-pay/acceptable-use-guidelines-for-websites/"
109+
/>
110+
),
111+
googleLink: (
112+
<a
113+
target="_blank"
114+
rel="noreferrer"
115+
href="https://androidpay.developers.google.com/terms/sellertos"
116+
/>
117+
),
118+
},
119+
} )
120+
/* eslint-enable jsx-a11y/anchor-has-content */
121+
}
122+
</div>
109123
</div>
110-
</div>
111-
<div className="express-checkout__link">
112-
<a href={ customizeAppearanceURL }>
113-
{ __(
114-
'Customize',
115-
'woocommerce-gateway-stripe'
116-
) }
117-
</a>
118-
</div>
119-
</li>
124+
<div className="express-checkout__link">
125+
<a href={ customizeAppearanceURL }>
126+
{ __(
127+
'Customize',
128+
'woocommerce-gateway-stripe'
129+
) }
130+
</a>
131+
</div>
132+
</li>
133+
) }
120134
{ displayLinkPaymentMethod && (
121135
<li className="express-checkout has-icon-border">
122136
<div className="express-checkout__checkbox loadable-checkbox label-hidden">

includes/class-wc-stripe-blocks-support.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,14 @@ public function get_payment_method_data() {
188188
$js_params,
189189
// Blocks-specific options
190190
[
191-
'icons' => $this->get_icons(),
192-
'supports' => $this->get_supported_features(),
193-
'showSavedCards' => $this->get_show_saved_cards(),
194-
'showSaveOption' => $this->get_show_save_option(),
195-
'isAdmin' => is_admin(),
196-
'shouldShowPaymentRequestButton' => $this->should_show_payment_request_button(),
197-
'button' => [
191+
'icons' => $this->get_icons(),
192+
'supports' => $this->get_supported_features(),
193+
'showSavedCards' => $this->get_show_saved_cards(),
194+
'showSaveOption' => $this->get_show_save_option(),
195+
'isAdmin' => is_admin(),
196+
'shouldShowPaymentRequestButton' => $this->should_show_payment_request_button(),
197+
'shouldShowExpressCheckoutButton' => $this->should_show_express_checkout_button(),
198+
'button' => [
198199
'customLabel' => $this->payment_request_configuration->get_button_label(),
199200
],
200201
]
@@ -255,10 +256,15 @@ private function should_show_payment_request_button() {
255256
* @return boolean True if ECEs should be displayed, false otherwise.
256257
*/
257258
private function should_show_express_checkout_button() {
259+
// Don't show if ECEs are turned off in settings.
260+
if ( ! $this->express_checkout_configuration->express_checkout_helper->is_express_checkout_enabled() ) {
261+
return false;
262+
}
263+
258264
// Don't show if ECEs are supposed to be hidden on the cart page.
259265
if (
260266
has_block( 'woocommerce/cart' )
261-
&& ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_cart_page()()
267+
&& ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_cart_page()
262268
) {
263269
return false;
264270
}

includes/payment-methods/class-wc-stripe-express-checkout-helper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ public function should_show_express_checkout_button() {
542542
return false;
543543
}
544544

545+
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
546+
if ( ! isset( $available_gateways['stripe'] ) ) {
547+
return false;
548+
}
549+
545550
// Don't show if on the cart or checkout page, or if page contains the cart or checkout
546551
// shortcodes, with items in the cart that aren't supported.
547552
if (
@@ -561,7 +566,7 @@ public function should_show_express_checkout_button() {
561566
return false;
562567
}
563568

564-
// Don't show if product page PRB is disabled.
569+
// Don't show if product page ECE is disabled.
565570
if ( $this->is_product() && ! $this->should_show_ece_on_product_pages() ) {
566571
return false;
567572
}

includes/payment-methods/class-wc-stripe-payment-request.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ public function should_show_payment_request_button() {
967967
return false;
968968
}
969969

970+
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
971+
if ( ! isset( $available_gateways['stripe'] ) ) {
972+
return false;
973+
}
974+
970975
// Don't show if on the cart or checkout page, or if page contains the cart or checkout
971976
// shortcodes, with items in the cart that aren't supported.
972977
if (

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 8.9.0 - xxxx-xx-xx =
114+
* Fix - Hide express checkout when credit card payments are not enabled.
114115
* Fix - Fix issues when detaching payment methods on staging sites (with the new checkout experience enabled).
115116
* Fix - Display a notice if taxes vary by customer's billing address when checking out using the Stripe Express Checkout Element.
116117
* Tweak - Makes the new Stripe Express Checkout Element enabled by default.

tests/phpunit/test-wc-stripe-express-checkout-helper.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
/**
44
* These tests make assertions against class WC_Stripe_Express_Checkout_Helper.
55
*
6-
* @package WooCommerce_Stripe/Tests/WC_Stripe_Express_Checkout_Helper
6+
* @package WooCommerce_Stripe/Tests/WC_Stripe_Express_Checkout_Helper_Test
77
*/
88

99
/**
10-
* WC_Stripe_Express_Checkout_Helper class.
10+
* WC_Stripe_Express_Checkout_Helper_Test class.
1111
*/
1212
class WC_Stripe_Express_Checkout_Helper_Test extends WP_UnitTestCase {
1313
public function set_up() {
@@ -54,6 +54,10 @@ public function test_hides_ece_if_cannot_compute_taxes() {
5454
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
5555
define( 'WOOCOMMERCE_CHECKOUT', true );
5656
}
57+
$original_gateways = WC()->payment_gateways()->payment_gateways;
58+
WC()->payment_gateways()->payment_gateways = [
59+
'stripe' => new WC_Gateway_Stripe(),
60+
];
5761

5862
// Create virtual product and add to cart.
5963
$virtual_product = WC_Helper_Product::create_simple_product();
@@ -85,6 +89,47 @@ public function test_hides_ece_if_cannot_compute_taxes() {
8589
$shippable_product = WC_Helper_Product::create_simple_product();
8690
WC()->cart->add_to_cart( $shippable_product->get_id(), 1 );
8791
$this->assertTrue( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
92+
93+
// Restore original gateways.
94+
WC()->payment_gateways()->payment_gateways = $original_gateways;
95+
}
96+
97+
/**
98+
* Test should_show_express_checkout_button, gateway logic.
99+
*/
100+
public function test_hides_ece_if_stripe_gateway_unavailable() {
101+
$wc_stripe_ece_helper_mock = $this->createPartialMock(
102+
WC_Stripe_Express_Checkout_Helper::class,
103+
[
104+
'is_product',
105+
'allowed_items_in_cart',
106+
'should_show_ece_on_cart_page',
107+
'should_show_ece_on_checkout_page',
108+
]
109+
);
110+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'is_product' )->willReturn( false );
111+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'allowed_items_in_cart' )->willReturn( true );
112+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'should_show_ece_on_cart_page' )->willReturn( true );
113+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'should_show_ece_on_checkout_page' )->willReturn( true );
114+
$wc_stripe_ece_helper_mock->testmode = true;
115+
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
116+
define( 'WOOCOMMERCE_CHECKOUT', true );
117+
}
118+
$original_gateways = WC()->payment_gateways()->payment_gateways;
119+
120+
// Hide if 'stripe' gateway is unavailable.
121+
update_option( 'woocommerce_calc_taxes', 'no' );
122+
WC()->payment_gateways()->payment_gateways = [
123+
'stripe' => new WC_Gateway_Stripe(),
124+
'stripe_alipay' => new WC_Gateway_Stripe_Alipay(),
125+
];
126+
$this->assertTrue( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
127+
128+
unset( WC()->payment_gateways()->payment_gateways['stripe'] );
129+
$this->assertFalse( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
130+
131+
// Restore original gateways.
132+
WC()->payment_gateways()->payment_gateways = $original_gateways;
88133
}
89134

90135
/**

0 commit comments

Comments
 (0)