2020-03-30 12:07:49 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PAYMENT_METHOD_NAME } from './constants';
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-05-08 15:32:20 +00:00
|
|
|
import { Elements, useStripe } from '@stripe/react-stripe-js';
|
2020-04-01 14:58:50 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
2020-03-30 12:07:49 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
|
|
|
/**
|
2020-04-02 17:04:15 +00:00
|
|
|
* @typedef {import('../stripe-utils/type-defs').Stripe} Stripe
|
|
|
|
* @typedef {import('../stripe-utils/type-defs').StripePaymentRequest} StripePaymentRequest
|
2020-03-30 12:07:49 +00:00
|
|
|
* @typedef {import('@woocommerce/type-defs/registered-payment-method-props').RegisteredPaymentMethodProps} RegisteredPaymentMethodProps
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stripe Credit Card component
|
|
|
|
*
|
|
|
|
* @param {RegisteredPaymentMethodProps} props Incoming props
|
|
|
|
*/
|
2020-04-14 16:52:23 +00:00
|
|
|
const CreditCardComponent = ( {
|
|
|
|
billing,
|
|
|
|
eventRegistration,
|
|
|
|
emitResponse,
|
|
|
|
components,
|
|
|
|
} ) => {
|
2020-03-30 12:07:49 +00:00
|
|
|
const { ValidationInputError, CheckboxControl } = components;
|
2020-04-07 14:29:59 +00:00
|
|
|
const { customerId } = billing;
|
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-07 14:29:59 +00:00
|
|
|
const [ shouldSavePayment, setShouldSavePayment ] = useState(
|
|
|
|
customerId ? true : false
|
|
|
|
);
|
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-03-30 12:07:49 +00:00
|
|
|
shouldSavePayment,
|
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 );
|
|
|
|
}
|
|
|
|
setSourceId( 0 );
|
|
|
|
};
|
|
|
|
const renderedCardElement = getStripeServerData().inline_cc_form ? (
|
|
|
|
<InlineCard
|
|
|
|
onChange={ onChange }
|
|
|
|
inputErrorComponent={ ValidationInputError }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<CardElements
|
|
|
|
onChange={ onChange }
|
|
|
|
inputErrorComponent={ ValidationInputError }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ renderedCardElement }
|
2020-04-07 14:29:59 +00:00
|
|
|
{ customerId > 0 && (
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__save-card-info"
|
|
|
|
label={ __(
|
|
|
|
'Save payment information to my account for future purchases.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ shouldSavePayment }
|
|
|
|
onChange={ () =>
|
|
|
|
setShouldSavePayment( ! shouldSavePayment )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
2020-03-30 12:07:49 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const StripeCreditCard = ( props ) => {
|
|
|
|
const { locale } = getStripeServerData().button;
|
2020-04-08 16:36:04 +00:00
|
|
|
const { activePaymentMethod, stripe } = props;
|
2020-03-30 12:07:49 +00:00
|
|
|
|
|
|
|
return activePaymentMethod === PAYMENT_METHOD_NAME ? (
|
2020-04-08 16:36:04 +00:00
|
|
|
<Elements stripe={ stripe } locale={ locale }>
|
2020-03-30 12:07:49 +00:00
|
|
|
<CreditCardComponent { ...props } />
|
|
|
|
</Elements>
|
|
|
|
) : null;
|
|
|
|
};
|
2020-04-29 10:57:58 +00:00
|
|
|
|
|
|
|
export const getStripeCreditCardIcons = () => {
|
|
|
|
return Object.entries( getStripeServerData().icons ).map(
|
|
|
|
( [ id, { src, alt } ] ) => {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
src,
|
|
|
|
alt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|