All files / app/payment/paymentMethod StripePaymentMethod.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  24x     24x 24x   24x 24x                               24x 24x 24x 24x 24x 24x 24x 24x   24x 9x 9x 9x 9x   9x 9x 9x 9x 9x     9x                                                 9x 1x                               9x 5x 1x     4x     9x 5x             9x           9x                                         10x 10x 10x   10x 1x     9x       24x  
import { PaymentInitializeOptions, StripeElementOptions } from '@bigcommerce/checkout-sdk';
import React, { useCallback, FunctionComponent } from 'react';
import { Omit } from 'utility-types';
 
import { withCheckout, CheckoutContextProps } from '../../checkout';
import { TranslatedString } from '../../locale';
 
import HostedWidgetPaymentMethod, { HostedWidgetPaymentMethodProps } from './HostedWidgetPaymentMethod';
import StripeV3CustomCardForm from './StripeV3CustomCardForm';
 
export type StripePaymentMethodProps = Omit<HostedWidgetPaymentMethodProps, 'containerId'>;
 
export interface StripeOptions {
    alipay?: StripeElementOptions;
    card: StripeElementOptions;
    cardCvc: StripeElementOptions;
    cardExpiry: StripeElementOptions;
    cardNumber: StripeElementOptions;
    iban: StripeElementOptions;
    idealBank: StripeElementOptions;
}
interface WithCheckoutStripePaymentMethodProps {
    storeUrl: string;
}
export enum StripeElementType {
    alipay = 'alipay',
    card = 'card',
    cardCvc = 'cardCvc',
    cardExpiry = 'cardExpiry',
    cardNumber = 'cardNumber',
    iban = 'iban',
    idealBank = 'idealBank',
}
const StripePaymentMethod: FunctionComponent<StripePaymentMethodProps & WithCheckoutStripePaymentMethodProps> = ({
      initializePayment,
      method,
      storeUrl,
      ...rest
  }) => {
    const { useIndividualCardFields } = method.initializationData;
    const paymentMethodType = method.id as StripeElementType;
    const additionalStripeV3Classes = paymentMethodType !== StripeElementType.alipay ? 'optimizedCheckout-form-input widget--stripev3' : '';
    const containerId = `stripe-${paymentMethodType}-component-field`;
    const classes = {
        base: 'form-input optimizedCheckout-form-input',
    };
    const stripeOptions: StripeOptions = {
        [StripeElementType.card]: {
            classes,
        },
        [StripeElementType.cardCvc]: {
            classes,
            placeholder: '',
        },
        [StripeElementType.cardExpiry]: {
            classes,
        },
        [StripeElementType.cardNumber]: {
            classes,
            showIcon: true,
            placeholder: '',
        },
        [StripeElementType.iban]: {
            classes,
            supportedCountries: ['SEPA'],
        },
        [StripeElementType.idealBank]: {
            classes,
        },
    };
 
    const getIndividualCardElementOptions = useCallback((stripeInitializeOptions: StripeOptions) => {
        return {
            cardNumberElementOptions: {
                ...stripeInitializeOptions[StripeElementType.cardNumber],
                containerId: 'stripe-card-number-component-field',
            },
            cardExpiryElementOptions: {
                ...stripeInitializeOptions[StripeElementType.cardExpiry],
                containerId: 'stripe-expiry-component-field',
            },
            cardCvcElementOptions: {
                ...stripeInitializeOptions[StripeElementType.cardCvc],
                containerId: 'stripe-cvc-component-field',
            },
        };
    }, []);
 
    const getStripeOptions = useCallback((shouldRenderCustomComponents: boolean, stripeInitializeOptions: StripeOptions) => {
        if (shouldRenderCustomComponents) {
            return getIndividualCardElementOptions(stripeInitializeOptions);
        }
 
        return stripeInitializeOptions[paymentMethodType];
    }, [paymentMethodType, getIndividualCardElementOptions]);
 
    const initializeStripePayment = useCallback(async (options: PaymentInitializeOptions) => {
        return initializePayment({
            ...options,
            stripev3: { containerId,
                options: getStripeOptions(useIndividualCardFields, stripeOptions) },
        });
    }, [initializePayment, containerId, getStripeOptions, useIndividualCardFields, stripeOptions]);
 
    const renderCustomPaymentForm = () => {
        const optionsCustomForm = getIndividualCardElementOptions(stripeOptions);
 
        return <StripeV3CustomCardForm options={ optionsCustomForm } />;
    };
 
    return <>
        <HostedWidgetPaymentMethod
            { ...rest }
            additionalContainerClassName= { additionalStripeV3Classes }
            containerId={ containerId }
            hideContentWhenSignedOut
            initializePayment={ initializeStripePayment }
            method={ method }
            renderCustomPaymentForm={ renderCustomPaymentForm }
            shouldRenderCustomInstrument={ useIndividualCardFields }
        />
        {
            method.id === 'iban' &&
                <p className="stripe-sepa-mandate-disclaimer">
                    <TranslatedString data={ {storeUrl} } id="payment.stripe_sepa_mandate_disclaimer" />
                </p>
        }
    </>;
};
 
function mapFromCheckoutProps(
    { checkoutState }: CheckoutContextProps) {
    const { data: { getConfig } } = checkoutState;
    const config = getConfig();
 
    if (!config) {
        return null;
    }
 
    return {
        storeUrl: config.links.siteLink,
    };
}
export default withCheckout(mapFromCheckoutProps)(StripePaymentMethod);