2020-05-14 23:55:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useEffect, useState, useRef, useCallback } from '@wordpress/element';
|
|
|
|
import { useStripe } from '@stripe/react-stripe-js';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
getPaymentRequest,
|
|
|
|
updatePaymentRequest,
|
|
|
|
canDoPaymentRequest,
|
|
|
|
normalizeShippingAddressForCheckout,
|
|
|
|
normalizeShippingOptionSelectionsForCheckout,
|
|
|
|
getStripeServerData,
|
|
|
|
pluckAddress,
|
|
|
|
normalizeShippingOptions,
|
|
|
|
} from '../stripe-utils';
|
|
|
|
import { useEventHandlers } from './use-event-handlers';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('../stripe-utils/type-defs').StripePaymentRequest} StripePaymentRequest
|
|
|
|
*/
|
|
|
|
|
|
|
|
export const useInitialization = ( {
|
|
|
|
billing,
|
|
|
|
shippingData,
|
|
|
|
setExpressPaymentError,
|
|
|
|
onClick,
|
|
|
|
onClose,
|
|
|
|
onSubmit,
|
|
|
|
} ) => {
|
|
|
|
const stripe = useStripe();
|
|
|
|
/**
|
|
|
|
* @type {[ StripePaymentRequest|null, function( StripePaymentRequest ):void]}
|
|
|
|
*/
|
|
|
|
// @ts-ignore
|
|
|
|
const [ paymentRequest, setPaymentRequest ] = useState( null );
|
|
|
|
const [ isFinished, setIsFinished ] = useState( false );
|
|
|
|
const [ isProcessing, setIsProcessing ] = useState( false );
|
|
|
|
const [ canMakePayment, setCanMakePayment ] = useState( false );
|
2020-11-20 16:40:58 +00:00
|
|
|
const [ paymentRequestType, setPaymentRequestType ] = useState( '' );
|
2020-05-14 23:55:22 +00:00
|
|
|
const currentShipping = useRef( shippingData );
|
|
|
|
const {
|
|
|
|
paymentRequestEventHandlers,
|
|
|
|
clearPaymentRequestEventHandler,
|
|
|
|
setPaymentRequestEventHandler,
|
|
|
|
} = useEventHandlers();
|
|
|
|
|
|
|
|
// Update refs when any change.
|
|
|
|
useEffect( () => {
|
|
|
|
currentShipping.current = shippingData;
|
2020-11-20 16:40:58 +00:00
|
|
|
}, [ shippingData ] );
|
2020-05-14 23:55:22 +00:00
|
|
|
|
2020-11-20 16:40:58 +00:00
|
|
|
// Create the initial paymentRequest object. Note, we can't do anything if stripe isn't available yet or we have zero total.
|
2020-05-14 23:55:22 +00:00
|
|
|
useEffect( () => {
|
2020-11-20 16:40:58 +00:00
|
|
|
if (
|
|
|
|
! stripe ||
|
|
|
|
! billing.cartTotal.value ||
|
|
|
|
isFinished ||
|
|
|
|
isProcessing ||
|
|
|
|
paymentRequest
|
|
|
|
) {
|
2020-05-14 23:55:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-11-20 16:40:58 +00:00
|
|
|
const pr = getPaymentRequest( {
|
|
|
|
total: billing.cartTotal,
|
|
|
|
currencyCode: billing.currency.code.toLowerCase(),
|
|
|
|
countryCode: getSetting( 'baseLocation', {} )?.country,
|
|
|
|
shippingRequired: shippingData.needsShipping,
|
|
|
|
cartTotalItems: billing.cartTotalItems,
|
|
|
|
stripe,
|
|
|
|
} );
|
|
|
|
canDoPaymentRequest( pr ).then( ( result ) => {
|
|
|
|
setPaymentRequest( pr );
|
|
|
|
setPaymentRequestType( result.requestType || '' );
|
|
|
|
setCanMakePayment( result.canPay );
|
|
|
|
} );
|
2020-05-14 23:55:22 +00:00
|
|
|
}, [
|
|
|
|
billing.cartTotal,
|
|
|
|
billing.currency.code,
|
|
|
|
shippingData.needsShipping,
|
|
|
|
billing.cartTotalItems,
|
|
|
|
stripe,
|
|
|
|
isProcessing,
|
|
|
|
isFinished,
|
2020-11-20 16:40:58 +00:00
|
|
|
paymentRequest,
|
2020-05-14 23:55:22 +00:00
|
|
|
] );
|
|
|
|
|
2020-11-20 16:40:58 +00:00
|
|
|
// When the payment button is clicked, update the request and show it.
|
|
|
|
const onButtonClick = useCallback( () => {
|
2020-05-14 23:55:22 +00:00
|
|
|
setIsProcessing( true );
|
|
|
|
setIsFinished( false );
|
|
|
|
setExpressPaymentError( '' );
|
2020-11-20 16:40:58 +00:00
|
|
|
updatePaymentRequest( {
|
|
|
|
// @ts-ignore
|
|
|
|
paymentRequest,
|
|
|
|
total: billing.cartTotal,
|
|
|
|
currencyCode: billing.currency.code.toLowerCase(),
|
|
|
|
cartTotalItems: billing.cartTotalItems,
|
|
|
|
} );
|
2020-05-14 23:55:22 +00:00
|
|
|
onClick();
|
2020-11-20 16:40:58 +00:00
|
|
|
}, [
|
|
|
|
onClick,
|
|
|
|
paymentRequest,
|
|
|
|
setExpressPaymentError,
|
|
|
|
billing.cartTotal,
|
|
|
|
billing.currency.code,
|
|
|
|
billing.cartTotalItems,
|
|
|
|
] );
|
2020-05-14 23:55:22 +00:00
|
|
|
|
2020-11-17 10:51:27 +00:00
|
|
|
const abortPayment = useCallback( ( paymentMethod ) => {
|
2020-05-14 23:55:22 +00:00
|
|
|
paymentMethod.complete( 'fail' );
|
|
|
|
setIsProcessing( false );
|
|
|
|
setIsFinished( true );
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const completePayment = useCallback( ( paymentMethod ) => {
|
|
|
|
paymentMethod.complete( 'success' );
|
|
|
|
setIsFinished( true );
|
|
|
|
setIsProcessing( false );
|
|
|
|
}, [] );
|
|
|
|
|
2020-11-20 16:40:58 +00:00
|
|
|
// whenever paymentRequest changes, hook in event listeners.
|
2020-05-14 23:55:22 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const noop = { removeAllListeners: () => void null };
|
|
|
|
let shippingAddressChangeEvent = noop,
|
|
|
|
shippingOptionChangeEvent = noop,
|
|
|
|
sourceChangeEvent = noop,
|
|
|
|
cancelChangeEvent = noop;
|
2020-11-20 16:40:58 +00:00
|
|
|
|
|
|
|
if ( paymentRequest ) {
|
|
|
|
const cancelHandler = () => {
|
|
|
|
setIsFinished( false );
|
|
|
|
setIsProcessing( false );
|
|
|
|
setPaymentRequest( null );
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
const shippingAddressChangeHandler = ( event ) => {
|
|
|
|
const newShippingAddress = normalizeShippingAddressForCheckout(
|
|
|
|
event.shippingAddress
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
isShallowEqual(
|
|
|
|
pluckAddress( newShippingAddress ),
|
|
|
|
pluckAddress( currentShipping.current.shippingAddress )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// the address is the same so no change needed.
|
|
|
|
event.updateWith( {
|
|
|
|
status: 'success',
|
|
|
|
shippingOptions: normalizeShippingOptions(
|
|
|
|
currentShipping.current.shippingRates
|
|
|
|
),
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
// the address is different so let's set the new address and
|
|
|
|
// register the handler to be picked up by the shipping rate
|
|
|
|
// change event.
|
|
|
|
currentShipping.current.setShippingAddress(
|
|
|
|
normalizeShippingAddressForCheckout(
|
|
|
|
event.shippingAddress
|
|
|
|
)
|
|
|
|
);
|
|
|
|
setPaymentRequestEventHandler(
|
|
|
|
'shippingAddressChange',
|
|
|
|
event
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const shippingOptionChangeHandler = ( event ) => {
|
|
|
|
currentShipping.current.setSelectedRates(
|
|
|
|
normalizeShippingOptionSelectionsForCheckout(
|
|
|
|
event.shippingOption
|
|
|
|
)
|
|
|
|
);
|
|
|
|
setPaymentRequestEventHandler( 'shippingOptionChange', event );
|
|
|
|
};
|
|
|
|
|
|
|
|
const sourceHandler = ( paymentMethod ) => {
|
|
|
|
if (
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
! getStripeServerData().allowPrepaidCard &&
|
|
|
|
paymentMethod.source.card.funding
|
|
|
|
) {
|
|
|
|
setExpressPaymentError(
|
2021-11-08 14:07:42 +00:00
|
|
|
/* eslint-disable-next-line @wordpress/i18n-text-domain */
|
2020-11-20 16:40:58 +00:00
|
|
|
__(
|
|
|
|
"Sorry, we're not accepting prepaid cards at this time.",
|
|
|
|
'woocommerce-gateway-stripe'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setPaymentRequestEventHandler( 'sourceEvent', paymentMethod );
|
|
|
|
// kick off checkout processing step.
|
|
|
|
onSubmit();
|
|
|
|
};
|
|
|
|
|
2020-05-14 23:55:22 +00:00
|
|
|
// @ts-ignore
|
|
|
|
shippingAddressChangeEvent = paymentRequest.on(
|
|
|
|
'shippingaddresschange',
|
|
|
|
shippingAddressChangeHandler
|
|
|
|
);
|
|
|
|
// @ts-ignore
|
|
|
|
shippingOptionChangeEvent = paymentRequest.on(
|
|
|
|
'shippingoptionchange',
|
|
|
|
shippingOptionChangeHandler
|
|
|
|
);
|
|
|
|
// @ts-ignore
|
|
|
|
sourceChangeEvent = paymentRequest.on( 'source', sourceHandler );
|
|
|
|
// @ts-ignore
|
|
|
|
cancelChangeEvent = paymentRequest.on( 'cancel', cancelHandler );
|
|
|
|
}
|
2020-11-20 16:40:58 +00:00
|
|
|
|
2020-05-14 23:55:22 +00:00
|
|
|
return () => {
|
|
|
|
if ( paymentRequest ) {
|
|
|
|
shippingAddressChangeEvent.removeAllListeners();
|
|
|
|
shippingOptionChangeEvent.removeAllListeners();
|
|
|
|
sourceChangeEvent.removeAllListeners();
|
|
|
|
cancelChangeEvent.removeAllListeners();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
paymentRequest,
|
|
|
|
canMakePayment,
|
|
|
|
isProcessing,
|
|
|
|
setPaymentRequestEventHandler,
|
|
|
|
setExpressPaymentError,
|
|
|
|
onSubmit,
|
2020-11-20 16:40:58 +00:00
|
|
|
onClose,
|
2020-05-14 23:55:22 +00:00
|
|
|
] );
|
2020-11-20 16:40:58 +00:00
|
|
|
|
2020-05-14 23:55:22 +00:00
|
|
|
return {
|
|
|
|
paymentRequest,
|
|
|
|
paymentRequestEventHandlers,
|
|
|
|
clearPaymentRequestEventHandler,
|
|
|
|
isProcessing,
|
|
|
|
canMakePayment,
|
|
|
|
onButtonClick,
|
|
|
|
abortPayment,
|
|
|
|
completePayment,
|
2020-11-20 16:40:58 +00:00
|
|
|
paymentRequestType,
|
2020-05-14 23:55:22 +00:00
|
|
|
};
|
|
|
|
};
|