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';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { ValidationInputError } from '../../providers/validation';
|
|
|
|
import { useStoreCart } from '../cart/use-store-cart';
|
|
|
|
import { useStoreCartCoupons } from '../cart/use-store-cart-coupons';
|
|
|
|
import { useEmitResponse } from '../use-emit-response';
|
|
|
|
import { useCheckoutContext } from '../../providers/cart-checkout/checkout-state';
|
|
|
|
import { usePaymentMethodDataContext } from '../../providers/cart-checkout/payment-methods';
|
|
|
|
import { useShippingDataContext } from '../../providers/cart-checkout/shipping';
|
|
|
|
import { useCustomerDataContext } from '../../providers/cart-checkout/customer';
|
2021-05-25 11:49:13 +00:00
|
|
|
import { prepareTotalItems } from './utils';
|
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-05-25 11:49:13 +00:00
|
|
|
export const usePaymentMethodInterface = (): Record< string, unknown > => {
|
2020-03-11 10:50:12 +00:00
|
|
|
const {
|
|
|
|
isCalculating,
|
|
|
|
isComplete,
|
|
|
|
isIdle,
|
|
|
|
isProcessing,
|
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,
|
2020-04-07 14:29:59 +00:00
|
|
|
customerId,
|
2020-03-11 10:50:12 +00:00
|
|
|
} = useCheckoutContext();
|
|
|
|
const {
|
|
|
|
currentStatus,
|
|
|
|
activePaymentMethod,
|
2020-04-03 11:50:54 +00:00
|
|
|
onPaymentProcessing,
|
2020-04-06 12:18:35 +00:00
|
|
|
setExpressPaymentError,
|
2021-03-22 17:55:12 +00:00
|
|
|
shouldSavePayment,
|
2020-03-11 10:50:12 +00:00
|
|
|
} = usePaymentMethodDataContext();
|
|
|
|
const {
|
|
|
|
shippingErrorStatus,
|
|
|
|
shippingErrorTypes,
|
|
|
|
shippingRates,
|
|
|
|
shippingRatesLoading,
|
|
|
|
selectedRates,
|
|
|
|
setSelectedRates,
|
2020-04-08 16:36:04 +00:00
|
|
|
isSelectingRate,
|
2020-03-11 10:50:12 +00:00
|
|
|
onShippingRateSuccess,
|
|
|
|
onShippingRateFail,
|
|
|
|
onShippingRateSelectSuccess,
|
|
|
|
onShippingRateSelectFail,
|
|
|
|
needsShipping,
|
2020-03-26 13:31:09 +00:00
|
|
|
} = useShippingDataContext();
|
2020-11-20 15:13:35 +00:00
|
|
|
const {
|
|
|
|
billingData,
|
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
} = useCustomerDataContext();
|
2020-03-11 10:50:12 +00:00
|
|
|
const { cartTotals } = useStoreCart();
|
|
|
|
const { appliedCoupons } = useStoreCartCoupons();
|
2020-04-14 16:52:23 +00:00
|
|
|
const { noticeContexts, responseTypes } = useEmitResponse();
|
2020-03-11 10:50:12 +00:00
|
|
|
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',
|
|
|
|
link:
|
|
|
|
'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/4228',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
setExpressPaymentError( errorMessage );
|
|
|
|
},
|
|
|
|
[ setExpressPaymentError ]
|
|
|
|
);
|
|
|
|
|
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: {
|
|
|
|
billingData,
|
|
|
|
cartTotal: currentCartTotal.current,
|
|
|
|
currency: getCurrencyFromPriceResponse( cartTotals ),
|
2020-03-12 01:08:20 +00:00
|
|
|
cartTotalItems: currentCartTotals.current,
|
2021-04-22 11:37:27 +00:00
|
|
|
displayPricesIncludingTax: getSetting(
|
|
|
|
'displayCartPricesIncludingTax',
|
|
|
|
false
|
2021-05-25 11:49:13 +00:00
|
|
|
) as boolean,
|
2020-03-11 10:50:12 +00:00
|
|
|
appliedCoupons,
|
2020-04-07 14:29:59 +00:00
|
|
|
customerId,
|
2020-03-11 10:50:12 +00:00
|
|
|
},
|
2021-05-25 11:49:13 +00:00
|
|
|
checkoutStatus: {
|
|
|
|
isCalculating,
|
|
|
|
isComplete,
|
|
|
|
isIdle,
|
|
|
|
isProcessing,
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
ValidationInputError,
|
|
|
|
PaymentMethodIcons,
|
|
|
|
PaymentMethodLabel,
|
2021-11-22 09:41:54 +00:00
|
|
|
LoadingMask,
|
2021-05-25 11:49:13 +00:00
|
|
|
},
|
|
|
|
emitResponse: {
|
|
|
|
noticeContexts,
|
|
|
|
responseTypes,
|
|
|
|
},
|
2020-03-11 10:50:12 +00:00
|
|
|
eventRegistration: {
|
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
|
|
|
onShippingRateSuccess,
|
|
|
|
onShippingRateFail,
|
|
|
|
onShippingRateSelectSuccess,
|
|
|
|
onShippingRateSelectFail,
|
2020-04-03 11:50:54 +00:00
|
|
|
onPaymentProcessing,
|
2020-03-11 10:50:12 +00:00
|
|
|
},
|
|
|
|
onSubmit,
|
2021-05-25 11:49:13 +00:00
|
|
|
paymentStatus: currentStatus,
|
2021-06-16 12:44:40 +00:00
|
|
|
setExpressPaymentError: deprecatedSetExpressPaymentError,
|
2021-05-25 11:49:13 +00:00
|
|
|
shippingData: {
|
|
|
|
shippingRates,
|
|
|
|
shippingRatesLoading,
|
|
|
|
selectedRates,
|
|
|
|
setSelectedRates,
|
|
|
|
isSelectingRate,
|
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
needsShipping,
|
|
|
|
},
|
|
|
|
shippingStatus: {
|
|
|
|
shippingErrorStatus,
|
|
|
|
shippingErrorTypes,
|
|
|
|
},
|
2021-03-22 17:55:12 +00:00
|
|
|
shouldSavePayment,
|
2020-03-11 10:50:12 +00:00
|
|
|
};
|
|
|
|
};
|