Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.freddygenicho:mpesa-android:1.1.1'
}
dependencies {
implementation("com.github.freddygenicho:mpesa-android:1.1.1")
}
<dependency>
<groupId>com.github.freddygenicho</groupId>
<artifactId>mpesa-android</artifactId>
<version>1.1.1</version>
</dependency>
libraryDependencies += "com.github.freddygenicho" % "mpesa-android" % "1.1.1"
:dependencies [[com.github.freddygenicho/mpesa-android "1.1.1"]]
Mpesa-Android makes it easy to process online checkout using mpesa as the preferred method of payment.
To get started follow this link to create an account with safaricom developer here.
To make an API call, you will need:
Remember to replace the test key with your live key in production.
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. Lipa na Mpesa Test credentials
Basic Usage:
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. ** Add the dependency **
dependencies {
compile 'com.github.freddygenicho:mpesa-android:1.1.1'
}
Step 3. ** Initialize the SDK **
Create an instance of Mpesa class:
Note:
For Development Purpose use Mode.SANDOX
For Production Purpose use Mode.PRODUCTION
Mpesa mpesa = new Mpesa(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, Mode.SANDBOX);
Step 4. Call getToken()
method to get an authorization token from mpesa API
mpesa.getToken(new TokenListener() {
@Override
public void onToken(Token token) {
//todo save token or call the stkpush();
}
@Override
public void OnError(Throwable throwable) {
//called when an error occures
}
});
public void onToken(Token token)
This method is called when initializing the Mpesa instance is successful. Therefore, you can use this method to make a transaction.
public void OnError(Throwable throwable))
In case of any error an exception is thrown. You can Log.error(throwable.getMessage)
to view the error. Make sure your credentials are correct.
REQUIREMENTS: 1. A business shortcode. For testing purposes. 2. A pass key. 3. The amount to transact. 4. The phone number making the payment.
You can get test credential here
To make a transaction , create an instance of STKPush
and pass in Token
, STKPush
, and
a callback listener new STKListener()
.
STKPush stkPush = new STKPush();
stkPush.setBusinessShortCode(BUSINESS_SHORT_CODE);
stkPush.setTimestamp(STKPush.getTimestamp());
stkPush.setTransactionType(TRANSACTION_TYPE);
stkPush.setAmount(ENTER_AMOUNT);
stkPush.setPartyA(STKPush.sanitizePhoneNumber(ENTER_PHONE_NUMBER));
stkPush.setPartyB(ENTER_PARTYB);
stkPush.setPhoneNumber(STKPush.sanitizePhoneNumber(ENTER_PHONE_NUMBER));
stkPush.setCallBackURL(ENTER_CALLBACKURL);
stkPush.setAccountReference("test");
stkPush.setTransactionDesc("some description");
Call this method to pop up the STK push
mpesa.startStkPush(token, stkPush, new STKListener() {
@Override
public void onResponse(STKPushResponse stkPushResponse) {
Log.e(TAG, "onResponse: " + stkPushResponse.toJson(stkPushResponse));
}
@Override
public void onError(Throwable throwable) {
Log.e(TAG, "onError: " + throwable.getMessage());
}
});
This project is licensed under the MIT License - see the LICENSE.md file for details