Skip to content

carloscj6/MpesaSTKPush

Repository files navigation

MpesaSTKPush

Lipa na Mpesa STK Push library Android.

Setup

In your build.gradle file add the following dependency;

dependencies {
   implementation 'com.revosleap:mpesapush:1.0.0'
}

In your gradle.properties file, add the consumer and consumer secret keys given from Safaricom developer site. Add as shown below;

DARAJA_CONSUMER_KEY="xxxxxxxxx"
DARAJA_CONSUMER_SECRET="xxxxxxxxx"

Then add internet permission in your manifest as:

<uses-permission android:name="android.permission.INTERNET"/>

Usage

This is pretty simple. After a successful setup, there are two ways to use the library.

Method 1

MpesaPay.sendPush(...)

Sample

MpesaPay.sendPush("07xxxxxxxx", //number to send push to
                "100", //amount of cash
                "174379", //the paybill number
                "174379", //Receiving number
                "APPLES3", //Account number
                "fruits", //Description
                "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", //passkey, provided by safaricom
                "https://spurquoteapp.ga/pusher/pusher.php?title=stk_push&message=result&push_type=individual&regId="// Your callback url

            )

Method 2

Use the a given model class named TransactionDetails

Kotlin

MpesaPay.sendPush(TransactionDetails(....))

Sample

val details= TransactionDetails(
                "07xxxxxxxx", //number to send push to
                "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", //passkey, provided by safaricom
                "120", //amount of cash
                "174379", //the paybill number
                "APPLES3", //Account number
                "https://spurquoteapp.ga/pusher/pusher.php?title=stk_push&message=result&push_type=individual&regId=",
                "174379", //Sending number
                "174379", //Receiving number
                "fruits",
                "CustomerPayBillOnline" //Transaction type
            )

Java

MpesaPay.sendPush(new TransactionDetails(....))

Sample

TransactionDetails details= new TransactionDetails(
              "07xxxxxxxx", //number to send push to
              "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", //passkey, provided by safaricom
              "120", //amount of cash
              "174379", //the paybill number
              "APPLES3", //Account number
              "https://spurquoteapp.ga/pusher/pusher.php?title=stk_push&message=result&push_type=individual&regId=",
              "174379", //Sending number
              "174379", //Receiving number
              "fruits",
              "CustomerPayBillOnline" //Transaction type
          );
          
 MpesaPay.sendPush(details)

and you are good to go.

Visit Safaricom Documentation for more information about the given parameters.

Credits

Thanks to LNMOnlineAndroidSample for the inspiration to come up with this library.

Libraries used

  • Retrofit
  • Retrofit converter Gson
  • okhttp3 Logging interceptor

Contributions

Feel free to fork and create pull requests

Coded with 💙 by revosleap.

About

Lipa na Mpesa STK Push library Android.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published