Skip to content

Commit

Permalink
[ECE] Hide express checkout if cards are disabled (#3589)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
annemirasol authored Nov 8, 2024
1 parent c97f908 commit eb0f1f8
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 72 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
12 changes: 12 additions & 0 deletions client/blocks/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const expressCheckoutElementsGooglePay = ( api ) => ( {
),
edit: <GooglePayPreview />,
canMakePayment: ( { cart } ) => {
if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) {
return false;
}

// eslint-disable-next-line camelcase
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
return false;
Expand Down Expand Up @@ -53,6 +57,10 @@ const expressCheckoutElementsApplePay = ( api ) => ( {
),
edit: <ApplePayPreview />,
canMakePayment: ( { cart } ) => {
if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) {
return false;
}

// eslint-disable-next-line camelcase
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
return false;
Expand Down Expand Up @@ -80,6 +88,10 @@ const expressCheckoutElementsStripeLink = ( api ) => ( {
),
edit: <StripeLinkPreview />,
canMakePayment: ( { cart } ) => {
if ( ! getBlocksConfiguration()?.shouldShowExpressCheckoutButton ) {
return false;
}

// eslint-disable-next-line camelcase
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
return false;
Expand Down
136 changes: 75 additions & 61 deletions client/settings/payment-request-section/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const PaymentRequestSection = () => {
}
};

const displayExpressPaymentMethods = enabledMethodIds.includes( 'card' );
const displayLinkPaymentMethod =
enabledMethodIds.includes( 'card' ) &&
availablePaymentMethodIds.includes( linkMethodID );
Expand All @@ -53,70 +54,83 @@ const PaymentRequestSection = () => {
<Card className="express-checkouts">
<CardBody size={ 0 }>
<ul className="express-checkouts-list">
<li className="express-checkout has-icon-border">
<div className="express-checkout__checkbox">
<CheckboxControl
checked={ isPaymentRequestEnabled }
onChange={ updateIsPaymentRequestEnabled }
/>
</div>
<div className="express-checkout__icon">
<PaymentRequestIcon size="medium" />
</div>
<div className="express-checkout__label-container">
<div className="express-checkout__label">
{ __(
'Apple Pay / Google Pay',
'woocommerce-gateway-stripe'
) }
{ ! displayExpressPaymentMethods &&
! displayLinkPaymentMethod && (
<li className="express-checkout">
<div>
{ __(
'Credit card / debit card must be enabled as a payment method in order to use Express Checkout.',
'woocommerce-gateway-stripe'
) }
</div>
</li>
) }
{ displayExpressPaymentMethods && (
<li className="express-checkout has-icon-border">
<div className="express-checkout__checkbox">
<CheckboxControl
checked={ isPaymentRequestEnabled }
onChange={ updateIsPaymentRequestEnabled }
/>
</div>
<div className="express-checkout__description">
{
/* 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: (
<a
target="_blank"
rel="noreferrer"
href="https://stripe.com/apple-pay/legal"
/>
),
appleLink: (
<a
target="_blank"
rel="noreferrer"
href="https://developer.apple.com/apple-pay/acceptable-use-guidelines-for-websites/"
/>
),
googleLink: (
<a
target="_blank"
rel="noreferrer"
href="https://androidpay.developers.google.com/terms/sellertos"
/>
<div className="express-checkout__icon">
<PaymentRequestIcon size="medium" />
</div>
<div className="express-checkout__label-container">
<div className="express-checkout__label">
{ __(
'Apple Pay / Google Pay',
'woocommerce-gateway-stripe'
) }
</div>
<div className="express-checkout__description">
{
/* 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'
),
},
} )
/* eslint-enable jsx-a11y/anchor-has-content */
}
components: {
stripeLink: (
<a
target="_blank"
rel="noreferrer"
href="https://stripe.com/apple-pay/legal"
/>
),
appleLink: (
<a
target="_blank"
rel="noreferrer"
href="https://developer.apple.com/apple-pay/acceptable-use-guidelines-for-websites/"
/>
),
googleLink: (
<a
target="_blank"
rel="noreferrer"
href="https://androidpay.developers.google.com/terms/sellertos"
/>
),
},
} )
/* eslint-enable jsx-a11y/anchor-has-content */
}
</div>
</div>
</div>
<div className="express-checkout__link">
<a href={ customizeAppearanceURL }>
{ __(
'Customize',
'woocommerce-gateway-stripe'
) }
</a>
</div>
</li>
<div className="express-checkout__link">
<a href={ customizeAppearanceURL }>
{ __(
'Customize',
'woocommerce-gateway-stripe'
) }
</a>
</div>
</li>
) }
{ displayLinkPaymentMethod && (
<li className="express-checkout has-icon-border">
<div className="express-checkout__checkbox loadable-checkbox label-hidden">
Expand Down
22 changes: 14 additions & 8 deletions includes/class-wc-stripe-blocks-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
]
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions includes/payment-methods/class-wc-stripe-payment-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 47 additions & 2 deletions tests/phpunit/test-wc-stripe-express-checkout-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand 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();
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit eb0f1f8

Please sign in to comment.