Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GooglePayPaymentMethodLauncher in Payment Sheet custom flow #4113

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions payments-core/api/payments-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1070,15 +1070,15 @@ public final class com/stripe/android/googlepaylauncher/GooglePayLauncherContrac

public final class com/stripe/android/googlepaylauncher/GooglePayLauncherModule {
public fun <init> ()V
public final fun provideGooglePayRepositoryFactory (Landroid/content/Context;)Lkotlin/jvm/functions/Function1;
public final fun provideGooglePayRepositoryFactory (Landroid/content/Context;Lcom/stripe/android/Logger;)Lkotlin/jvm/functions/Function1;
}

public final class com/stripe/android/googlepaylauncher/GooglePayLauncherModule_ProvideGooglePayRepositoryFactoryFactory : dagger/internal/Factory {
public fun <init> (Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule;Ljavax/inject/Provider;)V
public static fun create (Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule;Ljavax/inject/Provider;)Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule_ProvideGooglePayRepositoryFactoryFactory;
public fun <init> (Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule;Ljavax/inject/Provider;Ljavax/inject/Provider;)V
public static fun create (Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule;Ljavax/inject/Provider;Ljavax/inject/Provider;)Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule_ProvideGooglePayRepositoryFactoryFactory;
public synthetic fun get ()Ljava/lang/Object;
public fun get ()Lkotlin/jvm/functions/Function1;
public static fun provideGooglePayRepositoryFactory (Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule;Landroid/content/Context;)Lkotlin/jvm/functions/Function1;
public static fun provideGooglePayRepositoryFactory (Lcom/stripe/android/googlepaylauncher/GooglePayLauncherModule;Landroid/content/Context;Lcom/stripe/android/Logger;)Lkotlin/jvm/functions/Function1;
}

public final class com/stripe/android/googlepaylauncher/GooglePayPaymentMethodLauncher {
Expand Down Expand Up @@ -5207,11 +5207,11 @@ public final class com/stripe/android/paymentsheet/flowcontroller/DefaultFlowCon
}

