This SDK allows android developers to easily make MPESA payments on their apps.
The SDK includes the following:
- Android native SDK to accept MPESA payments in less than 10 lines of code
- A sample backend in Laravel you can easily deploy to your server to quickly get started.Get it here.
- A sample DB schema for the backend
- Integration with Firebase Cloud Messaging to easily send push notifications to the app after payments
- A complete sample application you can easily run to see the SDK in action
- A free hosted backend you can use for testing purposes.
- This nice README 😀
To get started you need the following from the Safaricom Developer dashboard
- Your Consumer Key
- Your Consumer Secret
- The business shortcode
- A Firebase project with a server key. Create one here
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
orPRODUCTION
.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 usingresult.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:
-
A business shortcode. For testing purposes , get one here.
-
A pass key. For testing, you can get one from the same link.
-
The amount to transact.
-
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 topay()
methodSTKPush.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 :)
-
Clone the backend from here.If you are new to Laravel you can quickly catch up here.
-
Upload the repository to your server and set up the necessary configurations(Apache, Composer, Laravel, MySQL/PostgreSQL etc).
-
Create a sample database in Postgresql or Mysql and connect it with the project by editing the
.env
file. -
Run the migrations :
php artisan migrate
from the project root. -
Get the url to your backend. I am going to assume you have deployed it to
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 fileFIREBASE_SERVER_KEY=YOUR_SERVER_KEY
-
On your android app, pass the user's Firebase token to
STKPush.Builder
by calling this methodbuilder.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 theapp/
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.