elisei/pagbank-payment-magento

View on GitHub
view/base/web/js/model/credit-card-validation/credit-card-number-validator.js

Summary

Maintainability
A
2 hrs
Test Coverage
/**
 * PagBank Payment Magento Module.
 *
 * Copyright © 2023 PagBank. All rights reserved.
 *
 * @author    Bruno Elisei <brunoelisei@o2ti.com>
 * @license   See LICENSE for license details.
 */

/* @api */
define([
    'mageUtils',
    'Magento_Payment/js/model/credit-card-validation/credit-card-number-validator/luhn10-validator',
    'PagBank_PaymentMagento/js/model/credit-card-validation/credit-card-type'
], function (utils, luhn10, creditCardTypes) {
    'use strict';

    /**
     * @param {*} card
     * @param {*} isPotentiallyValid
     * @param {*} isValid
     * @return {Object}
     */
    function resultWrapper(card, isPotentiallyValid, isValid) {
        return {
            card: card,
            isValid: isValid,
            isPotentiallyValid: isPotentiallyValid
        };
    }

    return (value) => {
        var potentialTypes,
            cardType,
            valid,
            i,
            maxLength;

        if (utils.isEmpty(value)) {
            return resultWrapper(null, false, false);
        }

        value = value.replace(/\s+/g, '');

        if (!/^\d*$/.test(value)) {
            return resultWrapper(null, false, false);
        }

        potentialTypes = creditCardTypes.getCardTypes(value);

        if (potentialTypes.length === 0) {
            return resultWrapper(null, false, false);
        } else if (potentialTypes.length !== 1) {
            return resultWrapper(null, true, false);
        }

        cardType = potentialTypes[0];

        valid = luhn10(value);

        for (i = 0; i < cardType.lengths.length; i++) {
            if (cardType.lengths[i] === value.length) {
                return resultWrapper(cardType, valid, valid);
            }
        }

        maxLength = Math.max.apply(null, cardType.lengths);

        if (value.length < maxLength) {
            return resultWrapper(cardType, true, false);
        }

        return resultWrapper(cardType, false, false);
    };
});