With the use of this SDK, Android app developers may quickly accept payments through MPESA.
You can use this service for free.
The following is contained in the SDK:
- It takes less than 10 lines of code to accept MPESA payments using the native Android SDK.
- You may easily deploy an example Laravel backend to your server to get going right away.Get it here.
- A model backend database schema
- After payments, integration with Firebase Cloud Messaging makes it simple to deliver push alerts to the app.
- You may quickly launch a complete sample application to see how the SDK works.
- A free hosted backend you can use for testing purposes.
- This lovely README ๐
You will require the following from 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
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { mavenCentral() maven { url 'https://jitpack.io' } } } dependencies { implementation 'com.github.samwelnyandoro:MpesaIntegrationAndroid:1.1.0' }
-
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 usingresult.code
and the error message usingresult.message
. Make sure your credentials are correctpublic 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)
When Safaricom receives the request and accepts it for processing, this function is invoked. A popup for the user to input their MPESA pin will appear.
Congrats! You completed an MPESA transaction successfully.