All files / app/payment/paymentMethod PaymentMethodTitle.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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228  7x 7x 7x   7x 7x 7x 7x     7x 7x 7x                             4345x   207x 207x     207x                                                                                                                                                                                                                           207x       207x                 7x 207x 207x 207x 207x 207x   207x 207x   207x 207x 136x     71x 1x     70x 13x   13x       13x       207x                                                   209x 209x 209x   209x       209x         7x  
import { LanguageService, PaymentMethod } from '@bigcommerce/checkout-sdk';
import { number } from 'card-validator';
import { compact, startCase } from 'lodash';
import React, { memo, Fragment, FunctionComponent } from 'react';
 
import { withCheckout, CheckoutContextProps } from '../../checkout';
import { connectFormik, ConnectFormikProps } from '../../common/form';
import { withLanguage, WithLanguageProps } from '../../locale';
import { mapFromPaymentMethodCardType, CreditCardIconList } from '../creditCard';
import { PaymentFormValues } from '../PaymentForm';
 
import getPaymentMethodName from './getPaymentMethodName';
import PaymentMethodId from './PaymentMethodId';
import PaymentMethodType from './PaymentMethodType';
 
export interface PaymentMethodTitleProps {
    method: PaymentMethod;
    isSelected?: boolean;
}
 
interface WithCdnPathProps {
    cdnBasePath: string;
}
 
function getPaymentMethodTitle(
    language: LanguageService,
    basePath: string
): (method: PaymentMethod) => { logoUrl: string; titleText: string } {
    const cdnPath = (path: string) => `${basePath}${path}`;
 
    return method => {
        const methodName = getPaymentMethodName(language)(method);
        // TODO: API could provide the data below so UI can read simply read it.
        // However, I'm not sure how we deal with translation yet. TBC.
        const customTitles: { [key: string]: { logoUrl: string; titleText: string } } = {
            [PaymentMethodType.CreditCard]: {
                logoUrl: '',
                titleText: methodName,
            },
            [PaymentMethodType.PaypalCredit]: {
                logoUrl: cdnPath('/img/payment-providers/paypal-credit.png'),
                titleText: '',
            },
            [PaymentMethodId.PaypalCommerce]: {
                logoUrl: cdnPath('/img/payment-providers/paypal_commerce_logo.svg'),
                titleText: '',
            },
            [PaymentMethodId.PaypalCommerceCredit]: {
                logoUrl: cdnPath('/img/payment-providers/paypal_commerce_pay_later.svg'),
                titleText: '',
            },
            [PaymentMethodId.PaypalCommerceAlternativeMethod]: {
                logoUrl: method.logoUrl || '',
                titleText: (method.logoUrl ? '' : method.config.displayName) || '',
            },
            [PaymentMethodType.VisaCheckout]: {
                logoUrl: cdnPath('/img/payment-providers/visa-checkout.png'),
                titleText: '',
            },
            [PaymentMethodId.Affirm]: {
                logoUrl: cdnPath('/img/payment-providers/affirm-checkout-header.png'),
                titleText: language.translate('payment.affirm_display_name_text'),
            },
            [PaymentMethodId.Afterpay]: {
                logoUrl: cdnPath('/img/payment-providers/afterpay-badge-blackonmint.png'),
                titleText: methodName,
            },
            [PaymentMethodId.Amazon]: {
                logoUrl: cdnPath('/img/payment-providers/amazon-header.png'),
                titleText: '',
            },
            [PaymentMethodId.AmazonPay]: {
                logoUrl: cdnPath('/img/payment-providers/amazon-header.png'),
                titleText: '',
            },
            [PaymentMethodId.ChasePay]: {
                logoUrl: cdnPath('/img/payment-providers/chase-pay.png'),
                titleText: '',
            },
            [PaymentMethodId.Clearpay]: {
                logoUrl: cdnPath('/img/payment-providers/clearpay-header.png'),
                titleText: methodName,
            },
            [PaymentMethodType.GooglePay]: {
                logoUrl: cdnPath('/img/payment-providers/google-pay.png'),
                titleText: '',
            },
            [PaymentMethodId.DigitalRiver]: {
                logoUrl: cdnPath('/img/payment-providers/digital-river-header.png'),
                titleText: '',
            },
            [PaymentMethodId.Klarna]: {
                logoUrl: cdnPath('/img/payment-providers/klarna-header.png'),
                titleText: method.config && method.config.displayName || '',
            },
            [PaymentMethodId.Laybuy]: {
                logoUrl: cdnPath('/img/payment-providers/laybuy-checkout-header.png'),
                titleText: '',
            },
            [PaymentMethodId.Masterpass]: {
                logoUrl: 'https://masterpass.com/dyn/img/acc/global/mp_mark_hor_blk.svg',
                titleText: '',
            },
            [PaymentMethodType.Paypal]: {
                logoUrl: cdnPath('/img/payment-providers/paypalpaymentsprouk.png'),
                titleText: '',
            },
            [PaymentMethodId.Quadpay]: {
                logoUrl: cdnPath('/img/payment-providers/quadpay.png'),
                titleText: language.translate('payment.quadpay_display_name_text'),
            },
            [PaymentMethodId.Sezzle]: {
                logoUrl: cdnPath('/img/payment-providers/sezzle-checkout-header.png'),
                titleText: language.translate('payment.sezzle_display_name_text'),
            },
            [PaymentMethodId.Zip]: {
                logoUrl: cdnPath('/img/payment-providers/zip.png'),
                titleText: language.translate('payment.zip_display_name_text'),
            },
            [PaymentMethodType.Barclaycard]: {
                logoUrl: cdnPath(`/img/payment-providers/barclaycard_${method.id.toLowerCase()}.png`),
                titleText: '',
            },
            [PaymentMethodId.AdyenV2]: {
                logoUrl: `https://checkoutshopper-live.adyen.com/checkoutshopper/images/logos/${(method.method === 'scheme') ? 'card' : method.method}.svg`,
                titleText: (method.config.displayName === 'Credit Card' ? language.translate('payment.adyen_credit_debit_card_text') : method.config.displayName) || '',
            },
            [PaymentMethodId.Mollie]: {
                logoUrl: method.method === 'credit_card' ? '' : cdnPath(`/img/payment-providers/mollie_${method.method}.svg`),
                titleText: method.method === 'credit_card' ? startCase(methodName) : methodName,
            },
            [PaymentMethodId.Checkoutcom]: {
                logoUrl: ['credit_card', 'checkoutcom'].includes(method.id) ? '' : cdnPath(`/img/payment-providers/checkoutcom_${method.id.toLowerCase()}.svg`),
                titleText: methodName,
            },
            [PaymentMethodId.StripeV3]: {
                logoUrl: '',
                titleText: method.method === 'iban' ? language.translate('payment.stripe_sepa_display_name_text') : methodName,
            },
        };
 
        // KLUDGE: 'paypal' is actually a credit card method. It is the only
        // exception to the rule below. We should probably fix it on API level,
        // but apparently it would break LCO if we are not careful.
        Iif (method.id === PaymentMethodId.PaypalPaymentsPro && method.method === PaymentMethodType.CreditCard) {
            return customTitles[PaymentMethodType.CreditCard];
        }
 
        return (
            customTitles[method.gateway || ''] ||
            customTitles[method.id] ||
            customTitles[method.method] ||
            customTitles[PaymentMethodType.CreditCard]
        );
    };
}
 
