2020-04-14 16:52:23 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/registered-payment-method-props').EmitResponseProps} EmitResponseProps
|
|
|
|
* @typedef {import('../stripe-utils/type-defs').Stripe} Stripe
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the modal for PaymentIntent authorizations.
|
|
|
|
*
|
2020-10-20 09:50:33 +00:00
|
|
|
* @param {Object} params Params object.
|
|
|
|
* @param {Stripe} params.stripe The stripe object.
|
|
|
|
* @param {Object} params.paymentDetails The payment details from the
|
|
|
|
* server after checkout processing.
|
|
|
|
* @param {string} params.errorContext Context where errors will be added.
|
|
|
|
* @param {string} params.errorType Type of error responses.
|
|
|
|
* @param {string} params.successType Type of success responses.
|
2020-04-14 16:52:23 +00:00
|
|
|
*/
|
2020-10-20 09:50:33 +00:00
|
|
|
const openIntentModal = ( {
|
|
|
|
stripe,
|
|
|
|
paymentDetails,
|
|
|
|
errorContext,
|
|
|
|
errorType,
|
|
|
|
successType,
|
|
|
|
} ) => {
|
|
|
|
const checkoutResponse = { type: successType };
|
2020-04-14 16:52:23 +00:00
|
|
|
if (
|
|
|
|
! paymentDetails.setup_intent &&
|
|
|
|
! paymentDetails.payment_intent_secret
|
|
|
|
) {
|
|
|
|
return checkoutResponse;
|
|
|
|
}
|
|
|
|
const isSetupIntent = !! paymentDetails.setupIntent;
|
|
|
|
const verificationUrl = paymentDetails.verification_endpoint;
|
|
|
|
const intentSecret = isSetupIntent
|
|
|
|
? paymentDetails.setup_intent
|
|
|
|
: paymentDetails.payment_intent_secret;
|
|
|
|
return stripe[ isSetupIntent ? 'confirmCardSetup' : 'confirmCardPayment' ](
|
|
|
|
intentSecret
|
|
|
|
)
|
2020-09-07 17:31:10 +00:00
|
|
|
.then( function ( response ) {
|
2020-04-14 16:52:23 +00:00
|
|
|
if ( response.error ) {
|
|
|
|
throw response.error;
|
|
|
|
}
|
|
|
|
const intent =
|
|
|
|
response[ isSetupIntent ? 'setupIntent' : 'paymentIntent' ];
|
|
|
|
if (
|
|
|
|
intent.status !== 'requires_capture' &&
|
|
|
|
intent.status !== 'succeeded'
|
|
|
|
) {
|
|
|
|
return checkoutResponse;
|
|
|
|
}
|
|
|
|
checkoutResponse.redirectUrl = verificationUrl;
|
|
|
|
return checkoutResponse;
|
|
|
|
} )
|
2020-09-07 17:31:10 +00:00
|
|
|
.catch( function ( error ) {
|
2020-10-20 09:50:33 +00:00
|
|
|
checkoutResponse.type = errorType;
|
2020-04-14 16:52:23 +00:00
|
|
|
checkoutResponse.message = error.message;
|
|
|
|
checkoutResponse.retry = true;
|
2020-10-20 09:50:33 +00:00
|
|
|
checkoutResponse.messageContext = errorContext;
|
2020-04-14 16:52:23 +00:00
|
|
|
// Reports back to the server.
|
|
|
|
window.fetch( verificationUrl + '&is_ajax' );
|
|
|
|
return checkoutResponse;
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-10-20 09:50:33 +00:00
|
|
|
export const usePaymentIntents = (
|
|
|
|
stripe,
|
|
|
|
subscriber,
|
|
|
|
setSourceId,
|
|
|
|
emitResponse
|
|
|
|
) => {
|
2020-04-14 16:52:23 +00:00
|
|
|
useEffect( () => {
|
2020-10-20 09:50:33 +00:00
|
|
|
const unsubscribe = subscriber( async ( { processingResponse } ) => {
|
2020-04-14 16:52:23 +00:00
|
|
|
const paymentDetails = processingResponse.paymentDetails || {};
|
2020-10-20 09:50:33 +00:00
|
|
|
const response = await openIntentModal( {
|
|
|
|
stripe,
|
|
|
|
paymentDetails,
|
|
|
|
errorContext: emitResponse.noticeContexts.PAYMENTS,
|
|
|
|
errorType: emitResponse.responseTypes.ERROR,
|
|
|
|
successType: emitResponse.responseTypes.SUCCESS,
|
|
|
|
} );
|
|
|
|
if (
|
|
|
|
response.type === emitResponse.responseTypes.ERROR &&
|
|
|
|
response.retry
|
|
|
|
) {
|
|
|
|
setSourceId( '0' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2020-04-14 16:52:23 +00:00
|
|
|
} );
|
|
|
|
return () => unsubscribe();
|
2020-10-20 09:50:33 +00:00
|
|
|
}, [
|
|
|
|
subscriber,
|
|
|
|
emitResponse.noticeContexts.PAYMENTS,
|
|
|
|
emitResponse.responseTypes.ERROR,
|
|
|
|
emitResponse.responseTypes.SUCCESS,
|
|
|
|
setSourceId,
|
|
|
|
stripe,
|
|
|
|
] );
|
2020-04-14 16:52:23 +00:00
|
|
|
};
|