All files / app/payment/paymentMethod AdyenV2PaymentMethod.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178  24x     24x 24x   24x 24x                   24x 24x 24x 24x                 24x 6x 6x 6x   6x         6x 6x 6x 6x 6x 6x 6x 6x                       6x 2x   2x 1x   1x 1x   1x       6x         6x 1x     6x 1x 1x       6x 1x   1x 1x 1x       6x 4x   4x                                               6x               6x 6x             6x       6x                                                                 24x  
import { AdyenCreditCardComponentOptions, AdyenIdealComponentOptions  } from '@bigcommerce/checkout-sdk';
import React, { createRef, useCallback, useRef, useState, FunctionComponent, RefObject } from 'react';
import { Omit } from 'utility-types';
 
import { TranslatedString } from '../../locale';
import { Modal } from '../../ui/modal';
 
import AdyenV2CardValidation from './AdyenV2CardValidation';
import HostedWidgetPaymentMethod, { HostedWidgetPaymentMethodProps } from './HostedWidgetPaymentMethod';
 
export type AdyenPaymentMethodProps = Omit<HostedWidgetPaymentMethodProps, 'containerId' | 'hideContentWhenSignedOut'>;
 
export interface AdyenOptions {
    scheme: AdyenCreditCardComponentOptions;
    bcmc: AdyenCreditCardComponentOptions;
    ideal: AdyenIdealComponentOptions;
}
 
export enum AdyenV2PaymentMethodType {
    scheme = 'scheme',
    bcmc = 'bcmc',
    ideal = 'ideal',
}
 
interface AdyenPaymentMethodRef {
    additionalActionContentRef: RefObject<HTMLDivElement>;
    shouldShowModal: boolean;
    cancelAdditionalAction?(): void;
}
 
const AdyenV2PaymentMethod: FunctionComponent<AdyenPaymentMethodProps> = ({
    initializePayment,
    method,
    ...rest
}) => {
    const ref = useRef<AdyenPaymentMethodRef>({
        shouldShowModal: true,
        additionalActionContentRef: createRef(),
    });
 
    const [additionalActionContent, setAdditionalActionContent] = useState<HTMLElement>();
    const containerId = `adyen-${method.id}-component-field`;
    const additionalActionContainerId = `adyen-${method.id}-additional-action-component-field`;
    const cardVerificationContainerId = `adyen-${method.id}-tsv-component-field`;
    const threeDS2ContainerId = `adyen-${method.id}-additional-action-component-field`;
    const component = method.id as AdyenV2PaymentMethodType;
    const shouldHideInstrumentExpiryDate = component === AdyenV2PaymentMethodType.bcmc;
    const adyenOptions: AdyenOptions = {
        [AdyenV2PaymentMethodType.scheme]: {
            hasHolderName: true,
        },
        [AdyenV2PaymentMethodType.bcmc]: {
            hasHolderName: false,
        },
        [AdyenV2PaymentMethodType.ideal]: {
            showImage: true,
        },
    };
 
    const onBeforeLoad = useCallback((shopperInteraction: boolean)  => {
        ref.current.shouldShowModal = shopperInteraction;
 
        if (ref.current.shouldShowModal) {
            const div = document.createElement('div');
 
            div.setAttribute('id', additionalActionContainerId);
            setAdditionalActionContent(div);
        } else {
            setAdditionalActionContent(undefined);
        }
    }, [additionalActionContainerId]);
 
    const onComplete = useCallback(() => {
        setAdditionalActionContent(undefined);
        ref.current.cancelAdditionalAction = undefined;
    }, []);
 
    const onLoad = useCallback((cancel?) => {
        ref.current.cancelAdditionalAction = cancel;
    }, []);
 
    const appendAdditionalActionContent = useCallback(() => {
        Eif (ref.current.additionalActionContentRef.current && additionalActionContent) {
            ref.current.additionalActionContentRef.current.appendChild(additionalActionContent);
        }
    }, [additionalActionContent]);
 
    const cancelAdditionalActionModalFlow = useCallback(() => {
        setAdditionalActionContent(undefined);
 
        Eif (ref.current.cancelAdditionalAction) {
            ref.current.cancelAdditionalAction();
            ref.current.cancelAdditionalAction = undefined;
        }
    }, []);
 
    const initializeAdyenPayment: HostedWidgetPaymentMethodProps['initializePayment'] = useCallback((options, selectedInstrument) => {
        const selectedInstrumentId = selectedInstrument?.bigpayToken;
 
        return initializePayment({
            ...options,
            adyenv2: {
                cardVerificationContainerId: selectedInstrumentId && cardVerificationContainerId,
                containerId,
                hasVaultedInstruments: !!selectedInstrumentId,
                options: adyenOptions[component],
                threeDS2ContainerId,
                additionalActionOptions: {
                    containerId: additionalActionContainerId,
                    onBeforeLoad,
                    onComplete,
                    onLoad,
                },
                threeDS2Options: {
                    widgetSize: '05',
                    onBeforeLoad,
                    onComplete,
                    onLoad,
                },
            },
        });
    }, [initializePayment, component, cardVerificationContainerId, containerId, additionalActionContainerId, threeDS2ContainerId, adyenOptions, onBeforeLoad, onComplete, onLoad]);
 
    const validateInstrument = (shouldShowNumberField: boolean) => {
        return <AdyenV2CardValidation
            paymentMethodType={ method.method }
            shouldShowNumberField={ shouldShowNumberField }
            verificationFieldsContainerId={ cardVerificationContainerId }
        />;
    };
 
    const isAccountInstrument = () => {
        switch (method.method) {
        case 'directEbanking':
        case 'giropay':
        case 'ideal':
        case 'sepadirectdebit':
            return true;
        default:
            return false;
        }
    };
 
    return <>
        <HostedWidgetPaymentMethod
            { ...rest }
            containerId={ containerId }
            hideContentWhenSignedOut
            initializePayment={ initializeAdyenPayment }
            isAccountInstrument={ isAccountInstrument() }
            method={ method }
            shouldHideInstrumentExpiryDate={ shouldHideInstrumentExpiryDate }
            validateInstrument={ validateInstrument }
        />
 
        <Modal
            additionalBodyClassName="modal-body--center"
            closeButtonLabel={ <TranslatedString id="common.close_action" /> }
            isOpen={ !!additionalActionContent && ref.current.shouldShowModal }
            onAfterOpen={ appendAdditionalActionContent }
            onRequestClose={ cancelAdditionalActionModalFlow }
            shouldShowCloseButton={ true }
        >
            <div
                ref={ ref.current.additionalActionContentRef }
                style={ { width: '100%' } }
            />
        </Modal>
        { !additionalActionContent &&
            <div
                id= { additionalActionContainerId }
                style={ { display: 'none' } }
            /> }
    </>;
};
 
export default AdyenV2PaymentMethod;