Skip to content

Instantly share code, notes, and snippets.

@realdm
Created April 15, 2023 18:23
Show Gist options
  • Save realdm/6a7f780b4c6c7778f5b5202a8c2d1ef6 to your computer and use it in GitHub Desktop.
Save realdm/6a7f780b4c6c7778f5b5202a8c2d1ef6 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompactActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MpesaSdk.pay(
amount = "20.0",
transactionReference = "transactin_reference",
activity = this, // Actvity
onPaymentComplete = {
if(it == null) {
// payment was not successful (Eg: Show error message on your app)
}
else {
// payment was successful do what ever you need (Eg: Show success message)
it.conversationId
it.transactionId
}
}
)
}
class App : Application(){
companion object {
private val debugShortCode = "debug_short_code"
private val mpesaShortCode = "production_short_code"
private const val mPesaApiKey = "mpesa_prod_api_key"
private const val debugApiKey = "mpesa_debug_api_key"
private const val debugPublicKey = """mpesa_debug_public_key"""
private const val mPesaPublicKey = """mpesa_production_public_key"""
}
override fun onCreate() {
super.onCreate()
setupMpesaSdk()
}
private fun setupMpesaSdk() {
MpesaSdk.init(
apiKey = if (Features.SandboxMpesa.isEnabled) debugApiKey else mPesaApiKey,
publicKey = if (Features.SandboxMpesa.isEnabled) debugPublicKey else mPesaPublicKey,
serviceProviderCode = if (Features.SandboxMpesa.isEnabled) debugShortCode else mpesaShortCode,
serviceProviderName = "Ruca", //ruca.co.mz
endpointUrl = if (Features.SandboxMpesa.isEnabled) MpesaSdk.SANDBOX_BASE_URL else MpesaSdk.PRODUCTION_BASE_URL,
serviceProviderLogoUrl = "logo_url"
)
}
}
fun RenderContent() {
var amount by remember {
mutableStateOf("0")
}
var launchPayment by remember {
mutableStateOf(false)
}
val result = remember { mutableStateOf<C2BPaymentSuccess?>(null) }
val launcher = rememberLauncherForActivityResult(C2BResultContract()) {
result.value = it
launchPayment = false
}
result.value?.run {
launchPayment = false
Toast.makeText(LocalContext.current, "Payment result is: $this", Toast.LENGTH_LONG).show()
}
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Column(
modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
value = amount,
onValueChange = {
amount = it
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
label = {
Text("Valor a pagar")
}
)
Button(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
onClick = {
result.value = null
launchPayment = true
}) {
Text(modifier = Modifier.padding(8.dp), text = "Pagar com M-pesa")
}
}
if (launchPayment) {
MpesaSdk.pay(
amount = amount,
launcher = launcher,
transactionReference = UUID.randomUUID().toString().take(6)
)
}
}
}
sealed class Features(val isEnabled: Boolean) {
object SandboxMpesa : Features(BuildConfig.DEBUG)
}
@realdm
Copy link
Author

realdm commented Apr 15, 2023

  1. Configure the sdk in the App.kt file.
    PS Don't forget to register your app in the manifest: More info here https://developer.android.com/guide/topics/manifest/manifest-intro
  2. Depending on how your app is built use the Composable or Activity example to use the sdk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment