This SDK allows android developers to easily make MPESA payments on their apps.
The SDK includes the following:
To get started you need the following from the Safaricom Developer dashboard
For testing purposes, you can get test credentials here. Use the Lipa Na Mpesa Online Shortcode and Lipa Na Mpesa Online Passkey from the link.
Add the SDK to your project
compile 'com.bdhobare:mpesa:0.0.6'
Initialize the SDK
Mpesa.with(context, CONSUMER_KEY, CONSUMER_SECRET);
You can optionally pass the mode as the third parameter , either SANDBOX
or PRODUCTION
.
Mpesa.with(context, CONSUMER_KEY, CONSUMER_SECRET, Mode.PRODUCTION);
Implement the AuthListener
interface in your activity.The interface provides two methods
public void onAuthError(Pair<Integer, String> result)
This method is called when initializing the Mpesa instance fails.You can get the response code using result.code
and the error message using result.message
. Make sure your credentials are correct
public void onAuthSuccess()
This method is called once the Mpesa instance initializes successfully.You can only make a successful transaction after this method is called.Therefore, you can use this method to update the user interface like enabling a disabled button.
Before making a transaction, your need to make sure you have the following items:
The phone number making the payment. It can be in 07xx.. or 254xx.. or +254xx... formats. The SDK will sanitize it for you.
To make a transaction , you build an STKPush
instance and pass it to pay()
method
STKPush.Builder builder = new Builder(BUSINESS_SHORT_CODE, PASSKEY, amount,BUSINESS_SHORT_CODE, phone);
STKPush push = builder.build();
Mpesa.getInstance().pay(context, push);
Make sure your activity implements MpesaListener
. The interface provides two methods you will need to implement
public void onMpesaError(Pair<Integer, String> result)
This method is called if the request could not be processed. You can get the error message generated by using result.message
public void onMpesaSuccess(String MerchantRequestID, String CheckoutRequestID, String CustomerMessage)
This method is called once the request has been received by Safaricom and accepted for processing.The user will be presented with the prompt to enter their MPESA pin
Congrats! You have successfully made an MPESA transaction.
Most of the times you want to receive the transaction details on your server, store them in your database and then send a confirmation notification to the user.
I have include a complete working backend in Laravel you can easily deploy to your server.
For running the sample project, you don't need to deploy your own backend. I have already set up one for you here to use freely. Just replace the credentials and hit run :)
.env
file.php artisan migrate
from the project root.http://YOUR_URL
Set up the callback url to the STKPush.Builder
by calling this method:
builder.setCallBackURL(http://YOUR_URL/mpesa)
Please note that the url is YOUR_URL/mpesa
not YOUR_URL
. /mpesa
is a route in the backend.
Once the above steps are finished, the response from Safaricom will be sent to the backend and inserted into the database.
To send a confirmation notification to the user, you need to add Firebase to your app by following this short guide here
The sample app already has a fully working Firebase implementation.You can copy paste the code to avoid repeating the process. You only need to download your google-services.json file and put it in the app/ folder.
Get your server key from the Firebase Dashboard by going to Project Settings >> Cloud Messaging
Login into your backend server and open the .env
file. Add this line to the file
FIREBASE_SERVER_KEY=YOUR_SERVER_KEY
On your android app, pass the user's Firebase token to STKPush.Builder
by calling this method
builder.setFirebaseRegID(token);
This token is not the server's key from Step 2. It is the one got from onTokenRefresh()
on the user's phone.
Note: This firebase token will be appended to the callback url to get a url of the format http://YOUR_URL/mpesa/token
To go live you only need to pass one extra parameter when initializing the SDK
Mpesa.with(context, CONSUMER_KEY, CONSUMER_SECRET, Mode.PRODUCTION);
As it is with the sandbox, you need to implement AuthListener
interface.
To build the sample project, clone this project, open it in Android Studio and replace the credentials in MainActivity.java
with yours from the Safaricom dashboard.
Create a Firebase project as described above and download your google-services.json
file to the app/
folder.
Hit RUN in Android Studio.
The project is really young and I would appreciate any pull requests or bug reports.Use JIRA for bug reports or email me directly.