2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
useCheckoutContext,
|
|
|
|
usePaymentMethodDataContext,
|
2020-03-26 13:31:09 +00:00
|
|
|
useShippingDataContext,
|
2020-11-20 15:13:35 +00:00
|
|
|
useCustomerDataContext,
|
2020-03-11 10:50:12 +00:00
|
|
|
} from '@woocommerce/base-context';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-02-04 15:30:28 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
2020-03-11 10:50:12 +00:00
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
2020-03-12 01:08:20 +00:00
|
|
|
import { DISPLAY_CART_PRICES_INCLUDING_TAX } from '@woocommerce/block-settings';
|
2020-03-30 12:07:49 +00:00
|
|
|
import { ValidationInputError } from '@woocommerce/base-components/validation';
|
2020-05-13 15:48:03 +00:00
|
|
|
import {
|
|
|
|
PaymentMethodIcons,
|
|
|
|
PaymentMethodLabel,
|
|
|
|
} from '@woocommerce/base-components/cart-checkout';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-01-19 15:55:44 +00:00
|
|
|
import { useStoreCartCoupons, useStoreCart, useEmitResponse } from '..';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/registered-payment-method-props').RegisteredPaymentMethodProps} RegisteredPaymentMethodProps
|
2020-03-12 01:08:20 +00:00
|
|
|
* @typedef {import('@woocommerce/type-defs/cart').CartTotalItem} CartTotalItem
|
2020-03-11 10:50:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-03-12 01:08:20 +00:00
|
|
|
* Prepares the total items into a shape usable for display as passed on to
|
|
|
|
* registered payment methods.
|
|
|
|
*
|
2020-03-11 10:50:12 +00:00
|
|
|
* @param {Object} totals Current cart total items
|
|
|
|
* @param {boolean} needsShipping Whether or not shipping is needed.
|
|
|
|
*
|
|
|
|
* @return {CartTotalItem[]} Array for cart total items prepared for use.
|
|
|
|
*/
|
2020-03-12 01:08:20 +00:00
|
|
|
export const prepareTotalItems = ( totals, needsShipping ) => {
|
2020-03-11 10:50:12 +00:00
|
|
|
const newTotals = [];
|
2020-03-12 01:08:20 +00:00
|
|
|
const factory = ( label, property ) => {
|
|
|
|
const value = parseInt( totals[ property ], 10 );
|
|
|
|
const tax = parseInt( totals[ property + '_tax' ], 10 );
|
|
|
|
return {
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
valueWithTax: value + tax,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
newTotals.push(
|
|
|
|
factory(
|
|
|
|
__( 'Subtotal:', 'woo-gutenberg-products-block' ),
|
|
|
|
'total_items'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
newTotals.push(
|
|
|
|
factory( __( 'Fees:', 'woo-gutenberg-products-block' ), 'total_fees' )
|
|
|
|
);
|
|
|
|
newTotals.push(
|
|
|
|
factory(
|
|
|
|
__( 'Discount:', 'woo-gutenberg-products-block' ),
|
|
|
|
'total_discount'
|
|
|
|
)
|
|
|
|
);
|
2020-03-11 10:50:12 +00:00
|
|
|
newTotals.push( {
|
|
|
|
label: __( 'Taxes:', 'woo-gutenberg-products-block' ),
|
|
|
|
value: parseInt( totals.total_tax, 10 ),
|
2020-03-12 01:08:20 +00:00
|
|
|
valueWithTax: parseInt( totals.total_tax, 10 ),
|
2020-03-11 10:50:12 +00:00
|
|
|
} );
|
|
|
|
if ( needsShipping ) {
|
2020-03-12 01:08:20 +00:00
|
|
|
newTotals.push(
|
|
|
|
factory(
|
|
|
|
__( 'Shipping:', 'woo-gutenberg-products-block' ),
|
|
|
|
'total_shipping'
|
|
|
|
)
|
|
|
|
);
|
2020-03-11 10:50:12 +00:00
|
|
|
}
|
|
|
|
return newTotals;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {RegisteredPaymentMethodProps} Interface to use as payment method props.
|
|
|
|
*/
|
|
|
|
export const usePaymentMethodInterface = () => {
|
|
|
|
const {
|
|
|
|
isCalculating,
|
|
|
|
isComplete,
|
|
|
|
isIdle,
|
|
|
|
isProcessing,
|
2020-04-14 16:52:23 +00:00
|
|
|
onCheckoutAfterProcessingWithSuccess,
|
|
|
|
onCheckoutAfterProcessingWithError,
|
|
|
|
onCheckoutBeforeProcessing,
|
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,
|
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-11-20 15:13:35 +00:00
|
|
|
|
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 ] );
|
|
|
|
|
|
|
|
return {
|
|
|
|
checkoutStatus: {
|
|
|
|
isCalculating,
|
|
|
|
isComplete,
|
|
|
|
isIdle,
|
|
|
|
isProcessing,
|
|
|
|
},
|
2020-04-06 11:28:46 +00:00
|
|
|
paymentStatus: currentStatus,
|
2020-03-11 10:50:12 +00:00
|
|
|
shippingStatus: {
|
|
|
|
shippingErrorStatus,
|
|
|
|
shippingErrorTypes,
|
|
|
|
},
|
|
|
|
shippingData: {
|
|
|
|
shippingRates,
|
|
|
|
shippingRatesLoading,
|
|
|
|
selectedRates,
|
|
|
|
setSelectedRates,
|
2020-04-08 16:36:04 +00:00
|
|
|
isSelectingRate,
|
2020-03-11 10:50:12 +00:00
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
needsShipping,
|
|
|
|
},
|
|
|
|
billing: {
|
|
|
|
billingData,
|
|
|
|
cartTotal: currentCartTotal.current,
|
|
|
|
currency: getCurrencyFromPriceResponse( cartTotals ),
|
2020-03-12 01:08:20 +00:00
|
|
|
cartTotalItems: currentCartTotals.current,
|
|
|
|
displayPricesIncludingTax: DISPLAY_CART_PRICES_INCLUDING_TAX,
|
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
|
|
|
},
|
|
|
|
eventRegistration: {
|
2020-04-14 16:52:23 +00:00
|
|
|
onCheckoutAfterProcessingWithSuccess,
|
|
|
|
onCheckoutAfterProcessingWithError,
|
|
|
|
onCheckoutBeforeProcessing,
|
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
|
|
|
},
|
2020-03-30 12:07:49 +00:00
|
|
|
components: {
|
|
|
|
ValidationInputError,
|
2020-04-29 10:57:58 +00:00
|
|
|
PaymentMethodIcons,
|
2020-05-13 15:48:03 +00:00
|
|
|
PaymentMethodLabel,
|
2020-03-30 12:07:49 +00:00
|
|
|
},
|
2020-04-14 16:52:23 +00:00
|
|
|
emitResponse: {
|
|
|
|
noticeContexts,
|
|
|
|
responseTypes,
|
|
|
|
},
|
2020-03-11 10:50:12 +00:00
|
|
|
onSubmit,
|
|
|
|
activePaymentMethod,
|
2020-04-06 12:18:35 +00:00
|
|
|
setExpressPaymentError,
|
2020-03-11 10:50:12 +00:00
|
|
|
};
|
|
|
|
};
|