From 64e5719200606c4ceb116490653bb355859744d4 Mon Sep 17 00:00:00 2001 From: Anne Mirasol Date: Wed, 5 Mar 2025 14:22:46 -0600 Subject: [PATCH 1/2] Update settings to control Amazon Pay via enabled payment methods --- client/data/settings/__tests__/hooks.js | 87 +++++++++++++++++-- client/data/settings/hooks.js | 30 ++++++- ...ass-wc-rest-stripe-settings-controller.php | 22 ----- ...ass-wc-stripe-express-checkout-element.php | 2 +- ...lass-wc-stripe-express-checkout-helper.php | 14 +-- .../class-wc-stripe-upe-payment-gateway.php | 2 +- ...c-stripe-upe-payment-method-amazon-pay.php | 34 ++++++++ ...ass-wc-rest-stripe-settings-controller.php | 1 - 8 files changed, 147 insertions(+), 45 deletions(-) diff --git a/client/data/settings/__tests__/hooks.js b/client/data/settings/__tests__/hooks.js index f421fa8368..e460aa0bb2 100644 --- a/client/data/settings/__tests__/hooks.js +++ b/client/data/settings/__tests__/hooks.js @@ -29,6 +29,7 @@ import { PAYMENT_METHOD_CARD, PAYMENT_METHOD_EPS, PAYMENT_METHOD_GIROPAY, + PAYMENT_METHOD_AMAZON_PAY, } from 'wcstripe/stripe-utils/constants'; jest.mock( '@wordpress/data' ); @@ -322,12 +323,6 @@ describe( 'Settings hooks tests', () => { testedValue: [ 'checkout', 'cart' ], fallbackValue: [], }, - useAmazonPayEnabledSettings: { - hook: useAmazonPayEnabledSettings, - storeKey: 'is_amazon_pay_enabled', - testedValue: true, - fallbackValue: false, - }, useAmazonPayButtonSize: { hook: useAmazonPayButtonSize, storeKey: 'amazon_pay_button_size', @@ -399,4 +394,84 @@ describe( 'Settings hooks tests', () => { } ); } ); + + describe( 'useAmazonPayEnabledSettings()', () => { + test( 'returns true when PAYMENT_METHOD_AMAZON_PAY is in enabled_payment_method_ids', () => { + selectors = { + getSettings: jest.fn( () => ( { + enabled_payment_method_ids: [ PAYMENT_METHOD_AMAZON_PAY ], + } ) ), + }; + + const { result } = renderHook( useAmazonPayEnabledSettings ); + const [ value ] = result.current; + + expect( value ).toBeTruthy(); + } ); + + test( 'returns false when PAYMENT_METHOD_AMAZON_PAY is not in enabled_payment_method_ids', () => { + selectors = { + getSettings: jest.fn( () => ( { + enabled_payment_method_ids: [ PAYMENT_METHOD_CARD ], + } ) ), + }; + + const { result } = renderHook( useAmazonPayEnabledSettings ); + const [ value ] = result.current; + + expect( value ).toBeFalsy(); + } ); + + test( 'updates enabled_payment_method_ids when enabling Amazon Pay', () => { + actions = { + updateSettingsValues: jest.fn(), + }; + + selectors = { + getSettings: jest.fn( () => ( { + enabled_payment_method_ids: [ PAYMENT_METHOD_CARD ], + } ) ), + }; + + const { result } = renderHook( useAmazonPayEnabledSettings ); + const [ , action ] = result.current; + + act( () => { + action( true ); + } ); + + expect( actions.updateSettingsValues ).toHaveBeenCalledWith( { + enabled_payment_method_ids: [ + PAYMENT_METHOD_CARD, + PAYMENT_METHOD_AMAZON_PAY, + ], + } ); + } ); + + test( 'updates enabled_payment_method_ids when disabling Amazon Pay', () => { + actions = { + updateSettingsValues: jest.fn(), + }; + + selectors = { + getSettings: jest.fn( () => ( { + enabled_payment_method_ids: [ + PAYMENT_METHOD_CARD, + PAYMENT_METHOD_AMAZON_PAY, + ], + } ) ), + }; + + const { result } = renderHook( useAmazonPayEnabledSettings ); + const [ , action ] = result.current; + + act( () => { + action( false ); + } ); + + expect( actions.updateSettingsValues ).toHaveBeenCalledWith( { + enabled_payment_method_ids: [ PAYMENT_METHOD_CARD ], + } ); + } ); + } ); } ); diff --git a/client/data/settings/hooks.js b/client/data/settings/hooks.js index bbe74e9c93..b9fc214b90 100644 --- a/client/data/settings/hooks.js +++ b/client/data/settings/hooks.js @@ -1,6 +1,7 @@ import { useSelect, useDispatch } from '@wordpress/data'; import { useCallback } from 'react'; import { STORE_NAME } from '../constants'; +import { PAYMENT_METHOD_AMAZON_PAY } from 'wcstripe/stripe-utils/constants'; const EMPTY_ARR = []; @@ -156,9 +157,32 @@ export const usePaymentRequestLocations = makeSettingsHook( 'payment_request_button_locations', EMPTY_ARR ); -export const useAmazonPayEnabledSettings = makeSettingsHook( - 'is_amazon_pay_enabled' -); +export const useAmazonPayEnabledSettings = () => { + const [ + enabledMethodIds, + updateEnabledMethodIds, + ] = useEnabledPaymentMethodIds(); + const isAmazonPayEnabled = enabledMethodIds.includes( + PAYMENT_METHOD_AMAZON_PAY + ); + + const updateIsAmazonPayEnabled = ( isEnabled ) => { + if ( isEnabled ) { + updateEnabledMethodIds( [ + ...enabledMethodIds, + PAYMENT_METHOD_AMAZON_PAY, + ] ); + } else { + updateEnabledMethodIds( [ + ...enabledMethodIds.filter( + ( id ) => id !== PAYMENT_METHOD_AMAZON_PAY + ), + ] ); + } + }; + + return [ isAmazonPayEnabled, updateIsAmazonPayEnabled ]; +}; export const useAmazonPayButtonSize = makeSettingsHook( 'amazon_pay_button_size', '' diff --git a/includes/admin/class-wc-rest-stripe-settings-controller.php b/includes/admin/class-wc-rest-stripe-settings-controller.php index 102df6d55e..9f3c6d3b80 100644 --- a/includes/admin/class-wc-rest-stripe-settings-controller.php +++ b/includes/admin/class-wc-rest-stripe-settings-controller.php @@ -88,11 +88,6 @@ public function register_routes() { 'type' => 'boolean', 'validate_callback' => 'rest_validate_request_arg', ], - 'is_amazon_pay_enabled' => [ - 'description' => __( 'If Amazon Pay should be enabled.', 'woocommerce-gateway-stripe' ), - 'type' => 'boolean', - 'validate_callback' => 'rest_validate_request_arg', - ], 'amazon_pay_button_size' => [ 'description' => __( 'Express checkout button sizes.', 'woocommerce-gateway-stripe' ), 'type' => 'string', @@ -266,7 +261,6 @@ public function get_settings() { 'individual_payment_method_settings' => $is_upe_enabled ? WC_Stripe_Helper::get_upe_individual_payment_method_settings( $this->gateway ) : WC_Stripe_Helper::get_legacy_individual_payment_method_settings(), /* Settings > Express checkouts */ - 'is_amazon_pay_enabled' => 'yes' === $this->gateway->get_option( 'amazon_pay' ), 'amazon_pay_button_size' => $this->gateway->get_validated_option( 'amazon_pay_button_size' ), 'amazon_pay_button_locations' => $this->gateway->get_validated_option( 'amazon_pay_button_locations' ), 'is_payment_request_enabled' => 'yes' === $this->gateway->get_option( 'payment_request' ), @@ -307,7 +301,6 @@ public function update_settings( WP_REST_Request $request ) { /* Settings > Express checkouts */ $this->update_is_payment_request_enabled( $request ); $this->update_payment_request_settings( $request ); - $this->update_is_amazon_pay_enabled( $request ); $this->update_amazon_pay_settings( $request ); /* Settings > Payments & transactions */ @@ -395,21 +388,6 @@ private function update_is_test_mode_enabled( WP_REST_Request $request ) { $this->gateway->update_option( 'testmode', $is_test_mode_enabled ? 'yes' : 'no' ); } - /** - * Updates the "Amazon Pay" enable/disable settings. - * - * @param WP_REST_Request $request Request object. - */ - private function update_is_amazon_pay_enabled( WP_REST_Request $request ) { - $is_amazon_pay_enabled = $request->get_param( 'is_amazon_pay_enabled' ); - - if ( null === $is_amazon_pay_enabled ) { - return; - } - - $this->gateway->update_option( 'amazon_pay', $is_amazon_pay_enabled ? 'yes' : 'no' ); - } - /** * Updates the "payment request" enable/disable settings. * diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index d8d0480614..1671f3b47a 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -190,7 +190,7 @@ public function javascript_params() { 'locale' => WC_Stripe_Helper::convert_wc_locale_to_stripe_locale( get_locale() ), 'is_link_enabled' => WC_Stripe_UPE_Payment_Method_Link::is_link_enabled(), 'is_express_checkout_enabled' => $this->express_checkout_helper->is_express_checkout_enabled(), - 'is_amazon_pay_enabled' => $this->express_checkout_helper->is_amazon_pay_enabled(), + 'is_amazon_pay_enabled' => WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled(), 'is_payment_request_enabled' => $this->express_checkout_helper->is_payment_request_enabled(), ], 'nonce' => [ 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 a52fd75a73..792b948baa 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php @@ -1359,7 +1359,9 @@ public function get_button_locations() { * @return boolean */ public function is_express_checkout_enabled() { - return $this->is_payment_request_enabled() || $this->is_amazon_pay_enabled() || WC_Stripe_UPE_Payment_Method_Link::is_link_enabled(); + return $this->is_payment_request_enabled() || + WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled() || + WC_Stripe_UPE_Payment_Method_Link::is_link_enabled(); } /** @@ -1371,16 +1373,6 @@ public function is_payment_request_enabled() { return isset( $this->stripe_settings['payment_request'] ) && 'yes' === $this->stripe_settings['payment_request']; } - /** - * Returns whether Amazon Pay is enabled. - * - * @return boolean - */ - public function is_amazon_pay_enabled() { - return WC_Stripe_Feature_Flags::is_amazon_pay_available() && - isset( $this->stripe_settings['amazon_pay'] ) && 'yes' === $this->stripe_settings['amazon_pay']; - } - /** * Returns whether Stripe express checkout element should use the Blocks API. * diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index b915daa702..86b89b6d35 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -450,7 +450,7 @@ public function javascript_params() { $stripe_params['cartContainsSubscription'] = $this->is_subscription_item_in_cart(); $stripe_params['accountCountry'] = WC_Stripe::get_instance()->account->get_account_country(); $stripe_params['isPaymentRequestEnabled'] = $express_checkout_helper->is_payment_request_enabled(); - $stripe_params['isAmazonPayEnabled'] = $express_checkout_helper->is_amazon_pay_enabled(); + $stripe_params['isAmazonPayEnabled'] = WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled(); $stripe_params['isLinkEnabled'] = WC_Stripe_UPE_Payment_Method_Link::is_link_enabled(); // Add appearance settings. diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-amazon-pay.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-amazon-pay.php index 9b5cb91d77..c00e5fda5f 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-amazon-pay.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-amazon-pay.php @@ -26,6 +26,40 @@ public function __construct() { ); } + /** + * Return if Amazon Pay is enabled. + * + * @return bool + */ + public static function is_amazon_pay_enabled() { + // Amazon Pay is disabled if feature flag is disabled. + if ( ! WC_Stripe_Feature_Flags::is_amazon_pay_available() ) { + return false; + } + + // Amazon Pay is disabled if UPE is disabled. + if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) { + return false; + } + + $upe_enabled_method_ids = WC_Stripe_Helper::get_settings( null, 'upe_checkout_experience_accepted_payments' ); + + return is_array( $upe_enabled_method_ids ) && in_array( self::STRIPE_ID, $upe_enabled_method_ids, true ); + } + + /** + * Returns whether the payment method is available. + * + * Amazon Pay is rendered as an express checkout method only, for now. + * We return false here so that it isn't considered available by WooCommerce + * and rendered as a standard payment method at checkout. + * + * @return bool + */ + public function is_available() { + return false; + } + /** * Returns whether the payment method requires automatic capture. * diff --git a/tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller.php b/tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller.php index 8ec55991d1..86bcbf1a9f 100644 --- a/tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller.php +++ b/tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller.php @@ -361,7 +361,6 @@ public function boolean_field_provider() { 'is_stripe_enabled' => [ 'is_stripe_enabled', 'enabled' ], 'is_test_mode_enabled' => [ 'is_test_mode_enabled', 'testmode' ], 'is_payment_request_enabled' => [ 'is_payment_request_enabled', 'payment_request' ], - 'is_amazon_pay_enabled' => [ 'is_amazon_pay_enabled', 'amazon_pay' ], 'is_spe_enabled' => [ 'is_spe_enabled', 'single_payment_element' ], 'is_manual_capture_enabled' => [ 'is_manual_capture_enabled', 'capture', true ], 'is_saved_cards_enabled' => [ 'is_saved_cards_enabled', 'saved_cards' ], From e6afd379ae2481be7e7e4e21b67491b8bbf6d71f Mon Sep 17 00:00:00 2001 From: Anne Mirasol Date: Fri, 7 Mar 2025 10:29:15 -0600 Subject: [PATCH 2/2] Add changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index 958ae53bfb..4bf90b6880 100644 --- a/changelog.txt +++ b/changelog.txt @@ -43,6 +43,7 @@ * Add - Add Amazon Pay payment method class. * Fix - Prevent potential duplicate renewal charges by ensuring subscription integration hooks are only attached once per Gateway ID * Update - Update Amazon Pay icon to use image from WooCommerce Design Library. +* Update - Enable/disable Amazon Pay by adding/removing it from the enabled payment methods list. = 9.2.0 - 2025-02-13 = * Fix - Fix missing product_id parameter for the express checkout add-to-cart operation. diff --git a/readme.txt b/readme.txt index 1b5c888057..d7b07b7474 100644 --- a/readme.txt +++ b/readme.txt @@ -153,5 +153,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o * Add - Add Amazon Pay payment method class. * Fix - Prevent potential duplicate renewal charges by ensuring subscription integration hooks are only attached once per Gateway ID * Update - Update Amazon Pay icon to use image from WooCommerce Design Library. +* Update - Enable/disable Amazon Pay by adding/removing it from the enabled payment methods list. [See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).