2020-10-05 11:59:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
usePaymentMethods,
|
|
|
|
usePaymentMethodInterface,
|
|
|
|
useEmitResponse,
|
2021-04-08 12:31:12 +00:00
|
|
|
useStoreNotices,
|
|
|
|
} from '@woocommerce/base-context/hooks';
|
2020-10-05 11:59:20 +00:00
|
|
|
import { cloneElement } from '@wordpress/element';
|
|
|
|
import {
|
|
|
|
useEditorContext,
|
|
|
|
usePaymentMethodDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
2021-02-02 04:51:47 +00:00
|
|
|
import classNames from 'classnames';
|
2021-02-19 15:16:39 +00:00
|
|
|
import RadioControlAccordion from '@woocommerce/base-components/radio-control-accordion';
|
2020-10-05 11:59:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-02-02 04:51:47 +00:00
|
|
|
import PaymentMethodCard from './payment-method-card';
|
2020-10-05 11:59:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component used to render all non-saved payment method options.
|
|
|
|
*
|
|
|
|
* @return {*} The rendered component.
|
|
|
|
*/
|
|
|
|
const PaymentMethodOptions = () => {
|
2020-11-20 17:45:12 +00:00
|
|
|
const {
|
|
|
|
setActivePaymentMethod,
|
2021-02-02 04:51:47 +00:00
|
|
|
activeSavedToken,
|
|
|
|
setActiveSavedToken,
|
2021-06-16 12:44:40 +00:00
|
|
|
isExpressPaymentMethodActive,
|
2021-02-02 04:51:47 +00:00
|
|
|
customerPaymentMethods,
|
2020-11-20 17:45:12 +00:00
|
|
|
} = usePaymentMethodDataContext();
|
2020-10-05 11:59:20 +00:00
|
|
|
const { paymentMethods } = usePaymentMethods();
|
|
|
|
const {
|
|
|
|
activePaymentMethod,
|
|
|
|
...paymentMethodInterface
|
|
|
|
} = usePaymentMethodInterface();
|
|
|
|
const { noticeContexts } = useEmitResponse();
|
|
|
|
const { removeNotice } = useStoreNotices();
|
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
|
2021-02-02 04:51:47 +00:00
|
|
|
const options = Object.keys( paymentMethods ).map( ( name ) => {
|
|
|
|
const { edit, content, label, supports } = paymentMethods[ name ];
|
|
|
|
const component = isEditor ? edit : content;
|
|
|
|
return {
|
|
|
|
value: name,
|
|
|
|
label:
|
|
|
|
typeof label === 'string'
|
|
|
|
? label
|
|
|
|
: cloneElement( label, {
|
|
|
|
components: paymentMethodInterface.components,
|
|
|
|
} ),
|
|
|
|
name: `wc-saved-payment-method-token-${ name }`,
|
|
|
|
content: (
|
2021-03-11 20:40:18 +00:00
|
|
|
<PaymentMethodCard showSaveOption={ supports.showSaveOption }>
|
2021-02-02 04:51:47 +00:00
|
|
|
{ cloneElement( component, {
|
|
|
|
activePaymentMethod,
|
|
|
|
...paymentMethodInterface,
|
|
|
|
} ) }
|
|
|
|
</PaymentMethodCard>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const updateToken = ( value ) => {
|
|
|
|
setActivePaymentMethod( value );
|
|
|
|
setActiveSavedToken( '' );
|
|
|
|
removeNotice( 'wc-payment-error', noticeContexts.PAYMENTS );
|
|
|
|
};
|
|
|
|
|
|
|
|
const isSinglePaymentMethod =
|
|
|
|
Object.keys( customerPaymentMethods ).length === 0 &&
|
|
|
|
Object.keys( paymentMethods ).length === 1;
|
|
|
|
|
|
|
|
const singleOptionClass = classNames( {
|
|
|
|
'disable-radio-control': isSinglePaymentMethod,
|
|
|
|
} );
|
|
|
|
|
2021-06-16 12:44:40 +00:00
|
|
|
return isExpressPaymentMethodActive ? null : (
|
2021-02-02 04:51:47 +00:00
|
|
|
<RadioControlAccordion
|
|
|
|
id={ 'wc-payment-method-options' }
|
|
|
|
className={ singleOptionClass }
|
|
|
|
selected={ activeSavedToken ? null : activePaymentMethod }
|
|
|
|
onChange={ updateToken }
|
|
|
|
options={ options }
|
2020-10-05 11:59:20 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentMethodOptions;
|