2020-03-10 13:39:21 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
STATUS,
|
|
|
|
DEFAULT_PAYMENT_DATA,
|
|
|
|
DEFAULT_PAYMENT_METHOD_DATA,
|
|
|
|
} from './constants';
|
|
|
|
import reducer from './reducer';
|
2020-03-26 11:11:46 +00:00
|
|
|
import {
|
|
|
|
statusOnly,
|
|
|
|
error,
|
|
|
|
failed,
|
|
|
|
success,
|
|
|
|
setRegisteredPaymentMethod,
|
|
|
|
setRegisteredExpressPaymentMethod,
|
|
|
|
} from './actions';
|
|
|
|
import {
|
|
|
|
usePaymentMethods,
|
|
|
|
useExpressPaymentMethods,
|
|
|
|
} from './use-payment-method-registration';
|
2020-03-26 13:31:09 +00:00
|
|
|
import { useBillingDataContext } from '../billing';
|
2020-04-03 11:50:54 +00:00
|
|
|
import { useCheckoutContext } from '../checkout-state';
|
|
|
|
import {
|
|
|
|
EMIT_TYPES,
|
|
|
|
emitterSubscribers,
|
|
|
|
emitEvent,
|
|
|
|
emitEventWithAbort,
|
|
|
|
reducer as emitReducer,
|
|
|
|
} from './event-emit';
|
|
|
|
import { useValidationContext } from '../validation';
|
2020-03-10 13:39:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useState,
|
|
|
|
useReducer,
|
2020-03-26 11:11:46 +00:00
|
|
|
useCallback,
|
|
|
|
useEffect,
|
2020-04-03 11:50:54 +00:00
|
|
|
useRef,
|
|
|
|
useMemo,
|
2020-03-10 13:39:21 +00:00
|
|
|
} from '@wordpress/element';
|
2020-03-26 11:11:46 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2020-04-06 12:18:35 +00:00
|
|
|
import { useStoreNotices } from '@woocommerce/base-hooks';
|
2020-03-10 13:39:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/contexts').PaymentMethodDataContext} PaymentMethodDataContext
|
|
|
|
* @typedef {import('@woocommerce/type-defs/contexts').PaymentStatusDispatch} PaymentStatusDispatch
|
|
|
|
* @typedef {import('@woocommerce/type-defs/contexts').PaymentStatusDispatchers} PaymentStatusDispatchers
|
2020-03-26 13:31:09 +00:00
|
|
|
* @typedef {import('@woocommerce/type-defs/billing').BillingData} BillingData
|
2020-03-26 11:11:46 +00:00
|
|
|
* @typedef {import('@woocommerce/type-defs/contexts').CustomerPaymentMethod} CustomerPaymentMethod
|
2020-03-10 13:39:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const {
|
|
|
|
STARTED,
|
|
|
|
PROCESSING,
|
|
|
|
COMPLETE,
|
|
|
|
PRISTINE,
|
|
|
|
ERROR,
|
|
|
|
FAILED,
|
|
|
|
SUCCESS,
|
|
|
|
} = STATUS;
|
|
|
|
|
|
|
|
const PaymentMethodDataContext = createContext( DEFAULT_PAYMENT_METHOD_DATA );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {PaymentMethodDataContext} The data and functions exposed by the
|
|
|
|
* payment method context provider.
|
|
|
|
*/
|
|
|
|
export const usePaymentMethodDataContext = () => {
|
|
|
|
return useContext( PaymentMethodDataContext );
|
|
|
|
};
|
|
|
|
|
2020-04-03 11:50:54 +00:00
|
|
|
const isSuccessResponse = ( response ) => {
|
|
|
|
return (
|
|
|
|
( typeof response === 'object' &&
|
|
|
|
typeof response.billingData !== 'undefined' &&
|
|
|
|
typeof response.paymentMethodData !== 'undefined' ) ||
|
|
|
|
response === true
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isFailResponse = ( response ) => {
|
|
|
|
return response && typeof response.fail === 'object';
|
|
|
|
};
|
|
|
|
|
|
|
|
const isErrorResponse = ( response ) => {
|
|
|
|
return response && typeof response.errorMessage !== 'undefined';
|
|
|
|
};
|
|
|
|
|
2020-03-10 13:39:21 +00:00
|
|
|
/**
|
|
|
|
* PaymentMethodDataProvider is automatically included in the
|
|
|
|
* CheckoutDataProvider.
|
|
|
|
*
|
|
|
|
* This provides the api interface (via the context hook) for payment method
|
|
|
|
* status and data.
|
|
|
|
*
|
|
|
|
* @param {Object} props Incoming props for provider
|
2020-04-01 09:27:53 +00:00
|
|
|
* @param {Object} props.children The wrapped components in this
|
2020-03-10 13:39:21 +00:00
|
|
|
* provider.
|
|
|
|
* @param {string} props.activePaymentMethod The initial active payment method
|
|
|
|
* to set for the context.
|
|
|
|
*/
|
|
|
|
export const PaymentMethodDataProvider = ( {
|
|
|
|
children,
|
|
|
|
activePaymentMethod: initialActivePaymentMethod,
|
|
|
|
} ) => {
|
2020-03-26 13:31:09 +00:00
|
|
|
const { setBillingData } = useBillingDataContext();
|
2020-04-03 11:50:54 +00:00
|
|
|
const {
|
|
|
|
isComplete: checkoutIsComplete,
|
|
|
|
isProcessingComplete: checkoutIsProcessingComplete,
|
2020-04-08 15:46:34 +00:00
|
|
|
isCalculating: checkoutIsCalculating,
|
2020-04-03 11:50:54 +00:00
|
|
|
hasError: checkoutHasError,
|
|
|
|
} = useCheckoutContext();
|
2020-03-10 13:39:21 +00:00
|
|
|
const [ activePaymentMethod, setActive ] = useState(
|
|
|
|
initialActivePaymentMethod
|
|
|
|
);
|
2020-04-03 11:50:54 +00:00
|
|
|
const [ observers, subscriber ] = useReducer( emitReducer, {} );
|
|
|
|
const currentObservers = useRef( observers );
|
2020-04-08 00:12:00 +00:00
|
|
|
const customerPaymentMethods = getSetting( 'customerPaymentMethods', {} );
|
2020-03-10 13:39:21 +00:00
|
|
|
const [ paymentStatus, dispatch ] = useReducer(
|
|
|
|
reducer,
|
|
|
|
DEFAULT_PAYMENT_DATA
|
|
|
|
);
|
|
|
|
const setActivePaymentMethod = ( paymentMethodSlug ) => {
|
|
|
|
setActive( paymentMethodSlug );
|
|
|
|
dispatch( statusOnly( PRISTINE ) );
|
|
|
|
};
|
2020-03-26 11:11:46 +00:00
|
|
|
const paymentMethodsInitialized = usePaymentMethods( ( paymentMethod ) =>
|
|
|
|
dispatch( setRegisteredPaymentMethod( paymentMethod ) )
|
|
|
|
);
|
|
|
|
const expressPaymentMethodsInitialized = useExpressPaymentMethods(
|
|
|
|
( paymentMethod ) => {
|
|
|
|
dispatch( setRegisteredExpressPaymentMethod( paymentMethod ) );
|
|
|
|
}
|
|
|
|
);
|
2020-04-03 11:50:54 +00:00
|
|
|
const { setValidationErrors } = useValidationContext();
|
2020-04-06 12:18:35 +00:00
|
|
|
const { addErrorNotice, removeNotice } = useStoreNotices();
|
2020-04-03 11:50:54 +00:00
|
|
|
|
2020-04-06 12:18:35 +00:00
|
|
|
const setExpressPaymentError = ( message ) => {
|
|
|
|
addErrorNotice( message, {
|
|
|
|
context: 'wc/express-payment-area',
|
|
|
|
id: 'wc-express-payment-error',
|
|
|
|
} );
|
|
|
|
if ( ! message ) {
|
|
|
|
removeNotice( 'wc-express-payment-error' );
|
|
|
|
}
|
|
|
|
};
|
2020-04-03 11:50:54 +00:00
|
|
|
// ensure observers are always current.
|
|
|
|
useEffect( () => {
|
|
|
|
currentObservers.current = observers;
|
|
|
|
}, [ observers ] );
|
|
|
|
const onPaymentProcessing = useMemo(
|
|
|
|
() => emitterSubscribers( subscriber ).onPaymentProcessing,
|
|
|
|
[ subscriber ]
|
|
|
|
);
|
|
|
|
const onPaymentSuccess = useMemo(
|
|
|
|
() => emitterSubscribers( subscriber ).onPaymentSuccess,
|
|
|
|
[ subscriber ]
|
|
|
|
);
|
|
|
|
const onPaymentFail = useMemo(
|
|
|
|
() => emitterSubscribers( subscriber ).onPaymentFail,
|
|
|
|
[ subscriber ]
|
|
|
|
);
|
|
|
|
const onPaymentError = useMemo(
|
|
|
|
() => emitterSubscribers( subscriber ).onPaymentError,
|
|
|
|
[ subscriber ]
|
|
|
|
);
|
|
|
|
|
2020-04-08 15:46:34 +00:00
|
|
|
const currentStatus = useMemo(
|
|
|
|
() => ( {
|
|
|
|
isPristine: paymentStatus.currentStatus === PRISTINE,
|
|
|
|
isStarted: paymentStatus.currentStatus === STARTED,
|
|
|
|
isProcessing: paymentStatus.currentStatus === PROCESSING,
|
|
|
|
isFinished: [ ERROR, FAILED, SUCCESS ].includes(
|
|
|
|
paymentStatus.currentStatus
|
|
|
|
),
|
|
|
|
hasError: paymentStatus.currentStatus === ERROR,
|
|
|
|
hasFailed: paymentStatus.currentStatus === FAILED,
|
|
|
|
isSuccessful: paymentStatus.currentStatus === SUCCESS,
|
|
|
|
} ),
|
|
|
|
[ paymentStatus.currentStatus ]
|
|
|
|
);
|
|
|
|
|
2020-04-03 11:50:54 +00:00
|
|
|
// flip payment to processing if checkout processing is complete and there
|
|
|
|
// are no errors.
|
|
|
|
useEffect( () => {
|
2020-04-08 15:46:34 +00:00
|
|
|
if (
|
|
|
|
checkoutIsProcessingComplete &&
|
|
|
|
! checkoutHasError &&
|
|
|
|
! checkoutIsCalculating &&
|
|
|
|
! currentStatus.isFinished
|
|
|
|
) {
|
2020-04-03 11:50:54 +00:00
|
|
|
setPaymentStatus().processing();
|
|
|
|
}
|
2020-04-08 15:46:34 +00:00
|
|
|
}, [
|
|
|
|
checkoutIsProcessingComplete,
|
|
|
|
checkoutHasError,
|
|
|
|
checkoutIsCalculating,
|
|
|
|
currentStatus.isFinished,
|
|
|
|
] );
|
2020-04-03 11:50:54 +00:00
|
|
|
|
2020-03-26 11:11:46 +00:00
|
|
|
// set initial active payment method if it's undefined.
|
|
|
|
useEffect( () => {
|
|
|
|
const paymentMethodKeys = Object.keys( paymentStatus.paymentMethods );
|
|
|
|
if (
|
|
|
|
paymentMethodsInitialized &&
|
|
|
|
! activePaymentMethod &&
|
|
|
|
paymentMethodKeys.length > 0
|
|
|
|
) {
|
|
|
|
setActivePaymentMethod(
|
|
|
|
Object.keys( paymentStatus.paymentMethods )[ 0 ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
activePaymentMethod,
|
|
|
|
paymentMethodsInitialized,
|
|
|
|
paymentStatus.paymentMethods,
|
|
|
|
] );
|
2020-03-10 13:39:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {PaymentStatusDispatch}
|
|
|
|
*/
|
2020-03-26 11:11:46 +00:00
|
|
|
const setPaymentStatus = useCallback(
|
|
|
|
() => ( {
|
|
|
|
started: () => dispatch( statusOnly( STARTED ) ),
|
|
|
|
processing: () => dispatch( statusOnly( PROCESSING ) ),
|
|
|
|
completed: () => dispatch( statusOnly( COMPLETE ) ),
|
|
|
|
/**
|
|
|
|
* @param {string} errorMessage An error message
|
|
|
|
*/
|
|
|
|
error: ( errorMessage ) => dispatch( error( errorMessage ) ),
|
|
|
|
/**
|
2020-04-03 11:50:54 +00:00
|
|
|
* @param {string} errorMessage An error message
|
|
|
|
* @param {Object} paymentMethodData Arbitrary payment method data to
|
|
|
|
* accompany the checkout submission.
|
|
|
|
* @param {BillingData|null} [billingData] The billing data accompanying the
|
|
|
|
* payment method.
|
2020-03-26 11:11:46 +00:00
|
|
|
*/
|
2020-03-26 13:31:09 +00:00
|
|
|
failed: ( errorMessage, paymentMethodData, billingData = null ) => {
|
|
|
|
if ( billingData ) {
|
|
|
|
setBillingData( billingData );
|
|
|
|
}
|
2020-03-26 11:11:46 +00:00
|
|
|
dispatch(
|
|
|
|
failed( {
|
|
|
|
errorMessage,
|
|
|
|
paymentMethodData,
|
|
|
|
} )
|
2020-03-26 13:31:09 +00:00
|
|
|
);
|
|
|
|
},
|
2020-03-26 11:11:46 +00:00
|
|
|
/**
|
2020-04-03 11:50:54 +00:00
|
|
|
* @param {Object} [paymentMethodData] Arbitrary payment method data to
|
|
|
|
* accompany the checkout.
|
|
|
|
* @param {BillingData|null} [billingData] The billing data accompanying the
|
|
|
|
* payment method.
|
2020-03-26 11:11:46 +00:00
|
|
|
*/
|
2020-04-03 11:50:54 +00:00
|
|
|
success: ( paymentMethodData = {}, billingData = null ) => {
|
2020-03-26 13:31:09 +00:00
|
|
|
if ( billingData ) {
|
|
|
|
setBillingData( billingData );
|
|
|
|
}
|
2020-03-26 11:11:46 +00:00
|
|
|
dispatch(
|
|
|
|
success( {
|
|
|
|
paymentMethodData,
|
|
|
|
} )
|
2020-03-26 13:31:09 +00:00
|
|
|
);
|
|
|
|
},
|
2020-03-26 11:11:46 +00:00
|
|
|
} ),
|
|
|
|
[ dispatch ]
|
|
|
|
);
|
2020-03-23 20:13:41 +00:00
|
|
|
|
2020-04-03 11:50:54 +00:00
|
|
|
// emit events.
|
|
|
|
useEffect( () => {
|
|
|
|
// Note: the nature of this event emitter is that it will bail on a
|
|
|
|
// successful payment (that is an observer hooked in that returns an
|
|
|
|
// object in the shape of a successful payment). However, this still
|
|
|
|
// allows for other observers that return true for continuing through
|
|
|
|
// to the next observer (or bailing if there's a problem).
|
|
|
|
if ( currentStatus.isProcessing ) {
|
|
|
|
emitEventWithAbort(
|
|
|
|
currentObservers.current,
|
|
|
|
EMIT_TYPES.PAYMENT_PROCESSING,
|
|
|
|
{}
|
|
|
|
).then( ( response ) => {
|
|
|
|
if ( isSuccessResponse( response ) ) {
|
|
|
|
setPaymentStatus().success(
|
|
|
|
response.paymentMethodData,
|
|
|
|
response.billingData
|
|
|
|
);
|
|
|
|
} else if ( isFailResponse( response ) ) {
|
|
|
|
setPaymentStatus().failed(
|
|
|
|
response.fail.errorMessage,
|
2020-04-06 12:18:35 +00:00
|
|
|
response.fail.paymentMethodData,
|
|
|
|
response.fail.billingData
|
2020-04-03 11:50:54 +00:00
|
|
|
);
|
|
|
|
} else if ( isErrorResponse( response ) ) {
|
|
|
|
setPaymentStatus().error( response.errorMessage );
|
|
|
|
setValidationErrors( response.validationErrors );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
currentStatus.isSuccessful &&
|
|
|
|
checkoutIsComplete &&
|
|
|
|
! checkoutHasError
|
|
|
|
) {
|
|
|
|
emitEvent(
|
|
|
|
currentObservers.current,
|
|
|
|
EMIT_TYPES.PAYMENT_SUCCESS,
|
|
|
|
{}
|
|
|
|
).then( () => {
|
|
|
|
setPaymentStatus().completed();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( currentStatus.hasFailed ) {
|
|
|
|
emitEvent( currentObservers.current, EMIT_TYPES.PAYMENT_FAIL, {} );
|
|
|
|
}
|
|
|
|
if ( currentStatus.hasError ) {
|
|
|
|
emitEvent( currentObservers.current, EMIT_TYPES.PAYMENT_ERROR, {} );
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
currentStatus,
|
|
|
|
setValidationErrors,
|
|
|
|
setPaymentStatus,
|
|
|
|
checkoutIsComplete,
|
|
|
|
checkoutHasError,
|
|
|
|
] );
|
2020-03-30 12:07:49 +00:00
|
|
|
|
2020-03-10 13:39:21 +00:00
|
|
|
/**
|
|
|
|
* @type {PaymentMethodDataContext}
|
|
|
|
*/
|
|
|
|
const paymentData = {
|
|
|
|
setPaymentStatus,
|
|
|
|
currentStatus,
|
|
|
|
paymentStatuses: STATUS,
|
|
|
|
paymentMethodData: paymentStatus.paymentMethodData,
|
|
|
|
errorMessage: paymentStatus.errorMessage,
|
|
|
|
activePaymentMethod,
|
|
|
|
setActivePaymentMethod,
|
2020-04-03 11:50:54 +00:00
|
|
|
onPaymentProcessing,
|
|
|
|
onPaymentSuccess,
|
|
|
|
onPaymentFail,
|
|
|
|
onPaymentError,
|
2020-03-26 11:11:46 +00:00
|
|
|
customerPaymentMethods,
|
|
|
|
paymentMethods: paymentStatus.paymentMethods,
|
|
|
|
expressPaymentMethods: paymentStatus.expressPaymentMethods,
|
|
|
|
paymentMethodsInitialized,
|
|
|
|
expressPaymentMethodsInitialized,
|
2020-04-06 12:18:35 +00:00
|
|
|
setExpressPaymentError,
|
2020-03-10 13:39:21 +00:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<PaymentMethodDataContext.Provider value={ paymentData }>
|
|
|
|
{ children }
|
|
|
|
</PaymentMethodDataContext.Provider>
|
|
|
|
);
|
|
|
|
};
|