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-04-08 16:36:04 +00:00
|
|
|
import { 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( '' );
|
|
|
|
useEffect( () => {
|
|
|
|
Promise.resolve( stripePromise ).then( ( { error } ) => {
|
|
|
|
if ( error ) {
|
|
|
|
setErrorMessage( error.message );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}, [ stripePromise, setErrorMessage ] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( errorMessage ) {
|
|
|
|
throw new Error( errorMessage );
|
|
|
|
}
|
|
|
|
}, [ errorMessage ] );
|
|
|
|
|
2020-04-14 06:01:29 +00:00
|
|
|
return <StripeCreditCard stripe={ stripePromise } { ...props } />;
|
|
|
|
};
|
|
|
|
|
2020-04-29 10:57:58 +00:00
|
|
|
const StripeLabel = ( { components = {} } ) => {
|
|
|
|
const { PaymentMethodIcons } = components;
|
|
|
|
|
|
|
|
if ( ! PaymentMethodIcons || cardIcons.length === 0 ) {
|
|
|
|
return __( 'Credit/Debit Card', 'woo-gutenberg-products-block' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return <PaymentMethodIcons icons={ cardIcons } />;
|
|
|
|
};
|
|
|
|
|
|
|
|
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: {
|
|
|
|
savePaymentInfo: true,
|
|
|
|
},
|
2020-03-30 12:07:49 +00:00
|
|
|
};
|
2020-04-02 17:04:15 +00:00
|
|
|
|
|
|
|
export default stripeCcPaymentMethod;
|