From 6f434a4f706f2c9ff30c52e300d8b8f50b50d87e Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Fri, 12 Jul 2024 19:18:24 -0500 Subject: [PATCH] Disable ECE for non shipping products if Tax is calculated on billing address (#9089) --- ...-non-shipping-products-tax-billing-address | 4 +++ client/express-checkout/utils/normalize.js | 1 + .../express-checkout/utils/test/normalize.js | 2 ++ ...ayments-express-checkout-button-helper.php | 32 +++++++++++++++++++ .../class-fraud-prevention-service.php | 13 ++++++-- 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 changelog/as-disable-ece-non-shipping-products-tax-billing-address diff --git a/changelog/as-disable-ece-non-shipping-products-tax-billing-address b/changelog/as-disable-ece-non-shipping-products-tax-billing-address new file mode 100644 index 00000000000..846d41bc0df --- /dev/null +++ b/changelog/as-disable-ece-non-shipping-products-tax-billing-address @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Disable ECE for non shipping products if Tax is calculated on billing address. diff --git a/client/express-checkout/utils/normalize.js b/client/express-checkout/utils/normalize.js index e07cc89e450..a5ab114fffc 100644 --- a/client/express-checkout/utils/normalize.js +++ b/client/express-checkout/utils/normalize.js @@ -87,6 +87,7 @@ export const normalizePayForOrderData = ( event, paymentMethodId ) => { payment_method: 'woocommerce_payments', 'wcpay-payment-method': paymentMethodId, express_payment_type: event?.expressPaymentType, + 'wcpay-fraud-prevention-token': window.wcpayFraudPreventionToken ?? '', }; }; diff --git a/client/express-checkout/utils/test/normalize.js b/client/express-checkout/utils/test/normalize.js index 96dca4c5b49..aaa15dee135 100644 --- a/client/express-checkout/utils/test/normalize.js +++ b/client/express-checkout/utils/test/normalize.js @@ -302,6 +302,7 @@ describe( 'Express checkout normalization', () => { expect( normalizePayForOrderData( event, 'pm_123456' ) ).toEqual( { payment_method: 'woocommerce_payments', 'wcpay-payment-method': 'pm_123456', + 'wcpay-fraud-prevention-token': 'token123', express_payment_type: 'express', } ); } ); @@ -315,6 +316,7 @@ describe( 'Express checkout normalization', () => { ).toEqual( { payment_method: 'woocommerce_payments', 'wcpay-payment-method': '', + 'wcpay-fraud-prevention-token': 'token123', express_payment_type: undefined, } ); } ); diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php b/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php index 5d52fa94691..eddb8bf24ef 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php @@ -417,6 +417,23 @@ public function should_show_express_checkout_button() { return true; } + // Non-shipping product and billing is calculated based on shopper billing addres. Excludes Pay for Order page. + if ( + // If the product doesn't needs shipping. + ( + // on the product page. + ( $this->is_product() && ! $this->product_needs_shipping( $this->get_product() ) ) || + + // on the cart or checkout page. + ( ( $this->is_cart() || $this->is_checkout() ) && ! WC()->cart->needs_shipping() ) + ) + + // ...and billing is calculated based on billing address. + && 'billing' === get_option( 'woocommerce_tax_based_on' ) + ) { + return false; + } + // Cart total is 0 or is on product page and product price is 0. // Exclude pay-for-order pages from this check. if ( @@ -431,6 +448,21 @@ public function should_show_express_checkout_button() { return true; } + /** + * Check if the passed product needs to be shipped. + * + * @param WC_Product $product The product to check. + * + * @return bool Returns true if the product requires shipping; otherwise, returns false. + */ + public function product_needs_shipping( WC_Product $product ) { + if ( ! $product ) { + return false; + } + + return wc_shipping_enabled() && 0 !== wc_get_shipping_method_count( true ) && $product->needs_shipping(); + } + /** * Checks to make sure product type is supported. * diff --git a/includes/fraud-prevention/class-fraud-prevention-service.php b/includes/fraud-prevention/class-fraud-prevention-service.php index 9bef49875c5..db783718a44 100644 --- a/includes/fraud-prevention/class-fraud-prevention-service.php +++ b/includes/fraud-prevention/class-fraud-prevention-service.php @@ -87,9 +87,9 @@ public static function maybe_append_fraud_prevention_token() { return; } - // Don't add the token if the user isn't on the cart, checkout or product page. + // Don't add the token if the user isn't on the cart, checkout, product or pay for order page. // Checking the product and cart page too because the user can pay quickly via the payment buttons on that page. - if ( ! is_checkout() && ! is_cart() && ! is_product() ) { + if ( ! is_checkout() && ! is_cart() && ! is_product() && ! $instance->is_pay_for_order_page() ) { return; } @@ -103,6 +103,15 @@ public static function maybe_append_fraud_prevention_token() { ); } + /** + * Checks if this is the Pay for Order page. + * + * @return bool + */ + public function is_pay_for_order_page() { + return is_checkout() && isset( $_GET['pay_for_order'] ); // phpcs:ignore WordPress.Security.NonceVerification + } + /** * Sets a instance to be used in request cycle. * Introduced primarily for supporting unit tests.