2020-03-30 12:07:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-04-15 15:15:56 +00:00
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
2020-03-30 12:07:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-14 23:35:17 +00:00
|
|
|
import { getStripeServerData, loadStripe } from '../stripe-utils';
|
2020-04-29 10:57:58 +00:00
|
|
|
import { StripeCreditCard, getStripeCreditCardIcons } from './payment-method';
|
2020-04-02 17:04:15 +00:00
|
|
|
import { PAYMENT_METHOD_NAME } from './constants';
|
2020-03-30 12:07:49 +00:00
|
|
|
|
2020-04-08 16:36:04 +00:00
|
|
|
const stripePromise = loadStripe();
|
|
|
|
|
2020-04-15 15:15:56 +00:00
|
|
|
const StripeComponent = ( props ) => {
|
|
|
|
const [ errorMessage, setErrorMessage ] = useState( '' );
|
2020-05-13 15:48:03 +00:00
|
|
|
|
2020-04-15 15:15:56 +00:00
|
|
|
useEffect( () => {
|
|
|
|
Promise.resolve( stripePromise ).then( ( { error } ) => {
|
|
|
|
if ( error ) {
|
|
|
|
setErrorMessage( error.message );
|
|
|
|
}
|
|
|
|
} );
|
2020-05-14 23:35:17 +00:00
|
|
|
}, [ setErrorMessage ] );
|
2020-04-15 15:15:56 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( errorMessage ) {
|
|
|
|
throw new Error( errorMessage );
|
|
|
|
}
|
|
|
|
}, [ errorMessage ] );
|
|
|
|
|
2020-04-14 06:01:29 +00:00
|
|
|
return <StripeCreditCard stripe={ stripePromise } { ...props } />;
|
|
|
|
};
|
|
|
|
|
2020-05-13 15:48:03 +00:00
|
|
|
const StripeLabel = ( props ) => {
|
|
|
|
const { PaymentMethodLabel } = props.components;
|
2020-04-29 10:57:58 +00:00
|
|
|
|
2020-12-22 19:52:37 +00:00
|
|
|
const labelText = getStripeServerData().title
|
|
|
|
? getStripeServerData().title
|
|
|
|
: __( 'Credit / Debit Card', 'woo-gutenberg-products-block' );
|
|
|
|
|
|
|
|
return <PaymentMethodLabel text={ labelText } />;
|
2020-04-29 10:57:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const cardIcons = getStripeCreditCardIcons();
|
2020-04-02 17:04:15 +00:00
|
|
|
const stripeCcPaymentMethod = {
|
2020-04-09 15:22:34 +00:00
|
|
|
name: PAYMENT_METHOD_NAME,
|
2020-04-29 10:57:58 +00:00
|
|
|
label: <StripeLabel />,
|
2020-04-15 15:15:56 +00:00
|
|
|
content: <StripeComponent />,
|
|
|
|
edit: <StripeComponent />,
|
2020-04-29 10:57:58 +00:00
|
|
|
icons: cardIcons,
|
2020-04-09 11:44:29 +00:00
|
|
|
canMakePayment: () => stripePromise,
|
2020-03-30 12:07:49 +00:00
|
|
|
ariaLabel: __(
|
|
|
|
'Stripe Credit Card payment method',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
2020-05-12 15:12:28 +00:00
|
|
|
supports: {
|
2021-01-24 13:59:13 +00:00
|
|
|
showSavedCards: getStripeServerData().showSavedCards,
|
|
|
|
showSaveOption: getStripeServerData().showSaveOption,
|
2020-05-12 15:12:28 +00:00
|
|
|
},
|
2020-03-30 12:07:49 +00:00
|
|
|
};
|
2020-04-02 17:04:15 +00:00
|
|
|
|
|
|
|
export default stripeCcPaymentMethod;
|