Move paymentResult to the payment store (https://github.com/woocommerce/woocommerce-blocks/pull/7692)
* Move paymentResult to the payment store * bot: update checkstyle.xml * Update docs * Fix typerror * bot: update checkstyle.xml * bot: update checkstyle.xml Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tarun Vijwani <tarun.vijwani@automattic.com>
This commit is contained in:
parent
98480c4dc2
commit
a73d6af443
|
@ -197,7 +197,6 @@ export const CheckoutEventsProvider = ( {
|
||||||
checkoutState.orderId,
|
checkoutState.orderId,
|
||||||
checkoutState.customerId,
|
checkoutState.customerId,
|
||||||
checkoutState.orderNotes,
|
checkoutState.orderNotes,
|
||||||
checkoutState.paymentResult,
|
|
||||||
previousStatus,
|
previousStatus,
|
||||||
previousHasError,
|
previousHasError,
|
||||||
createErrorNotice,
|
createErrorNotice,
|
||||||
|
|
|
@ -4,7 +4,6 @@ export const ACTION_TYPES = {
|
||||||
SET_COMPLETE: 'SET_CHECKOUT_COMPLETE',
|
SET_COMPLETE: 'SET_CHECKOUT_COMPLETE',
|
||||||
SET_BEFORE_PROCESSING: 'SET_BEFORE_PROCESSING',
|
SET_BEFORE_PROCESSING: 'SET_BEFORE_PROCESSING',
|
||||||
SET_AFTER_PROCESSING: 'SET_AFTER_PROCESSING',
|
SET_AFTER_PROCESSING: 'SET_AFTER_PROCESSING',
|
||||||
SET_PAYMENT_RESULT: 'SET_PAYMENT_RESULT',
|
|
||||||
SET_PROCESSING: 'SET_CHECKOUT_IS_PROCESSING',
|
SET_PROCESSING: 'SET_CHECKOUT_IS_PROCESSING',
|
||||||
SET_HAS_ERROR: 'SET_CHECKOUT_HAS_ERROR',
|
SET_HAS_ERROR: 'SET_CHECKOUT_HAS_ERROR',
|
||||||
SET_CUSTOMER_ID: 'SET_CHECKOUT_CUSTOMER_ID',
|
SET_CUSTOMER_ID: 'SET_CHECKOUT_CUSTOMER_ID',
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
/**
|
|
||||||
* External dependencies
|
|
||||||
*/
|
|
||||||
import { PaymentResult } from '@woocommerce/types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
|
@ -60,16 +55,6 @@ export const __internalSetRedirectUrl = ( redirectUrl: string ) => ( {
|
||||||
redirectUrl,
|
redirectUrl,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
/**
|
|
||||||
* Store the result of the payment attempt from the /checkout StoreApi call
|
|
||||||
*
|
|
||||||
* @param data The result of the payment attempt through the StoreApi /checkout endpoints
|
|
||||||
*/
|
|
||||||
export const __internalSetPaymentResult = ( data: PaymentResult ) => ( {
|
|
||||||
type: types.SET_PAYMENT_RESULT,
|
|
||||||
data,
|
|
||||||
} );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether the checkout has an error or not
|
* Set whether the checkout has an error or not
|
||||||
*
|
*
|
||||||
|
@ -157,7 +142,6 @@ export type CheckoutAction =
|
||||||
| typeof __internalSetIdle
|
| typeof __internalSetIdle
|
||||||
| typeof __internalSetComplete
|
| typeof __internalSetComplete
|
||||||
| typeof __internalSetProcessing
|
| typeof __internalSetProcessing
|
||||||
| typeof __internalSetPaymentResult
|
|
||||||
| typeof __internalSetBeforeProcessing
|
| typeof __internalSetBeforeProcessing
|
||||||
| typeof __internalSetAfterProcessing
|
| typeof __internalSetAfterProcessing
|
||||||
| typeof __internalSetRedirectUrl
|
| typeof __internalSetRedirectUrl
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
import { isSameAddress } from '@woocommerce/base-utils';
|
import { isSameAddress } from '@woocommerce/base-utils';
|
||||||
import { PaymentResult } from '@woocommerce/types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
|
@ -14,8 +13,6 @@ export type CheckoutState = {
|
||||||
status: STATUS;
|
status: STATUS;
|
||||||
// If any of the totals, taxes, shipping, etc need to be calculated, the count will be increased here
|
// If any of the totals, taxes, shipping, etc need to be calculated, the count will be increased here
|
||||||
calculatingCount: number;
|
calculatingCount: number;
|
||||||
// The result of the payment processing
|
|
||||||
paymentResult: PaymentResult | null;
|
|
||||||
// True when the checkout is in an error state. Whatever caused the error (validation/payment method) will likely have triggered a notice.
|
// True when the checkout is in an error state. Whatever caused the error (validation/payment method) will likely have triggered a notice.
|
||||||
hasError: boolean;
|
hasError: boolean;
|
||||||
// This is the url that checkout will redirect to when it's ready.
|
// This is the url that checkout will redirect to when it's ready.
|
||||||
|
@ -47,6 +44,5 @@ export const defaultState: CheckoutState = {
|
||||||
checkoutData.shipping_address
|
checkoutData.shipping_address
|
||||||
),
|
),
|
||||||
shouldCreateAccount: false,
|
shouldCreateAccount: false,
|
||||||
paymentResult: null,
|
|
||||||
extensionData: {},
|
extensionData: {},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
/**
|
|
||||||
* External dependencies
|
|
||||||
*/
|
|
||||||
import { PaymentResult } from '@woocommerce/types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
|
@ -35,13 +30,6 @@ const reducer = ( state = defaultState, action: CheckoutAction ) => {
|
||||||
: state;
|
: state;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case types.SET_PAYMENT_RESULT:
|
|
||||||
newState = {
|
|
||||||
...state,
|
|
||||||
paymentResult: action.data as PaymentResult,
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
|
|
||||||
case types.SET_COMPLETE:
|
case types.SET_COMPLETE:
|
||||||
newState = {
|
newState = {
|
||||||
...state,
|
...state,
|
||||||
|
|
|
@ -37,28 +37,6 @@ describe.only( 'Checkout Store Reducer', () => {
|
||||||
).toEqual( expectedState );
|
).toEqual( expectedState );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
it( 'should handle SET_PAYMENT_RESULT', () => {
|
|
||||||
const mockResponse = {
|
|
||||||
message: 'success',
|
|
||||||
redirectUrl: 'https://example.com',
|
|
||||||
paymentStatus: 'not set' as const,
|
|
||||||
paymentDetails: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const expectedState = {
|
|
||||||
...defaultState,
|
|
||||||
status: STATUS.IDLE,
|
|
||||||
paymentResult: mockResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(
|
|
||||||
reducer(
|
|
||||||
defaultState,
|
|
||||||
actions.__internalSetPaymentResult( mockResponse )
|
|
||||||
)
|
|
||||||
).toEqual( expectedState );
|
|
||||||
} );
|
|
||||||
|
|
||||||
it( 'should handle SET_COMPLETE', () => {
|
it( 'should handle SET_COMPLETE', () => {
|
||||||
const expectedState = {
|
const expectedState = {
|
||||||
...defaultState,
|
...defaultState,
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
*/
|
*/
|
||||||
import type { CheckoutResponse } from '@woocommerce/types';
|
import type { CheckoutResponse } from '@woocommerce/types';
|
||||||
import { store as noticesStore } from '@wordpress/notices';
|
import { store as noticesStore } from '@wordpress/notices';
|
||||||
|
import { dispatch as wpDispatch, select as wpSelect } from '@wordpress/data';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
|
import { STORE_KEY as PAYMENT_STORE_KEY } from '../payment/constants';
|
||||||
import { removeNoticesByStatus } from '../../utils/notices';
|
import { removeNoticesByStatus } from '../../utils/notices';
|
||||||
import {
|
import {
|
||||||
getPaymentResultFromCheckoutResponse,
|
getPaymentResultFromCheckoutResponse,
|
||||||
|
@ -40,7 +42,11 @@ export const __internalProcessCheckoutResponse = (
|
||||||
} ) => {
|
} ) => {
|
||||||
const paymentResult = getPaymentResultFromCheckoutResponse( response );
|
const paymentResult = getPaymentResultFromCheckoutResponse( response );
|
||||||
dispatch.__internalSetRedirectUrl( paymentResult?.redirectUrl || '' );
|
dispatch.__internalSetRedirectUrl( paymentResult?.redirectUrl || '' );
|
||||||
dispatch.__internalSetPaymentResult( paymentResult );
|
// The local `dispatch` here is bound to the actions of the data store. We need to use the global dispatch here
|
||||||
|
// to dispatch an action on a different store.
|
||||||
|
wpDispatch( PAYMENT_STORE_KEY ).__internalSetPaymentResult(
|
||||||
|
paymentResult
|
||||||
|
);
|
||||||
dispatch.__internalSetAfterProcessing();
|
dispatch.__internalSetAfterProcessing();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -90,15 +96,16 @@ export const __internalEmitAfterProcessingEvents: emitAfterProcessingEventsType
|
||||||
( { observers, notices } ) => {
|
( { observers, notices } ) => {
|
||||||
return ( { select, dispatch, registry } ) => {
|
return ( { select, dispatch, registry } ) => {
|
||||||
const { createErrorNotice } = registry.dispatch( noticesStore );
|
const { createErrorNotice } = registry.dispatch( noticesStore );
|
||||||
const state = select.getCheckoutState();
|
const checkoutState = select.getCheckoutState();
|
||||||
const data = {
|
const data = {
|
||||||
redirectUrl: state.redirectUrl,
|
redirectUrl: checkoutState.redirectUrl,
|
||||||
orderId: state.orderId,
|
orderId: checkoutState.orderId,
|
||||||
customerId: state.customerId,
|
customerId: checkoutState.customerId,
|
||||||
orderNotes: state.orderNotes,
|
orderNotes: checkoutState.orderNotes,
|
||||||
processingResponse: state.paymentResult,
|
processingResponse:
|
||||||
|
wpSelect( PAYMENT_STORE_KEY ).getPaymentResult(),
|
||||||
};
|
};
|
||||||
if ( state.hasError ) {
|
if ( checkoutState.hasError ) {
|
||||||
// allow payment methods or other things to customize the error
|
// allow payment methods or other things to customize the error
|
||||||
// with a fallback if nothing customizes it.
|
// with a fallback if nothing customizes it.
|
||||||
emitEventWithAbort(
|
emitEventWithAbort(
|
||||||
|
|
|
@ -9,6 +9,7 @@ import { DataRegistry } from '@wordpress/data';
|
||||||
*/
|
*/
|
||||||
import type { EventObserversType } from '../../base/context/event-emit/types';
|
import type { EventObserversType } from '../../base/context/event-emit/types';
|
||||||
import type { CheckoutState } from './default-state';
|
import type { CheckoutState } from './default-state';
|
||||||
|
import type { PaymentState } from '../payment/default-state';
|
||||||
import type { DispatchFromMap, SelectFromMap } from '../mapped-types';
|
import type { DispatchFromMap, SelectFromMap } from '../mapped-types';
|
||||||
import * as selectors from './selectors';
|
import * as selectors from './selectors';
|
||||||
import * as actions from './actions';
|
import * as actions from './actions';
|
||||||
|
@ -19,7 +20,7 @@ export type CheckoutAfterProcessingWithErrorEventData = {
|
||||||
orderId: CheckoutState[ 'orderId' ];
|
orderId: CheckoutState[ 'orderId' ];
|
||||||
customerId: CheckoutState[ 'customerId' ];
|
customerId: CheckoutState[ 'customerId' ];
|
||||||
orderNotes: CheckoutState[ 'orderNotes' ];
|
orderNotes: CheckoutState[ 'orderNotes' ];
|
||||||
processingResponse: CheckoutState[ 'paymentResult' ];
|
processingResponse: PaymentState[ 'paymentResult' ];
|
||||||
};
|
};
|
||||||
export type CheckoutAndPaymentNotices = {
|
export type CheckoutAndPaymentNotices = {
|
||||||
checkoutNotices: Notice[];
|
checkoutNotices: Notice[];
|
||||||
|
|
|
@ -15,4 +15,5 @@ export enum ACTION_TYPES {
|
||||||
REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD = 'REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD',
|
REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD = 'REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD',
|
||||||
INITIALIZE_PAYMENT_METHODS = 'INITIALIZE_PAYMENT_METHODS',
|
INITIALIZE_PAYMENT_METHODS = 'INITIALIZE_PAYMENT_METHODS',
|
||||||
SET_PAYMENT_METHOD_DATA = 'SET_PAYMENT_METHOD_DATA',
|
SET_PAYMENT_METHOD_DATA = 'SET_PAYMENT_METHOD_DATA',
|
||||||
|
SET_PAYMENT_RESULT = 'SET_PAYMENT_RESULT',
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {
|
||||||
PlainPaymentMethods,
|
PlainPaymentMethods,
|
||||||
PlainExpressPaymentMethods,
|
PlainExpressPaymentMethods,
|
||||||
} from '@woocommerce/type-defs/payments';
|
} from '@woocommerce/type-defs/payments';
|
||||||
|
import type { PaymentResult } from '@woocommerce/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
|
@ -112,6 +113,16 @@ export const __internalSetPaymentMethodData = (
|
||||||
paymentMethodData,
|
paymentMethodData,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store the result of the payment attempt from the /checkout StoreApi call
|
||||||
|
*
|
||||||
|
* @param data The result of the payment attempt through the StoreApi /checkout endpoints
|
||||||
|
*/
|
||||||
|
export const __internalSetPaymentResult = ( data: PaymentResult ) => ( {
|
||||||
|
type: ACTION_TYPES.SET_PAYMENT_RESULT,
|
||||||
|
data,
|
||||||
|
} );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the available payment methods.
|
* Set the available payment methods.
|
||||||
* An available payment method is one that has been validated and can make a payment.
|
* An available payment method is one that has been validated and can make a payment.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
import type { EmptyObjectType } from '@woocommerce/types';
|
import type { EmptyObjectType, PaymentResult } from '@woocommerce/types';
|
||||||
import { getSetting } from '@woocommerce/settings';
|
import { getSetting } from '@woocommerce/settings';
|
||||||
import {
|
import {
|
||||||
PlainPaymentMethods,
|
PlainPaymentMethods,
|
||||||
|
@ -14,7 +14,7 @@ import {
|
||||||
import { SavedPaymentMethod } from './types';
|
import { SavedPaymentMethod } from './types';
|
||||||
import { STATUS as PAYMENT_STATUS } from './constants';
|
import { STATUS as PAYMENT_STATUS } from './constants';
|
||||||
|
|
||||||
export interface PaymentMethodDataState {
|
export interface PaymentState {
|
||||||
status: string;
|
status: string;
|
||||||
activePaymentMethod: string;
|
activePaymentMethod: string;
|
||||||
activeSavedToken: string;
|
activeSavedToken: string;
|
||||||
|
@ -25,11 +25,12 @@ export interface PaymentMethodDataState {
|
||||||
| Record< string, SavedPaymentMethod[] >
|
| Record< string, SavedPaymentMethod[] >
|
||||||
| EmptyObjectType;
|
| EmptyObjectType;
|
||||||
paymentMethodData: Record< string, unknown >;
|
paymentMethodData: Record< string, unknown >;
|
||||||
|
paymentResult: PaymentResult | null;
|
||||||
paymentMethodsInitialized: boolean;
|
paymentMethodsInitialized: boolean;
|
||||||
expressPaymentMethodsInitialized: boolean;
|
expressPaymentMethodsInitialized: boolean;
|
||||||
shouldSavePaymentMethod: boolean;
|
shouldSavePaymentMethod: boolean;
|
||||||
}
|
}
|
||||||
export const defaultPaymentMethodDataState: PaymentMethodDataState = {
|
export const defaultPaymentState: PaymentState = {
|
||||||
status: PAYMENT_STATUS.PRISTINE,
|
status: PAYMENT_STATUS.PRISTINE,
|
||||||
activePaymentMethod: '',
|
activePaymentMethod: '',
|
||||||
activeSavedToken: '',
|
activeSavedToken: '',
|
||||||
|
@ -39,6 +40,7 @@ export const defaultPaymentMethodDataState: PaymentMethodDataState = {
|
||||||
Record< string, SavedPaymentMethod[] > | EmptyObjectType
|
Record< string, SavedPaymentMethod[] > | EmptyObjectType
|
||||||
>( 'customerPaymentMethods', {} ),
|
>( 'customerPaymentMethods', {} ),
|
||||||
paymentMethodData: {},
|
paymentMethodData: {},
|
||||||
|
paymentResult: null,
|
||||||
paymentMethodsInitialized: false,
|
paymentMethodsInitialized: false,
|
||||||
expressPaymentMethodsInitialized: false,
|
expressPaymentMethodsInitialized: false,
|
||||||
shouldSavePaymentMethod: false,
|
shouldSavePaymentMethod: false,
|
||||||
|
|
|
@ -2,20 +2,17 @@
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
import type { Reducer } from 'redux';
|
import type { Reducer } from 'redux';
|
||||||
import { objectHasProp } from '@woocommerce/types';
|
import { objectHasProp, PaymentResult } from '@woocommerce/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import {
|
import { defaultPaymentState, PaymentState } from './default-state';
|
||||||
defaultPaymentMethodDataState,
|
|
||||||
PaymentMethodDataState,
|
|
||||||
} from './default-state';
|
|
||||||
import { ACTION_TYPES } from './action-types';
|
import { ACTION_TYPES } from './action-types';
|
||||||
import { STATUS } from './constants';
|
import { STATUS } from './constants';
|
||||||
|
|
||||||
const reducer: Reducer< PaymentMethodDataState > = (
|
const reducer: Reducer< PaymentState > = (
|
||||||
state = defaultPaymentMethodDataState,
|
state = defaultPaymentState,
|
||||||
action
|
action
|
||||||
) => {
|
) => {
|
||||||
let newState = state;
|
let newState = state;
|
||||||
|
@ -76,6 +73,13 @@ const reducer: Reducer< PaymentMethodDataState > = (
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case ACTION_TYPES.SET_PAYMENT_RESULT:
|
||||||
|
newState = {
|
||||||
|
...state,
|
||||||
|
paymentResult: action.data as PaymentResult,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
|
||||||
case ACTION_TYPES.REMOVE_AVAILABLE_PAYMENT_METHOD:
|
case ACTION_TYPES.REMOVE_AVAILABLE_PAYMENT_METHOD:
|
||||||
const previousAvailablePaymentMethods = {
|
const previousAvailablePaymentMethods = {
|
||||||
...state.availablePaymentMethods,
|
...state.availablePaymentMethods,
|
||||||
|
|
|
@ -7,29 +7,29 @@ import deprecated from '@wordpress/deprecated';
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import { PaymentMethodDataState } from './default-state';
|
import { PaymentState } from './default-state';
|
||||||
import { filterActiveSavedPaymentMethods } from './utils/filter-active-saved-payment-methods';
|
import { filterActiveSavedPaymentMethods } from './utils/filter-active-saved-payment-methods';
|
||||||
import { STATUS as PAYMENT_STATUS } from './constants';
|
import { STATUS as PAYMENT_STATUS } from './constants';
|
||||||
|
|
||||||
export const isPaymentPristine = ( state: PaymentMethodDataState ) =>
|
export const isPaymentPristine = ( state: PaymentState ) =>
|
||||||
state.status === PAYMENT_STATUS.PRISTINE;
|
state.status === PAYMENT_STATUS.PRISTINE;
|
||||||
|
|
||||||
export const isPaymentStarted = ( state: PaymentMethodDataState ) =>
|
export const isPaymentStarted = ( state: PaymentState ) =>
|
||||||
state.status === PAYMENT_STATUS.STARTED;
|
state.status === PAYMENT_STATUS.STARTED;
|
||||||
|
|
||||||
export const isPaymentProcessing = ( state: PaymentMethodDataState ) =>
|
export const isPaymentProcessing = ( state: PaymentState ) =>
|
||||||
state.status === PAYMENT_STATUS.PROCESSING;
|
state.status === PAYMENT_STATUS.PROCESSING;
|
||||||
|
|
||||||
export const isPaymentSuccess = ( state: PaymentMethodDataState ) =>
|
export const isPaymentSuccess = ( state: PaymentState ) =>
|
||||||
state.status === PAYMENT_STATUS.SUCCESS;
|
state.status === PAYMENT_STATUS.SUCCESS;
|
||||||
|
|
||||||
export const hasPaymentError = ( state: PaymentMethodDataState ) =>
|
export const hasPaymentError = ( state: PaymentState ) =>
|
||||||
state.status === PAYMENT_STATUS.ERROR;
|
state.status === PAYMENT_STATUS.ERROR;
|
||||||
|
|
||||||
export const isPaymentFailed = ( state: PaymentMethodDataState ) =>
|
export const isPaymentFailed = ( state: PaymentState ) =>
|
||||||
state.status === PAYMENT_STATUS.FAILED;
|
state.status === PAYMENT_STATUS.FAILED;
|
||||||
|
|
||||||
export const isPaymentFinished = ( state: PaymentMethodDataState ) => {
|
export const isPaymentFinished = ( state: PaymentState ) => {
|
||||||
return (
|
return (
|
||||||
state.status === PAYMENT_STATUS.SUCCESS ||
|
state.status === PAYMENT_STATUS.SUCCESS ||
|
||||||
state.status === PAYMENT_STATUS.ERROR ||
|
state.status === PAYMENT_STATUS.ERROR ||
|
||||||
|
@ -37,40 +37,36 @@ export const isPaymentFinished = ( state: PaymentMethodDataState ) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isExpressPaymentMethodActive = (
|
export const isExpressPaymentMethodActive = ( state: PaymentState ) => {
|
||||||
state: PaymentMethodDataState
|
|
||||||
) => {
|
|
||||||
return Object.keys( state.availableExpressPaymentMethods ).includes(
|
return Object.keys( state.availableExpressPaymentMethods ).includes(
|
||||||
state.activePaymentMethod
|
state.activePaymentMethod
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getActiveSavedToken = ( state: PaymentMethodDataState ) => {
|
export const getActiveSavedToken = ( state: PaymentState ) => {
|
||||||
return typeof state.paymentMethodData === 'object' &&
|
return typeof state.paymentMethodData === 'object' &&
|
||||||
objectHasProp( state.paymentMethodData, 'token' )
|
objectHasProp( state.paymentMethodData, 'token' )
|
||||||
? state.paymentMethodData.token + ''
|
? state.paymentMethodData.token + ''
|
||||||
: '';
|
: '';
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getActivePaymentMethod = ( state: PaymentMethodDataState ) => {
|
export const getActivePaymentMethod = ( state: PaymentState ) => {
|
||||||
return state.activePaymentMethod;
|
return state.activePaymentMethod;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAvailablePaymentMethods = ( state: PaymentMethodDataState ) => {
|
export const getAvailablePaymentMethods = ( state: PaymentState ) => {
|
||||||
return state.availablePaymentMethods;
|
return state.availablePaymentMethods;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAvailableExpressPaymentMethods = (
|
export const getAvailableExpressPaymentMethods = ( state: PaymentState ) => {
|
||||||
state: PaymentMethodDataState
|
|
||||||
) => {
|
|
||||||
return state.availableExpressPaymentMethods;
|
return state.availableExpressPaymentMethods;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPaymentMethodData = ( state: PaymentMethodDataState ) => {
|
export const getPaymentMethodData = ( state: PaymentState ) => {
|
||||||
return state.paymentMethodData;
|
return state.paymentMethodData;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSavedPaymentMethods = ( state: PaymentMethodDataState ) => {
|
export const getSavedPaymentMethods = ( state: PaymentState ) => {
|
||||||
return state.savedPaymentMethods;
|
return state.savedPaymentMethods;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -78,9 +74,7 @@ export const getSavedPaymentMethods = ( state: PaymentMethodDataState ) => {
|
||||||
* Filters the list of saved payment methods and returns only the ones which
|
* Filters the list of saved payment methods and returns only the ones which
|
||||||
* are active and supported by the payment gateway
|
* are active and supported by the payment gateway
|
||||||
*/
|
*/
|
||||||
export const getActiveSavedPaymentMethods = (
|
export const getActiveSavedPaymentMethods = ( state: PaymentState ) => {
|
||||||
state: PaymentMethodDataState
|
|
||||||
) => {
|
|
||||||
const availablePaymentMethodKeys = Object.keys(
|
const availablePaymentMethodKeys = Object.keys(
|
||||||
state.availablePaymentMethods
|
state.availablePaymentMethods
|
||||||
);
|
);
|
||||||
|
@ -91,13 +85,11 @@ export const getActiveSavedPaymentMethods = (
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const paymentMethodsInitialized = ( state: PaymentMethodDataState ) => {
|
export const paymentMethodsInitialized = ( state: PaymentState ) => {
|
||||||
return state.paymentMethodsInitialized;
|
return state.paymentMethodsInitialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const expressPaymentMethodsInitialized = (
|
export const expressPaymentMethodsInitialized = ( state: PaymentState ) => {
|
||||||
state: PaymentMethodDataState
|
|
||||||
) => {
|
|
||||||
return state.expressPaymentMethodsInitialized;
|
return state.expressPaymentMethodsInitialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -105,7 +97,7 @@ export const expressPaymentMethodsInitialized = (
|
||||||
* @deprecated - use these selectors instead: isPaymentPristine, isPaymentStarted, isPaymentProcessing,
|
* @deprecated - use these selectors instead: isPaymentPristine, isPaymentStarted, isPaymentProcessing,
|
||||||
* isPaymentFinished, hasPaymentError, isPaymentSuccess, isPaymentFailed
|
* isPaymentFinished, hasPaymentError, isPaymentSuccess, isPaymentFailed
|
||||||
*/
|
*/
|
||||||
export const getCurrentStatus = ( state: PaymentMethodDataState ) => {
|
export const getCurrentStatus = ( state: PaymentState ) => {
|
||||||
deprecated( 'getCurrentStatus', {
|
deprecated( 'getCurrentStatus', {
|
||||||
since: '8.9.0',
|
since: '8.9.0',
|
||||||
alternative:
|
alternative:
|
||||||
|
@ -126,10 +118,14 @@ export const getCurrentStatus = ( state: PaymentMethodDataState ) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getShouldSavePaymentMethod = ( state: PaymentMethodDataState ) => {
|
export const getShouldSavePaymentMethod = ( state: PaymentState ) => {
|
||||||
return state.shouldSavePaymentMethod;
|
return state.shouldSavePaymentMethod;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getState = ( state: PaymentMethodDataState ) => {
|
export const getPaymentResult = ( state: PaymentState ) => {
|
||||||
|
return state.paymentResult;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getState = ( state: PaymentState ) => {
|
||||||
return state;
|
return state;
|
||||||
};
|
};
|
||||||
|
|
|
@ -180,4 +180,25 @@ describe( 'paymentMethodDataReducer', () => {
|
||||||
activeSavedToken: '',
|
activeSavedToken: '',
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
it( 'should handle SET_PAYMENT_RESULT', () => {
|
||||||
|
const mockResponse = {
|
||||||
|
message: 'success',
|
||||||
|
redirectUrl: 'https://example.com',
|
||||||
|
paymentStatus: 'not set',
|
||||||
|
paymentDetails: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectedState = {
|
||||||
|
...originalState,
|
||||||
|
paymentResult: mockResponse,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
reducer( originalState, {
|
||||||
|
type: ACTION_TYPES.SET_PAYMENT_RESULT,
|
||||||
|
data: mockResponse,
|
||||||
|
} )
|
||||||
|
).toEqual( expectedState );
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
|
@ -645,22 +645,6 @@
|
||||||
<file name="assets/js/data/cart/controls.js">
|
<file name="assets/js/data/cart/controls.js">
|
||||||
<error line="18" column="31" severity="error" message="Parameter 'preserveCartData' implicitly has an 'any' type." source="TS7006" />
|
<error line="18" column="31" severity="error" message="Parameter 'preserveCartData' implicitly has an 'any' type." source="TS7006" />
|
||||||
</file>
|
</file>
|
||||||
<file name="assets/js/data/checkout/thunks.ts">
|
|
||||||
<error line="5" column="10" severity="error" message="Module '"@wordpress/notices"' has no exported member 'store'." source="TS2305" />
|
|
||||||
<error line="57" column="11" severity="error" message="Property 'createErrorNotice' does not exist on type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions")'." source="TS2339" />
|
|
||||||
<error line="92" column="12" severity="error" message="Property 'createErrorNotice' does not exist on type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions")'." source="TS2339" />
|
|
||||||
</file>
|
|
||||||
<file name="assets/js/data/checkout/reducers.ts">
|
|
||||||
<error line="164" column="25" severity="error" message="Property 'SET_PRISTINE' does not exist on type '{ readonly SET_IDLE: "SET_IDLE"; readonly SET_REDIRECT_URL: "SET_REDIRECT_URL"; readonly SET_COMPLETE: "SET_CHECKOUT_COMPLETE"; readonly SET_BEFORE_PROCESSING: "SET_BEFORE_PROCESSING"; ... 11 more ...; readonly SET_IS_CART: "SET_IS_CART"; }'." source="TS2339" />
|
|
||||||
</file>
|
|
||||||
<file name="assets/js/data/checkout/index.ts">
|
|
||||||
<error line="21" column="44" severity="error" message="Argument of type '{ reducer: (state: CheckoutState | undefined, action: actions.CheckoutAction) => CheckoutState; selectors: typeof selectors; actions: typeof actions; }' is not assignable to parameter of type 'StoreConfig<CheckoutState>'.
|
|
||||||
Types of property 'actions' are incompatible.
|
|
||||||
Type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/assets/js/data/checkout/actions")' is not assignable to type '{ [k: string]: (...args: readonly any[]) => AnyAction | Generator<any, any, unknown>; }'.
|
|
||||||
Property '__internalProcessCheckoutResponse' is incompatible with index signature.
|
|
||||||
Type '(response: CheckoutResponse) => ({ dispatch, }: { dispatch: DispatchFromMap<typeof actions>; }) => void' is not assignable to type '(...args: readonly any[]) => AnyAction | Generator<any, any, unknown>'.
|
|
||||||
Type '({ dispatch, }: { dispatch: DispatchFromMap<typeof actions>; }) => void' is not assignable to type 'AnyAction | Generator<any, any, unknown>'." source="TS2345" />
|
|
||||||
</file>
|
|
||||||
<file name="assets/js/previews/cart.ts">
|
<file name="assets/js/previews/cart.ts">
|
||||||
<error line="75" column="4" severity="error" message="Property 'price_range' is missing in type '{ currency_code: "USD"; currency_symbol: string; currency_minor_unit: number; currency_decimal_separator: string; currency_thousand_separator: string; currency_prefix: string; currency_suffix: string; price: string; regular_price: string; sale_price: string; raw_prices: { ...; }; }' but required in type 'CartItemPrices'." source="TS2741" />
|
<error line="75" column="4" severity="error" message="Property 'price_range' is missing in type '{ currency_code: "USD"; currency_symbol: string; currency_minor_unit: number; currency_decimal_separator: string; currency_thousand_separator: string; currency_prefix: string; currency_suffix: string; price: string; regular_price: string; sale_price: string; raw_prices: { ...; }; }' but required in type 'CartItemPrices'." source="TS2741" />
|
||||||
<error line="141" column="4" severity="error" message="Property 'price_range' is missing in type '{ currency_code: "USD"; currency_symbol: string; currency_minor_unit: number; currency_decimal_separator: string; currency_thousand_separator: string; currency_prefix: string; currency_suffix: string; price: string; regular_price: string; sale_price: string; raw_prices: { ...; }; }' but required in type 'CartItemPrices'." source="TS2741" />
|
<error line="141" column="4" severity="error" message="Property 'price_range' is missing in type '{ currency_code: "USD"; currency_symbol: string; currency_minor_unit: number; currency_decimal_separator: string; currency_thousand_separator: string; currency_prefix: string; currency_suffix: string; price: string; regular_price: string; sale_price: string; raw_prices: { ...; }; }' but required in type 'CartItemPrices'." source="TS2741" />
|
||||||
|
@ -696,15 +680,31 @@
|
||||||
<error line="120" column="41" severity="error" message="Property 'validationErrors' does not exist on type 'never'." source="TS2339" />
|
<error line="120" column="41" severity="error" message="Property 'validationErrors' does not exist on type 'never'." source="TS2339" />
|
||||||
</file>
|
</file>
|
||||||
<file name="assets/js/data/payment/actions.ts">
|
<file name="assets/js/data/payment/actions.ts">
|
||||||
<error line="51" column="19" severity="error" message="Binding element 'select' implicitly has an 'any' type." source="TS7031" />
|
<error line="52" column="19" severity="error" message="Binding element 'select' implicitly has an 'any' type." source="TS7031" />
|
||||||
<error line="51" column="27" severity="error" message="Binding element 'dispatch' implicitly has an 'any' type." source="TS7031" />
|
<error line="52" column="27" severity="error" message="Binding element 'dispatch' implicitly has an 'any' type." source="TS7031" />
|
||||||
<error line="122" column="19" severity="error" message="Binding element 'dispatch' implicitly has an 'any' type." source="TS7031" />
|
<error line="133" column="19" severity="error" message="Binding element 'dispatch' implicitly has an 'any' type." source="TS7031" />
|
||||||
<error line="122" column="29" severity="error" message="Binding element 'select' implicitly has an 'any' type." source="TS7031" />
|
<error line="133" column="29" severity="error" message="Binding element 'select' implicitly has an 'any' type." source="TS7031" />
|
||||||
<error line="170" column="19" severity="error" message="Binding element 'select' implicitly has an 'any' type." source="TS7031" />
|
<error line="181" column="19" severity="error" message="Binding element 'select' implicitly has an 'any' type." source="TS7031" />
|
||||||
<error line="170" column="27" severity="error" message="Binding element 'dispatch' implicitly has an 'any' type." source="TS7031" />
|
<error line="181" column="27" severity="error" message="Binding element 'dispatch' implicitly has an 'any' type." source="TS7031" />
|
||||||
|
</file>
|
||||||
|
<file name="assets/js/data/checkout/thunks.ts">
|
||||||
|
<error line="5" column="10" severity="error" message="Module '"@wordpress/notices"' has no exported member 'store'." source="TS2305" />
|
||||||
|
<error line="63" column="11" severity="error" message="Property 'createErrorNotice' does not exist on type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions")'." source="TS2339" />
|
||||||
|
<error line="98" column="12" severity="error" message="Property 'createErrorNotice' does not exist on type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions")'." source="TS2339" />
|
||||||
|
</file>
|
||||||
|
<file name="assets/js/data/checkout/reducers.ts">
|
||||||
|
<error line="152" column="25" severity="error" message="Property 'SET_PRISTINE' does not exist on type '{ readonly SET_IDLE: "SET_IDLE"; readonly SET_REDIRECT_URL: "SET_REDIRECT_URL"; readonly SET_COMPLETE: "SET_CHECKOUT_COMPLETE"; readonly SET_BEFORE_PROCESSING: "SET_BEFORE_PROCESSING"; ... 10 more ...; readonly SET_IS_CART: "SET_IS_CART"; }'." source="TS2339" />
|
||||||
|
</file>
|
||||||
|
<file name="assets/js/data/checkout/index.ts">
|
||||||
|
<error line="21" column="44" severity="error" message="Argument of type '{ reducer: (state: CheckoutState | undefined, action: actions.CheckoutAction) => CheckoutState; selectors: typeof selectors; actions: typeof actions; }' is not assignable to parameter of type 'StoreConfig<CheckoutState>'.
|
||||||
|
Types of property 'actions' are incompatible.
|
||||||
|
Type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/assets/js/data/checkout/actions")' is not assignable to type '{ [k: string]: (...args: readonly any[]) => AnyAction | Generator<any, any, unknown>; }'.
|
||||||
|
Property '__internalProcessCheckoutResponse' is incompatible with index signature.
|
||||||
|
Type '(response: CheckoutResponse) => ({ dispatch, }: { dispatch: DispatchFromMap<typeof actions>; }) => void' is not assignable to type '(...args: readonly any[]) => AnyAction | Generator<any, any, unknown>'.
|
||||||
|
Type '({ dispatch, }: { dispatch: DispatchFromMap<typeof actions>; }) => void' is not assignable to type 'AnyAction | Generator<any, any, unknown>'." source="TS2345" />
|
||||||
</file>
|
</file>
|
||||||
<file name="assets/js/data/payment/index.ts">
|
<file name="assets/js/data/payment/index.ts">
|
||||||
<error line="25" column="44" severity="error" message="Argument of type '{ reducer: Reducer<PaymentMethodDataState, AnyAction>; selectors: typeof selectors; actions: typeof actions; controls: any; }' is not assignable to parameter of type 'StoreConfig<PaymentMethodDataState>'.
|
<error line="25" column="44" severity="error" message="Argument of type '{ reducer: Reducer<PaymentState, AnyAction>; selectors: typeof selectors; actions: typeof actions; controls: any; }' is not assignable to parameter of type 'StoreConfig<PaymentState>'.
|
||||||
Types of property 'actions' are incompatible.
|
Types of property 'actions' are incompatible.
|
||||||
Type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/assets/js/data/payment/actions")' is not assignable to type '{ [k: string]: (...args: readonly any[]) => AnyAction | Generator<any, any, unknown>; }'.
|
Type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/assets/js/data/payment/actions")' is not assignable to type '{ [k: string]: (...args: readonly any[]) => AnyAction | Generator<any, any, unknown>; }'.
|
||||||
Property '__internalUpdateAvailablePaymentMethods' is incompatible with index signature.
|
Property '__internalUpdateAvailablePaymentMethods' is incompatible with index signature.
|
||||||
|
|
|
@ -35,7 +35,6 @@ The following data is available:
|
||||||
- `orderId`: The order id for the order attached to the current checkout.
|
- `orderId`: The order id for the order attached to the current checkout.
|
||||||
- `customerId`: The ID of the customer if the customer has an account, or `0` for guests.
|
- `customerId`: The ID of the customer if the customer has an account, or `0` for guests.
|
||||||
- `calculatingCount`: If any of the totals, taxes, shipping, etc need to be calculated, the count will be increased here.
|
- `calculatingCount`: If any of the totals, taxes, shipping, etc need to be calculated, the count will be increased here.
|
||||||
- `paymentResult`: The result of processing the payment.
|
|
||||||
- `useShippingAsBilling`: Should the billing form be hidden and inherit the shipping address?
|
- `useShippingAsBilling`: Should the billing form be hidden and inherit the shipping address?
|
||||||
- `shouldCreateAccount`: Should a user account be created with this order?
|
- `shouldCreateAccount`: Should a user account be created with this order?
|
||||||
- `extensionData`: This is used by plugins that extend Cart & Checkout to pass custom data to the Store API on checkout processing
|
- `extensionData`: This is used by plugins that extend Cart & Checkout to pass custom data to the Store API on checkout processing
|
||||||
|
@ -65,7 +64,6 @@ The following actions can be dispatched from the Checkout data store:
|
||||||
- `__internalSetIdle()`: Set `state.status` to `idle`
|
- `__internalSetIdle()`: Set `state.status` to `idle`
|
||||||
- `__internalSetComplete()`: Set `state.status` to `complete`
|
- `__internalSetComplete()`: Set `state.status` to `complete`
|
||||||
- `__internalSetProcessing()`: Set `state.status` to `processing`
|
- `__internalSetProcessing()`: Set `state.status` to `processing`
|
||||||
- `__internalSetPaymentResult( response: PaymentResult )`: Set `state.paymentResult` to `response`
|
|
||||||
- `__internalSetBeforeProcessing()`: Set `state.status` to `before_processing`
|
- `__internalSetBeforeProcessing()`: Set `state.status` to `before_processing`
|
||||||
- `__internalSetAfterProcessing()`: Set `state.status` to `after_processing`
|
- `__internalSetAfterProcessing()`: Set `state.status` to `after_processing`
|
||||||
- `__internalSrocessCheckoutResponse( response: CheckoutResponse )`: This is a thunk that will extract the paymentResult from the CheckoutResponse, and dispatch 3 actions: `__internalSetRedirectUrl`, `__internalSetPaymentResult` and `__internalSetAfterProcessing`.
|
- `__internalSrocessCheckoutResponse( response: CheckoutResponse )`: This is a thunk that will extract the paymentResult from the CheckoutResponse, and dispatch 3 actions: `__internalSetRedirectUrl`, `__internalSetPaymentResult` and `__internalSetAfterProcessing`.
|
||||||
|
|
|
@ -405,6 +405,31 @@ const expressPaymentMethodsInitialized =
|
||||||
store.expressPaymentMethodsInitialized();
|
store.expressPaymentMethodsInitialized();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### getPaymentResult
|
||||||
|
|
||||||
|
Returns the result of the last payment attempt
|
||||||
|
|
||||||
|
#### _Returns_
|
||||||
|
|
||||||
|
`object` - An object with the following properties:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
{
|
||||||
|
message: string;
|
||||||
|
paymentStatus: 'success' | 'failure' | 'pending' | 'error' | 'not set';
|
||||||
|
paymentDetails: Record< string, string > | Record< string, never >;
|
||||||
|
redirectUrl: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = select( 'wc/store/payment' );
|
||||||
|
const expressPaymentMethodsInitialized =
|
||||||
|
store.expressPaymentMethodsInitialized();
|
||||||
|
```
|
||||||
|
|
||||||
<!-- FEEDBACK -->
|
<!-- FEEDBACK -->
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
Loading…
Reference in New Issue