Created
April 15, 2023 18:23
-
-
Save realdm/6a7f780b4c6c7778f5b5202a8c2d1ef6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Features(val isEnabled: Boolean) { | |
object SandboxMpesa : Features(BuildConfig.DEBUG) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS Don't forget to register your app in the manifest: More info here https://developer.android.com/guide/topics/manifest/manifest-intro