Step1: Steps to generate header secret and encrypted data


Generate header_secret:

{
"client_id":"42Zuw71Ok7e2TGAgHPKttM7PFGMspJLLy3ewq15dhgjtGM9l",
"client_secret":"MDB9krmA8OqYdgjTKflkXXU7BTNAJgVDEWBmhWjQ8YBvAPNKNPLbxnJGSKcKiEV9",
"epoch": "1728987398"
}
Key :- (This key is unique for each client) (Will Provided by iServeU)

Sample Code:

val dataToEncrypt = JSONObject().apply {
put(“client_id“, “42Zuw71Ok7e2TGAgHPKttM7PFGMspJLLy3ewq15dhgjtGM9l“)
put(“client_secret”,
”MDB9krmA8OqYdgjTKflkXXU7BTNAJgVDEWBmhWjQ8YBvAPNKNPLbxnJGSKcKiEV9”)
put(“epoch“,”1728987398”)
}

 

Step2: Get epoch using code


Convert this JSON to String then pass the string and key into encrypt method.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
val epochTime = Instant.now().epochSecond
}

 

Step3: How to call and encrypt the Header secret


Sample Code:

val encryptedData = generateEncryptedData(dataToEncrypt.toString().toByteArray(), ENCRYPTION_KEY)

NOTE:

ENCRYPTION_KEY will be provided by iServeU.

Encrypt method (Kotlin):

private fun generateEncryptedData(dataToEncrypt : ByteArray, key: String?): String?
{
    val AES_KEY_SIZE: Int = 256
    val AES_BLOCK_SIZE: Int = 16
    try {
        val iv = ByteArray(AES_BLOCK_SIZE)
        val random = SecureRandom()
        random.nextBytes(iv)
val decodedKey: ByteArray = Base64.decodeBase64(key)
        val secretKeySpec = SecretKeySpec(decodedKey, "AES")
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, IvParameterSpec(iv))
        val paddedData: ByteArray = pkcs7Padding(dataToEncrypt, AES_BLOCK_SIZE)
        val encrypted = cipher.doFinal(paddedData)
        val result = ByteArray(iv.size + encrypted.size)
        System.arraycopy(iv, 0, result, 0, iv.size)
        System.arraycopy(encrypted, 0, result, iv.size, encrypted.size)
        return Base64.encodeBase64String(result)
    } catch (e: Exception) {
        return null
    }
}
private fun pkcs7Padding(data: ByteArray, blockSize: Int): ByteArray {
    val padding = blockSize - (data.size % blockSize)
    val padText = ByteArray(padding)
    Arrays.fill(padText, padding.toByte())
    val paddedData = ByteArray(data.size + padding)
    System.arraycopy(data, 0, paddedData, 0, data.size)
    System.arraycopy(padText, 0, paddedData, data.size, padding)
    return paddedData
}

NOTE:

  1. The encrypted method will return one header_secret which will be sent in place of client id and clientsecret.
  2. In place of client_id and client_secret send headerSecrets.

 

Step4: Data Model Base64 Encryption of Transaction data


val dataModel = DataModel(/*add all required params*/)
val gson = Gson()
val getData = gson.toJson(dataModel)
val bytes = getData.toString().toByteArray()
val responseToBase64 = String(android.util.Base64.encode(bytes, android.util.Base64.NO_WRAP))

 

Step5: Intent Data to Send


val intent = Intent(this@MainDemoActivity, MorefunServiceActivity::class.java)
intent.putExtra("data", responseToBase64)
intent.putExtra("supportEncryption", true)
startActivity(intent)

 

Step6: Callback data handle


@Override
    public void onPosSDKFinish(String callBackData) {
        Log.d("onPosSDKFinish:", "callBackData: ");
        jsonDataText.setText("callBackData: " + callBackData);
            byte[] encryptedData = Base64.decode(callBackData, Base64.NO_WRAP);
            String decodedData = new String(encryptedData, StandardCharsets.UTF_8);
            jsonDataText.setText("callBackData: " + decodedData);
    }
     @Override
    public void onPosSDKError(String message) {
        Log.d("onFailure:", "message: " + message);
        jsonDataText.setText("message: " + message);
            byte[] encryptedData = Base64.decode(message, Base64.NO_WRAP);
            String decodedMessage = new String(encryptedData, StandardCharsets.UTF_8);
            jsonDataText.setText("callBackData: " + decodedMessage);
    }