Skip to content

Commit

Permalink
Merge pull request #129 from VictorKabata/feat/android-sample
Browse files Browse the repository at this point in the history
Added android sample for M-Pesa Express
  • Loading branch information
VictorKabata authored Sep 4, 2024
2 parents 715d506 + a7bc43f commit b41615c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish_swift_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
uses: actions/checkout@v4

- name: Download directory with swift package
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: swiftpackage
path: swiftpackage
Expand Down
8 changes: 8 additions & 0 deletions samples/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)

implementation(platform(libs.koin.bom))
implementation(libs.koin.android)

implementation("io.github.victorkabata:daraja-multiplatform:0.9.6")

testImplementation(libs.junit)

androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)

debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
3 changes: 3 additions & 0 deletions samples/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

<application
android:name=".DarajaAndroidApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.vickbt.daraja.android

import android.app.Application
import com.vickbt.daraja.android.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

class DarajaAndroidApplication:Application() {

override fun onCreate() {
super.onCreate()

startKoin {
androidContext(this@DarajaAndroidApplication)
modules(appModule)
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.vickbt.daraja.android.di

import com.vickbt.darajakmp.Daraja
import org.koin.dsl.module

val appModule = module {
// Provide a single instance of Daraja
single {
Daraja.Builder()
.setConsumerKey("consumer_key")
.setConsumerSecret("consumer_secret")
.setPassKey("pass_key")
.isSandbox()
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

package com.vickbt.daraja.android.ui.screen

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.Send
import androidx.compose.material.icons.rounded.Send
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.FloatingActionButtonDefaults
Expand All @@ -29,23 +31,29 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.vickbt.daraja.android.ui.theme.DarajaAndroidTheme
import com.vickbt.darajakmp.Daraja
import com.vickbt.darajakmp.utils.DarajaTransactionType
import com.vickbt.darajakmp.utils.onFailure
import com.vickbt.darajakmp.utils.onSuccess
import org.koin.compose.koinInject

@Composable
fun MpesaExpressScreen(modifier: Modifier = Modifier) {
fun MpesaExpressScreen(modifier: Modifier = Modifier, daraja: Daraja = koinInject()) {

val context = LocalContext.current

val tillNumber by remember { mutableStateOf("174379") }
var amount by remember { mutableIntStateOf(1) }
var phoneNumber by remember { mutableStateOf("") }

var isLoading by remember { mutableStateOf(false) }

Column(
modifier = modifier.wrapContentSize(),
verticalArrangement = Arrangement.spacedBy(
Expand All @@ -68,6 +76,7 @@ fun MpesaExpressScreen(modifier: Modifier = Modifier) {
label = { Text(text = "Amount") },
colors = TextFieldDefaults.outlinedTextFieldColors(focusedBorderColor = MaterialTheme.colorScheme.primary),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
//keyboardActions = KeyboardOptions.Default.copy(imeAction = ImeAction.Next)
)

OutlinedTextField(
Expand All @@ -84,30 +93,53 @@ fun MpesaExpressScreen(modifier: Modifier = Modifier) {
label = { Text(text = "Phone Number") },
colors = TextFieldDefaults.outlinedTextFieldColors(focusedBorderColor = MaterialTheme.colorScheme.primary),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Phone),
// keyboardActions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done)
)

FloatingActionButton(
modifier = Modifier,
shape = CircleShape,
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary,
elevation = FloatingActionButtonDefaults.elevation(),
onClick = {},
) {
Icon(
modifier = Modifier.size(28.dp),
imageVector = Icons.AutoMirrored.Rounded.Send,
contentDescription = "Pay"
)
Log.e("VicKbt", "Is loading: $isLoading")

Box(modifier = Modifier){
FloatingActionButton(
modifier = Modifier.align(Alignment.Center),
shape = CircleShape,
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary,
elevation = FloatingActionButtonDefaults.elevation(),
onClick = {
isLoading = true

daraja.mpesaExpress(
businessShortCode = tillNumber,
amount = amount,
phoneNumber = phoneNumber,
transactionType = DarajaTransactionType.CustomerPayBillOnline,
transactionDesc = "Empty transaction to test SDK",
callbackUrl = "https://mydomain.com",
accountReference = "CompanyX"
).onSuccess {
Toast.makeText(context, "Success: $it", Toast.LENGTH_SHORT).show()
}.onFailure {
Toast.makeText(context, "Error: ${it.errorMessage}", Toast.LENGTH_SHORT).show()
}

},
) {
Icon(
modifier = Modifier.size(28.dp),
imageVector = Icons.AutoMirrored.Rounded.Send,
contentDescription = "Pay"
)
}

if (isLoading){
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center), trackColor = MaterialTheme.colorScheme.onPrimary)
}
}
}

}

@Composable
@Preview(showBackground = true)
fun MpesaExpressScreenPreview() {
DarajaAndroidTheme(darkTheme = true) {
MpesaExpressScreen()
}
MpesaExpressScreen()
}
4 changes: 4 additions & 0 deletions samples/android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.4"
activityCompose = "1.9.1"
composeBom = "2024.04.01"
koinBom = "3.5.6"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand All @@ -25,6 +26,9 @@ androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-man
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }

koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koinBom" }
koin-android = { module = "io.insert-koin:koin-androidx-compose" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Expand Down

0 comments on commit b41615c

Please sign in to comment.