All files / app/payment/paymentMethod getPaymentMethodName.ts

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  29x   29x 29x                   29x     524x 524x   524x 485x     524x                   524x       29x     524x 524x 5x     519x 3x     516x 5x     511x 2x     509x 3x     506x 8x     498x 3x     495x 3x     492x 3x     489x 3x     486x 3x        
import { LanguageService, PaymentMethod } from '@bigcommerce/checkout-sdk';
import { capitalize, get } from 'lodash';
 
import PaymentMethodId from './PaymentMethodId';
import PaymentMethodType from './PaymentMethodType';
 
/**
 * Always return the translated name of a payment method unless it is a
 * multi-option payment method or it doesn't have any translation. It's possible
 * to translate the gateway name of multi-option methods, i.e.: AfterPay.
 * However, because the options provided by the gateway can vary a lot, i.e.:
 * "Pay by Installment", therefore it's not feasible to do the translation on
 * the UI level.
 */
export default function getPaymentMethodName(
    language: LanguageService
): (method: Partial<PaymentMethod> & Pick<PaymentMethod, 'id'>) => string {
    return method => {
        let name = getTranslatedPaymentMethodName(language)(method);
 
        if (!name || method.method === PaymentMethodType.MultiOption) {
            name = method.config && method.config.displayName;
        }
 
        Iif (!name) {
            name = capitalize(
                // FIXME: I'm not entirely sure why we have to do this. But for some
                // reason this is required for Masterpass provided by Square.
                get(method, 'initializationData.paymentData.cardData.digital_wallet_type') ||
                method.method ||
                method.id
            );
        }
 
        return name;
    };
}
 
export function getTranslatedPaymentMethodName(
    language: LanguageService
): (method: Partial<PaymentMethod> & Pick<PaymentMethod, 'id'>) => string | undefined {
    return method => {
        if (method.id === PaymentMethodId.Affirm) {
            return language.translate('payment.affirm_name_text');
        }
 
        if (method.id === PaymentMethodId.Afterpay) {
            return language.translate('payment.afterpay_name_text');
        }
 
        if (method.id === PaymentMethodId.Amazon) {
            return language.translate('payment.amazon_name_text');
        }
 
        if (method.id === PaymentMethodId.AmazonPay) {
            return language.translate('payment.amazon_name_text');
        }
 
        if (method.id === PaymentMethodId.Clearpay) {
            return language.translate('payment.clearpay_name_text');
        }
 
        if (method.id === PaymentMethodId.Klarna) {
            return language.translate('payment.klarna_name_text');
        }
 
        if (method.method === PaymentMethodType.Paypal) {
            return language.translate('payment.paypal_name_text');
        }
 
        if (method.method === PaymentMethodType.PaypalCredit) {
            return language.translate('payment.paypal_credit_name_text');
        }
 
        if (method.method === PaymentMethodType.Chasepay) {
            return language.translate('payment.chasepay_name_text');
        }
 
        if (method.method === PaymentMethodType.VisaCheckout) {
            return language.translate('payment.vco_name_text');
        }
 
        if (method.method === PaymentMethodType.GooglePay) {
            return language.translate('payment.google_pay_name_text');
        }
    };
}