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.

 
val epochTime : Long = System.currentTimeMillis()

 

Step 3: How to generate encrypted Header secret


Sample Code:

 
val encryptedData = generateEncryptedData(dataToEncrypt.toString(), ENCRYPTION_KEY)
 
 
NOTE:
ENCRYPTION_KEY will be provided by iServeU.
 

Encrypt method:

 
import android.util.Base64
import java.nio.charset.StandardCharsets 
import java.security.SecureRandom 
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec 
import javax.crypto.spec.SecretKeySpec
fun generateEncryptedData (incomingJsonReq: String,apiKey: String): String { 
    val decodedKey = Base64.decode(apiKey, Base64.NO_WRAP)
    // Generate a random IV 
    val iv = ByteArray(16)
    SecureRandom().nextBytes(iv)
    // Convert payload to byte array
    val payloadBytes = incomingJsonReq.toString().toByteArray()
    // Initialize AES cipher
    val secretKeySpec = SecretKeySpec(decodedKey, "AES") 
    val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") 
    val ivSpec = IvParameterSpec(iv) 
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec)
    // Encrypt the data
    val encryptedBytes = cipher.doFinal(payloadBytes)
    // Combine IV and encrypted data
    val result = ByteArray(iv.size + encryptedBytes.size) 
    System.arraycopy(iv, 0, result, 0, iv.size) 
    System.arraycopy(encryptedBytes, 0, result, iv.size, encryptedBytes.size)
    // Return Base64 encoded result
    return Base64.encodeToString(result, Base64.NO_WRAP)
}

 

 

Note: 

The encrypted method will return one header_secret which will be sent in place of client id and client secret.

Note: 

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(), 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)


 
private val activityResultLauncher = 
      registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == RESULT_OK && result.data != null) {
           val data = result.data!!.getStringExtra("encodedSkipReceiptData") 
           val encryptedData = Base64.decode(data, Base64.DEFAULT)
           val decodeData = String(encryptedData, charset("UTF-8")) 
           val gson = Gson()
           val skipReceiptData = gson.fromJson(decodeData, SkipReceiptData::class.java) 
           Log.d("SKIP_RECEIPT_DATA", skipReceiptData.toString())
           val transactionResponseData =
               gson.fromJson(skipReceiptData.data, TransactionResponse::class.java) 
           Toast.makeText(
               this@MainActivity, """
               Status :${transactionResponseData.status}
               Transaction Mode :${transactionResponseData.transactionMode} 
               Transaction ID :${transactionResponseData.txId}
               Transaction Status :${transactionResponseData.apiComment} 
               Balance :${transactionResponseData.balance}
               Transaction Amount :${skipReceiptData.transactionAmount} 
               Transaction Date :${transactionResponseData.createdDate}
               Transaction client ref id :${skipReceiptData.clientRefId} 
            """, Toast.LENGTH_LONG
           ).show()
    }
}

Skip Receipt Data-

 
import android.util.Base64
import com.google.gson.Gson
data class SkipReceiptData(
  var data: String? = "",
  var status: String? = "",
  var statusDesc: String? = "",
  var clientRefId: String? = "",
  var transactionAmount: String? = ""
  var paramB: String? = "",
  var paramC: String? = ""
){
  fun toBase64(): String {
    // Create a Gson instance for JSON serialization.
    val gson = Gson()
    // Convert the current instance to a JSON string.
    val json = gson.toJson(this)
    // Encode the JSON string to Base64 and return it.
    return Base64.encodeToString(json.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
 }
}
 
data class TransactionResponse(
val transactionMode: String? = null, 
val txId: String? = null,
val bankName: String? = null, 
val updatedDate: String? = null, 
val iin: String? = null,
val originIdentifier: String? = null, 
val createdDate: String? = null, 
val apiTid: String? = null,
val balance: String? = null,
val isRetriable: Boolean? = null,
val ministatement: List<MiniStatementDomain?>? = null, 
val apiComment: String? = null,
val gateway: Int? = null, 
val errors: Any? = null, 
val status: String? = null,
val authCode: String? = null, 
val uidRefId: String? = null,
val depositId: String? = null, 
val statusCode: String? = null,
val clientRefId: String? = null
) {
data class MiniStatementDomain( 
val date: String? = null,
val amount: String? = null, 
val txntype: String? = null, 
val txnDesc: String? = null,
)
}