Android MPESA library to request STK Push using MPESA Daraja API.
(Please note that this library is still under development,Feel free to contact me personally if you meet any challenge,Thanks)
https://github.com/bensalcie/Trivia-Money
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.github.bensalcie:payhero-android-mpesa:0.1.6'
}
private var mApiClient: DarajaApiClient? = null //Intitialization before on create
mApiClient = DarajaApiClient(
"xxxconsumerkeyxxxxx",
"xxxxxconsumersecretxxxx",
Environment.SANDBOX
)
//use Environment.PRODUCTION when on production
//get consumerkey and secret from https://developer.safaricom.co.ke/user/me/apps
mApiClient!!.setIsDebug(true) //Set True to enable logging, false to disable.
getAccessToken()//make request availabe and ready for processing.
//Access token Method being called.
private fun getAccessToken() {
mApiClient!!.setGetAccessToken(true)
mApiClient!!.mpesaService()!!.getAccessToken().enqueue(object : Callback<AccessToken> {
override fun onResponse(call: Call<AccessToken?>, response: Response<AccessToken>) {
if (response.isSuccessful) {
mApiClient!!.setAuthToken(response.body()?.accessToken)
}
}
override fun onFailure(call: Call<AccessToken?>, t: Throwable) {}
})
}
btnDeposit.setOnClickListener {
val amount = etAmount.text.toString()
val phone =etPhone.text.toString()
if (amount.isNotEmpty() && phone.isNotEmpty()) {
btnDeposit.text = getString(R.string.processing)
performSTKPush(phone, amount)
} else {
etPhone.error = getString(R.string.errorm)
etAmount.error = getString(R.string.errorm)
}
}
private fun performSTKPush(amount: String, phone_number: String) {
//Handle progresss here
//credentials here are test credentials
val timestamp = Utils.getTimestamp()
val stkPush = STKPush("acc ref",amount,"business number","callback url",
Utils.sanitizePhoneNumber(phone_number)!!,"business number",Utils.getPassword("business number",
"passkey", timestamp!!)!!
, Utils.sanitizePhoneNumber(phone_number)!!,timestamp,"Trans. desc",Environment.TransactionType.CustomerPayBillOnline)
mApiClient!!.setGetAccessToken(false)
mApiClient!!.mpesaService()!!.sendPush(stkPush).enqueue(object : Callback<STKResponse> {
override fun onResponse(call: Call<STKResponse>, response: Response<STKResponse>) {
mProgressDialog!!.dismiss()
try {
if (response.isSuccessful) {
if (response.body() != null) {
Log.d("PAYMENTS", "onResponse: Time to process, confirm etc : "+response.body().component1());
//handle response here
//response contains CheckoutRequestID,CustomerMessage,MerchantRequestID,ResponseCode,ResponseDescription
}
} else {
//Timber.e("Response %s", response.errorBody()!!.string())
}
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onFailure(call: Call<STKResponse>, t: Throwable) {
//mProgressDialog!!.dismiss()
//Timber.e(t)
}
})
}