package com.reactlibrary.verifone.Ttk import android.util.Log import com.reactlibrary.verifone.utils.Convert import com.reactlibrary.verifone.utils.Utils enum class tags(val value: String) { MessageID("01"), ECRnumber("02"), ERN("03"), CloseOperation("30"), TransactionAmount("04"), InvoiceNumber("0B"), AuthorizationID("0C"), SRVsubfunction("1A"), PAN("09"), RRN("18"), TransactionMode("08"), Hash("17"), Currency("1C"); fun get() = value } /** * @Depricated - используем BerTlv2.class */ public class BerTvl(val command: MutableList) { class Builder { private val command: MutableList = mutableListOf() private var arr: ByteArray = byteArrayOf() private fun addTag(newTag: tags, commandString: String) = apply { val enc = Convert.toHex(commandString) val length = enc.length / 2 this.command.add(newTag.get()) this.writeTagLength(length) this.writeCommand(enc) } /** * */ private fun addTagBCD(newTag: tags, commandString: String) = apply { var b: ByteArray = byteArrayOf() commandString.forEach { b = Utils.append(b, byteArrayOf(it.toByte())) } val length = b.size arr = Utils.append(arr, byteArrayOf(newTag.get().toByte())) arr = Utils.append(arr, byteArrayOf(length.toByte())) arr = Utils.append(arr, b) } private fun writeTagLength(length: Int) { when (calculateBytesCountForLength(length)) { 1 -> { if (length > 9) { this.command.add(java.lang.Integer.toHexString(length)) } else { this.command.add("0" + java.lang.Integer.toHexString(length)) } } 2 -> { this.command.add("127") this.command.add(length.toString()) } } } private fun writeCommand(cmd: String) { var i = 1 while (i < cmd.length) { val tmp = cmd[i - 1] + "" + cmd[i] this.command.add(tmp) i += 2 } } private fun calculateBytesCountForLength(aLength: Int): Int { val ret: Byte if (aLength < 128) { ret = 1 } else if (aLength < 256) { ret = 2 } else if (aLength < 65536) { ret = 3 } else { if (aLength >= 16777216) { throw IllegalStateException("length [$aLength] out of range (0x1000000)") } ret = 4 } return ret.toInt() } fun addOperationType(type: String) = apply { this.addTag(tags.MessageID, type) } fun addECRNumber(n: Int) = apply { this.addTag(tags.ECRnumber, n.toString()) } fun addERN(n: Int) = apply { this.addTagBCD(tags.ERN, n.toString()) } fun addInvoiceNumber(n: String) = apply { this.addTag(tags.InvoiceNumber, n) } fun addSRVsubfunction(n: String) = apply { this.addTag(tags.SRVsubfunction, n.toString()) } fun addSRVsubfunction(n: Int) = apply { this.addTag(tags.SRVsubfunction, n.toString()) } fun addAmount(n: Int) = apply { this.addTag(tags.TransactionAmount, n.toString()) } fun closeOperation() = apply { this.addTag(tags.CloseOperation, "") } fun addTransactionMode(mode: String) = apply { this.addTagBCD(tags.TransactionMode, mode) } fun addRRN(rrn: String) = apply { this.addTagBCD(tags.RRN, rrn) } fun addPan(pan: String) = apply { this.addTag(tags.PAN, pan.replace("*", "")) } fun addHash(hash: String) = apply { this.addTag(tags.Hash, hash) } fun AddRnn(rnn: Int) = apply { this.addTag(tags.RRN, rnn.toString()) } fun buildCommand(): String { var result: String = "" val length = this.command.size + 2 val low = length val high = (length shr 8) if (high > 0) { result = high.toString() result += " " } else { result += "00 " } result += Convert.decimal2hex(low) result += " " result += "96" result += " " result += "F3" result += " " this.command.forEach { commandByte -> result += commandByte result += " " } Log.d("ru.lab24.vxlib", "Команда для терминала " + result) return result } fun buildCommandByteArray(): ByteArray { var result: String = "" val length = this.command.size + 2 + arr.size Log.e("ru.lab24.vxlib", "Длинна команды " + length + " из них ASSCII BCD" + arr.size) val low = length val high = (length shr 8) if (high > 0) { result = high.toString() result += " " } else { result += "00 " } result += Convert.decimal2hex(low) result += " " result += "96" result += " " result += "F2" result += " " this.command.forEach { commandByte -> result += commandByte result += " " } arr = Utils.append(Convert.fromHexString(result), arr) Log.e("ru.lab24.vxlib", "Команда для терминала " + result + "\n" + arr.toString()) return arr } } }