项目作者: bensalcie

项目描述 :
Android MPESA library to request STK Push using MPESA Daraja API.
高级语言: Kotlin
项目地址: git://github.com/bensalcie/payhero-android-mpesa.git
创建时间: 2020-10-04T08:53:54Z
项目社区:https://github.com/bensalcie/payhero-android-mpesa

开源协议:

下载


Payhero Android MPESA Library

Android MPESA library to request STK Push using MPESA Daraja API.

Screenshots





(Please note that this library is still under development,Feel free to contact me personally if
you meet any challenge,Thanks)

Sample of Implementation can be found here:

https://github.com/bensalcie/Trivia-Money

How to use the library

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Step 2. Add the dependency

  1. dependencies {
  2. implementation 'com.squareup.retrofit2:retrofit:2.5.0'
  3. implementation 'com.github.bensalcie:payhero-android-mpesa:0.1.6'
  4. }

Step 3. Add this in onCreate() method.

  1. private var mApiClient: DarajaApiClient? = null //Intitialization before on create
  2. mApiClient = DarajaApiClient(
  3. "xxxconsumerkeyxxxxx",
  4. "xxxxxconsumersecretxxxx",
  5. Environment.SANDBOX
  6. )
  7. //use Environment.PRODUCTION when on production
  8. //get consumerkey and secret from https://developer.safaricom.co.ke/user/me/apps
  9. mApiClient!!.setIsDebug(true) //Set True to enable logging, false to disable.
  10. getAccessToken()//make request availabe and ready for processing.

Step 4.Define access token method.

  1. //Access token Method being called.
  2. private fun getAccessToken() {
  3. mApiClient!!.setGetAccessToken(true)
  4. mApiClient!!.mpesaService()!!.getAccessToken().enqueue(object : Callback<AccessToken> {
  5. override fun onResponse(call: Call<AccessToken?>, response: Response<AccessToken>) {
  6. if (response.isSuccessful) {
  7. mApiClient!!.setAuthToken(response.body()?.accessToken)
  8. }
  9. }
  10. override fun onFailure(call: Call<AccessToken?>, t: Throwable) {}
  11. })
  12. }

Step 5. Initiate STK Push

  1. btnDeposit.setOnClickListener {
  2. val amount = etAmount.text.toString()
  3. val phone =etPhone.text.toString()
  4. if (amount.isNotEmpty() && phone.isNotEmpty()) {
  5. btnDeposit.text = getString(R.string.processing)
  6. performSTKPush(phone, amount)
  7. } else {
  8. etPhone.error = getString(R.string.errorm)
  9. etAmount.error = getString(R.string.errorm)
  10. }
  11. }
  12. private fun performSTKPush(amount: String, phone_number: String) {
  13. //Handle progresss here
  14. //credentials here are test credentials
  15. val timestamp = Utils.getTimestamp()
  16. val stkPush = STKPush("acc ref",amount,"business number","callback url",
  17. Utils.sanitizePhoneNumber(phone_number)!!,"business number",Utils.getPassword("business number",
  18. "passkey", timestamp!!)!!
  19. , Utils.sanitizePhoneNumber(phone_number)!!,timestamp,"Trans. desc",Environment.TransactionType.CustomerPayBillOnline)
  20. mApiClient!!.setGetAccessToken(false)
  21. mApiClient!!.mpesaService()!!.sendPush(stkPush).enqueue(object : Callback<STKResponse> {
  22. override fun onResponse(call: Call<STKResponse>, response: Response<STKResponse>) {
  23. mProgressDialog!!.dismiss()
  24. try {
  25. if (response.isSuccessful) {
  26. if (response.body() != null) {
  27. Log.d("PAYMENTS", "onResponse: Time to process, confirm etc : "+response.body().component1());
  28. //handle response here
  29. //response contains CheckoutRequestID,CustomerMessage,MerchantRequestID,ResponseCode,ResponseDescription
  30. }
  31. } else {
  32. //Timber.e("Response %s", response.errorBody()!!.string())
  33. }
  34. } catch (e: Exception) {
  35. e.printStackTrace()
  36. }
  37. }
  38. override fun onFailure(call: Call<STKResponse>, t: Throwable) {
  39. //mProgressDialog!!.dismiss()
  40. //Timber.e(t)
  41. }
  42. })
  43. }

Buy Me A Coffee

See Real Demo Here