2022-07-08 05:53:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { objectHasProp } from '@woocommerce/types';
|
2022-11-15 12:27:39 +00:00
|
|
|
import deprecated from '@wordpress/deprecated';
|
2023-01-16 22:06:48 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import type { GlobalPaymentMethod } from '@woocommerce/types';
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-11-18 12:13:00 +00:00
|
|
|
import { PaymentState } from './default-state';
|
2022-11-16 09:38:48 +00:00
|
|
|
import { filterActiveSavedPaymentMethods } from './utils/filter-active-saved-payment-methods';
|
2022-11-15 12:27:39 +00:00
|
|
|
import { STATUS as PAYMENT_STATUS } from './constants';
|
|
|
|
|
2023-01-16 22:06:48 +00:00
|
|
|
const globalPaymentMethods: Record< string, string > = {};
|
2023-02-14 12:08:19 +00:00
|
|
|
|
2023-01-16 22:06:48 +00:00
|
|
|
if ( getSetting( 'globalPaymentMethods' ) ) {
|
|
|
|
getSetting< GlobalPaymentMethod[] >( 'globalPaymentMethods' ).forEach(
|
|
|
|
( method ) => {
|
|
|
|
globalPaymentMethods[ method.id ] = method.title;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:19 +00:00
|
|
|
export const isPaymentPristine = ( state: PaymentState ) => {
|
|
|
|
deprecated( 'isPaymentPristine', {
|
|
|
|
since: '9.6.0',
|
|
|
|
alternative: 'isPaymentIdle',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return state.status === PAYMENT_STATUS.IDLE;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isPaymentIdle = ( state: PaymentState ) =>
|
|
|
|
state.status === PAYMENT_STATUS.IDLE;
|
2022-11-15 12:27:39 +00:00
|
|
|
|
2023-02-14 12:08:19 +00:00
|
|
|
export const isPaymentStarted = ( state: PaymentState ) => {
|
|
|
|
deprecated( 'isPaymentStarted', {
|
|
|
|
since: '9.6.0',
|
|
|
|
alternative: 'isExpressPaymentStarted',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
return state.status === PAYMENT_STATUS.EXPRESS_STARTED;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isExpressPaymentStarted = ( state: PaymentState ) => {
|
|
|
|
return state.status === PAYMENT_STATUS.EXPRESS_STARTED;
|
|
|
|
};
|
2022-11-15 12:27:39 +00:00
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const isPaymentProcessing = ( state: PaymentState ) =>
|
2022-11-15 12:27:39 +00:00
|
|
|
state.status === PAYMENT_STATUS.PROCESSING;
|
|
|
|
|
2023-02-14 12:08:19 +00:00
|
|
|
export const isPaymentReady = ( state: PaymentState ) =>
|
|
|
|
state.status === PAYMENT_STATUS.READY;
|
|
|
|
|
|
|
|
export const isPaymentSuccess = ( state: PaymentState ) => {
|
|
|
|
deprecated( 'isPaymentSuccess', {
|
|
|
|
since: '9.6.0',
|
|
|
|
alternative: 'isPaymentReady',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return state.status === PAYMENT_STATUS.READY;
|
|
|
|
};
|
2022-11-15 12:27:39 +00:00
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const hasPaymentError = ( state: PaymentState ) =>
|
2022-11-15 12:27:39 +00:00
|
|
|
state.status === PAYMENT_STATUS.ERROR;
|
|
|
|
|
2023-02-14 12:08:19 +00:00
|
|
|
export const isPaymentFailed = ( state: PaymentState ) => {
|
|
|
|
deprecated( 'isPaymentFailed', {
|
|
|
|
since: '9.6.0',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
2022-11-15 12:27:39 +00:00
|
|
|
|
2023-02-14 12:08:19 +00:00
|
|
|
return state.status === PAYMENT_STATUS.ERROR;
|
2022-11-15 12:27:39 +00:00
|
|
|
};
|
2022-07-08 05:53:24 +00:00
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const isExpressPaymentMethodActive = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return Object.keys( state.availableExpressPaymentMethods ).includes(
|
|
|
|
state.activePaymentMethod
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getActiveSavedToken = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return typeof state.paymentMethodData === 'object' &&
|
|
|
|
objectHasProp( state.paymentMethodData, 'token' )
|
|
|
|
? state.paymentMethodData.token + ''
|
|
|
|
: '';
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getActivePaymentMethod = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.activePaymentMethod;
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getAvailablePaymentMethods = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.availablePaymentMethods;
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getAvailableExpressPaymentMethods = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.availableExpressPaymentMethods;
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getPaymentMethodData = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.paymentMethodData;
|
|
|
|
};
|
|
|
|
|
2022-12-28 16:30:46 +00:00
|
|
|
export const getIncompatiblePaymentMethods = ( state: PaymentState ) => {
|
2023-02-24 10:57:24 +00:00
|
|
|
const {
|
|
|
|
availablePaymentMethods,
|
|
|
|
availableExpressPaymentMethods,
|
|
|
|
paymentMethodsInitialized,
|
|
|
|
expressPaymentMethodsInitialized,
|
|
|
|
} = state;
|
|
|
|
|
|
|
|
if ( ! paymentMethodsInitialized || ! expressPaymentMethodsInitialized ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-16 22:06:48 +00:00
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries( globalPaymentMethods ).filter( ( [ k ] ) => {
|
|
|
|
return ! (
|
|
|
|
k in
|
|
|
|
{
|
2023-02-24 10:57:24 +00:00
|
|
|
...availablePaymentMethods,
|
|
|
|
...availableExpressPaymentMethods,
|
2023-01-16 22:06:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
);
|
2022-12-28 16:30:46 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getSavedPaymentMethods = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.savedPaymentMethods;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the list of saved payment methods and returns only the ones which
|
|
|
|
* are active and supported by the payment gateway
|
|
|
|
*/
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getActiveSavedPaymentMethods = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
const availablePaymentMethodKeys = Object.keys(
|
|
|
|
state.availablePaymentMethods
|
|
|
|
);
|
|
|
|
|
|
|
|
return filterActiveSavedPaymentMethods(
|
|
|
|
availablePaymentMethodKeys,
|
|
|
|
state.savedPaymentMethods
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const paymentMethodsInitialized = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.paymentMethodsInitialized;
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const expressPaymentMethodsInitialized = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.expressPaymentMethodsInitialized;
|
|
|
|
};
|
|
|
|
|
2022-11-15 12:27:39 +00:00
|
|
|
/**
|
2023-02-14 12:08:19 +00:00
|
|
|
* @deprecated - Use these selectors instead: isPaymentIdle, isPaymentProcessing,
|
|
|
|
* hasPaymentError
|
2022-11-15 12:27:39 +00:00
|
|
|
*/
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getCurrentStatus = ( state: PaymentState ) => {
|
2022-11-15 12:27:39 +00:00
|
|
|
deprecated( 'getCurrentStatus', {
|
|
|
|
since: '8.9.0',
|
2023-02-14 12:08:19 +00:00
|
|
|
alternative: 'isPaymentIdle, isPaymentProcessing, hasPaymentError',
|
2022-11-15 12:27:39 +00:00
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/7666',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return {
|
2023-02-14 12:08:19 +00:00
|
|
|
get isPristine() {
|
|
|
|
deprecated( 'isPristine', {
|
|
|
|
since: '9.6.0',
|
|
|
|
alternative: 'isIdle',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
} );
|
|
|
|
return isPaymentIdle( state );
|
|
|
|
}, // isPristine is the same as isIdle.
|
|
|
|
isIdle: isPaymentIdle( state ),
|
|
|
|
isStarted: isExpressPaymentStarted( state ),
|
2022-11-15 12:27:39 +00:00
|
|
|
isProcessing: isPaymentProcessing( state ),
|
2023-02-14 12:08:19 +00:00
|
|
|
get isFinished() {
|
|
|
|
deprecated( 'isFinished', {
|
|
|
|
since: '9.6.0',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
return hasPaymentError( state ) || isPaymentReady( state );
|
|
|
|
},
|
2022-11-15 12:27:39 +00:00
|
|
|
hasError: hasPaymentError( state ),
|
2023-02-14 12:08:19 +00:00
|
|
|
get hasFailed() {
|
|
|
|
deprecated( 'hasFailed', {
|
|
|
|
since: '9.6.0',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
return hasPaymentError( state );
|
|
|
|
},
|
|
|
|
get isSuccessful() {
|
|
|
|
deprecated( 'isSuccessful', {
|
|
|
|
since: '9.6.0',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
return isPaymentReady( state );
|
|
|
|
},
|
2022-11-15 12:27:39 +00:00
|
|
|
isDoingExpressPayment: isExpressPaymentMethodActive( state ),
|
|
|
|
};
|
2022-07-08 05:53:24 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getShouldSavePaymentMethod = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state.shouldSavePaymentMethod;
|
|
|
|
};
|
|
|
|
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getPaymentResult = ( state: PaymentState ) => {
|
|
|
|
return state.paymentResult;
|
|
|
|
};
|
|
|
|
|
2022-12-07 09:12:55 +00:00
|
|
|
// We should avoid using this selector and instead use the focused selectors
|
|
|
|
// We're keeping it because it's used in our unit test: assets/js/blocks/cart-checkout-shared/payment-methods/test/payment-methods.js
|
|
|
|
// to mock the selectors.
|
2022-11-18 12:13:00 +00:00
|
|
|
export const getState = ( state: PaymentState ) => {
|
2022-07-08 05:53:24 +00:00
|
|
|
return state;
|
|
|
|
};
|