Othman El Jazouli
03/06/2024, 1:08 AM// google pay confirmation
var googlePayInfoJson: String? by remember { mutableStateOf(null) }
googlePayInfoJson?.let {
MyGooglePayConfirmation(
googlePayInfoJson = it,
onConfirmation = {
googlePayInfoJson = null
...
},
onError = {
googlePayInfoJson = null
...
}
)
}
@Composable
fun MyGooglePayConfirmation(
googlePayInfoJson: String,
onConfirmation: () -> Unit,
onError: () -> Unit
) {
val paymentsClient: PaymentsClient = get()
val resolvePaymentForResult = rememberLauncherForActivityResult(
ActivityResultContracts.StartIntentSenderForResult()
) { result: ActivityResult ->
when (result.resultCode) {
RESULT_OK -> ... onConfirmation()
RESULT_CANCELED -> ...
}
}
val request = PaymentDataRequest.fromJson(googlePayInfoJson)
paymentsClient.loadPaymentData(request).addOnCompleteListener { completedTask ->
if (completedTask.isSuccessful) {
onConfirmation()
} else {
when (val exception = completedTask.exception) {
is ResolvableApiException -> {
resolvePaymentForResult.launch(
IntentSenderRequest.Builder(exception.resolution).build()
)
}
is ApiException -> {
onError()
}
else -> {
onError()
}
}
}
}
}
the issue is that when the main screen shows it there is a chance it may recompose and I end up with multiple google pay prompts, i tried multiple ways to have it compose only once but of course that’s not how it should be done, Im not sure what to do at this point, maybe the whole thing is badly setup, maybe i should export the prompt as an activity with result instead of composeBen Trengrove [G]
03/06/2024, 1:14 AMOthman El Jazouli
03/06/2024, 1:16 AMOthman El Jazouli
03/06/2024, 1:17 AMOthman El Jazouli
03/06/2024, 1:28 AM