2020-06-26 12:01:35 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Elements, useStripe } from '@stripe/react-stripe-js';
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
|
2020-03-30 12:07:49 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-04-08 16:36:04 +00:00
|
|
|
import { getStripeServerData } from '../stripe-utils';
|
2020-04-01 14:58:50 +00:00
|
|
|
import { useCheckoutSubscriptions } from './use-checkout-subscriptions';
|
|
|
|
import { InlineCard, CardElements } from './elements';
|
2020-03-30 12:07:49 +00:00
|
|
|
|
|
|
|
/**
|
2020-04-02 17:04:15 +00:00
|
|
|
* @typedef {import('../stripe-utils/type-defs').Stripe} Stripe
|
|
|
|
* @typedef {import('../stripe-utils/type-defs').StripePaymentRequest} StripePaymentRequest
|
2021-12-07 13:02:57 +00:00
|
|
|
* @typedef {import('@woocommerce/type-defs/payment-method-interface').PaymentMethodInterface} RegisteredPaymentMethodProps
|
2020-03-30 12:07:49 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-13 15:48:03 +00:00
|
|
|
export const getStripeCreditCardIcons = () => {
|
|
|
|
return Object.entries( getStripeServerData().icons ).map(
|
|
|
|
( [ id, { src, alt } ] ) => {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
src,
|
|
|
|
alt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-30 12:07:49 +00:00
|
|
|
/**
|
|
|
|
* Stripe Credit Card component
|
|
|
|
*
|
|
|
|
* @param {RegisteredPaymentMethodProps} props Incoming props
|
|
|
|
*/
|
2020-04-14 16:52:23 +00:00
|
|
|
const CreditCardComponent = ( {
|
|
|
|
billing,
|
|
|
|
eventRegistration,
|
|
|
|
emitResponse,
|
|
|
|
components,
|
|
|
|
} ) => {
|
2020-05-13 15:48:03 +00:00
|
|
|
const { ValidationInputError, PaymentMethodIcons } = components;
|
2020-05-08 15:32:20 +00:00
|
|
|
const [ sourceId, setSourceId ] = useState( '' );
|
2020-03-30 12:07:49 +00:00
|
|
|
const stripe = useStripe();
|
2020-04-01 14:58:50 +00:00
|
|
|
const onStripeError = useCheckoutSubscriptions(
|
2020-03-30 12:07:49 +00:00
|
|
|
eventRegistration,
|
|
|
|
billing,
|
|
|
|
sourceId,
|
2020-03-31 13:21:54 +00:00
|
|
|
setSourceId,
|
2020-04-14 16:52:23 +00:00
|
|
|
emitResponse,
|
2020-05-08 15:32:20 +00:00
|
|
|
stripe
|
2020-03-30 12:07:49 +00:00
|
|
|
);
|
|
|
|
const onChange = ( paymentEvent ) => {
|
|
|
|
if ( paymentEvent.error ) {
|
|
|
|
onStripeError( paymentEvent );
|
|
|
|
}
|
2020-05-10 23:41:10 +00:00
|
|
|
setSourceId( '0' );
|
2020-03-30 12:07:49 +00:00
|
|
|
};
|
2020-05-13 15:48:03 +00:00
|
|
|
const cardIcons = getStripeCreditCardIcons();
|
|
|
|
|
2020-03-30 12:07:49 +00:00
|
|
|
const renderedCardElement = getStripeServerData().inline_cc_form ? (
|
|
|
|
<InlineCard
|
|
|
|
onChange={ onChange }
|
|
|
|
inputErrorComponent={ ValidationInputError }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<CardElements
|
|
|
|
onChange={ onChange }
|
|
|
|
inputErrorComponent={ ValidationInputError }
|
|
|
|
/>
|
|
|
|
);
|
2020-05-13 15:48:03 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ renderedCardElement }
|
|
|
|
{ PaymentMethodIcons && cardIcons.length && (
|
|
|
|
<PaymentMethodIcons icons={ cardIcons } align="left" />
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
2020-03-30 12:07:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const StripeCreditCard = ( props ) => {
|
|
|
|
const { locale } = getStripeServerData().button;
|
2020-10-05 13:25:40 +00:00
|
|
|
const { stripe } = props;
|
2020-03-30 12:07:49 +00:00
|
|
|
|
2020-10-05 13:25:40 +00:00
|
|
|
return (
|
2020-04-08 16:36:04 +00:00
|
|
|
<Elements stripe={ stripe } locale={ locale }>
|
2020-03-30 12:07:49 +00:00
|
|
|
<CreditCardComponent { ...props } />
|
|
|
|
</Elements>
|
2020-10-05 13:25:40 +00:00
|
|
|
);
|
2020-03-30 12:07:49 +00:00
|
|
|
};
|