public final class com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController_Factory : dagger/internal/Factory {
public fun <init> (Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;)V
public static fun create (Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController_Factory;
public fun <init> (Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;)V
public static fun create (Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController_Factory;
public fun get ()Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController;
public synthetic fun get ()Ljava/lang/Object;
public static fun newInstance (Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lcom/stripe/android/paymentsheet/model/PaymentOptionFactory;Lcom/stripe/android/paymentsheet/PaymentOptionCallback;Lcom/stripe/android/paymentsheet/PaymentSheetResultCallback;Landroidx/activity/result/ActivityResultCaller;Lcom/stripe/android/paymentsheet/flowcontroller/FlowControllerInitializer;Lcom/stripe/android/paymentsheet/analytics/EventReporter;Lcom/stripe/android/paymentsheet/flowcontroller/FlowControllerViewModel;Lcom/stripe/android/networking/StripeApiRepository;Lcom/stripe/android/PaymentController;Ldagger/Lazy;Ljavax/inject/Provider;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController;
public static fun newInstance (Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lcom/stripe/android/paymentsheet/model/PaymentOptionFactory;Lcom/stripe/android/paymentsheet/PaymentOptionCallback;Lcom/stripe/android/paymentsheet/PaymentSheetResultCallback;Landroidx/activity/result/ActivityResultCaller;Lcom/stripe/android/paymentsheet/flowcontroller/FlowControllerInitializer;Lcom/stripe/android/paymentsheet/analytics/EventReporter;Lcom/stripe/android/paymentsheet/flowcontroller/FlowControllerViewModel;Lcom/stripe/android/PaymentController;Ldagger/Lazy;Ljavax/inject/Provider;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController;
}

public final class com/stripe/android/paymentsheet/injection/DaggerFlowControllerComponent : com/stripe/android/paymentsheet/injection/FlowControllerComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import java.util.Locale
class GooglePayLauncher internal constructor(
lifecycleScope: CoroutineScope,
private val config: Config,
private val googlePayRepositoryFactory: (GooglePayEnvironment) -> GooglePayRepository,
private val readyCallback: ReadyCallback,
private val activityResultLauncher: ActivityResultLauncher<GooglePayLauncherContract.Args>,
private val googlePayRepositoryFactory: (GooglePayEnvironment) -> GooglePayRepository,
analyticsRequestFactory: AnalyticsRequestFactory,
analyticsRequestExecutor: AnalyticsRequestExecutor = DefaultAnalyticsRequestExecutor()
analyticsRequestExecutor: AnalyticsRequestExecutor
Comment on lines 32 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reordering to match the order of GooglePayPaymentMethodLauncher, no changes to public API.

) {
private var isReady = false

Expand All @@ -56,23 +56,24 @@ class GooglePayLauncher internal constructor(
) : this(
activity.lifecycleScope,
config,
googlePayRepositoryFactory = {
DefaultGooglePayRepository(
activity.application,
it
)
},
readyCallback,
activity.registerForActivityResult(
GooglePayLauncherContract()
) {
resultCallback.onResult(it)
},
googlePayRepositoryFactory = {
DefaultGooglePayRepository(
activity.application,
it
)
},
AnalyticsRequestFactory(
activity,
PaymentConfiguration.getInstance(activity).publishableKey,
setOf(PRODUCT_USAGE)
)
),
DefaultAnalyticsRequestExecutor()
)

/**
Expand All @@ -94,23 +95,24 @@ class GooglePayLauncher internal constructor(
) : this(
fragment.viewLifecycleOwner.lifecycleScope,
config,
googlePayRepositoryFactory = {
DefaultGooglePayRepository(
fragment.requireActivity().application,
it
)
},
readyCallback,
fragment.registerForActivityResult(
GooglePayLauncherContract()
) {
resultCallback.onResult(it)
},
googlePayRepositoryFactory = {
DefaultGooglePayRepository(
fragment.requireActivity().application,
it
)
},
AnalyticsRequestFactory(
fragment.requireContext(),
PaymentConfiguration.getInstance(fragment.requireContext()).publishableKey,
setOf(PRODUCT_USAGE)
)
),
DefaultAnalyticsRequestExecutor()
)

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.googlepaylauncher

import android.content.Context
import com.stripe.android.Logger
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
Expand All @@ -12,10 +13,12 @@ class GooglePayLauncherModule {
@Singleton
fun provideGooglePayRepositoryFactory(
appContext: Context,
logger: Logger
): (GooglePayEnvironment) -> GooglePayRepository = { environment ->
DefaultGooglePayRepository(
appContext,
environment
environment,
logger
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ViewModelStoreOwner
import com.google.android.gms.common.api.Status
import com.stripe.android.PaymentConfiguration
import com.stripe.android.PaymentController
import com.stripe.android.StripeIntentResult
import com.stripe.android.googlepaylauncher.GooglePayConfig
import com.stripe.android.googlepaylauncher.GooglePayEnvironment
import com.stripe.android.googlepaylauncher.GooglePayLauncherResult
import com.stripe.android.googlepaylauncher.StripeGooglePayContract
import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher
import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherContract
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.StripeIntent
import com.stripe.android.networking.ApiRequest
import com.stripe.android.networking.StripeApiRepository
import com.stripe.android.payments.PaymentFlowResult
import com.stripe.android.payments.PaymentFlowResultProcessor
import com.stripe.android.payments.core.injection.Injectable
Expand Down Expand Up @@ -74,7 +71,6 @@ internal class DefaultFlowController @Inject internal constructor(
private val flowControllerInitializer: FlowControllerInitializer,
private val eventReporter: EventReporter,
private val viewModel: FlowControllerViewModel,
private val stripeApiRepository: StripeApiRepository,
private val paymentController: PaymentController,
/**
* [PaymentConfiguration] is [Lazy] because the client might set publishableKey and
Expand All @@ -93,7 +89,8 @@ internal class DefaultFlowController @Inject internal constructor(
Provider<PaymentFlowResultProcessor<out StripeIntent, StripeIntentResult<StripeIntent>>>
) : PaymentSheet.FlowController, Injector {
private val paymentOptionActivityLauncher: ActivityResultLauncher<PaymentOptionContract.Args>
private var googlePayActivityLauncher: ActivityResultLauncher<StripeGooglePayContract.Args>
private var googlePayActivityLauncher:
ActivityResultLauncher<GooglePayPaymentMethodLauncherContract.Args>

/**
* [FlowControllerComponent] is hold to inject into [Activity]s and created
Expand Down Expand Up @@ -145,7 +142,7 @@ internal class DefaultFlowController @Inject internal constructor(
)
googlePayActivityLauncher =
activityResultCaller.registerForActivityResult(
StripeGooglePayContract(),
GooglePayPaymentMethodLauncherContract(),
::onGooglePayResult
)
}
Expand Down Expand Up @@ -234,28 +231,28 @@ internal class DefaultFlowController @Inject internal constructor(
)
}

val config = initData.config
val paymentSelection = viewModel.paymentSelection
if (paymentSelection == PaymentSelection.GooglePay) {
if (initData.stripeIntent !is PaymentIntent) {
error("Google Pay is currently supported only for PaymentIntents")
}
// initData.config.googlePay is guaranteed not to be null or GooglePay would be disabled
val config = requireNotNull(initData.config)
val googlePayConfig = requireNotNull(config.googlePay)

googlePayActivityLauncher.launch(
StripeGooglePayContract.Args(
config = GooglePayConfig(
environment = when (config?.googlePay?.environment) {
GooglePayPaymentMethodLauncherContract.Args(
config = GooglePayPaymentMethodLauncher.Config(
environment = when (googlePayConfig.environment) {
PaymentSheet.GooglePayConfiguration.Environment.Production ->
GooglePayEnvironment.Production
else ->
GooglePayEnvironment.Test
},
amount = initData.stripeIntent.amount?.toInt(),
countryCode = config?.googlePay?.countryCode.orEmpty(),
currencyCode = initData.stripeIntent.currency.orEmpty(),
merchantName = config?.merchantDisplayName,
transactionId = initData.stripeIntent.id
merchantCountryCode = googlePayConfig.countryCode,
merchantName = config.merchantDisplayName
),
statusBarColor = statusBarColor()
currencyCode = (initData.stripeIntent as? PaymentIntent)?.currency
?: googlePayConfig.currencyCode.orEmpty(),
amount = (initData.stripeIntent as? PaymentIntent)?.amount?.toInt() ?: 0,
transactionId = initData.stripeIntent.id
)
)
} else {
Expand Down Expand Up @@ -294,10 +291,10 @@ internal class DefaultFlowController @Inject internal constructor(
}

internal fun onGooglePayResult(
googlePayResult: GooglePayLauncherResult
googlePayResult: GooglePayPaymentMethodLauncher.Result
) {
when (googlePayResult) {
is GooglePayLauncherResult.PaymentData -> {
is GooglePayPaymentMethodLauncher.Result.Completed -> {
runCatching {
viewModel.initData
}.fold(
Expand All @@ -319,25 +316,20 @@ internal class DefaultFlowController @Inject internal constructor(
}
)
}
is GooglePayLauncherResult.Error -> {
is GooglePayPaymentMethodLauncher.Result.Failed -> {
eventReporter.onPaymentFailure(PaymentSelection.GooglePay)
paymentResultCallback.onPaymentSheetResult(
PaymentSheetResult.Failed(
GooglePayException(
googlePayResult.exception,
googlePayResult.googlePayStatus
googlePayResult.error
)
)
)
}
is GooglePayLauncherResult.Canceled -> {
is GooglePayPaymentMethodLauncher.Result.Canceled -> {
// don't log cancellations as failures
paymentResultCallback.onPaymentSheetResult(PaymentSheetResult.Canceled)
}
else -> {
eventReporter.onPaymentFailure(PaymentSelection.GooglePay)
// TODO(mshafrir-stripe): handle other outcomes; for now, treat these as payment failures
}
}
}

Expand Down Expand Up @@ -458,9 +450,8 @@ internal class DefaultFlowController @Inject internal constructor(
}

class GooglePayException(
val throwable: Throwable,
val googleStatus: Status?
) : Exception()
val throwable: Throwable
) : Exception(throwable)

@Parcelize
data class Args(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class DefaultFlowControllerInitializer @Inject constructor(
clientSecret: ClientSecret,
paymentSheetConfiguration: PaymentSheet.Configuration?
) = withContext(workContext) {
val isGooglePayReady = isGooglePayReady(clientSecret, paymentSheetConfiguration)
val isGooglePayReady = isGooglePayReady(paymentSheetConfiguration)
paymentSheetConfiguration?.customer?.let { customerConfig ->
createWithCustomer(
clientSecret,
Expand All @@ -51,11 +51,9 @@ internal class DefaultFlowControllerInitializer @Inject constructor(
}

private suspend fun isGooglePayReady(
clientSecret: ClientSecret,
paymentSheetConfiguration: PaymentSheet.Configuration?
): Boolean {
return clientSecret is PaymentIntentClientSecret &&
paymentSheetConfiguration?.googlePay?.environment?.let { environment ->
return paymentSheetConfiguration?.googlePay?.environment?.let { environment ->
googlePayRepositoryFactory(
when (environment) {
PaymentSheet.GooglePayConfiguration.Environment.Production ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ class GooglePayLauncherTest {
val launcher = GooglePayLauncher(
testScope,
CONFIG,
{ FakeGooglePayRepository(true) },
readyCallback,
fragment.registerForActivityResult(
GooglePayLauncherContract(),
FakeActivityResultRegistry(GooglePayLauncher.Result.Completed)
) {
resultCallback.onResult(it)
},
{ FakeGooglePayRepository(true) },
analyticsRequestFactory,
analyticsRequestExecutor
)
Expand All @@ -91,14 +91,14 @@ class GooglePayLauncherTest {
GooglePayLauncher(
testScope,
CONFIG,
{ FakeGooglePayRepository(true) },
readyCallback,
fragment.registerForActivityResult(
GooglePayLauncherContract(),
FakeActivityResultRegistry(GooglePayLauncher.Result.Completed)
) {
resultCallback.onResult(it)
},
{ FakeGooglePayRepository(true) },
analyticsRequestFactory,
analyticsRequestExecutor
)
Expand All @@ -114,14 +114,14 @@ class GooglePayLauncherTest {
val launcher = GooglePayLauncher(
testScope,
CONFIG,
{ FakeGooglePayRepository(false) },
readyCallback,
fragment.registerForActivityResult(
GooglePayLauncherContract(),
FakeActivityResultRegistry(GooglePayLauncher.Result.Completed)
) {
resultCallback.onResult(it)
},
{ FakeGooglePayRepository(false) },
analyticsRequestFactory,
analyticsRequestExecutor
)
Expand Down
Loading