stripe-android
stripe-android copied to clipboard
[Feature] GooglePayPaymentMethodLauncher for compose
Is your feature request related to a problem? Please describe.
GooglePayPaymentMethodLauncher
works without an issue for activities
/fragments
, but it will be very nice to consider support it an alternative creation method like the PaymentLauncher.Companion.createForCompose
for JetpackCompose
usage.
Describe the solution you'd like
A way to create the GooglePayPaymentMethodLauncher
with just context, without activity/fragment parameter
Describe alternatives you've considered
As I have 1 activity that setups the composable graphs + theme, I can move in that activity the init of the GooglePayPaymentMethodLauncher
and keep in the activity a reference to use afterwards with any LocalContext.current
with the usage of Context.getActivity()
, but I don't like it 😞
fun Context.getActivity(): AppCompatActivity? = when (this) {
is AppCompatActivity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
}
Note: I don't mind working on it myself if it is possible, shall I fork the project and open a PR ?
Hi @GMetaxakis, Thanks for filing the issue. That's definitely something we want to support better. I'll take a look and test the PR.
@brnunes-stripe any luck? I saw a comment on an email on me few days ago but i don't see it here, could i help with anything?
Hi @GMetaxakis, yes, I deleted because I figured out the issue. I'll finish woking on this and submit a PR soon.
Hi @GMetaxakis, yes, I deleted because I figured out the issue. I'll finish woking on this and submit a PR soon.
Great news 😁 happy to have helped
This is implemented in #5274, we're just doing a review and will submit it.
Hello, Version 20.8.0 released today contains the changes mentioned above. Please try it out and let us know if you have any feedback!
Hello, Version 20.8.0 released today contains the changes mentioned above. Please try it out and let us know if you have any feedback!
When I am trying to use it with a wrong clientSecret
it crashes the app instead of showing any error, not sure if this is the actual problem to be honest
java.lang.IllegalStateException: Attempting to launch an unregistered ActivityResultLauncher with
contract com.stripe.android.googlepaylauncher.GooglePayLauncherContract@5f9f58c and
input PaymentIntentArgs(clientSecret=***, config=Config(environment=Test, merchantCountryCode=**, merchantName=***, isEmailRequired=false, billingAddressConfig=BillingAddressConfig(isRequired=false, format=Min, isPhoneNumberRequired=false), existingPaymentMethodRequired=true, allowCreditCards=true)). You must ensure the ActivityResultLauncher is registered before calling launch().
example of usage
PaymentConfiguration.init(
context = LocalContext.current,
publishableKey = publishableKey,
stripeAccountId = stripeAccountId,
)
paymentLauncherGoogle = GooglePayLauncher.rememberLauncher(
config = GooglePayLauncher.Config(
environment = GooglePayEnvironment.Test,
merchantCountryCode = "GR",
merchantName = "Any"
),
readyCallback = {
if (it) {
paymentLauncherGoogle?.presentForPaymentIntent(paymentIntentSecret)
} else {
resultCallback.onPaymentResult(PaymentResult.Failed(Exception("The Gpay isn't ready")))
}
},
resultCallback = { it.mapGoogleResultToGenericResult(resultCallback) }
)
Hi @GMetaxakis I cannot reproduce the crash you're seeing. Can you share more of your code? The full Activity would be very helpful. Also take a look at the example in GooglePayLauncherComposeActivity, starting from it might help.