2020-04-01 14:58:50 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-05-08 15:32:20 +00:00
|
|
|
import { useEffect, useCallback, useState } from '@wordpress/element';
|
2020-04-01 14:58:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-08 15:32:20 +00:00
|
|
|
import { getErrorMessageForTypeAndCode } from '../stripe-utils';
|
2020-04-14 16:52:23 +00:00
|
|
|
import { usePaymentIntents } from './use-payment-intents';
|
2020-05-08 15:32:20 +00:00
|
|
|
import { usePaymentProcessing } from './use-payment-processing';
|
2020-04-01 14:58:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/registered-payment-method-props').EventRegistrationProps} EventRegistrationProps
|
|
|
|
* @typedef {import('@woocommerce/type-defs/registered-payment-method-props').BillingDataProps} BillingDataProps
|
2020-04-14 16:52:23 +00:00
|
|
|
* @typedef {import('@woocommerce/type-defs/registered-payment-method-props').EmitResponseProps} EmitResponseProps
|
2020-04-02 17:04:15 +00:00
|
|
|
* @typedef {import('../stripe-utils/type-defs').Stripe} Stripe
|
2020-05-08 15:32:20 +00:00
|
|
|
* @typedef {import('react').Dispatch<string>} SourceIdDispatch
|
2020-04-01 14:58:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom hook for the Stripe processing and event observer logic.
|
|
|
|
*
|
2020-05-08 15:32:20 +00:00
|
|
|
* @param {EventRegistrationProps} eventRegistration Event registration functions.
|
|
|
|
* @param {BillingDataProps} billing Various billing data items.
|
|
|
|
* @param {string} sourceId Current set stripe source id.
|
|
|
|
* @param {SourceIdDispatch} setSourceId Setter for stripe source id.
|
|
|
|
* @param {EmitResponseProps} emitResponse Various helpers for usage with observer
|
|
|
|
* response objects.
|
|
|
|
* @param {Stripe} stripe The stripe.js object.
|
2020-04-01 14:58:50 +00:00
|
|
|
*
|
|
|
|
* @return {function(Object):Object} Returns a function for handling stripe error.
|
|
|
|
*/
|
|
|
|
export const useCheckoutSubscriptions = (
|
|
|
|
eventRegistration,
|
|
|
|
billing,
|
|
|
|
sourceId,
|
|
|
|
setSourceId,
|
2020-04-14 16:52:23 +00:00
|
|
|
emitResponse,
|
2020-05-08 15:32:20 +00:00
|
|
|
stripe
|
2020-04-01 14:58:50 +00:00
|
|
|
) => {
|
2020-04-06 12:18:35 +00:00
|
|
|
const [ error, setError ] = useState( '' );
|
2020-05-08 15:32:20 +00:00
|
|
|
const onStripeError = useCallback( ( event ) => {
|
|
|
|
const type = event.error.type;
|
|
|
|
const code = event.error.code || '';
|
|
|
|
const message =
|
|
|
|
getErrorMessageForTypeAndCode( type, code ) ?? event.error.message;
|
|
|
|
setError( message );
|
|
|
|
return message;
|
|
|
|
}, [] );
|
|
|
|
const {
|
|
|
|
onCheckoutAfterProcessingWithSuccess,
|
|
|
|
onPaymentProcessing,
|
|
|
|
onCheckoutAfterProcessingWithError,
|
|
|
|
} = eventRegistration;
|
2020-04-14 16:52:23 +00:00
|
|
|
usePaymentIntents(
|
|
|
|
stripe,
|
2020-05-08 15:32:20 +00:00
|
|
|
onCheckoutAfterProcessingWithSuccess,
|
2020-10-20 09:50:33 +00:00
|
|
|
setSourceId,
|
2020-04-14 16:52:23 +00:00
|
|
|
emitResponse
|
|
|
|
);
|
2020-05-08 15:32:20 +00:00
|
|
|
usePaymentProcessing(
|
|
|
|
onStripeError,
|
|
|
|
error,
|
|
|
|
stripe,
|
|
|
|
billing,
|
|
|
|
emitResponse,
|
|
|
|
sourceId,
|
|
|
|
setSourceId,
|
|
|
|
onPaymentProcessing
|
|
|
|
);
|
2020-04-01 14:58:50 +00:00
|
|
|
// hook into and register callbacks for events.
|
|
|
|
useEffect( () => {
|
2020-04-14 16:52:23 +00:00
|
|
|
const onError = ( { processingResponse } ) => {
|
|
|
|
if ( processingResponse?.paymentDetails?.errorMessage ) {
|
|
|
|
return {
|
|
|
|
type: emitResponse.responseTypes.ERROR,
|
|
|
|
message: processingResponse.paymentDetails.errorMessage,
|
|
|
|
messageContext: emitResponse.noticeContexts.PAYMENTS,
|
2020-04-06 12:18:35 +00:00
|
|
|
};
|
2020-04-01 14:58:50 +00:00
|
|
|
}
|
2020-04-17 20:18:54 +00:00
|
|
|
// so we don't break the observers.
|
|
|
|
return true;
|
2020-04-01 14:58:50 +00:00
|
|
|
};
|
2020-05-08 15:32:20 +00:00
|
|
|
const unsubscribeAfterProcessing = onCheckoutAfterProcessingWithError(
|
2020-04-14 16:52:23 +00:00
|
|
|
onError
|
|
|
|
);
|
2020-04-01 14:58:50 +00:00
|
|
|
return () => {
|
2020-04-14 16:52:23 +00:00
|
|
|
unsubscribeAfterProcessing();
|
2020-04-01 14:58:50 +00:00
|
|
|
};
|
|
|
|
}, [
|
2020-05-08 15:32:20 +00:00
|
|
|
onCheckoutAfterProcessingWithError,
|
|
|
|
emitResponse.noticeContexts.PAYMENTS,
|
|
|
|
emitResponse.responseTypes.ERROR,
|
2020-04-01 14:58:50 +00:00
|
|
|
] );
|
2020-05-08 15:32:20 +00:00
|
|
|
return onStripeError;
|
2020-04-01 14:58:50 +00:00
|
|
|
};
|