Skip to content

Commit 1e35576

Browse files
authored
Documentation
1 parent 5956463 commit 1e35576

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Payhero Android MPESA Library [![](https://jitpack.io/v/bensalcie/payhero-android-mpesa.svg)](https://jitpack.io/#bensalcie/payhero-android-mpesa)
2+
Android MPESA library to request STK Push using MPESA Daraja API.
3+
# Screenshots
4+
  <p float="center">
5+
<img src="https://github.com/bensalcie/payhero-android-mpesa/blob/main/screen.jpg" width="200" />
6+
<img src="https://github.com/bensalcie/payhero-android-mpesa/blob/main/screentwo.jpg" width="200" />
7+
</p>
8+
9+
10+
## How to use the library
11+
12+
## Step 1. Add the JitPack repository to your build file
13+
Add it in your root build.gradle at the end of repositories:
14+
```
15+
allprojects {
16+
repositories {
17+
...
18+
maven { url 'https://jitpack.io' }
19+
}
20+
}
21+
```
22+
## Step 2. Add the dependency
23+
```
24+
dependencies {
25+
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
26+
implementation 'com.github.bensalcie:payhero-android-mpesa:0.1.5'
27+
}
28+
```
29+
## Step 3. Add this in onCreate() method.
30+
```
31+
private var mApiClient: DarajaApiClient? = null //Intitialization before on create
32+
33+
mApiClient = DarajaApiClient(
34+
"xxxconsumerkeyxxxxx",
35+
"xxxxxconsumersecretxxxx",
36+
Environment.SANDBOX
37+
)
38+
//use Environment.PRODUCTION when on production
39+
//get consumerkey and secret from https://developer.safaricom.co.ke/user/me/apps
40+
mApiClient!!.setIsDebug(true) //Set True to enable logging, false to disable.
41+
getAccessToken()//make request availabe and ready for processing.
42+
```
43+
## Step 4.Define access token method.
44+
//Access token Method being called.
45+
private fun getAccessToken() {
46+
mApiClient!!.setGetAccessToken(true)
47+
mApiClient!!.mpesaService()!!.getAccessToken().enqueue(object : Callback<AccessToken> {
48+
override fun onResponse(call: Call<AccessToken?>, response: Response<AccessToken>) {
49+
if (response.isSuccessful) {
50+
mApiClient!!.setAuthToken(response.body()?.accessToken)
51+
}
52+
}
53+
override fun onFailure(call: Call<AccessToken?>, t: Throwable) {}
54+
})
55+
}
56+
## Step 5. Initiate STK Push
57+
```
58+
btnDeposit.setOnClickListener {
59+
val amount = etAmount.text.toString()
60+
val phone =etPhone.text.toString()
61+
if (amount.isNotEmpty() && phone.isNotEmpty()) {
62+
btnDeposit.text = getString(R.string.processing)
63+
performSTKPush(phone, amount)
64+
} else {
65+
etPhone.error = getString(R.string.errorm)
66+
etAmount.error = getString(R.string.errorm)
67+
}
68+
}
69+
private fun performSTKPush(amount: String, phone_number: String) {
70+
//Handle progresss here
71+
//credentials here are test credentials
72+
val timestamp = Utils.getTimestamp()
73+
val stkPush = STKPush("MPESA Android Test",amount,"174379","http://mpesa-requestbin.herokuapp.com/1ajipzt1",
74+
Utils.sanitizePhoneNumber(phone_number)!!,"174379",Utils.getPassword("174379",
75+
"bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", timestamp!!)!!
76+
, Utils.sanitizePhoneNumber(phone_number)!!,timestamp,"Testing",Environment.TransactionType.CustomerPayBillOnline)
77+
mApiClient!!.setGetAccessToken(false)
78+
mApiClient!!.mpesaService()!!.sendPush(stkPush).enqueue(object : Callback<STKResponse> {
79+
override fun onResponse(call: Call<STKResponse>, response: Response<STKResponse>) {
80+
mProgressDialog!!.dismiss()
81+
try {
82+
if (response.isSuccessful) {
83+
//handle response here
84+
//response contains CheckoutRequestID,CustomerMessage,MerchantRequestID,ResponseCode,ResponseDescription
85+
} else {
86+
//Timber.e("Response %s", response.errorBody()!!.string())
87+
}
88+
} catch (e: Exception) {
89+
e.printStackTrace()
90+
}
91+
}
92+
93+
override fun onFailure(call: Call<STKResponse>, t: Throwable) {
94+
//mProgressDialog!!.dismiss()
95+
//Timber.e(t)
96+
}
97+
})
98+
}
99+
100+
```
101+
<a href="https://www.buymeacoffee.com/bensalcie" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

0 commit comments

Comments
 (0)