Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Last active September 4, 2020 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakenickels/02e725a0ab8133086af2533c2a7150ae to your computer and use it in GitHub Desktop.
Save fakenickels/02e725a0ab8133086af2533c2a7150ae to your computer and use it in GitHub Desktop.
const A = require('arcsecond')
const tlv = A.recursiveParser(() => A.coroutine(function* () {
const id = yield A.sequenceOf([
A.digit,
A.digit,
]).map(v => v.join(''));
if(id == '80') {
const length = yield A.sequenceOf([
A.digit,
A.digit,
A.digit,
]).map(v => v.join(''));
const transactionInformations = yield A.sequenceOf(
Array.from({length: Number(length)}).map(() => A.anyChar)
).map(v => v.join(''))
return {
id,
length,
value: parser.run(transactionInformations)
}
}
const length = yield A.sequenceOf([
A.digit,
A.digit,
]).map(v => v.join(''));
const value = yield A.sequenceOf(
Array.from({length: Number(length)}).map(() => A.anyChar)
).map(v => v.join(''))
if(id === '26') {
return {id, length, value: parser.run(value)}
}
return {id, length, value}
}));
const parser = A.many(tlv)
const emv = `00020101021226410005Cielo0116123456789012000102082009130352040000530398654120000000001005802BR5905CIELO6014SANTO ANDRE SP801010033"https://www.cielo.com.br/qrcode"011613050329197F190A0212150518113349030410000404000105020006020163049872`
const getIdValue = (id, tree) => {
const field = tree.find(field => field.id === id)
return field ? field.value : null
}
const treeToObject = tree => {
const merchantAccountInformation = getIdValue("26", tree).result;
const transactionInformations = getIdValue("80", tree).result;
return {
payloadFormatIndicator: getIdValue('00', tree),
pointOfInitiationMethod: getIdValue("01", tree),
merchantAccountInformation: {
globallyUniqueIdentifier: getIdValue("00", merchantAccountInformation),
merchantAccountInformation: getIdValue("01", merchantAccountInformation),
logicNumber: getIdValue("02", merchantAccountInformation),
},
merchantCategory: getIdValue("52", tree),
transactionCurrency: getIdValue("53", tree),
transactionAmount: getIdValue("54", tree),
countryCode: getIdValue("58", tree),
merchantName: getIdValue("59", tree),
merchantCity: getIdValue("60", tree),
transactionInformations: {
globallyUniqueIdentifier: getIdValue("00", transactionInformations),
transactionId: getIdValue("01", transactionInformations),
transactionDate: getIdValue("02", transactionInformations),
mainProduct: getIdValue("03", transactionInformations),
subProduct: getIdValue("04", transactionInformations),
paymentInstallments: getIdValue("05", transactionInformations),
transactionType: getIdValue("06", transactionInformations),
},
crc: getIdValue("63", tree),
};
};
console.log(treeToObject(parser.run(emv).result))
module.exports.parser = parser
module.exports.treeToObject = treeToObject
const obj = {
payloadFormatIndicator: '01',
pointOfInitiationMethod: '12',
merchantAccountInformation: {
globallyUniqueIdentifier: 'Cielo',
merchantAccountInformation: '1234567890120001',
logicNumber: '20091303'
},
merchantCategory: '0000',
transactionCurrency: '986',
transactionAmount: '000000000100',
countryCode: 'BR',
merchantName: 'CIELO',
merchantCity: 'SANTO ANDRE SP',
transactionInformations: {
globallyUniqueIdentifier: '"https://www.cielo.com.br/qrcode"',
transactionId: '13050329197F190A',
transactionDate: '150518113349',
mainProduct: '1000',
subProduct: '0001',
paymentInstallments: '00',
transactionType: '01'
},
crc: '9872'
}
[
{
"id": "00",
"length": "02",
"value": "01"
},
{
"id": "01",
"length": "02",
"value": "12"
},
{
"id": "26",
"length": "41",
"value": {
"isError": false,
"result": [
{
"id": "00",
"length": "05",
"value": "Cielo"
},
{
"id": "01",
"length": "16",
"value": "1234567890120001"
},
{
"id": "02",
"length": "08",
"value": "20091303"
}
],
"index": 41,
"data": null
}
},
{
"id": "52",
"length": "04",
"value": "0000"
},
{
"id": "53",
"length": "03",
"value": "986"
},
{
"id": "54",
"length": "12",
"value": "000000000100"
},
{
"id": "58",
"length": "02",
"value": "BR"
},
{
"id": "59",
"length": "05",
"value": "CIELO"
},
{
"id": "60",
"length": "14",
"value": "SANTO ANDRE SP"
},
{
"id": "80",
"length": "101",
"value": {
"isError": false,
"result": [
{
"id": "00",
"length": "33",
"value": "\"https://www.cielo.com.br/qrcode\""
},
{
"id": "01",
"length": "16",
"value": "13050329197F190A"
},
{
"id": "02",
"length": "12",
"value": "150518113349"
},
{
"id": "03",
"length": "04",
"value": "1000"
},
{
"id": "04",
"length": "04",
"value": "0001"
},
{
"id": "05",
"length": "02",
"value": "00"
},
{
"id": "06",
"length": "02",
"value": "01"
}
],
"index": 101,
"data": null
}
},
{
"id": "63",
"length": "04",
"value": "9872"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment