Geração de comprovantes

Geração de comprovantes

É possivel gerar os comprovantes a partir das informações retornadas da própria transação. A seguir, apresentamos um exemplo em JavaScript, que pode ser encontrado no aplicativo de exemplo, no arquivo log.js.

const PAYMENT_TYPE_CREDIT = 0
const PAYMENT_TYPE_DEBIT = 1
const PAYMENT_TYPE_CREDIT_WITH_INSTALLMENTS = 2
const PAYMENT_TYPE_PIX = 4

const RECEIPT_TYPE_PAYMENT = 0
const RECEIPT_TYPE_VOID = 1

function logPaymentReceipts(data) {
  logReceipts(RECEIPT_TYPE_PAYMENT, data)
}

function logVoidReceipts(data) {
  logReceipts(RECEIPT_TYPE_VOID, data)
}

function logReceipts(logNode, receiptType, data) {
  let liSellerReceipt = document.createElement('li')
  let liDashedLine = document.createElement('li')
  let liCustomerReceipt = document.createElement('li')

  logNode.appendChild(liSellerReceipt)
  logNode.appendChild(liDashedLine)
  logNode.appendChild(liCustomerReceipt)

  appendTitledReceipt(liSellerReceipt, 'VIA ESTABELECIMENTO', receiptType, data)
  appendTitledReceipt(liCustomerReceipt, 'VIA CLIENTE', receiptType, data)

  let dashedLine = document.createElement('hr')
  dashedLine.style.border = '1px dashed #000'

  liDashedLine.appendChild(dashedLine)

  liCustomerReceipt.scrollIntoView()
}

function appendTitledReceipt(node, title, receiptType, data) {
  let receiptTitle = document.createElement('h3')
  receiptTitle.textContent = title || 'COMPROVANTE'

  node.appendChild(receiptTitle)
  node.appendChild(document.createElement('hr'))

  appendReceipt(node, receiptType, data)
}

