AepsDataModel
data class AepsDataModel(
var apiUserName: String = "", var userName: String = "", var clientRefID: String = "", var transactionType: String = "", var transactionAmount: String = "", var shopName: String = "", var brandName: String = "", var paramB: String = "", var paramC: String = "", var agent: String = "", var location: String = "", var headerSecrets: String = "", var skipReceipt: Boolean = false, var passKey: String = "" ) |
Step 1: Steps to generate header secret and encrypted data
Generate header_secret:
{
"client_id":"42Zuw71Ok7e2TGAgHPKttM7PFGMspJLLy3ewq15dhgjtGM9l", "client_secret":"MDB9krmA8OqYdgjTKflkXXU7BTNAJgVDEWBmhWjQ8YBvAPNKNPLbxnJGSKcKiEV9", "epoch": "1724136430" } |
Key :- (This key is unique for each client) (Will Provided by iServeU)
|
Sample Code:
val dataToEncrypt = JSONObject().apply {
put(client_id, client_id_value) put(client_secret,client_secret_value) put(epoch,epoch_value) } |
Step 2: 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 } |
Step 3: How to generate encrypted Header secret
Sample Code:
val encryptedData = generateEncryptedData(dataToEncrypt.toString().toByteArray(),
ENCRYPTION_KEY) NOTE:- ENCRYPTION_KEY will be provided by iServeU. |
Encrypt method:
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 } |
! Notes The encrypted method will return one header_secret which will be sent in place of client id and client secret. |
! Notes In place of client_id and client_secret send headerSecrets. |
Step 4:Base64 Encryption of Transaction data
fun getBase64DataModel(aepsDataModel:AepsDataModel):String
{
val gson = Gson()
val getData = gson.toJson(aepsDataModel)
val bytes = getData.toString().toByteArray()
val responseToBase64 = String(android.util.Base64.encode(bytes, android.util.Base64.NO_WRAP)) return responseToBase64
}
|
Send Data To Service
private fun sendDataToService(
context: Context, activityResultLauncher: ManagedActivityResultLauncher<Intent, ActivityResult> ) { try { val encryptedHeader =generateEncryptedData(dataToEncrypt.toString().toByteArray(), ENCRYPTION_KEY) // Follow the step 1 to generate dataToEncrypt. val aepsDataModel = AepsDataModel( apiUserName =apiUserName , // Mandatory userName = userName, // Mandatory transactionType = transactionType, //mandatory, Set the transaction type 0-Balance Enquiry, 1- Cash Withdrawal, 2-Mini Statement, 3-Aadhaar Pay, 4-Cash Deposit transactionAmount = amount, // mandatory, Set the transaction amount for cash withdrawal, cash deposit, aadhaar pay and for others it to 0 clientRefID = referenceID, shopName = shopName, brandName = brandName, paramB = "", paramC = "", agent = "", location = "", passKey = "",// Mandatory provided by iServeU headerSecrets = encryptedHeader // Mandatory ) // Convert AepsDataModel to Base64 val encodedData = getBase64DataModel(aepsDataModel) val manager: PackageManager = context.packageManager val intent = manager.getLaunchIntentForPackage("com.isu.aepssdk") intent!!.addCategory(Intent.CATEGORY_LAUNCHER) intent.flags = 0 intent.putExtra("dataToAepsSdk", encodedData) activityResultLauncher.launch(intent) } catch (e: Exception) { Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show() } } |
Call back data handle(Kotlin)
val activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{ result -> if (result.resultCode == RESULT_OK && result.data != null) { val encodedData = result.data!!.getStringExtra("encodedSkipReceiptData") if (!encodedData.isNullOrEmpty()) { // Decode the Base64 encoded data val decodedBytes = Base64.decode(encodedData, Base64.DEFAULT) val decodedData = String(decodedBytes) // Parse the JSON back into SkipReceiptData val gson = Gson() val skipReceiptData = gson.fromJson(decodedData, SkipReceiptData::class.java) // Use the decoded SkipReceiptData for example Toast.makeText(this@MainActivity, "Status: ${skipReceiptData.status}, Transaction Amount: $ {skipReceiptData.transactionAmount}", Toast.LENGTH_LONG).show() } } } |