2020-04-01 14:58:50 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
CardElement,
|
|
|
|
CardNumberElement,
|
|
|
|
CardExpiryElement,
|
|
|
|
CardCvcElement,
|
|
|
|
} from '@stripe/react-stripe-js';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useElementOptions } from './use-element-options';
|
|
|
|
|
|
|
|
const baseTextInputStyles = 'wc-block-gateway-input';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InlineCard component
|
|
|
|
*/
|
|
|
|
export const InlineCard = ( {
|
|
|
|
inputErrorComponent: ValidationInputError,
|
|
|
|
onChange,
|
|
|
|
} ) => {
|
|
|
|
const [ isEmpty, setIsEmpty ] = useState( true );
|
|
|
|
const { options, onActive, error, setError } = useElementOptions( {
|
|
|
|
hidePostalCode: true,
|
|
|
|
} );
|
|
|
|
const errorCallback = ( event ) => {
|
|
|
|
if ( event.error ) {
|
|
|
|
setError( event.error.message );
|
|
|
|
} else {
|
|
|
|
setError( '' );
|
|
|
|
}
|
|
|
|
setIsEmpty( event.empty );
|
|
|
|
onChange( event );
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="wc-block-gateway-container wc-inline-card-element">
|
|
|
|
<CardElement
|
|
|
|
id="wc-stripe-inline-card-element"
|
|
|
|
className={ baseTextInputStyles }
|
|
|
|
options={ options }
|
|
|
|
onBlur={ () => onActive( isEmpty ) }
|
|
|
|
onFocus={ () => onActive( isEmpty ) }
|
|
|
|
onChange={ errorCallback }
|
|
|
|
/>
|
|
|
|
<label htmlFor="wc-stripe-inline-card-element">
|
|
|
|
{ __(
|
|
|
|
'Credit Card Information',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<ValidationInputError errorMessage={ error } />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CardElements component.
|
|
|
|
*/
|
|
|
|
export const CardElements = ( {
|
|
|
|
onChange,
|
|
|
|
inputErrorComponent: ValidationInputError,
|
|
|
|
} ) => {
|
2020-05-06 18:58:31 +00:00
|
|
|
const [ isEmpty, setIsEmpty ] = useState( {
|
|
|
|
cardNumber: true,
|
|
|
|
cardExpiry: true,
|
|
|
|
cardCvc: true,
|
|
|
|
} );
|
2020-04-01 14:58:50 +00:00
|
|
|
const {
|
|
|
|
options: cardNumOptions,
|
|
|
|
onActive: cardNumOnActive,
|
|
|
|
error: cardNumError,
|
|
|
|
setError: cardNumSetError,
|
|
|
|
} = useElementOptions( { showIcon: false } );
|
|
|
|
const {
|
|
|
|
options: cardExpiryOptions,
|
|
|
|
onActive: cardExpiryOnActive,
|
|
|
|
error: cardExpiryError,
|
|
|
|
setError: cardExpirySetError,
|
|
|
|
} = useElementOptions();
|
|
|
|
const {
|
|
|
|
options: cardCvcOptions,
|
|
|
|
onActive: cardCvcOnActive,
|
|
|
|
error: cardCvcError,
|
|
|
|
setError: cardCvcSetError,
|
|
|
|
} = useElementOptions();
|
2020-05-06 18:58:31 +00:00
|
|
|
const errorCallback = ( errorSetter, elementId ) => ( event ) => {
|
2020-04-01 14:58:50 +00:00
|
|
|
if ( event.error ) {
|
|
|
|
errorSetter( event.error.message );
|
|
|
|
} else {
|
|
|
|
errorSetter( '' );
|
|
|
|
}
|
2020-05-06 18:58:31 +00:00
|
|
|
setIsEmpty( { ...isEmpty, [ elementId ]: event.empty } );
|
2020-04-01 14:58:50 +00:00
|
|
|
onChange( event );
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<div className="wc-block-card-elements">
|
|
|
|
<div className="wc-block-gateway-container wc-card-number-element">
|
|
|
|
<CardNumberElement
|
2020-05-06 18:58:31 +00:00
|
|
|
onChange={ errorCallback( cardNumSetError, 'cardNumber' ) }
|
2020-04-01 14:58:50 +00:00
|
|
|
options={ cardNumOptions }
|
|
|
|
className={ baseTextInputStyles }
|
|
|
|
id="wc-stripe-card-number-element"
|
2020-05-06 18:58:31 +00:00
|
|
|
onFocus={ () => cardNumOnActive( isEmpty.cardNumber ) }
|
|
|
|
onBlur={ () => cardNumOnActive( isEmpty.cardNumber ) }
|
2020-04-01 14:58:50 +00:00
|
|
|
/>
|
|
|
|
<label htmlFor="wc-stripe-card-number-element">
|
|
|
|
{ __( 'Card Number', 'woo-gutenberg-product-blocks' ) }
|
|
|
|
</label>
|
|
|
|
<ValidationInputError errorMessage={ cardNumError } />
|
|
|
|
</div>
|
|
|
|
<div className="wc-block-gateway-container wc-card-expiry-element">
|
|
|
|
<CardExpiryElement
|
2020-05-06 18:58:31 +00:00
|
|
|
onChange={ errorCallback(
|
|
|
|
cardExpirySetError,
|
|
|
|
'cardExpiry'
|
|
|
|
) }
|
2020-04-01 14:58:50 +00:00
|
|
|
options={ cardExpiryOptions }
|
|
|
|
className={ baseTextInputStyles }
|
2020-05-06 18:58:31 +00:00
|
|
|
onFocus={ () => cardExpiryOnActive( isEmpty.cardExpiry ) }
|
|
|
|
onBlur={ () => cardExpiryOnActive( isEmpty.cardExpiry ) }
|
2020-04-01 14:58:50 +00:00
|
|
|
id="wc-stripe-card-expiry-element"
|
|
|
|
/>
|
|
|
|
<label htmlFor="wc-stripe-card-expiry-element">
|
|
|
|
{ __( 'Expiry Date', 'woo-gutenberg-product-blocks' ) }
|
|
|
|
</label>
|
|
|
|
<ValidationInputError errorMessage={ cardExpiryError } />
|
|
|
|
</div>
|
|
|
|
<div className="wc-block-gateway-container wc-card-cvc-element">
|
|
|
|
<CardCvcElement
|
2020-05-06 18:58:31 +00:00
|
|
|
onChange={ errorCallback( cardCvcSetError, 'cardCvc' ) }
|
2020-04-01 14:58:50 +00:00
|
|
|
options={ cardCvcOptions }
|
|
|
|
className={ baseTextInputStyles }
|
2020-05-06 18:58:31 +00:00
|
|
|
onFocus={ () => cardCvcOnActive( isEmpty.cardCvc ) }
|
|
|
|
onBlur={ () => cardCvcOnActive( isEmpty.cardCvc ) }
|
2020-04-01 14:58:50 +00:00
|
|
|
id="wc-stripe-card-code-element"
|
|
|
|
/>
|
|
|
|
<label htmlFor="wc-stripe-card-code-element">
|
|
|
|
{ __( 'CVV/CVC', 'woo-gutenberg-product-blocks' ) }
|
|
|
|
</label>
|
|
|
|
<ValidationInputError errorMessage={ cardCvcError } />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|