All files emvqr.js

81.48% Statements 88/108
78.13% Branches 25/32
76.92% Functions 10/13
82.08% Lines 87/106

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  1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     75x             3x   3x 3x 45x 45x 45x 3x 3x   45x 3x 3x   45x 3x 3x   45x 6x   45x 3x   45x 45x       3x 1x   2x     6x     6x 6x 12x 12x 12x 12x     6x     3x     3x 3x 12x 12x 12x     12x 12x     3x                                           45x 45x 45x 45x           45x 45x           12x 12x 12x 12x           12x 12x             12x 12x 12x 12x           12x 12x                                                     3x 3x 3x 3x   3x 3x       4269x 4269x 28455x 28455x 4260x   24195x 9x 24186x           4269x       69x     69x       1x            
 
const crc = require('./crc');
const models = require('./models');
const fs = require('fs')
const mcc_codes = require('./mcc_codes.json')
const currencyCode = require('./currencies.json')
const countries = require('./countries.json')
const getDataObjectName = models.getDataObjectName;
const getDataObjectNameSubData = models.getDataObjectNameSubData;
const getDataObjectAdditionalFieldsName = models.getDataObjectAdditionalFieldsName;
const getDataObjectNamePaymentSpecific = models.getDataObjectNamePaymentSpecific;
 
// 
let debug = { log: o => o };
 
/**
 * decode (public|exported)
 *  receive a text and return a emv parsed object
 */
function decode(emvString) {
    const emvObject = {};
    // parse emv string
    let inputText = emvString;
    while (inputText.length > 0) {
        debug.log('inputText', inputText);
        let { emvItem, remainingText } = readNext(inputText);
        if (emvItem.id == 52) {
            let translateCode = getObjects(mcc_codes, 'mcc', emvItem.data)
            emvItem.data = `${emvItem.data} (${translateCode[0].usda_description})`
        }
        if (emvItem.id == 53 ){
            let currencyTranslate = getObjects(currencyCode,'number',emvItem.data)
            emvItem.data = `${emvItem.data} (${currencyTranslate[0].code})`
        }
        if(emvItem.id == 58){
            let countriExplain = getObjects(countries,'Code',emvItem.data)
            emvItem.data = `${emvItem.data} (${countriExplain[0].Name})`
        }
        if (emvItem.name == 'Merchant Account Information') {
            emvItem.data = decodeSubData(emvItem.data)
        }
        if (emvItem.name == 'Additional Data Field Template') {
            emvItem.data = decodeAdditionalFields(emvItem.data)
        }
        emvObject[emvItem.id] = emvItem;
        inputText = remainingText;
    }
 
    // validate checksum
    if (!validateChecksum(emvString)) {
        throw new Error('checksum validation failed.');
    };
    return emvObject;
}
function decodeSubData(emvString) {
    const emvObject = {};
 
    // parse emv string
    let inputText = emvString;
    while (inputText.length > 0) {
        debug.log('inputText', inputText);
        let { emvItem, remainingText } = readNextSubData(inputText);
        emvObject[emvItem.id] = emvItem;
        inputText = remainingText;
    }
 
    return emvObject;
}
function decodeAdditionalFields(emvString) {
    const emvObject = {};
 
    // parse emv string
    let inputText = emvString;
    while (inputText.length > 0) {
        debug.log('inputText', inputText);
        let { emvItem, remainingText } = readNextAdditionalFields(inputText);
        Iif (emvItem.id == '60') {
            emvItem.data = decodePaymentSpecific(emvItem.data)
        }
        emvObject[emvItem.id] = emvItem;
        inputText = remainingText;
    }
 
    return emvObject;
}
function decodePaymentSpecific(emvString) {
    const emvObject = {};
 
    // parse emv string
    let inputText = emvString;
    while (inputText.length > 0) {
        debug.log('inputText', inputText);
        let { emvItem, remainingText } = readNextPaymentSpecific(inputText);
        emvObject[emvItem.id] = emvItem;
        inputText = remainingText;
    }
 
    return emvObject;
}
 
/**
 * readNext (private use)
 *  read next emvItem, and return the remaining string
 */
function readNext(inputText) {
    const id = inputText.substring(0, 2);
    const len = checkNumberType(parseInt(inputText.substring(2, 4)));
    const data = inputText.substring(4, len + 4);
    const emvItem = {
        id,
        name: getDataObjectName(id),
        len,
        data
    };
    const remainingText = inputText.substring(len + 4);
    return {
        emvItem,
        remainingText
    };
}
function readNextSubData(inputText) {
    const id = inputText.substring(0, 2);
    const len = checkNumberType(parseInt(inputText.substring(2, 4)));
    const data = inputText.substring(4, len + 4);
    const emvItem = {
        id,
        name: getDataObjectNameSubData(id),
        len,
        data
    };
    const remainingText = inputText.substring(len + 4);
    return {
        emvItem,
        remainingText
    };
}
 
function readNextAdditionalFields(inputText) {
    const id = inputText.substring(0, 2);
    const len = checkNumberType(parseInt(inputText.substring(2, 4)));
    const data = inputText.substring(4, len + 4);
    const emvItem = {
        id,
        name: getDataObjectAdditionalFieldsName(id),
        len,
        data
    };
    const remainingText = inputText.substring(len + 4);
    return {
        emvItem,
        remainingText
    };
}
function readNextPaymentSpecific(inputText) {
    const id = inputText.substring(0, 2);
    const len = checkNumberType(parseInt(inputText.substring(2, 4)));
    const data = inputText.substring(4, len + 4);
    const emvItem = {
        id,
        name: getDataObjectNamePaymentSpecific(id),
        len,
        data
    };
    const remainingText = inputText.substring(len + 4);
    return {
        emvItem,
        remainingText
    };
}
 
/**
 * validateChecksum (private use)
 *  you guess
 */
function validateChecksum(emvString) {
    const emvData = emvString.substring(0, emvString.length - 4);
    const checksum = emvString.substring(emvString.length - 4);
    debug.log('emvData', emvData);
    debug.log('checksum', checksum);
 
    const expectedCRC = crc.computeCRC(emvData);
    return expectedCRC === checksum;
}
 
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        Iif (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else
            if (i == key && obj[i] == val || i == key && val == '') { //
                objects.push(obj);
            } else Iif (obj[i] == val && key == '') {
                if (objects.lastIndexOf(obj) == -1) {
                    objects.push(obj);
                }
            }
    }
    return objects;
}
 
function checkNumberType(parsedNo) {
    Iif (isNaN(parsedNo)) {
        throw new Error('Invalid EMV data');
    } else {
        return parsedNo
    }
}
 
module.exports = {
    decode,
    enableDebugLog: () => (debug = console),
    decodeSubData,
    decodeAdditionalFields,
    decodePaymentSpecific
}