-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
rememberLauncher
to use Launchers in Compose (#5274)
- Loading branch information
1 parent
58408c8
commit 2c4e0e8
Showing
12 changed files
with
530 additions
and
75 deletions.
There are no files selected for viewing
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
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
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
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
166 changes: 166 additions & 0 deletions
166
example/src/main/java/com/stripe/example/activity/GooglePayLauncherComposeActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.stripe.example.activity | ||
|
||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.wrapContentWidth | ||
import androidx.compose.material.LinearProgressIndicator | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.ScaffoldState | ||
import androidx.compose.material.rememberScaffoldState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import com.stripe.android.googlepaylauncher.GooglePayEnvironment | ||
import com.stripe.android.googlepaylauncher.GooglePayLauncher | ||
import kotlinx.coroutines.launch | ||
|
||
class GooglePayLauncherComposeActivity : StripeIntentActivity() { | ||
private val googlePayConfig = GooglePayLauncher.Config( | ||
environment = GooglePayEnvironment.Test, | ||
merchantCountryCode = COUNTRY_CODE, | ||
merchantName = "Widget Store", | ||
billingAddressConfig = GooglePayLauncher.BillingAddressConfig( | ||
isRequired = true, | ||
format = GooglePayLauncher.BillingAddressConfig.Format.Full, | ||
isPhoneNumberRequired = false | ||
), | ||
existingPaymentMethodRequired = false | ||
) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
GooglePayLauncherScreen() | ||
} | ||
} | ||
|
||
@Composable | ||
private fun GooglePayLauncherScreen() { | ||
val scaffoldState = rememberScaffoldState() | ||
val scope = rememberCoroutineScope() | ||
|
||
var clientSecret by rememberSaveable { mutableStateOf("") } | ||
var googlePayReady by rememberSaveable { mutableStateOf<Boolean?>(null) } | ||
var completed by rememberSaveable { mutableStateOf(false) } | ||
|
||
LaunchedEffect(Unit) { | ||
if (clientSecret.isBlank()) { | ||
viewModel.createPaymentIntent(COUNTRY_CODE).observe( | ||
this@GooglePayLauncherComposeActivity | ||
) { result -> | ||
result.fold( | ||
onSuccess = { json -> | ||
clientSecret = json.getString("secret") | ||
}, | ||
onFailure = { error -> | ||
scope.launch { | ||
scaffoldState.snackbarHostState.showSnackbar( | ||
"Could not create PaymentIntent. ${error.message}" | ||
) | ||
} | ||
completed = true | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
val googlePayLauncher = GooglePayLauncher.rememberLauncher( | ||
config = googlePayConfig, | ||
readyCallback = { ready -> | ||
if (googlePayReady == null) { | ||
googlePayReady = ready | ||
|
||
if (!ready) { | ||
completed = true | ||
} | ||
|
||
scope.launch { | ||
scaffoldState.snackbarHostState.showSnackbar("Google Pay ready? $ready") | ||
} | ||
} | ||
}, | ||
resultCallback = { result -> | ||
when (result) { | ||
GooglePayLauncher.Result.Completed -> { | ||
"Successfully collected payment." | ||
} | ||
GooglePayLauncher.Result.Canceled -> { | ||
"Customer cancelled Google Pay." | ||
} | ||
is GooglePayLauncher.Result.Failed -> { | ||
"Google Pay failed. ${result.error.message}" | ||
} | ||
}.let { | ||
scope.launch { | ||
scaffoldState.snackbarHostState.showSnackbar(it) | ||
completed = true | ||
} | ||
} | ||
} | ||
) | ||
|
||
GooglePayLauncherScreen( | ||
scaffoldState = scaffoldState, | ||
clientSecret = clientSecret, | ||
googlePayReady = googlePayReady, | ||
completed = completed, | ||
onLaunchGooglePay = { googlePayLauncher.presentForPaymentIntent(it) } | ||
) | ||
} | ||
|
||
@Composable | ||
private fun GooglePayLauncherScreen( | ||
scaffoldState: ScaffoldState, | ||
clientSecret: String, | ||
googlePayReady: Boolean?, | ||
completed: Boolean, | ||
onLaunchGooglePay: (String) -> Unit | ||
) { | ||
Scaffold(scaffoldState = scaffoldState) { | ||
Column(Modifier.fillMaxWidth()) { | ||
if (googlePayReady == null || clientSecret.isBlank()) { | ||
LinearProgressIndicator(Modifier.fillMaxWidth()) | ||
} | ||
|
||
Spacer( | ||
Modifier | ||
.height(8.dp) | ||
.fillMaxWidth() | ||
) | ||
|
||
AndroidView( | ||
factory = { context -> | ||
GooglePayButton(context) | ||
}, | ||
modifier = Modifier | ||
.wrapContentWidth() | ||
.clickable( | ||
enabled = googlePayReady == true && | ||
clientSecret.isNotBlank() && !completed, | ||
onClick = { | ||
onLaunchGooglePay(clientSecret) | ||
} | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private companion object { | ||
private const val COUNTRY_CODE = "US" | ||
} | ||
} |
Oops, something went wrong.