Last active
September 4, 2020 03:59
-
-
Save georgelima/8f74e17f3ba61c9313441599fb4c8229 to your computer and use it in GitHub Desktop.
Cielo QRCode Parser Rec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const barcode = "00020101021226410005Cielo0116123456789012000102082009130352040000530398654120000000001005802BR5905CIELO6014SANTO ANDRE SP801010033”https://www.cielo.com.br/qrcode”011613050329197F190A0212150518113349030410000404000105020006020163049872" | |
const ID_SIZE = 2 | |
const LENGTH_SIZE = 2 | |
const TRANSACTION_INFO_LENGTH_SIZE = 3 | |
const MERCHANT_ACCOUNT_INFORMATION_ID = '26' | |
const TRANSACTION_INFORMATION_ID = '80' | |
const parseQRCode = (barcode, cursor = 0, items = []) => { | |
const id = barcode.substr(cursor, ID_SIZE); | |
const lengthSizeForId = id === TRANSACTION_INFORMATION_ID ? TRANSACTION_INFO_LENGTH_SIZE : LENGTH_SIZE | |
const dataSize = parseInt(barcode.substr(cursor + ID_SIZE, lengthSizeForId)); | |
if (cursor >= barcode.length) return items | |
const nextCursor = cursor + ID_SIZE + lengthSizeForId + dataSize | |
if (id === MERCHANT_ACCOUNT_INFORMATION_ID) { | |
const partToParse = barcode.slice(cursor + ID_SIZE + lengthSizeForId, nextCursor) | |
const merchantInfoItems = parseQRCode(partToParse, 0, []) | |
return parseQRCode( | |
barcode, | |
nextCursor, | |
[ | |
...items, | |
{ | |
id: id, | |
length: dataSize, | |
value: merchantInfoItems | |
} | |
] | |
); | |
} | |
if (id === TRANSACTION_INFORMATION_ID) { | |
const partToParse = barcode.slice(cursor + ID_SIZE + lengthSizeForId, nextCursor) | |
const transactionInfoItems = parseQRCode(partToParse, 0, []) | |
return parseQRCode( | |
barcode, | |
nextCursor, | |
[ | |
...items, | |
{ | |
id: id, | |
length: dataSize, | |
value: transactionInfoItems | |
} | |
] | |
); | |
} | |
return parseQRCode( | |
barcode, | |
nextCursor, | |
[ | |
...items, | |
{ | |
id: id, | |
length: dataSize, | |
value: barcode.substr(cursor + ID_SIZE + LENGTH_SIZE, dataSize) | |
} | |
] | |
); | |
} | |
const resultArray = parseQRCode(barcode, 0, []) | |
console.log(resultArray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment