package com.reactlibrary.verifone.Ttk import android.util.Log import com.reactlibrary.verifone.utils.Convert import com.reactlibrary.verifone.utils.Utils object Ttk_Tags{ val REQUEST_PROTOCOL = byteArrayOf(0x96.toByte(),0xF3.toByte()) val REQUEST_PAN = byteArrayOf(0x09.toByte()) val REQUEST_HASH = byteArrayOf(0x17.toByte()) val REQUEST_CLOSE_OPERATION = byteArrayOf(0x01.toByte()) val REQUEST_MESSAGE_ID = byteArrayOf(0x01.toByte()) val REQUEST_ECR_NUM = byteArrayOf(0x02.toByte()) val REQUEST_ERN = byteArrayOf(0x03.toByte()) val REQUEST_TRANSACTION_AMOUNT = byteArrayOf(0x04.toByte()) val REQUEST_TRANSACTION_MODE = byteArrayOf(0x08.toByte()) val REQUEST_INVOICE_NUMBER = byteArrayOf(0x0B.toByte()) val REQUEST_AUTH_ID = byteArrayOf(0x0C.toByte()) val REQUEST_ORDER_DETAILS = byteArrayOf(0x0D.toByte()) val REQUEST_COMMODITY_CODE = byteArrayOf(0x0E.toByte()) val REQUEST_RRN = byteArrayOf(0x18.toByte()) val REQUEST_CURRENCY = byteArrayOf(0x1B.toByte()) val REQUEST_LANGUAGE = byteArrayOf(0x1C.toByte()) val REQUEST_LOCAL_TIME = byteArrayOf(0x1E.toByte()) val REQUEST_SRV_SUBFUNCTION = byteArrayOf(0x1A.toByte()) } public class BerTlv2(val command: MutableList) { class Builder { private var byteCoommand: ByteArray = byteArrayOf() private fun addTagData(newTag: ByteArray, commandString: String) = apply { Log.d("ru.lab24.vxlib", "тег ====>" + Utils.getHexString(newTag)) this.byteCoommand = Utils.append(this.byteCoommand, newTag) val byteCommand = Convert.fromHexString(Convert.toHexSpacesString(commandString)); this.writeTagLength(byteCommand.size) Log.d("ru.lab24.vxlib", "команда ====>" + Convert.toHexSpacesString(commandString)) this.byteCoommand = Utils.append(this.byteCoommand, byteCommand) } private fun addTagByteData(newTag: ByteArray, command: ByteArray) = apply { Log.d("ru.lab24.vxlib", "тег ====>" + Utils.getHexString(newTag)) this.byteCoommand = Utils.append(this.byteCoommand, newTag) this.writeTagLength(command.size) Log.d("ru.lab24.vxlib", "команда ====>" + Utils.getHexString(newTag)) this.byteCoommand = Utils.append(this.byteCoommand, command) } private fun addTagBCD(newTag: ByteArray, commandString: String) = apply { Log.d("ru.lab24.vxlib", "тег ====>" + Utils.getHexString(newTag)) var b: ByteArray = byteArrayOf() commandString.forEach { b = Utils.append(b, byteArrayOf(it.toByte())) } val length = b.size this.byteCoommand = Utils.append(this.byteCoommand,newTag) this.writeTagLength(length) this.byteCoommand = Utils.append(this.byteCoommand, b) Log.d("ru.lab24.vxlib", "команда ====>" + Utils.getHexString(b )) } private fun Byte.toPositiveInt() = toInt() and 0xFF private fun writeTagLength(length: Int) { when (calculateBytesCountForLength(length)) { 1 -> { if (length > 9) { this.byteCoommand = Utils.append(this.byteCoommand, byteArrayOf(length.toByte())) Log.d("ru.lab24.vxlib", "длинна ====>" + Utils.getHexString(byteArrayOf(length.toByte()))) } else { // this.command.add("0" + java.lang.Integer.toHexString(length)) this.byteCoommand = Utils.append(this.byteCoommand, byteArrayOf(length.toByte())) Log.d("ru.lab24.vxlib", "длинна ====>" + Utils.getHexString(byteArrayOf(length.toByte()))) } } 2 -> { this.byteCoommand = Utils.append(this.byteCoommand, byteArrayOf(127.toByte(),length.toByte())) Log.d("ru.lab24.vxlib", "длинна ====>" + Utils.getHexString(byteArrayOf(127.toByte(),length.toByte()))) // this.command.add("127") // this.command.add(length.toString()) } } } 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.addTagData(Ttk_Tags.REQUEST_MESSAGE_ID, type) } fun addECRNumber(n: Int) = apply { this.addTagData(Ttk_Tags.REQUEST_ECR_NUM, n.toString()) } fun addERN(n: Int) = apply { this.addTagBCD(Ttk_Tags.REQUEST_ERN, n.toString()) } fun addInvoiceNumber(n: String) = apply { this.addTagData(Ttk_Tags.REQUEST_INVOICE_NUMBER, n) } fun addSRVsubfunction(n: String) = apply { this.addTagData(Ttk_Tags.REQUEST_SRV_SUBFUNCTION, n.toString()) } fun addSRVsubfunction(n: Int) = apply { this.addTagData(Ttk_Tags.REQUEST_SRV_SUBFUNCTION, n.toString()) } fun addAmount(n: Int) = apply { this.addTagData(Ttk_Tags.REQUEST_TRANSACTION_AMOUNT, n.toString()) } fun closeOperation() = apply { this.addTagData(Ttk_Tags.REQUEST_SRV_SUBFUNCTION, "") } fun addTransactionMode(mode: Byte) = apply { this.addTagByteData(Ttk_Tags.REQUEST_TRANSACTION_MODE, byteArrayOf(mode.toByte())) } fun addRRN(rrn: String) = apply { this.addTagBCD(Ttk_Tags.REQUEST_RRN, rrn) } fun addPan(pan: String) = apply { this.addTagData(Ttk_Tags.REQUEST_PAN, pan.replace("*", "")) } fun addHash(hash: String) = apply { this.addTagData(Ttk_Tags.REQUEST_HASH, hash) } fun AddRnn(rnn: Int) = apply { this.addTagData(Ttk_Tags.REQUEST_RRN, rnn.toString()) } fun buildCommandByteArray(): ByteArray { val length = this.byteCoommand.size +2 this.byteCoommand = Utils.append(Ttk_Tags.REQUEST_PROTOCOL, this.byteCoommand) val low = length val high = (length shr 8) this.byteCoommand = Utils.append(byteArrayOf(low.toByte()), this.byteCoommand) if (high > 0) { this.byteCoommand = Utils.append(byteArrayOf(high.toByte()), this.byteCoommand) } else { this.byteCoommand = Utils.append(byteArrayOf(0.toByte()), this.byteCoommand) } Log.e("ru.lab24.vxlib", "Длинна команды " + length +" ====> "+ Utils.getHexString( this.byteCoommand)) return this.byteCoommand } } }