2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-02-04 15:30:28 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
2021-06-16 12:44:40 +00:00
|
|
|
import { useCallback, useEffect, useRef } from '@wordpress/element';
|
2021-04-08 12:31:12 +00:00
|
|
|
import PaymentMethodLabel from '@woocommerce/base-components/cart-checkout/payment-method-label';
|
|
|
|
import PaymentMethodIcons from '@woocommerce/base-components/cart-checkout/payment-method-icons';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-06-16 12:44:40 +00:00
|
|
|
import deprecated from '@wordpress/deprecated';
|
2021-11-22 09:41:54 +00:00
|
|
|
import LoadingMask from '@woocommerce/base-components/loading-mask';
|
2021-12-07 13:02:57 +00:00
|
|
|
import type { PaymentMethodInterface } from '@woocommerce/types';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2022-11-24 14:20:44 +00:00
|
|
|
import {
|
|
|
|
CHECKOUT_STORE_KEY,
|
|
|
|
PAYMENT_STORE_KEY,
|
|
|
|
CART_STORE_KEY,
|
|
|
|
} from '@woocommerce/block-data';
|
2022-11-10 10:05:41 +00:00
|
|
|
import { ValidationInputError } from '@woocommerce/blocks-checkout';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreCart } from '../cart/use-store-cart';
|
|
|
|
import { useStoreCartCoupons } from '../cart/use-store-cart-coupons';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { noticeContexts, responseTypes } from '../../event-emit';
|
2022-06-21 14:09:22 +00:00
|
|
|
import { useCheckoutEventsContext } from '../../providers/cart-checkout/checkout-events';
|
2022-10-06 12:46:46 +00:00
|
|
|
import { usePaymentEventsContext } from '../../providers/cart-checkout/payment-events';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useShippingDataContext } from '../../providers/cart-checkout/shipping';
|
2021-05-25 11:49:13 +00:00
|
|
|
import { prepareTotalItems } from './utils';
|
2022-03-04 17:43:45 +00:00
|
|
|
import { useShippingData } from '../shipping/use-shipping-data';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-25 11:49:13 +00:00
|
|
|
* Returns am interface to use as payment method props.
|
2020-03-11 10:50:12 +00:00
|
|
|
*/
|
2021-12-07 13:02:57 +00:00
|
|
|
export const usePaymentMethodInterface = (): PaymentMethodInterface => {
|
2020-03-11 10:50:12 +00:00
|
|
|
const {
|
2021-04-26 15:27:22 +00:00
|
|
|
onCheckoutBeforeProcessing,
|
|
|
|
onCheckoutValidationBeforeProcessing,
|
2020-04-14 16:52:23 +00:00
|
|
|
onCheckoutAfterProcessingWithSuccess,
|
|
|
|
onCheckoutAfterProcessingWithError,
|
2020-03-11 10:50:12 +00:00
|
|
|
onSubmit,
|
2023-02-17 11:03:13 +00:00
|
|
|
onCheckoutSuccess,
|
|
|
|
onCheckoutFail,
|
|
|
|
onCheckoutValidation,
|
2022-06-21 14:09:22 +00:00
|
|
|
} = useCheckoutEventsContext();
|
2022-07-08 05:53:24 +00:00
|
|
|
|
2022-06-23 09:15:25 +00:00
|
|
|
const { isCalculating, isComplete, isIdle, isProcessing, customerId } =
|
|
|
|
useSelect( ( select ) => {
|
|
|
|
const store = select( CHECKOUT_STORE_KEY );
|
|
|
|
return {
|
|
|
|
isComplete: store.isComplete(),
|
|
|
|
isIdle: store.isIdle(),
|
|
|
|
isProcessing: store.isProcessing(),
|
|
|
|
customerId: store.getCustomerId(),
|
|
|
|
isCalculating: store.isCalculating(),
|
|
|
|
};
|
|
|
|
} );
|
2022-11-15 12:27:39 +00:00
|
|
|
const { paymentStatus, activePaymentMethod, shouldSavePayment } = useSelect(
|
2022-07-08 05:53:24 +00:00
|
|
|
( select ) => {
|
2022-10-06 12:46:46 +00:00
|
|
|
const store = select( PAYMENT_STORE_KEY );
|
2022-06-10 16:33:15 +00:00
|
|
|
|
2022-07-08 05:53:24 +00:00
|
|
|
return {
|
2022-11-15 12:27:39 +00:00
|
|
|
// The paymentStatus is exposed to third parties via the payment method interface so the API must not be changed
|
|
|
|
paymentStatus: {
|
2023-02-14 12:08:19 +00:00
|
|
|
get isPristine() {
|
|
|
|
deprecated( 'isPristine', {
|
|
|
|
since: '9.6.0',
|
|
|
|
alternative: 'isIdle',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
return store.isPaymentIdle();
|
|
|
|
}, // isPristine is the same as isIdle
|
|
|
|
isIdle: store.isPaymentIdle(),
|
|
|
|
isStarted: store.isExpressPaymentStarted(),
|
2022-11-15 12:27:39 +00:00
|
|
|
isProcessing: store.isPaymentProcessing(),
|
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 (
|
|
|
|
store.hasPaymentError() || store.isPaymentReady()
|
|
|
|
);
|
|
|
|
},
|
2022-11-15 12:27:39 +00:00
|
|
|
hasError: store.hasPaymentError(),
|
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 store.hasPaymentError();
|
|
|
|
},
|
|
|
|
get isSuccessful() {
|
|
|
|
deprecated( 'isSuccessful', {
|
|
|
|
since: '9.6.0',
|
|
|
|
plugin: 'WooCommerce Blocks',
|
|
|
|
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8110',
|
|
|
|
} );
|
|
|
|
return store.isPaymentReady();
|
|
|
|
},
|
|
|
|
isReady: store.isPaymentReady(),
|
2022-11-15 12:27:39 +00:00
|
|
|
isDoingExpressPayment: store.isExpressPaymentMethodActive(),
|
|
|
|
},
|
2022-07-08 05:53:24 +00:00
|
|
|
activePaymentMethod: store.getActivePaymentMethod(),
|
|
|
|
shouldSavePayment: store.getShouldSavePaymentMethod(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-10-06 12:46:46 +00:00
|
|
|
const { __internalSetExpressPaymentError } =
|
|
|
|
useDispatch( PAYMENT_STORE_KEY );
|
2022-07-08 05:53:24 +00:00
|
|
|
|
2023-02-17 11:03:13 +00:00
|
|
|
const { onPaymentProcessing, onPaymentSetup } = usePaymentEventsContext();
|
2020-03-11 10:50:12 +00:00
|
|
|
const {
|
|
|
|
shippingErrorStatus,
|
|
|
|
shippingErrorTypes,
|
|
|
|
onShippingRateSuccess,
|
|
|
|
onShippingRateFail,
|
|
|
|
onShippingRateSelectSuccess,
|
|
|
|
onShippingRateSelectFail,
|
2020-03-26 13:31:09 +00:00
|
|
|
} = useShippingDataContext();
|
2022-03-04 17:43:45 +00:00
|
|
|
const {
|
|
|
|
shippingRates,
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates,
|
2022-03-04 17:43:45 +00:00
|
|
|
selectedRates,
|
|
|
|
isSelectingRate,
|
|
|
|
selectShippingRate,
|
|
|
|
needsShipping,
|
|
|
|
} = useShippingData();
|
2022-11-24 14:20:44 +00:00
|
|
|
|
|
|
|
const { billingAddress, shippingAddress } = useSelect( ( select ) =>
|
|
|
|
select( CART_STORE_KEY ).getCustomerData()
|
|
|
|
);
|
|
|
|
const { setShippingAddress } = useDispatch( CART_STORE_KEY );
|
2021-12-07 13:02:57 +00:00
|
|
|
const { cartItems, cartFees, cartTotals, extensions } = useStoreCart();
|
2020-03-11 10:50:12 +00:00
|
|
|
const { appliedCoupons } = useStoreCartCoupons();
|
|
|
|
const currentCartTotals = useRef(
|
|
|
|
prepareTotalItems( cartTotals, needsShipping )
|
|
|
|
);
|
|
|
|
const currentCartTotal = useRef( {
|
|
|
|
label: __( 'Total', 'woo-gutenberg-products-block' ),
|
|
|
|
value: parseInt( cartTotals.total_price, 10 ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
currentCartTotals.current = prepareTotalItems(
|
|
|
|
cartTotals,
|
|
|
|
needsShipping
|
|
|
|
);
|
|
|
|
currentCartTotal.current = {
|
|
|
|
label: __( 'Total', 'woo-gutenberg-products-block' ),
|
|
|
|
value: parseInt( cartTotals.total_price, 10 ),
|
|
|
|
};
|
|
|
|
}, [ cartTotals, needsShipping ] );
|
|
|
|
|
2021-06-16 12:44:40 +00:00
|
|
|
const deprecatedSetExpressPaymentError = useCallback(
|
|
|
|
( errorMessage = '' ) => {
|
|
|
|
deprecated(
|
|
|
|
'setExpressPaymentError should only be used by Express Payment Methods (using the provided onError handler).',
|
|
|
|
{
|
|
|
|
alternative: '',
|
|
|
|
plugin: 'woocommerce-gutenberg-products-block',
|
2022-06-15 09:56:52 +00:00
|
|
|
link: 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/4228',
|
2021-06-16 12:44:40 +00:00
|
|
|
}
|
|
|
|
);
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetExpressPaymentError( errorMessage );
|
2021-06-16 12:44:40 +00:00
|
|
|
},
|
2022-10-05 10:04:16 +00:00
|
|
|
[ __internalSetExpressPaymentError ]
|
2021-06-16 12:44:40 +00:00
|
|
|
);
|
|
|
|
|
2020-03-11 10:50:12 +00:00
|
|
|
return {
|
2021-05-25 11:49:13 +00:00
|
|
|
activePaymentMethod,
|
2020-03-11 10:50:12 +00:00
|
|
|
billing: {
|
2021-12-07 13:02:57 +00:00
|
|
|
appliedCoupons,
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress,
|
|
|
|
billingData: billingAddress,
|
2020-03-11 10:50:12 +00:00
|
|
|
cartTotal: currentCartTotal.current,
|
2020-03-12 01:08:20 +00:00
|
|
|
cartTotalItems: currentCartTotals.current,
|
2021-12-07 13:02:57 +00:00
|
|
|
currency: getCurrencyFromPriceResponse( cartTotals ),
|
|
|
|
customerId,
|
2021-04-22 11:37:27 +00:00
|
|
|
displayPricesIncludingTax: getSetting(
|
|
|
|
'displayCartPricesIncludingTax',
|
|
|
|
false
|
2021-05-25 11:49:13 +00:00
|
|
|
) as boolean,
|
2021-12-07 13:02:57 +00:00
|
|
|
},
|
|
|
|
cartData: {
|
|
|
|
cartItems,
|
|
|
|
cartFees,
|
|
|
|
extensions,
|
2020-03-11 10:50:12 +00:00
|
|
|
},
|
2021-05-25 11:49:13 +00:00
|
|
|
checkoutStatus: {
|
|
|
|
isCalculating,
|
|
|
|
isComplete,
|
|
|
|
isIdle,
|
|
|
|
isProcessing,
|
|
|
|
},
|
|
|
|
components: {
|
2021-12-07 13:02:57 +00:00
|
|
|
LoadingMask,
|
2021-05-25 11:49:13 +00:00
|
|
|
PaymentMethodIcons,
|
|
|
|
PaymentMethodLabel,
|
2021-12-07 13:02:57 +00:00
|
|
|
ValidationInputError,
|
2021-05-25 11:49:13 +00:00
|
|
|
},
|
|
|
|
emitResponse: {
|
|
|
|
noticeContexts,
|
|
|
|
responseTypes,
|
|
|
|
},
|
2020-03-11 10:50:12 +00:00
|
|
|
eventRegistration: {
|
2021-12-07 13:02:57 +00:00
|
|
|
onCheckoutAfterProcessingWithError,
|
|
|
|
onCheckoutAfterProcessingWithSuccess,
|
2021-04-26 15:27:22 +00:00
|
|
|
onCheckoutBeforeProcessing,
|
|
|
|
onCheckoutValidationBeforeProcessing,
|
2023-02-17 11:03:13 +00:00
|
|
|
onCheckoutSuccess,
|
|
|
|
onCheckoutFail,
|
|
|
|
onCheckoutValidation,
|
2021-12-07 13:02:57 +00:00
|
|
|
onPaymentProcessing,
|
2023-02-17 11:03:13 +00:00
|
|
|
onPaymentSetup,
|
2020-03-11 10:50:12 +00:00
|
|
|
onShippingRateFail,
|
|
|
|
onShippingRateSelectFail,
|
2021-12-07 13:02:57 +00:00
|
|
|
onShippingRateSelectSuccess,
|
|
|
|
onShippingRateSuccess,
|
2020-03-11 10:50:12 +00:00
|
|
|
},
|
|
|
|
onSubmit,
|
2022-11-15 12:27:39 +00:00
|
|
|
paymentStatus,
|
2021-06-16 12:44:40 +00:00
|
|
|
setExpressPaymentError: deprecatedSetExpressPaymentError,
|
2021-05-25 11:49:13 +00:00
|
|
|
shippingData: {
|
2021-12-07 13:02:57 +00:00
|
|
|
isSelectingRate,
|
|
|
|
needsShipping,
|
2021-05-25 11:49:13 +00:00
|
|
|
selectedRates,
|
2022-03-14 11:29:25 +00:00
|
|
|
setSelectedRates: selectShippingRate,
|
2021-05-25 11:49:13 +00:00
|
|
|
setShippingAddress,
|
2021-12-07 13:02:57 +00:00
|
|
|
shippingAddress,
|
|
|
|
shippingRates,
|
2022-03-14 11:29:25 +00:00
|
|
|
shippingRatesLoading: isLoadingRates,
|
2021-05-25 11:49:13 +00:00
|
|
|
},
|
|
|
|
shippingStatus: {
|
|
|
|
shippingErrorStatus,
|
|
|
|
shippingErrorTypes,
|
|
|
|
},
|
2021-03-22 17:55:12 +00:00
|
|
|
shouldSavePayment,
|
2020-03-11 10:50:12 +00:00
|
|
|
};
|
|
|
|
};
|