stripe-android icon indicating copy to clipboard operation
stripe-android copied to clipboard

[Feature] GooglePayPaymentMethodLauncher for compose

Open GMetaxakis opened this issue 2 years ago • 8 comments

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 ?

GMetaxakis avatar Jun 23 '22 17:06 GMetaxakis

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 avatar Jun 24 '22 21:06 brnunes-stripe

@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?

GMetaxakis avatar Jul 05 '22 06:07 GMetaxakis

Hi @GMetaxakis, yes, I deleted because I figured out the issue. I'll finish woking on this and submit a PR soon.

brnunes-stripe avatar Jul 05 '22 18:07 brnunes-stripe

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

GMetaxakis avatar Jul 05 '22 19:07 GMetaxakis

This is implemented in #5274, we're just doing a review and will submit it.

brnunes-stripe avatar Jul 15 '22 07:07 brnunes-stripe

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!

brnunes-stripe avatar Aug 02 '22 00:08 brnunes-stripe

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) }
    )

GMetaxakis avatar Aug 04 '22 11:08 GMetaxakis

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.

brnunes-stripe avatar Aug 06 '22 01:08 brnunes-stripe