const PaymentMethodTitle: FunctionComponent<PaymentMethodTitleProps & WithLanguageProps & WithCdnPathProps & ConnectFormikProps<PaymentFormValues>> = ({
    cdnBasePath,
    formik: { values },
    isSelected,
    language,
    method,
}) => {
    const methodName = getPaymentMethodName(language)(method);
    const { logoUrl, titleText } = getPaymentMethodTitle(language, cdnBasePath)(method);
 
    const getSelectedCardType = () => {
        if (!isSelected) {
            return;
        }
 
        if ('hostedForm' in values && 'cardType' in values.hostedForm && values.hostedForm.cardType) {
            return values.hostedForm.cardType;
        }
 
        if ('ccNumber' in values && values.ccNumber) {
            const { card } = number(values.ccNumber);
 
            Iif (!card) {
                return;
            }
 
            return card.type;
        }
    };
 
    return (
        <Fragment>
            { logoUrl && <img
                alt={ methodName }
                className="paymentProviderHeader-img"
                data-test="payment-method-logo"
                src={ logoUrl }
            /> }
 
            { titleText && <span
                className="paymentProviderHeader-name"
                data-test="payment-method-name"
            >
                { titleText }
            </span> }
 
            <div className="paymentProviderHeader-cc">
                <CreditCardIconList
                    cardTypes={ compact(method.supportedCards.map(mapFromPaymentMethodCardType)) }
                    selectedCardType={ getSelectedCardType() }
                />
            </div>
        </Fragment>
    );
};
 
function mapToCdnPathProps({ checkoutState }: CheckoutContextProps): WithCdnPathProps | null {
    const { data: { getConfig } } = checkoutState;
    const config = getConfig();
 
    Iif (!config) {
        return null;
    }
 
    return {
        cdnBasePath: config.cdnPath,
    };
}
 
export default connectFormik(withLanguage(withCheckout(mapToCdnPathProps)(memo(PaymentMethodTitle))));