Block Checkout: Respect default saved payment method when using tokens (#50481)

* activeSavedToken state is unused

* Select is_default saved token by default

* changelog

* Unused objectHasProp
This commit is contained in:
Mike Jolley 2024-08-23 11:03:11 +01:00 committed by GitHub
parent ae6c278e53
commit 42cdc0e978
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 11 additions and 23 deletions

View File

@ -17,7 +17,6 @@ import { STATUS as PAYMENT_STATUS } from './constants';
export interface PaymentState {
status: string;
activePaymentMethod: string;
activeSavedToken: string;
// Available payment methods are payment methods which have been validated and can make payment.
availablePaymentMethods: PlainPaymentMethods;
availableExpressPaymentMethods: PlainExpressPaymentMethods;
@ -34,7 +33,6 @@ export interface PaymentState {
export const defaultPaymentState: PaymentState = {
status: PAYMENT_STATUS.IDLE,
activePaymentMethod: '',
activeSavedToken: '',
availablePaymentMethods: {},
availableExpressPaymentMethods: {},
savedPaymentMethods: getSetting<

View File

@ -2,7 +2,7 @@
* External dependencies
*/
import type { Reducer } from 'redux';
import { objectHasProp, PaymentResult } from '@woocommerce/types';
import { PaymentResult } from '@woocommerce/types';
/**
* Internal dependencies
@ -129,14 +129,8 @@ const reducer: Reducer< PaymentState > = (
break;
case ACTION_TYPES.SET_ACTIVE_PAYMENT_METHOD:
const activeSavedToken =
typeof state.paymentMethodData === 'object' &&
objectHasProp( action.paymentMethodData, 'token' )
? action.paymentMethodData.token + ''
: '';
newState = {
...state,
activeSavedToken,
activePaymentMethod: action.activePaymentMethod,
paymentMethodData:
action.paymentMethodData || state.paymentMethodData,

View File

@ -28,7 +28,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
@ -55,7 +54,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
} );
@ -79,7 +77,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
const nextState = reducer( stateWithRegisteredMethod, {
@ -104,7 +101,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
} );
@ -134,7 +130,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
} );
@ -162,7 +157,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
const nextState = reducer( stateWithRegisteredMethod, {
@ -187,7 +181,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
} );
@ -217,7 +210,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
const nextState = reducer( stateWithRegisteredMethod, {
@ -250,7 +242,6 @@ describe( 'paymentMethodDataReducer', () => {
shouldSavePaymentMethod: false,
errorMessage: '',
activePaymentMethod: '',
activeSavedToken: '',
incompatiblePaymentMethods: {},
} );
} );

View File

@ -25,11 +25,13 @@ export const setDefaultPaymentMethod = async (
const savedPaymentMethods =
select( PAYMENT_STORE_KEY ).getSavedPaymentMethods();
const flatSavedPaymentMethods = Object.keys( savedPaymentMethods ).flatMap(
( type ) => savedPaymentMethods[ type ]
);
const savedPaymentMethod =
Object.keys( savedPaymentMethods ).flatMap(
( type ) => savedPaymentMethods[ type ]
)[ 0 ] || undefined;
flatSavedPaymentMethods.find( ( method ) => method.is_default ) ||
flatSavedPaymentMethods[ 0 ] ||
undefined;
if ( savedPaymentMethod ) {
const token = savedPaymentMethod.tokenId.toString();
@ -61,7 +63,6 @@ export const setDefaultPaymentMethod = async (
}
dispatch( PAYMENT_STORE_KEY ).__internalSetPaymentIdle();
dispatch( PAYMENT_STORE_KEY ).__internalSetActivePaymentMethod(
paymentMethodKeys[ 0 ]
);

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Default saved payment method is now respected by block checkout.