2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
useCheckoutContext,
|
|
|
|
usePaymentMethodDataContext,
|
2020-03-26 13:31:09 +00:00
|
|
|
useShippingDataContext,
|
|
|
|
useBillingDataContext,
|
2020-03-11 10:50:12 +00:00
|
|
|
} from '@woocommerce/base-context';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils';
|
|
|
|
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';
|
|
|
|
import CheckboxControl from '@woocommerce/base-components/checkbox-control';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-26 13:31:09 +00:00
|
|
|
import { useStoreOrder, useStoreCartCoupons, useStoreCart } 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// @todo This will expose the consistent properties used as the payment method
|
|
|
|
// interface pulled from the various contexts exposing data for the interface.
|
|
|
|
// @todo need to also include notices interfaces here (maybe?).
|
|
|
|
/**
|
|
|
|
* @return {RegisteredPaymentMethodProps} Interface to use as payment method props.
|
|
|
|
*/
|
|
|
|
export const usePaymentMethodInterface = () => {
|
|
|
|
const {
|
|
|
|
isCalculating,
|
|
|
|
isComplete,
|
|
|
|
isIdle,
|
|
|
|
isProcessing,
|
|
|
|
onCheckoutCompleteSuccess,
|
|
|
|
onCheckoutCompleteError,
|
|
|
|
onCheckoutProcessing,
|
|
|
|
onSubmit,
|
2020-04-07 14:29:59 +00:00
|
|
|
customerId,
|
2020-03-11 10:50:12 +00:00
|
|
|
} = useCheckoutContext();
|
|
|
|
const {
|
|
|
|
currentStatus,
|
|
|
|
activePaymentMethod,
|
|
|
|
setActivePaymentMethod,
|
2020-04-03 11:50:54 +00:00
|
|
|
onPaymentProcessing,
|
|
|
|
onPaymentSuccess,
|
|
|
|
onPaymentFail,
|
|
|
|
onPaymentError,
|
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,
|
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
onShippingRateSuccess,
|
|
|
|
onShippingRateFail,
|
|
|
|
onShippingRateSelectSuccess,
|
|
|
|
onShippingRateSelectFail,
|
|
|
|
needsShipping,
|
2020-03-26 13:31:09 +00:00
|
|
|
} = useShippingDataContext();
|
|
|
|
const { billingData, setBillingData } = useBillingDataContext();
|
2020-03-11 10:50:12 +00:00
|
|
|
const { order, isLoading: orderLoading } = useStoreOrder();
|
|
|
|
const { cartTotals } = useStoreCart();
|
|
|
|
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 ] );
|
|
|
|
|
|
|
|
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,
|
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
needsShipping,
|
|
|
|
},
|
|
|
|
billing: {
|
|
|
|
billingData,
|
|
|
|
setBillingData,
|
|
|
|
order,
|
|
|
|
orderLoading,
|
|
|
|
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: {
|
|
|
|
onCheckoutCompleteSuccess,
|
|
|
|
onCheckoutCompleteError,
|
|
|
|
onCheckoutProcessing,
|
|
|
|
onShippingRateSuccess,
|
|
|
|
onShippingRateFail,
|
|
|
|
onShippingRateSelectSuccess,
|
|
|
|
onShippingRateSelectFail,
|
2020-04-03 11:50:54 +00:00
|
|
|
onPaymentProcessing,
|
|
|
|
onPaymentSuccess,
|
|
|
|
onPaymentFail,
|
|
|
|
onPaymentError,
|
2020-03-11 10:50:12 +00:00
|
|
|
},
|
2020-03-30 12:07:49 +00:00
|
|
|
components: {
|
|
|
|
ValidationInputError,
|
|
|
|
CheckboxControl,
|
|
|
|
},
|
2020-03-11 10:50:12 +00:00
|
|
|
onSubmit,
|
|
|
|
activePaymentMethod,
|
|
|
|
setActivePaymentMethod,
|
2020-04-06 12:18:35 +00:00
|
|
|
setExpressPaymentError,
|
2020-03-11 10:50:12 +00:00
|
|
|
};
|
|
|
|
};
|