Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cielo-qrcode-parser.js
function getAdditionalData(subTags, value) {
let additionalData = {}
let subPosition = 0
let subTotalStep = 2
let subSize = 0
subTags.map(subTag => {
additionalData[subTag] = { name: subTag }
additionalData[subTag].id = value.slice(subPosition, subTotalStep)
subPosition = subTotalStep
subTotalStep += 2
subSize = parseInt(value.slice(subPosition, subTotalStep))
subPosition = subTotalStep
subTotalStep += subSize
additionalData[subTag].value = value.slice(subPosition, subTotalStep)
subPosition = subTotalStep
subTotalStep += 2
})
return additionalData
}
export function cieloQrcodeParse(data) {
const tags = [
'payloadFormatIndicator',
'pointInitiationMethod',
'merchantAccountInformation',
'merchantCategoryCode',
'transactionCurrency',
'transactionAmount',
'countryCode',
'merchantName',
'merchantCity',
'transactionInformations',
'crc'
]
let payload = {}
let position = 0
let totalStep = 2
let size = 0
tags.map(tag => {
payload[tag] = { name: tag }
// Get ID - Always 2 positions (0, 2)
payload[tag].id = data.slice(position, totalStep)
position = totalStep
if(payload[tag].id == "80") {
totalStep += 3
}
else {
totalStep += 2
}
// Get Size - Always 2 positions (2, 4)
size = parseInt(data.slice(position, totalStep))
position = totalStep
totalStep += size
// Get Value (4, +size)
payload[tag].value = data.slice(position, totalStep)
if(payload[tag].id == "26") {
const subTags = [
'globallyUniqueIdentifier',
'merchantAccountInformation',
'logicNumber'
]
payload[tag].additionalData = getAdditionalData(subTags, payload[tag].value)
}
else if(payload[tag].id == "80") {
const subTags = [
'globallyUniqueIdentifier',
'transactionId',
'transactionDate',
'mainProduct',
'subProduct',
'paymentInstallments',
'transactionType'
]
payload[tag].additionalData = getAdditionalData(subTags, payload[tag].value)
}
position = totalStep
totalStep += 2
})
return payload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.