Android Mpesa Integration using Daraja Library (Part 1)
Daraja library comes in handy when you need to add Mpesa integration in your android project. In this series, we are going to use Mpesa test credentials available in the sandbox, apply for production credentials for your company, install Node, Install firebase Cli, write firebase function which will receive the response from Safaricom and implement firebase cloud messaging which will send the response to the device which initiated the process.
Let's start
First, create a new empty Android project then add Daraja library dependency in the build.gradle file.
implementation 'com.androidstudy:daraja:1.0.2'
Then let's have our layout ready by pasting this in our activity_main layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter the number to get Stk Push"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.285" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Pay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone" />
</androidx.constraintlayout.widget.ConstraintLayout>
For us to use to Safaricom services we first need to register, go ahead and do it here. After that let's create a new app by clicking on
After you will get these two Consumer Key and Consumer Secret. Please take note of them, we will use them shortly.
After got to Safaricom Portal and get test credentials. Take note of these
Paste this in your MainActivity, Remember to add Consumer and Secret Key where I have indicated so.
class MainActivity : AppCompatActivity() {
lateinit var daraja: Daraja
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
daraja = Daraja.with(
"Paste your consumer Key here",
"Paste your consumer Secret here",
Env.SANDBOX, //for Test use Env.PRODUCTION when in production
object : DarajaListener<AccessToken> {
override fun onResult(accessToken: AccessToken) {
Toast.makeText(
this@MainActivity,
"MPESA TOKEN : ${accessToken.access_token}",
Toast.LENGTH_SHORT
).show()
}
override fun onError(error: String) {
}
})
button.setOnClickListener {
val phoneNumber = phone.text.trim().toString().trim()
val lnmExpress = LNMExpress(
"174379",
"bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919",
TransactionType.CustomerPayBillOnline,
"1",
phoneNumber,
"174379",
phoneNumber,
"https://mycallback.com",
"001ABC",
"Goods Payment"
)
daraja.requestMPESAExpress(lnmExpress,
object : DarajaListener<LNMResult> {
override fun onResult(lnmResult: LNMResult) {
Toast.makeText(
this@MainActivity,
"Response here ${lnmResult.ResponseDescription}",
Toast.LENGTH_SHORT
).show()
}
override fun onError(error: String) {
Toast.makeText(
this@MainActivity,
"Error here $error",
Toast.LENGTH_SHORT
).show()
}
}
)
}
}
}
Note that we used the test credentials in the Daraja function.
This will now pay 1 bob to Safaricom test. Note that Safaricom refunds all the money used in the test. You may realize that our callback URL only has a placeholder, we will implement that on next Episode.
Right now you should be able to enter the phone number you want to pay from and stk menu will pop up if this doesn't happen try another sim card or change your sim card.
If works fine, follow for part two and three, where will be able to receive the callback and updates the UI, if it doesn't work go through again or Dm me on Twitter @Ronnieonly. See you in the next episode.
Next episode — https://medium.com/@otieno/android-mpesa-integration-using-daraja-library-part-2-5e5d07813963
Clone full project with callback implemented here, https://github.com/ronnieotieno/Lipa-na-Mpesa-Express-using-Daraja-Library-for-Android