Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amazon Pay ECE: update settings to use new payment method #4022

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* Add - Show upcoming legacy checkout experience deprecation notice.

= 9.2.0 - 2025-02-13 =
Expand Down
87 changes: 81 additions & 6 deletions client/data/settings/__tests__/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 ],
} );
} );
} );
} );
30 changes: 27 additions & 3 deletions client/data/settings/hooks.js
Original file line number Diff line number Diff line change
@@ -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 = [];

Expand Down Expand Up @@ -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',
''
Expand Down
22 changes: 0 additions & 22 deletions includes/admin/class-wc-rest-stripe-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ 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.
* Add - Show upcoming legacy checkout experience deprecation notice.

[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
Expand Down
Loading