M-PESA Integration on Android — Part 2

Licio Lentimo
DevCNairobi
Published in
3 min readAug 16, 2019

In the previous part, we dealt with creating the server-side logic for the Lipa Na M-PESA Online API on the Safaricom Developer Portal.

In this post, we are going to create a simple Android application to demonstrate how the API can be implemented on Android. We are going to use Java for this project. So follow through as I will provide code snippets and necessary steps.

Creating the Project

Open Android Studio and create a New Project with an Empty Activity. Head over to your build.gradle app module and import the following dependencies:

implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
implementation 'com.jakewharton.timber:timber:4.7.1'

implementation 'com.github.jumadeveloper:networkmanager:0.0.2'

implementation 'cn.pedant.sweetalert:library:1.3'

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okio:okio:2.1.0'

Create the Layout

Next, we will create a simple layout for our app with EditTexts and one button.

activity_main.xml

Creating our model

We will then have to create the model for our application and create the constructors for the Java classes. Right-click on your project and select New->Package and name it Model. Right-click on Model and select New->Java class and name it AccessToken. This class will be the model class to create our access token.

access token class

Next, right-click on Model and create a new Java class and name it STKPush. On your browser, go to http://www.jsonschema2pojo.org/. Paste the JSON schema for the Lipa na Mpesa API for M-PESA Express Request as shown below and click on Preview.

json to pojo

Copy the contents of the Preview to your STKPush class that we created.

Adding Consumer Secret and Consumer Key

Head over to your gradle.properties file and add your Consumer Key and Secret as shown below.

DARAJA_CONSUMER_KEY="add_consumer_key_here"
DARAJA_CONSUMER_SECRET
="add_consumer_secret_here"

We will then switch to our build.gradle app module file and configure them. Add the following lines of code:

buildTypes.each {    it.buildConfigField 'String', 'CONSUMER_KEY',DARAJA_CONSUMER_KEY
it.buildConfigField 'String', 'CONSUMER_SECRET', DARAJA_CONSUMER_SECRET
}

Creating interceptor for token

Right-click on your project and create a new package and name it Interceptor. Right-click on it and create a new Java class and name it AccessTokenInterceptor. Here we are going to encode the Consumer Key and Secret to create he auth token and pass it as a header for our JSON request.

Still on the same Interceptor package, create another Java class and name it AuthInterceptor. Interceptors are mechanisms of the OkHttp library that can add, remove, or replace request headers. They can also transform the body of those requests that have one. For example, you can use an application interceptor to add request body compression if you’re connecting to a webserver known to support it. Add the code below to the AuthInterceptor class:

Constants

Here, we will create a new Java class and name it Constants. This will house our static strings that we will call in our MainActivity.

Handling API calls

Create a new package and name it Services. Right-click on it and create a new Java class and name it STKPushService. Add the following piece of code:

Create another Java class and name it DarajaApiClient. This will utilize Retrofit and Gson to handle our API calls.

Verifying phone and timestamp

The final Java class we’ll create is called Utils. This will handle the regex format for phone numbers and generate the timestamp as well.

Code for our MainActivity

We are now set to implement the logic of our classes onto our MainActivity class. In our OnCreate method, we first bind our layout elements to the class and set an OnClickListener to our button. We the instantiate our DarajaApiClient class and request for an access token from the Daraja API. We then write a method to perform the STK push upon successful filling of entry fields of phone number and amount.

And that’s it! Run your app on your device and you should be prompted to enter your PIN number. I hope this will ease the burden of the developers out there that are stuck with integration of the Daraja API on Android. It’s that simple. Kindly feel free to reach out on Twitter or comment below for feedback.

You can test the app live here.

--

--

Licio Lentimo
DevCNairobi

I write content on Android and Web technologies. Currently focusing on Cybersecurity. Find me on liciolentimo.com