2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-10-06 12:46:46 +00:00
|
|
|
import { CHECKOUT_STORE_KEY, PAYMENT_STORE_KEY } from '@woocommerce/block-data';
|
2022-06-10 16:33:15 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2021-01-19 15:55:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-06-21 14:09:22 +00:00
|
|
|
import { useCheckoutEventsContext } from '../providers';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { usePaymentMethods } from './payment-methods/use-payment-methods';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
2020-04-30 09:52:36 +00:00
|
|
|
* Returns the submitButtonText, onSubmit interface from the checkout context,
|
|
|
|
* and an indication of submission status.
|
2020-03-11 10:50:12 +00:00
|
|
|
*/
|
|
|
|
export const useCheckoutSubmit = () => {
|
2020-04-30 09:52:36 +00:00
|
|
|
const {
|
|
|
|
isCalculating,
|
|
|
|
isBeforeProcessing,
|
|
|
|
isProcessing,
|
|
|
|
isAfterProcessing,
|
|
|
|
isComplete,
|
|
|
|
hasError,
|
2022-06-10 16:33:15 +00:00
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const store = select( CHECKOUT_STORE_KEY );
|
|
|
|
return {
|
|
|
|
isCalculating: store.isCalculating(),
|
|
|
|
isBeforeProcessing: store.isBeforeProcessing(),
|
|
|
|
isProcessing: store.isProcessing(),
|
|
|
|
isAfterProcessing: store.isAfterProcessing(),
|
|
|
|
isComplete: store.isComplete(),
|
|
|
|
hasError: store.hasError(),
|
|
|
|
};
|
|
|
|
} );
|
2024-10-29 11:45:40 +00:00
|
|
|
const {
|
|
|
|
activePaymentMethod,
|
|
|
|
isExpressPaymentMethodActive,
|
|
|
|
isPaymentMethodsInitialized,
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const store = select( PAYMENT_STORE_KEY );
|
2022-07-08 05:53:24 +00:00
|
|
|
|
2024-10-29 11:45:40 +00:00
|
|
|
return {
|
|
|
|
activePaymentMethod: store.getActivePaymentMethod(),
|
|
|
|
isExpressPaymentMethodActive: store.isExpressPaymentMethodActive(),
|
|
|
|
isPaymentMethodsInitialized: store.paymentMethodsInitialized(),
|
|
|
|
};
|
|
|
|
} );
|
2022-06-10 16:33:15 +00:00
|
|
|
|
2022-06-21 14:09:22 +00:00
|
|
|
const { onSubmit } = useCheckoutEventsContext();
|
2022-06-10 16:33:15 +00:00
|
|
|
|
2021-01-19 15:55:44 +00:00
|
|
|
const { paymentMethods = {} } = usePaymentMethods();
|
2020-04-30 09:52:36 +00:00
|
|
|
const paymentMethod = paymentMethods[ activePaymentMethod ] || {};
|
2021-06-16 12:44:40 +00:00
|
|
|
const waitingForProcessing =
|
|
|
|
isProcessing || isAfterProcessing || isBeforeProcessing;
|
|
|
|
const waitingForRedirect = isComplete && ! hasError;
|
2022-12-15 10:28:23 +00:00
|
|
|
const paymentMethodButtonLabel = paymentMethod.placeOrderButtonLabel;
|
2020-04-30 09:52:36 +00:00
|
|
|
|
|
|
|
return {
|
2022-12-15 10:28:23 +00:00
|
|
|
paymentMethodButtonLabel,
|
2020-04-30 09:52:36 +00:00
|
|
|
onSubmit,
|
|
|
|
isCalculating,
|
2024-10-29 11:45:40 +00:00
|
|
|
isDisabled:
|
|
|
|
isProcessing ||
|
|
|
|
isExpressPaymentMethodActive ||
|
|
|
|
! isPaymentMethodsInitialized,
|
2021-06-16 12:44:40 +00:00
|
|
|
waitingForProcessing,
|
|
|
|
waitingForRedirect,
|
2020-04-30 09:52:36 +00:00
|
|
|
};
|
2020-03-11 10:50:12 +00:00
|
|
|
};
|