https://kotlinlang.org logo
#compose
Title
# compose
o

Othman El Jazouli

03/06/2024, 1:08 AM
hey folks, I think I have a special usecase here, with google pay confirmation prompt, I would appreciate some help this is code from a screen that prompts it
Copy code
// google pay confirmation
    var googlePayInfoJson: String? by remember { mutableStateOf(null) }
    googlePayInfoJson?.let {
        MyGooglePayConfirmation(
            googlePayInfoJson = it,
            onConfirmation = {
                googlePayInfoJson = null
                ...
            },
            onError = {
                googlePayInfoJson = null
                ...
            }
        )
    }
Copy code
@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 compose
1
🧵 1
b

Ben Trengrove [G]

03/06/2024, 1:14 AM
Google Pay actually have a small library for Compose which might help? https://developers.googleblog.com/2023/05/jetpack-compose-buttons-for-google-pay-google-wallet.html
o

Othman El Jazouli

03/06/2024, 1:16 AM
yes I use this already, but it is only for the button asset from what I found, but does not actually do the prompt, or am I wrong
im stupid, i just had to wrap the request code in a LunchedEffect, tunnel vision, thanks for checking it out tho!