function appendReceipt(node, receiptType, data) {
  let receiptSpan = document.createElement('span')
  receiptSpan.style.whiteSpace = 'pre'

  const isPayment = receiptType == RECEIPT_TYPE_PAYMENT

  const isPix = data.paymentType == PAYMENT_TYPE_PIX
  const isCard = !isPix

  const hasSeller = !!data.sellerName
  const hasAddress = isPayment && data.address
  const hasDocument = data.documentType && data.document
  const hasSellerHeader = hasSeller || hasAddress || hasDocument

  if (hasSeller) {
    receiptSpan.appendChild(document.createTextNode(data.sellerName))
    receiptSpan.appendChild(document.createElement('br'))
  }

  if (hasAddress) {
    let receiptAddressSpan = document.createElement('span')
    receiptAddressSpan.style.fontSize = '12px'
    receiptAddressSpan.style.whiteSpace = 'pre'
    receiptAddressSpan.textContent = data.address

    receiptSpan.appendChild(receiptAddressSpan)
    receiptSpan.appendChild(document.createElement('br'))
  }

  if (hasDocument) {
    receiptSpan.appendChild(document.createTextNode(`${data.documentType}: ${data.document}`))
    receiptSpan.appendChild(document.createElement('br'))
  }

  if (hasSellerHeader) {
    receiptSpan.appendChild(document.createElement('br'))
  }

  const hasArqc = isPayment && isCard && data.arqc
  const hasAutoCode = isCard && data.autoCode
  const hasCv = isCard && data.cv
  const hasTransactionId = isPix && data.transactionId
  const hasNsu = isPayment && isCard && data.nsu
  const hasCodes = hasArqc || hasAutoCode || hasCv || hasTransactionId || hasNsu

  if (hasCodes) {
    let receiptCodesSpan = document.createElement('span')
    receiptCodesSpan.style.fontSize = '12px'
    receiptCodesSpan.style.whiteSpace = 'pre'

    if (hasArqc) {
      receiptCodesSpan.appendChild(document.createTextNode(`ARQC: ${data.arqc}`))
      receiptCodesSpan.appendChild(document.createElement('br'))
    }

    if (hasAutoCode) {
      receiptCodesSpan.appendChild(document.createTextNode(`AUTO: ${data.autoCode}`))
      receiptCodesSpan.appendChild(document.createElement('br'))
    }

    if (hasCv) {
      receiptCodesSpan.appendChild(document.createTextNode(`CV: ${data.cv}`))
      receiptCodesSpan.appendChild(document.createElement('br'))
    }

    if (hasTransactionId) {
      receiptCodesSpan.appendChild(document.createTextNode(`ID: ${data.transactionId}`))
      receiptCodesSpan.appendChild(document.createElement('br'))
    }

    if (hasNsu) {
      receiptCodesSpan.appendChild(document.createTextNode(`NSU: ${data.nsu}`))
      receiptCodesSpan.appendChild(document.createElement('br'))
    }

    receiptSpan.appendChild(receiptCodesSpan)
  }

  if (hasSellerHeader || hasCodes) {
    receiptSpan.appendChild(document.createElement('hr'))
  }

  const hasDatetime = data.date && data.hour

  const receiptDatetime = hasDatetime ? `${data.date} ${data.hour}` : ''
  const receiptValueStatus = getReceiptValueStatus(receiptType)

  let receiptDatetimeSpan = document.createElement('span')
  receiptDatetimeSpan.style.fontSize = '12px'
  receiptDatetimeSpan.style.whiteSpace = 'pre'
  receiptDatetimeSpan.textContent = `${hasDatetime ? `${receiptDatetime}    ` : ''}${receiptValueStatus}`

  receiptSpan.appendChild(receiptDatetimeSpan)
  receiptSpan.appendChild(document.createElement('br'))

  const hasValue = !!data.value

  if (hasValue) {
    const hasPaymentType = !!data.paymentType

    const receiptPaymentType = hasPaymentType ? getPaymentTypeLabel(data.paymentType) : ''
    const receiptValue = currencyFormatter.format(data.value / 100)

    let receiptValueSpan = document.createElement('span')
    receiptValueSpan.style.fontWeight = 'bold'
    receiptValueSpan.style.whiteSpace = 'pre'
    receiptValueSpan.textContent = `${hasPaymentType ? `${receiptPaymentType}    ` : ''}${receiptValue}`

    receiptSpan.appendChild(receiptValueSpan)
    receiptSpan.appendChild(document.createElement('br'))

    const hasInstallments = data.paymentType == PAYMENT_TYPE_CREDIT_WITH_INSTALLMENTS && data.installments

    if (hasInstallments) {
      const receiptInstallments = `${data.installments}X SEM JUROS DE`
      const receiptInstallmentValue = currencyFormatter.format(data.value / 100 / data.installments)

      receiptSpan.appendChild(document.createTextNode(`${receiptInstallments}    ${receiptInstallmentValue}`))
      receiptSpan.appendChild(document.createElement('br'))
    }
  }

  const hasBrandAndPan = isCard && data.brand && data.pan
  const hasAidInfo = isPayment && isCard && data.aidLabel && data.aid
  const hasCardInfo = hasBrandAndPan || hasAidInfo

  if (hasCardInfo) {
    let receiptCardInfoSpan = document.createElement('span')
    receiptCardInfoSpan.style.fontSize = '12px'
    receiptCardInfoSpan.style.whiteSpace = 'pre'

    if (hasBrandAndPan) {
      receiptCardInfoSpan.appendChild(document.createTextNode(`${data.brand}    ${data.pan} `))
      receiptCardInfoSpan.appendChild(document.createElement('br'))
    }

    if (hasAidInfo) {
      receiptCardInfoSpan.appendChild(document.createTextNode(`${data.aidLabel}    AID: ${data.aid} `))
      receiptCardInfoSpan.appendChild(document.createElement('br'))
    }

    receiptSpan.appendChild(receiptCardInfoSpan)
  }

  receiptSpan.appendChild(document.createElement('br'))

  const hasApprovalMessage = !!data.approvalMessage

  if (hasApprovalMessage) {
    let receiptApprovalMessageSpan = document.createElement('span')
    receiptApprovalMessageSpan.style.fontSize = '12px'
    receiptApprovalMessageSpan.style.whiteSpace = 'pre'
    receiptApprovalMessageSpan.textContent = data.approvalMessage

    receiptSpan.appendChild(receiptApprovalMessageSpan)
    receiptSpan.appendChild(document.createElement('br'))
  }

  node.appendChild(receiptSpan)
}