LucasCalazans
3/23/2018 - 2:08 AM

Verify the CC brand

Verify the CC brand

const CC_BRAND_ELO = 'ELO';
const CC_BRAND_VISA_ELECTRON = 'VISA_ELECTRON';
const CC_BRAND_VISA = 'VISA';
const CC_BRAND_MASTER = 'MASTER';
const CC_BRAND_AMEX = 'AMEX';
const CC_BRAND_DISCOVER = 'DISCOVER';
const CC_BRAND_DINERS = 'DINERS';
const CC_BRAND_DINERS_CARTE = 'DINERS_CARTE';

function getCardBrand(number)  {
    const tests = {
        [CC_BRAND_ELO]:             /^(636368|636369|438935|504175|451416|636297|5067|4576|4011|506699)/g,
        [CC_BRAND_VISA_ELECTRON]:   /^(4026|41)/g,
        [CC_BRAND_VISA]:            /^4/g,
        [CC_BRAND_MASTER]:          /^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/,
        [CC_BRAND_AMEX]:            /^3[47]/g,
        [CC_BRAND_DISCOVER]:        /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/g,
        [CC_BRAND_DINERS]:          /^36/g,
        [CC_BRAND_DINERS_CARTE]:    /^30[0-5]/g
    }

    const keys = Object.keys(tests);
    for(let i = 0; i < keys.length; i++) {
        const currentKey = keys[i];
        if(tests[currentKey].exec(number)) return currentKey;
    }
}