From eb33b99776329ae1749a949f9c2bb0dff1bfb55d Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Tue, 7 Sep 2021 16:18:22 -0700 Subject: [PATCH 1/8] swap paymentController for PaymentLauncher --- .../flowcontroller/DefaultFlowController.kt | 100 +++++++----------- 1 file changed, 39 insertions(+), 61 deletions(-) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt index 72d2b864195..f623b440a3a 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt @@ -12,21 +12,21 @@ import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.OnLifecycleEvent import androidx.lifecycle.ViewModelStoreOwner import com.stripe.android.PaymentConfiguration -import com.stripe.android.PaymentController -import com.stripe.android.StripeIntentResult import com.stripe.android.googlepaylauncher.GooglePayEnvironment import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherContract +import com.stripe.android.model.ConfirmPaymentIntentParams +import com.stripe.android.model.ConfirmSetupIntentParams import com.stripe.android.model.PaymentIntent -import com.stripe.android.model.StripeIntent -import com.stripe.android.networking.ApiRequest -import com.stripe.android.payments.PaymentFlowResult -import com.stripe.android.payments.PaymentFlowResultProcessor import com.stripe.android.payments.core.injection.Injectable import com.stripe.android.payments.core.injection.Injector import com.stripe.android.payments.core.injection.InjectorKey import com.stripe.android.payments.core.injection.UIContext import com.stripe.android.payments.core.injection.WeakMapInjectorRegistry +import com.stripe.android.payments.paymentlauncher.PaymentLauncher +import com.stripe.android.payments.paymentlauncher.PaymentLauncherContract +import com.stripe.android.payments.paymentlauncher.PaymentResult +import com.stripe.android.payments.paymentlauncher.StripePaymentLauncherAssistedFactory import com.stripe.android.paymentsheet.PaymentOptionCallback import com.stripe.android.paymentsheet.PaymentOptionContract import com.stripe.android.paymentsheet.PaymentOptionResult @@ -53,7 +53,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlinx.parcelize.Parcelize import javax.inject.Inject -import javax.inject.Provider import javax.inject.Singleton import kotlin.coroutines.CoroutineContext @@ -63,7 +62,6 @@ internal class DefaultFlowController @Inject internal constructor( private val lifecycleScope: CoroutineScope, lifecycleOwner: LifecycleOwner, private val statusBarColor: () -> Int?, - private val authHostSupplier: () -> AuthActivityStarterHost, private val paymentOptionFactory: PaymentOptionFactory, private val paymentOptionCallback: PaymentOptionCallback, private val paymentResultCallback: PaymentSheetResultCallback, @@ -73,22 +71,12 @@ internal class DefaultFlowController @Inject internal constructor( private val flowControllerInitializer: FlowControllerInitializer, private val eventReporter: EventReporter, private val viewModel: FlowControllerViewModel, - private val paymentController: PaymentController, + private val paymentLauncherFactory: StripePaymentLauncherAssistedFactory, /** * [PaymentConfiguration] is [Lazy] because the client might set publishableKey and * stripeAccountId after creating a [DefaultFlowController]. */ private val lazyPaymentConfiguration: Lazy, - /** - * [PaymentFlowResultProcessor] is wrapped with [Provider] because it needs - * [FlowControllerViewModel.initData] to be set, which might happen multiple times post - * [DefaultFlowController] creation and after [configureWithPaymentIntent] or - * [configureWithPaymentIntent] is called. - * TODO: Observe on [FlowControllerViewModel.initData] change and initialize - * paymentFlowResultProcessor afterwards. - */ - private val paymentFlowResultProcessorProvider: - Provider>>, @UIContext private val uiContext: CoroutineContext ) : PaymentSheet.FlowController, Injector { private val paymentOptionActivityLauncher: ActivityResultLauncher @@ -101,6 +89,8 @@ internal class DefaultFlowController @Inject internal constructor( */ lateinit var flowControllerComponent: FlowControllerComponent + internal var paymentLauncher: PaymentLauncher? = null + override fun inject(injectable: Injectable) { when (injectable) { is PaymentOptionsViewModel.Factory -> { @@ -114,15 +104,19 @@ internal class DefaultFlowController @Inject internal constructor( object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) fun onCreate() { - paymentController.registerLaunchersWithActivityResultCaller( - activityResultCaller, - ::onPaymentFlowResult + paymentLauncher = paymentLauncherFactory.create( + { lazyPaymentConfiguration.get().publishableKey }, + { lazyPaymentConfiguration.get().stripeAccountId }, + activityResultCaller.registerForActivityResult( + PaymentLauncherContract(), + ::onPaymentResult + ) ) } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun onDestroy() { - paymentController.unregisterLaunchers() + paymentLauncher = null } } ) @@ -270,14 +264,14 @@ internal class DefaultFlowController @Inject internal constructor( else -> null }?.let { confirmParams -> lifecycleScope.launch { - paymentController.startConfirmAndAuth( - authHostSupplier(), - confirmParams, - ApiRequest.Options( - apiKey = lazyPaymentConfiguration.get().publishableKey, - stripeAccount = lazyPaymentConfiguration.get().stripeAccountId - ) - ) + when (confirmParams) { + is ConfirmPaymentIntentParams -> { + paymentLauncher?.confirm(confirmParams) + } + is ConfirmSetupIntentParams -> { + paymentLauncher?.confirm(confirmParams) + } + } } } } @@ -394,49 +388,33 @@ internal class DefaultFlowController @Inject internal constructor( } } - internal fun onPaymentFlowResult( - paymentFlowResult: PaymentFlowResult.Unvalidated - ) { + internal fun onPaymentResult(paymentResult: PaymentResult) { lifecycleScope.launch { - runCatching { - paymentFlowResultProcessorProvider.get().processResult( - paymentFlowResult - ) - }.fold( - onSuccess = { - withContext(uiContext) { - paymentResultCallback.onPaymentSheetResult( - createPaymentSheetResult(it) - ) - } - }, - onFailure = { - withContext(uiContext) { - paymentResultCallback.onPaymentSheetResult( - PaymentSheetResult.Failed(it) - ) - } + paymentResultCallback.onPaymentSheetResult( + withContext(uiContext) { + createPaymentSheetResult(paymentResult) } ) } } private fun createPaymentSheetResult( - stripeIntentResult: StripeIntentResult - ) = when (stripeIntentResult.outcome) { - StripeIntentResult.Outcome.SUCCEEDED -> { + paymentResult: PaymentResult) + : PaymentSheetResult = when (paymentResult) { + is PaymentResult.Completed -> { PaymentSheetResult.Completed } - StripeIntentResult.Outcome.CANCELED -> { + is PaymentResult.Canceled -> { PaymentSheetResult.Canceled } + is PaymentResult.Failed -> { + PaymentSheetResult.Failed( + IllegalArgumentException("Failed to confirm intent: ${paymentResult.throwable.message}") + ) + } else -> { PaymentSheetResult.Failed( - error = stripeIntentResult.intent.lastErrorMessage?.let { - IllegalArgumentException( - "Failed to confirm ${stripeIntentResult.intent.javaClass.simpleName}: $it" - ) - } ?: RuntimeException("Failed to complete payment.") + RuntimeException("Failed to complete payment.") ) } } From 8c1919ab9d8f458fc7c1e50058c0425e388ee4d9 Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Tue, 7 Sep 2021 16:43:49 -0700 Subject: [PATCH 2/8] update unit tests --- .../flowcontroller/DefaultFlowController.kt | 1 + .../DefaultFlowControllerTest.kt | 151 +++--------------- 2 files changed, 25 insertions(+), 127 deletions(-) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt index f623b440a3a..e420f8e69b0 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt @@ -89,6 +89,7 @@ internal class DefaultFlowController @Inject internal constructor( */ lateinit var flowControllerComponent: FlowControllerComponent + @VisibleForTesting internal var paymentLauncher: PaymentLauncher? = null override fun inject(injectable: Injectable) { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt index bcd2f244f28..c3c5a7e00b0 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt @@ -29,6 +29,9 @@ import com.stripe.android.model.StripeIntent import com.stripe.android.networking.ApiRequest import com.stripe.android.payments.PaymentFlowResult import com.stripe.android.payments.PaymentFlowResultProcessor +import com.stripe.android.payments.paymentlauncher.PaymentLauncher +import com.stripe.android.payments.paymentlauncher.PaymentResult +import com.stripe.android.payments.paymentlauncher.StripePaymentLauncherAssistedFactory import com.stripe.android.paymentsheet.PaymentOptionCallback import com.stripe.android.paymentsheet.PaymentOptionContract import com.stripe.android.paymentsheet.PaymentOptionResult @@ -76,7 +79,8 @@ internal class DefaultFlowControllerTest { private val paymentOptionCallback = mock() private val paymentResultCallback = mock() - private val paymentController = mock() + private val paymentLauncherAssistedFactory = mock() + private val paymentLauncher = mock() private val eventReporter = mock() private val flowResultProcessor = @@ -403,8 +407,9 @@ internal class DefaultFlowControllerTest { } @Test - fun `confirmPayment() without paymentSelection should not call paymentController`() { - verifyNoMoreInteractions(paymentController) + fun `confirmPayment() without paymentSelection should not call paymentLauncher`() { + flowController.paymentLauncher = paymentLauncher + verifyNoMoreInteractions(paymentLauncher) flowController.configureWithPaymentIntent( PaymentSheetFixtures.CLIENT_SECRET, PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY @@ -414,8 +419,9 @@ internal class DefaultFlowControllerTest { } @Test - fun `confirmPaymentSelection() with new card payment method should start paymentController`() = + fun `confirmPaymentSelection() with new card payment method should start paymentlauncher`() = runBlockingTest { + flowController.paymentLauncher = paymentLauncher flowController.confirmPaymentSelection( NEW_CARD_PAYMENT_SELECTION, InitData( @@ -438,7 +444,8 @@ internal class DefaultFlowControllerTest { } @Test - fun `confirmPaymentSelection() with generic payment method should start paymentController`() { + fun `confirmPaymentSelection() with generic payment method should start paymentLauncher`() { + flowController.paymentLauncher = paymentLauncher flowController.confirmPaymentSelection( GENERIC_PAYMENT_SELECTION, InitData( @@ -479,10 +486,8 @@ internal class DefaultFlowControllerTest { stripeAccount = null ) - verify(paymentController).startConfirmAndAuth( - any(), + verify(paymentLauncher).confirm( eq(confirmPaymentIntentParams), - eq(apiOptions) ) } @@ -534,7 +539,7 @@ internal class DefaultFlowControllerTest { } @Test - fun `onGooglePayResult() when PaymentData result should invoke startConfirmAndAuth() with expected params`() = + fun `onGooglePayResult() when PaymentData result should invoke confirm() with expected params`() = testDispatcher.runBlockingTest { flowController.configureWithPaymentIntent( PaymentSheetFixtures.CLIENT_SECRET, @@ -542,20 +547,15 @@ internal class DefaultFlowControllerTest { ) { _, _ -> } + flowController.paymentLauncher = paymentLauncher + flowController.onGooglePayResult( GooglePayPaymentMethodLauncher.Result.Completed( paymentMethod = PaymentMethodFixtures.CARD_PAYMENT_METHOD ) ) - verify(paymentController).startConfirmAndAuth( - any(), - argWhere { - val params = (it as ConfirmPaymentIntentParams) - params.paymentMethodId == "pm_123456789" - }, - any() - ) + verify(paymentLauncher).confirm(any() as ConfirmPaymentIntentParams) } @Test @@ -580,15 +580,8 @@ internal class DefaultFlowControllerTest { } @Test - fun `onPaymentFlowResult when succeeded should invoke callback with Completed`() = + fun `onPaymentResult when succeeded should invoke callback with Completed`() = testDispatcher.runBlockingTest { - whenever(flowResultProcessor.processResult(any())).thenReturn( - PaymentIntentResult( - PaymentIntentFixtures.PI_WITH_SHIPPING, - StripeIntentResult.Outcome.SUCCEEDED - ) - ) - var isReadyState = false flowController.configureWithPaymentIntent( PaymentSheetFixtures.CLIENT_SECRET @@ -598,48 +591,7 @@ internal class DefaultFlowControllerTest { assertThat(isReadyState) .isTrue() - flowController.onPaymentFlowResult( - PaymentFlowResult.Unvalidated( - clientSecret = PaymentSheetFixtures.CLIENT_SECRET, - flowOutcome = StripeIntentResult.Outcome.CANCELED - ) - ) - - verify(paymentResultCallback).onPaymentSheetResult( - argWhere { paymentResult -> - paymentResult is PaymentSheetResult.Completed - } - ) - } - - @Test - fun `onPaymentFlowResult when processing payment method which has delay should invoke callback with Completed`() = - testDispatcher.runBlockingTest { - whenever(flowResultProcessor.processResult(any())).thenReturn( - PaymentIntentResult( - PaymentIntentFixtures.PI_WITH_SHIPPING.copy( - paymentMethod = PaymentMethodFixtures.SEPA_DEBIT_PAYMENT_METHOD, - status = StripeIntent.Status.Processing - ), - StripeIntentResult.Outcome.UNKNOWN - ) - ) - - var isReadyState = false - flowController.configureWithPaymentIntent( - PaymentSheetFixtures.CLIENT_SECRET - ) { isReady, _ -> - isReadyState = isReady - } - assertThat(isReadyState) - .isTrue() - - flowController.onPaymentFlowResult( - PaymentFlowResult.Unvalidated( - clientSecret = PaymentSheetFixtures.CLIENT_SECRET, - flowOutcome = StripeIntentResult.Outcome.UNKNOWN - ) - ) + flowController.onPaymentResult(PaymentResult.Completed) verify(paymentResultCallback).onPaymentSheetResult( argWhere { paymentResult -> @@ -649,43 +601,7 @@ internal class DefaultFlowControllerTest { } @Test - fun `onPaymentFlowResult when processing payment method which does not have delay should invoke callback with Failed`() = - testDispatcher.runBlockingTest { - whenever(flowResultProcessor.processResult(any())).thenReturn( - PaymentIntentResult( - PaymentIntentFixtures.PI_WITH_SHIPPING.copy( - paymentMethod = PaymentMethodFixtures.CARD_PAYMENT_METHOD, - status = StripeIntent.Status.Processing - ), - StripeIntentResult.Outcome.UNKNOWN - ) - ) - - var isReadyState = false - flowController.configureWithPaymentIntent( - PaymentSheetFixtures.CLIENT_SECRET - ) { isReady, _ -> - isReadyState = isReady - } - assertThat(isReadyState) - .isTrue() - - flowController.onPaymentFlowResult( - PaymentFlowResult.Unvalidated( - clientSecret = PaymentSheetFixtures.CLIENT_SECRET, - flowOutcome = StripeIntentResult.Outcome.UNKNOWN - ) - ) - - verify(paymentResultCallback).onPaymentSheetResult( - argWhere { paymentResult -> - paymentResult is PaymentSheetResult.Failed - } - ) - } - - @Test - fun `onPaymentFlowResult when canceled should invoke callback with Cancelled`() = + fun `onPaymentResult when canceled should invoke callback with Cancelled`() = testDispatcher.runBlockingTest { whenever(flowResultProcessor.processResult(any())).thenReturn( PaymentIntentResult( @@ -703,12 +619,7 @@ internal class DefaultFlowControllerTest { assertThat(isReadyState) .isTrue() - flowController.onPaymentFlowResult( - PaymentFlowResult.Unvalidated( - clientSecret = PaymentSheetFixtures.CLIENT_SECRET, - flowOutcome = StripeIntentResult.Outcome.CANCELED - ) - ) + flowController.onPaymentResult(PaymentResult.Canceled) verify(paymentResultCallback).onPaymentSheetResult( argWhere { paymentResult -> @@ -718,21 +629,9 @@ internal class DefaultFlowControllerTest { } @Test - fun `onPaymentFlowResult when error should invoke callback with Failed`() = + fun `onPaymentResult when error should invoke callback with Failed`() = testDispatcher.runBlockingTest { - whenever(flowResultProcessor.processResult(any())).thenReturn( - PaymentIntentResult( - PaymentIntentFixtures.PI_WITH_LAST_PAYMENT_ERROR, - StripeIntentResult.Outcome.FAILED - ) - ) - - flowController.onPaymentFlowResult( - PaymentFlowResult.Unvalidated( - clientSecret = PaymentSheetFixtures.CLIENT_SECRET, - flowOutcome = StripeIntentResult.Outcome.CANCELED - ) - ) + flowController.onPaymentResult(PaymentResult.Failed(Throwable("error"))) verify(paymentResultCallback).onPaymentSheetResult( argWhere { paymentResult -> @@ -759,7 +658,6 @@ internal class DefaultFlowControllerTest { testScope, lifeCycleOwner, { activity.window.statusBarColor }, - { AuthActivityStarterHost.create(activity) }, PaymentOptionFactory(activity.resources), paymentOptionCallback, paymentResultCallback, @@ -768,9 +666,8 @@ internal class DefaultFlowControllerTest { flowControllerInitializer, eventReporter, ViewModelProvider(activity)[FlowControllerViewModel::class.java], - paymentController, + paymentLauncherAssistedFactory, { PaymentConfiguration.getInstance(activity) }, - { flowResultProcessor }, testDispatcher ) From 53c9e8773064cf3285594700f404ebdd1a0d3e39 Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Tue, 7 Sep 2021 18:08:19 -0700 Subject: [PATCH 3/8] fix lint --- .../paymentsheet/flowcontroller/DefaultFlowController.kt | 8 +++++--- .../flowcontroller/DefaultFlowControllerTest.kt | 3 --- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt index e420f8e69b0..f59862d3748 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt @@ -400,8 +400,8 @@ internal class DefaultFlowController @Inject internal constructor( } private fun createPaymentSheetResult( - paymentResult: PaymentResult) - : PaymentSheetResult = when (paymentResult) { + paymentResult: PaymentResult + ): PaymentSheetResult = when (paymentResult) { is PaymentResult.Completed -> { PaymentSheetResult.Completed } @@ -410,7 +410,9 @@ internal class DefaultFlowController @Inject internal constructor( } is PaymentResult.Failed -> { PaymentSheetResult.Failed( - IllegalArgumentException("Failed to confirm intent: ${paymentResult.throwable.message}") + IllegalArgumentException( + "Failed to confirm intent: ${paymentResult.throwable.message}" + ) ) } else -> { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt index c3c5a7e00b0..34b9d2f56a3 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt @@ -12,7 +12,6 @@ import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat import com.stripe.android.ApiKeyFixtures import com.stripe.android.PaymentConfiguration -import com.stripe.android.PaymentController import com.stripe.android.PaymentIntentResult import com.stripe.android.StripeIntentResult import com.stripe.android.googlepaylauncher.GooglePayEnvironment @@ -27,7 +26,6 @@ import com.stripe.android.model.PaymentMethodCreateParamsFixtures import com.stripe.android.model.PaymentMethodFixtures import com.stripe.android.model.StripeIntent import com.stripe.android.networking.ApiRequest -import com.stripe.android.payments.PaymentFlowResult import com.stripe.android.payments.PaymentFlowResultProcessor import com.stripe.android.payments.paymentlauncher.PaymentLauncher import com.stripe.android.payments.paymentlauncher.PaymentResult @@ -47,7 +45,6 @@ import com.stripe.android.paymentsheet.model.PaymentOptionFactory import com.stripe.android.paymentsheet.model.PaymentSelection import com.stripe.android.paymentsheet.model.SavedSelection import com.stripe.android.view.ActivityScenarioFactory -import com.stripe.android.view.AuthActivityStarterHost import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job From 868bfb23657e7e740d3d0b73a28b26e40dc1c42e Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Tue, 7 Sep 2021 18:41:07 -0700 Subject: [PATCH 4/8] api check --- paymentsheet/api/paymentsheet.api | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paymentsheet/api/paymentsheet.api b/paymentsheet/api/paymentsheet.api index 2e0fc458475..2ed7bc8e060 100644 --- a/paymentsheet/api/paymentsheet.api +++ b/paymentsheet/api/paymentsheet.api @@ -537,11 +537,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 (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;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;Ljavax/inject/Provider;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController_Factory; + public fun (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;ILcom/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;Lkotlin/coroutines/CoroutineContext;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController; + public static fun newInstance (Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;Lkotlin/jvm/functions/Function0;Lcom/stripe/android/paymentsheet/model/PaymentOptionFactory;Lcom/stripe/android/paymentsheet/PaymentOptionCallback;Lcom/stripe/android/paymentsheet/PaymentSheetResultCallback;Landroidx/activity/result/ActivityResultCaller;ILcom/stripe/android/paymentsheet/flowcontroller/FlowControllerInitializer;Lcom/stripe/android/paymentsheet/analytics/EventReporter;Lcom/stripe/android/paymentsheet/flowcontroller/FlowControllerViewModel;Lcom/stripe/android/payments/paymentlauncher/StripePaymentLauncherAssistedFactory;Ldagger/Lazy;Lkotlin/coroutines/CoroutineContext;)Lcom/stripe/android/paymentsheet/flowcontroller/DefaultFlowController; } public final class com/stripe/android/paymentsheet/forms/FormViewModel_Factory : dagger/internal/Factory { From a499b797dd619df26b9b98c15fa6dc763ca241f0 Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Wed, 8 Sep 2021 15:57:39 -0700 Subject: [PATCH 5/8] mock created paymentlauncher via triggering lifecycle event to avoid making variable public --- .../resources/mockito-extensions/org.mockito.plugins.MockMaker | 1 + 1 file changed, 1 insertion(+) create mode 100644 paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 00000000000..1f0955d450f --- /dev/null +++ b/paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline From 25ead4abd15e56b4393bd372fb21165f61be11ac Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Wed, 8 Sep 2021 15:57:39 -0700 Subject: [PATCH 6/8] mock created paymentlauncher via triggering lifecycle event to avoid making variable public --- .../flowcontroller/DefaultFlowController.kt | 3 +-- .../DefaultFlowControllerTest.kt | 27 +++++++++++++------ .../org.mockito.plugins.MockMaker | 1 + 3 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt index f59862d3748..78e38e04e9f 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt @@ -89,8 +89,7 @@ internal class DefaultFlowController @Inject internal constructor( */ lateinit var flowControllerComponent: FlowControllerComponent - @VisibleForTesting - internal var paymentLauncher: PaymentLauncher? = null + private var paymentLauncher: PaymentLauncher? = null override fun inject(injectable: Injectable) { when (injectable) { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt index 34b9d2f56a3..c23843f3edd 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt @@ -7,6 +7,7 @@ import androidx.activity.result.ActivityResultLauncher import androidx.core.content.ContextCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.LifecycleRegistry import androidx.lifecycle.ViewModelProvider import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat @@ -27,8 +28,9 @@ import com.stripe.android.model.PaymentMethodFixtures import com.stripe.android.model.StripeIntent import com.stripe.android.networking.ApiRequest import com.stripe.android.payments.PaymentFlowResultProcessor -import com.stripe.android.payments.paymentlauncher.PaymentLauncher +import com.stripe.android.payments.paymentlauncher.PaymentLauncherContract import com.stripe.android.payments.paymentlauncher.PaymentResult +import com.stripe.android.payments.paymentlauncher.StripePaymentLauncher import com.stripe.android.payments.paymentlauncher.StripePaymentLauncherAssistedFactory import com.stripe.android.paymentsheet.PaymentOptionCallback import com.stripe.android.paymentsheet.PaymentOptionContract @@ -77,7 +79,7 @@ internal class DefaultFlowControllerTest { private val paymentResultCallback = mock() private val paymentLauncherAssistedFactory = mock() - private val paymentLauncher = mock() + private val paymentLauncher = mock() private val eventReporter = mock() private val flowResultProcessor = @@ -130,7 +132,21 @@ internal class DefaultFlowControllerTest { ) ).thenReturn(googlePayActivityLauncher) - whenever(lifeCycleOwner.lifecycle).thenReturn(mock()) + val hostActivityLauncher = mock>() + whenever( + activityResultCaller.registerForActivityResult( + any(), + any() + ) + ).thenReturn(hostActivityLauncher) + + whenever(paymentLauncherAssistedFactory.create(any(), any(), any())) + .thenReturn(paymentLauncher) + + // set lifecycle to CREATED to trigger creation of payment launcher object within flowcontroller. + val lifecycle = LifecycleRegistry(lifeCycleOwner) + lifecycle.currentState = Lifecycle.State.CREATED + whenever(lifeCycleOwner.lifecycle).thenReturn(lifecycle) } @AfterTest @@ -405,7 +421,6 @@ internal class DefaultFlowControllerTest { @Test fun `confirmPayment() without paymentSelection should not call paymentLauncher`() { - flowController.paymentLauncher = paymentLauncher verifyNoMoreInteractions(paymentLauncher) flowController.configureWithPaymentIntent( PaymentSheetFixtures.CLIENT_SECRET, @@ -418,7 +433,6 @@ internal class DefaultFlowControllerTest { @Test fun `confirmPaymentSelection() with new card payment method should start paymentlauncher`() = runBlockingTest { - flowController.paymentLauncher = paymentLauncher flowController.confirmPaymentSelection( NEW_CARD_PAYMENT_SELECTION, InitData( @@ -442,7 +456,6 @@ internal class DefaultFlowControllerTest { @Test fun `confirmPaymentSelection() with generic payment method should start paymentLauncher`() { - flowController.paymentLauncher = paymentLauncher flowController.confirmPaymentSelection( GENERIC_PAYMENT_SELECTION, InitData( @@ -544,8 +557,6 @@ internal class DefaultFlowControllerTest { ) { _, _ -> } - flowController.paymentLauncher = paymentLauncher - flowController.onGooglePayResult( GooglePayPaymentMethodLauncher.Result.Completed( paymentMethod = PaymentMethodFixtures.CARD_PAYMENT_METHOD diff --git a/paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 00000000000..1f0955d450f --- /dev/null +++ b/paymentsheet/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline From e328c29d1901aca62c5b83f30b33306fe583055e Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Wed, 8 Sep 2021 16:26:22 -0700 Subject: [PATCH 7/8] remove unused code and restrict visibility of classes --- payments-core/api/payments-core.api | 67 ----------------- .../com/stripe/android/PaymentController.kt | 5 +- .../payments/PaymentFlowResultProcessor.kt | 5 +- .../core/injection/PaymentCommonModule.kt | 75 ------------------- .../paymentsheet/PaymentSheetActivityTest.kt | 2 - 5 files changed, 2 insertions(+), 152 deletions(-) diff --git a/payments-core/api/payments-core.api b/payments-core/api/payments-core.api index 905ce7b6fba..1c0cb6bab97 100644 --- a/payments-core/api/payments-core.api +++ b/payments-core/api/payments-core.api @@ -499,30 +499,6 @@ public final class com/stripe/android/PaymentConfiguration$Companion { public static synthetic fun init$default (Lcom/stripe/android/PaymentConfiguration$Companion;Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)V } -public abstract interface class com/stripe/android/PaymentController { - public abstract fun confirmAndAuthenticateAlipay (Lcom/stripe/android/model/ConfirmPaymentIntentParams;Lcom/stripe/android/AlipayAuthenticator;Lcom/stripe/android/networking/ApiRequest$Options;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun confirmWeChatPay (Lcom/stripe/android/model/ConfirmPaymentIntentParams;Lcom/stripe/android/networking/ApiRequest$Options;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun getAuthenticateSourceResult (Landroid/content/Intent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun getPaymentIntentResult (Landroid/content/Intent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun getSetupIntentResult (Landroid/content/Intent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun handleNextAction (Lcom/stripe/android/view/AuthActivityStarterHost;Lcom/stripe/android/model/StripeIntent;Lcom/stripe/android/networking/ApiRequest$Options;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun registerLaunchersWithActivityResultCaller (Landroidx/activity/result/ActivityResultCaller;Landroidx/activity/result/ActivityResultCallback;)V - public abstract fun shouldHandlePaymentResult (ILandroid/content/Intent;)Z - public abstract fun shouldHandleSetupResult (ILandroid/content/Intent;)Z - public abstract fun shouldHandleSourceResult (ILandroid/content/Intent;)Z - public abstract fun startAuth (Lcom/stripe/android/view/AuthActivityStarterHost;Ljava/lang/String;Lcom/stripe/android/networking/ApiRequest$Options;Lcom/stripe/android/PaymentController$StripeIntentType;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun startAuthenticateSource (Lcom/stripe/android/view/AuthActivityStarterHost;Lcom/stripe/android/model/Source;Lcom/stripe/android/networking/ApiRequest$Options;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun startConfirmAndAuth (Lcom/stripe/android/view/AuthActivityStarterHost;Lcom/stripe/android/model/ConfirmStripeIntentParams;Lcom/stripe/android/networking/ApiRequest$Options;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun unregisterLaunchers ()V -} - -public final class com/stripe/android/PaymentController$StripeIntentType : java/lang/Enum { - public static final field PaymentIntent Lcom/stripe/android/PaymentController$StripeIntentType; - public static final field SetupIntent Lcom/stripe/android/PaymentController$StripeIntentType; - public static fun valueOf (Ljava/lang/String;)Lcom/stripe/android/PaymentController$StripeIntentType; - public static fun values ()[Lcom/stripe/android/PaymentController$StripeIntentType; -} - public final class com/stripe/android/PaymentIntentResult : com/stripe/android/StripeIntentResult { public static final field $stable I public static final field CREATOR Landroid/os/Parcelable$Creator; @@ -5095,17 +5071,6 @@ public abstract class com/stripe/android/payments/PaymentFlowResultProcessor { protected abstract fun retrieveStripeIntent (Ljava/lang/String;Lcom/stripe/android/networking/ApiRequest$Options;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } -public class com/stripe/android/payments/PaymentIntentFlowResultProcessor : com/stripe/android/payments/PaymentFlowResultProcessor { - public static final field $stable I - public fun (Landroid/content/Context;Ljavax/inject/Provider;Lcom/stripe/android/networking/StripeRepository;ZLkotlin/coroutines/CoroutineContext;)V - public synthetic fun (Landroid/content/Context;Ljavax/inject/Provider;Lcom/stripe/android/networking/StripeRepository;ZLkotlin/coroutines/CoroutineContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - protected fun cancelStripeIntent (Lcom/stripe/android/model/PaymentIntent;Lcom/stripe/android/networking/ApiRequest$Options;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public synthetic fun cancelStripeIntent (Lcom/stripe/android/model/StripeIntent;Lcom/stripe/android/networking/ApiRequest$Options;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - protected fun createStripeIntentResult (Lcom/stripe/android/model/PaymentIntent;ILjava/lang/String;)Lcom/stripe/android/PaymentIntentResult; - public synthetic fun createStripeIntentResult (Lcom/stripe/android/model/StripeIntent;ILjava/lang/String;)Lcom/stripe/android/StripeIntentResult; - protected fun retrieveStripeIntent (Ljava/lang/String;Lcom/stripe/android/networking/ApiRequest$Options;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - public abstract interface class com/stripe/android/payments/core/ActivityResultLauncherHost { public abstract fun onLauncherInvalidated ()V public abstract fun onNewActivityResultCaller (Landroidx/activity/result/ActivityResultCaller;Landroidx/activity/result/ActivityResultCallback;)V @@ -5292,30 +5257,6 @@ public final class com/stripe/android/payments/core/injection/PaymentCommonModul public static fun providePaymentConfiguration (Landroid/content/Context;)Lcom/stripe/android/PaymentConfiguration; } -public final class com/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvidePaymentFlowResultProcessor$payments_core_releaseFactory : dagger/internal/Factory { - public fun (Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;)V - public static fun create (Ljavax/inject/Provider;Ljavax/inject/Provider;Ljavax/inject/Provider;)Lcom/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvidePaymentFlowResultProcessor$payments_core_releaseFactory; - public fun get ()Lcom/stripe/android/payments/PaymentFlowResultProcessor; - public synthetic fun get ()Ljava/lang/Object; - public static fun providePaymentFlowResultProcessor$payments_core_release (Lcom/stripe/android/paymentsheet/model/ClientSecret;Lcom/stripe/android/payments/PaymentIntentFlowResultProcessor;Lcom/stripe/android/payments/SetupIntentFlowResultProcessor;)Lcom/stripe/android/payments/PaymentFlowResultProcessor; -} - -public final class com/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvidePaymentIntentFlowResultProcessor$payments_core_releaseFactory : dagger/internal/Factory { - public fun (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;)Lcom/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvidePaymentIntentFlowResultProcessor$payments_core_releaseFactory; - public fun get ()Lcom/stripe/android/payments/PaymentIntentFlowResultProcessor; - public synthetic fun get ()Ljava/lang/Object; - public static fun providePaymentIntentFlowResultProcessor$payments_core_release (Landroid/content/Context;Ldagger/Lazy;Lcom/stripe/android/networking/StripeApiRepository;ZLkotlin/coroutines/CoroutineContext;)Lcom/stripe/android/payments/PaymentIntentFlowResultProcessor; -} - -public final class com/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvideSetupIntentFlowResultProcessor$payments_core_releaseFactory : dagger/internal/Factory { - public fun (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;)Lcom/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvideSetupIntentFlowResultProcessor$payments_core_releaseFactory; - public fun get ()Lcom/stripe/android/payments/SetupIntentFlowResultProcessor; - public synthetic fun get ()Ljava/lang/Object; - public static fun provideSetupIntentFlowResultProcessor$payments_core_release (Landroid/content/Context;Ldagger/Lazy;Lcom/stripe/android/networking/StripeApiRepository;ZLkotlin/coroutines/CoroutineContext;)Lcom/stripe/android/payments/SetupIntentFlowResultProcessor; -} - public final class com/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvideStripeApiRepository$payments_core_releaseFactory : dagger/internal/Factory { public fun (Ljavax/inject/Provider;Ljavax/inject/Provider;)V public static fun create (Ljavax/inject/Provider;Ljavax/inject/Provider;)Lcom/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvideStripeApiRepository$payments_core_releaseFactory; @@ -5324,14 +5265,6 @@ public final class com/stripe/android/payments/core/injection/PaymentCommonModul public static fun provideStripeApiRepository$payments_core_release (Landroid/content/Context;Ldagger/Lazy;)Lcom/stripe/android/networking/StripeApiRepository; } -public final class com/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvideStripePaymentController$payments_core_releaseFactory : dagger/internal/Factory { - public fun (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;)Lcom/stripe/android/payments/core/injection/PaymentCommonModule_Companion_ProvideStripePaymentController$payments_core_releaseFactory; - public fun get ()Lcom/stripe/android/PaymentController; - public synthetic fun get ()Ljava/lang/Object; - public static fun provideStripePaymentController$payments_core_release (Landroid/content/Context;Lcom/stripe/android/networking/StripeApiRepository;Ldagger/Lazy;Z)Lcom/stripe/android/PaymentController; -} - public final class com/stripe/android/payments/core/injection/PaymentLauncherModule_ProvideApiRequestOptionsFactory : dagger/internal/Factory { public fun (Lcom/stripe/android/payments/core/injection/PaymentLauncherModule;Ljavax/inject/Provider;Ljavax/inject/Provider;)V public static fun create (Lcom/stripe/android/payments/core/injection/PaymentLauncherModule;Ljavax/inject/Provider;Ljavax/inject/Provider;)Lcom/stripe/android/payments/core/injection/PaymentLauncherModule_ProvideApiRequestOptionsFactory; diff --git a/payments-core/src/main/java/com/stripe/android/PaymentController.kt b/payments-core/src/main/java/com/stripe/android/PaymentController.kt index 5f73bf616c3..58b98d267bf 100644 --- a/payments-core/src/main/java/com/stripe/android/PaymentController.kt +++ b/payments-core/src/main/java/com/stripe/android/PaymentController.kt @@ -5,7 +5,6 @@ import android.content.Intent import androidx.activity.result.ActivityResultCallback import androidx.activity.result.ActivityResultCaller import androidx.activity.result.ActivityResultLauncher -import androidx.annotation.RestrictTo import androidx.fragment.app.Fragment import com.stripe.android.exception.APIConnectionException import com.stripe.android.exception.APIException @@ -22,9 +21,7 @@ import com.stripe.android.networking.ApiRequest import com.stripe.android.payments.PaymentFlowResult import com.stripe.android.view.AuthActivityStarterHost -// originally made public for paymentsheet -@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) -interface PaymentController { +internal interface PaymentController { /** * Confirm the Stripe Intent and resolve any next actions */ diff --git a/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt b/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt index 5583fb91da1..ba9899d9edd 100644 --- a/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt +++ b/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt @@ -1,7 +1,6 @@ package com.stripe.android.payments import android.content.Context -import androidx.annotation.RestrictTo import com.stripe.android.Logger import com.stripe.android.PaymentController import com.stripe.android.PaymentIntentResult @@ -20,7 +19,6 @@ import kotlin.coroutines.CoroutineContext /** * Class responsible for processing the result of a [PaymentController] confirm operation. */ -@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) // For paymentsheet sealed class PaymentFlowResultProcessor>( context: Context, private val publishableKeyProvider: Provider, @@ -110,8 +108,7 @@ sealed class PaymentFlowResultProcessor, stripeRepository: StripeRepository, diff --git a/payments-core/src/main/java/com/stripe/android/payments/core/injection/PaymentCommonModule.kt b/payments-core/src/main/java/com/stripe/android/payments/core/injection/PaymentCommonModule.kt index 0eca68950f5..9f306698f41 100644 --- a/payments-core/src/main/java/com/stripe/android/payments/core/injection/PaymentCommonModule.kt +++ b/payments-core/src/main/java/com/stripe/android/payments/core/injection/PaymentCommonModule.kt @@ -3,21 +3,12 @@ package com.stripe.android.payments.core.injection import android.content.Context import androidx.annotation.RestrictTo import com.stripe.android.PaymentConfiguration -import com.stripe.android.PaymentController -import com.stripe.android.StripeIntentResult -import com.stripe.android.StripePaymentController -import com.stripe.android.model.StripeIntent import com.stripe.android.networking.AnalyticsRequestExecutor import com.stripe.android.networking.AnalyticsRequestFactory import com.stripe.android.networking.DefaultAnalyticsRequestExecutor import com.stripe.android.networking.StripeApiRepository import com.stripe.android.networking.StripeRepository -import com.stripe.android.payments.PaymentFlowResultProcessor -import com.stripe.android.payments.PaymentIntentFlowResultProcessor -import com.stripe.android.payments.SetupIntentFlowResultProcessor import com.stripe.android.paymentsheet.model.ClientSecret -import com.stripe.android.paymentsheet.model.PaymentIntentClientSecret -import com.stripe.android.paymentsheet.model.SetupIntentClientSecret import dagger.Binds import dagger.Lazy import dagger.Module @@ -25,7 +16,6 @@ import dagger.Provides import javax.inject.Named import javax.inject.Provider import javax.inject.Singleton -import kotlin.coroutines.CoroutineContext /** * Common module providing payment related dependencies. @@ -71,71 +61,6 @@ abstract class PaymentCommonModule { { lazyPaymentConfiguration.get().publishableKey } ) - @Provides - @Singleton - internal fun provideStripePaymentController( - appContext: Context, - stripeApiRepository: StripeApiRepository, - lazyPaymentConfiguration: Lazy, - @Named(ENABLE_LOGGING) enableLogging: Boolean - ): PaymentController { - return StripePaymentController( - appContext, - { lazyPaymentConfiguration.get().publishableKey }, - stripeApiRepository, - enableLogging - ) - } - - @Provides - @Singleton - internal fun providePaymentIntentFlowResultProcessor( - appContext: Context, - lazyPaymentConfiguration: Lazy, - stripeApiRepository: StripeApiRepository, - @Named(ENABLE_LOGGING) enableLogging: Boolean, - @IOContext workContext: CoroutineContext - ) = PaymentIntentFlowResultProcessor( - appContext, - { lazyPaymentConfiguration.get().publishableKey }, - stripeApiRepository, - enableLogging, - workContext - ) - - @Provides - @Singleton - internal fun provideSetupIntentFlowResultProcessor( - appContext: Context, - lazyPaymentConfiguration: Lazy, - stripeApiRepository: StripeApiRepository, - @Named(ENABLE_LOGGING) enableLogging: Boolean, - @IOContext workContext: CoroutineContext - ) = SetupIntentFlowResultProcessor( - appContext, - { lazyPaymentConfiguration.get().publishableKey }, - stripeApiRepository, - enableLogging, - workContext - ) - - /** - * Fetch the correct [PaymentFlowResultProcessor] based on current [ClientSecret]. - * - * Should always be injected with [Provider]. - */ - @Provides - internal fun providePaymentFlowResultProcessor( - clientSecret: ClientSecret, - paymentIntentFlowResultProcessor: PaymentIntentFlowResultProcessor, - setupIntentFlowResultProcessor: SetupIntentFlowResultProcessor - ): PaymentFlowResultProcessor> { - return when (clientSecret) { - is PaymentIntentClientSecret -> paymentIntentFlowResultProcessor - is SetupIntentClientSecret -> setupIntentFlowResultProcessor - } - } - @Provides @Singleton fun provideAnalyticsRequestFactory( diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt index 77f9255c2c5..7cc136266b1 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt @@ -12,7 +12,6 @@ import com.google.common.truth.Truth.assertThat import com.stripe.android.ApiKeyFixtures import com.stripe.android.Logger import com.stripe.android.PaymentConfiguration -import com.stripe.android.PaymentController import com.stripe.android.PaymentIntentResult import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherContract @@ -720,7 +719,6 @@ internal class PaymentSheetActivityTest { mock>() whenever(paymentFlowResultProcessor.processResult(any())).thenReturn(paymentIntentResult) - val mockPaymentLauncher: PaymentController = mock() PaymentSheetViewModel( ApplicationProvider.getApplicationContext(), PaymentSheetFixtures.ARGS_CUSTOMER_WITH_GOOGLEPAY, From ffdb7d5e7a89321cc26c928952ff45ad60357909 Mon Sep 17 00:00:00 2001 From: Skyler Reimer Date: Wed, 8 Sep 2021 18:10:28 -0700 Subject: [PATCH 8/8] update tests and remove more dead code --- payments-core/api/payments-core.api | 11 ------- .../payments/PaymentFlowResultProcessor.kt | 2 +- .../flowcontroller/DefaultFlowController.kt | 3 -- .../flowcontroller/FlowControllerFactory.kt | 5 ---- .../injection/FlowControllerComponent.kt | 4 --- .../paymentsheet/PaymentSheetActivityTest.kt | 15 +--------- .../DefaultFlowControllerTest.kt | 29 +++++-------------- 7 files changed, 9 insertions(+), 60 deletions(-) diff --git a/payments-core/api/payments-core.api b/payments-core/api/payments-core.api index 6a82ef370d7..d45913e5fff 100644 --- a/payments-core/api/payments-core.api +++ b/payments-core/api/payments-core.api @@ -5060,17 +5060,6 @@ public final class com/stripe/android/payments/PaymentFlowResult$Unvalidated$Com public synthetic fun write (Ljava/lang/Object;Landroid/os/Parcel;I)V } -public abstract class com/stripe/android/payments/PaymentFlowResultProcessor { - public static final field $stable I - public synthetic fun (Landroid/content/Context;Ljavax/inject/Provider;Lcom/stripe/android/networking/StripeRepository;ZLkotlin/coroutines/CoroutineContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public synthetic fun (Landroid/content/Context;Ljavax/inject/Provider;Lcom/stripe/android/networking/StripeRepository;ZLkotlin/coroutines/CoroutineContext;Lkotlin/jvm/internal/DefaultConstructorMarker;)V - protected abstract fun cancelStripeIntent (Lcom/stripe/android/model/StripeIntent;Lcom/stripe/android/networking/ApiRequest$Options;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - protected abstract fun createStripeIntentResult (Lcom/stripe/android/model/StripeIntent;ILjava/lang/String;)Lcom/stripe/android/StripeIntentResult; - protected final fun getStripeRepository ()Lcom/stripe/android/networking/StripeRepository; - public fun processResult (Lcom/stripe/android/payments/PaymentFlowResult$Unvalidated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - protected abstract fun retrieveStripeIntent (Ljava/lang/String;Lcom/stripe/android/networking/ApiRequest$Options;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - public abstract interface class com/stripe/android/payments/core/ActivityResultLauncherHost { public abstract fun onLauncherInvalidated ()V public abstract fun onNewActivityResultCaller (Landroidx/activity/result/ActivityResultCaller;Landroidx/activity/result/ActivityResultCallback;)V diff --git a/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt b/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt index ba9899d9edd..fc301f0303f 100644 --- a/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt +++ b/payments-core/src/main/java/com/stripe/android/payments/PaymentFlowResultProcessor.kt @@ -19,7 +19,7 @@ import kotlin.coroutines.CoroutineContext /** * Class responsible for processing the result of a [PaymentController] confirm operation. */ -sealed class PaymentFlowResultProcessor>( +internal sealed class PaymentFlowResultProcessor>( context: Context, private val publishableKeyProvider: Provider, protected val stripeRepository: StripeRepository, diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt index 4aeaa696a72..19080ad6a6a 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt @@ -45,7 +45,6 @@ import com.stripe.android.paymentsheet.model.PaymentOptionFactory import com.stripe.android.paymentsheet.model.PaymentSelection import com.stripe.android.paymentsheet.model.SavedSelection import com.stripe.android.paymentsheet.model.SetupIntentClientSecret -import com.stripe.android.view.AuthActivityStarterHost import dagger.Lazy import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.isActive @@ -439,7 +438,6 @@ internal class DefaultFlowController @Inject internal constructor( lifecycleOwner: LifecycleOwner, activityResultCaller: ActivityResultCaller, statusBarColor: () -> Int?, - authHostSupplier: () -> AuthActivityStarterHost, paymentOptionFactory: PaymentOptionFactory, paymentOptionCallback: PaymentOptionCallback, paymentResultCallback: PaymentSheetResultCallback @@ -452,7 +450,6 @@ internal class DefaultFlowController @Inject internal constructor( .lifeCycleOwner(lifecycleOwner) .activityResultCaller(activityResultCaller) .statusBarColor(statusBarColor) - .authHostSupplier(authHostSupplier) .paymentOptionFactory(paymentOptionFactory) .paymentOptionCallback(paymentOptionCallback) .paymentResultCallback(paymentResultCallback) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/FlowControllerFactory.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/FlowControllerFactory.kt index 08ba2886d4d..b7f0e64bbde 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/FlowControllerFactory.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/FlowControllerFactory.kt @@ -11,7 +11,6 @@ import com.stripe.android.paymentsheet.PaymentOptionCallback import com.stripe.android.paymentsheet.PaymentSheet import com.stripe.android.paymentsheet.PaymentSheetResultCallback import com.stripe.android.paymentsheet.model.PaymentOptionFactory -import com.stripe.android.view.AuthActivityStarterHost import kotlinx.coroutines.CoroutineScope internal class FlowControllerFactory( @@ -21,7 +20,6 @@ internal class FlowControllerFactory( private val appContext: Context, private val activityResultCaller: ActivityResultCaller, private val statusBarColor: () -> Int?, - private val authHostSupplier: () -> AuthActivityStarterHost, private val paymentOptionFactory: PaymentOptionFactory, private val paymentOptionCallback: PaymentOptionCallback, private val paymentResultCallback: PaymentSheetResultCallback @@ -37,7 +35,6 @@ internal class FlowControllerFactory( activity.applicationContext, activity, { activity.window.statusBarColor }, - { AuthActivityStarterHost.create(activity) }, PaymentOptionFactory(activity.resources), paymentOptionCallback, paymentResultCallback @@ -54,7 +51,6 @@ internal class FlowControllerFactory( fragment.requireContext(), fragment, { fragment.activity?.window?.statusBarColor }, - { AuthActivityStarterHost.create(fragment) }, PaymentOptionFactory(fragment.resources), paymentOptionCallback, paymentResultCallback @@ -68,7 +64,6 @@ internal class FlowControllerFactory( lifecycleOwner = lifecycleOwner, activityResultCaller = activityResultCaller, statusBarColor = statusBarColor, - authHostSupplier = authHostSupplier, paymentOptionFactory = paymentOptionFactory, paymentOptionCallback = paymentOptionCallback, paymentResultCallback = paymentResultCallback diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/injection/FlowControllerComponent.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/injection/FlowControllerComponent.kt index 85adcb1a270..0369870623d 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/injection/FlowControllerComponent.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/injection/FlowControllerComponent.kt @@ -12,7 +12,6 @@ import com.stripe.android.paymentsheet.PaymentOptionsViewModel import com.stripe.android.paymentsheet.PaymentSheetResultCallback import com.stripe.android.paymentsheet.flowcontroller.DefaultFlowController import com.stripe.android.paymentsheet.model.PaymentOptionFactory -import com.stripe.android.view.AuthActivityStarterHost import dagger.BindsInstance import dagger.Component import kotlinx.coroutines.CoroutineScope @@ -52,9 +51,6 @@ internal interface FlowControllerComponent { @BindsInstance fun statusBarColor(statusBarColor: () -> Int?): Builder - @BindsInstance - fun authHostSupplier(authHostSupplier: () -> AuthActivityStarterHost): Builder - @BindsInstance fun paymentOptionFactory(paymentOptionFactory: PaymentOptionFactory): Builder diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt index 7cc136266b1..1b993429e1c 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt @@ -12,7 +12,6 @@ import com.google.common.truth.Truth.assertThat import com.stripe.android.ApiKeyFixtures import com.stripe.android.Logger import com.stripe.android.PaymentConfiguration -import com.stripe.android.PaymentIntentResult import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherContract import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherFactory @@ -23,7 +22,6 @@ import com.stripe.android.model.PaymentIntentFixtures import com.stripe.android.model.PaymentMethod import com.stripe.android.model.PaymentMethodCreateParamsFixtures import com.stripe.android.model.PaymentMethodFixtures -import com.stripe.android.payments.PaymentFlowResultProcessor import com.stripe.android.payments.paymentlauncher.PaymentResult import com.stripe.android.payments.paymentlauncher.StripePaymentLauncherAssistedFactory import com.stripe.android.paymentsheet.PaymentSheetViewModel.CheckoutIdentifier @@ -53,9 +51,7 @@ import kotlinx.coroutines.test.setMain import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith -import org.mockito.kotlin.any import org.mockito.kotlin.mock -import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner import kotlin.test.AfterTest import kotlin.test.BeforeTest @@ -498,10 +494,6 @@ internal class PaymentSheetActivityTest { @Test fun `successful payment should dismiss bottom sheet`() { - val viewModel = createViewModel( - paymentIntentResult = PaymentIntentResult(PaymentIntentFixtures.PI_SUCCEEDED) - ) - val scenario = activityScenario(viewModel) scenario.launch(intent).onActivity { activity -> // wait for bottom sheet to animate in @@ -712,13 +704,8 @@ internal class PaymentSheetActivityTest { private fun createViewModel( paymentIntent: PaymentIntent = PAYMENT_INTENT, - paymentMethods: List = PAYMENT_METHODS, - paymentIntentResult: PaymentIntentResult = PaymentIntentResult(paymentIntent) + paymentMethods: List = PAYMENT_METHODS ): PaymentSheetViewModel = runBlocking { - val paymentFlowResultProcessor = - mock>() - whenever(paymentFlowResultProcessor.processResult(any())).thenReturn(paymentIntentResult) - PaymentSheetViewModel( ApplicationProvider.getApplicationContext(), PaymentSheetFixtures.ARGS_CUSTOMER_WITH_GOOGLEPAY, diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt index c23843f3edd..1551b08bc8e 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowControllerTest.kt @@ -13,8 +13,6 @@ import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat import com.stripe.android.ApiKeyFixtures import com.stripe.android.PaymentConfiguration -import com.stripe.android.PaymentIntentResult -import com.stripe.android.StripeIntentResult import com.stripe.android.googlepaylauncher.GooglePayEnvironment import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherContract @@ -26,8 +24,6 @@ import com.stripe.android.model.PaymentMethodCreateParams import com.stripe.android.model.PaymentMethodCreateParamsFixtures import com.stripe.android.model.PaymentMethodFixtures import com.stripe.android.model.StripeIntent -import com.stripe.android.networking.ApiRequest -import com.stripe.android.payments.PaymentFlowResultProcessor import com.stripe.android.payments.paymentlauncher.PaymentLauncherContract import com.stripe.android.payments.paymentlauncher.PaymentResult import com.stripe.android.payments.paymentlauncher.StripePaymentLauncher @@ -82,9 +78,6 @@ internal class DefaultFlowControllerTest { private val paymentLauncher = mock() private val eventReporter = mock() - private val flowResultProcessor = - mock>>() - private val paymentOptionActivityLauncher = mock>() @@ -132,18 +125,17 @@ internal class DefaultFlowControllerTest { ) ).thenReturn(googlePayActivityLauncher) - val hostActivityLauncher = mock>() whenever( activityResultCaller.registerForActivityResult( any(), any() ) - ).thenReturn(hostActivityLauncher) + ).thenReturn(mock()) whenever(paymentLauncherAssistedFactory.create(any(), any(), any())) .thenReturn(paymentLauncher) - // set lifecycle to CREATED to trigger creation of payment launcher object within flowcontroller. + // set lifecycle to CREATED to trigger creation of payment launcher object within flowController. val lifecycle = LifecycleRegistry(lifeCycleOwner) lifecycle.currentState = Lifecycle.State.CREATED whenever(lifeCycleOwner.lifecycle).thenReturn(lifecycle) @@ -491,10 +483,6 @@ internal class DefaultFlowControllerTest { mandateId = null, mandateData = null, ) - val apiOptions = ApiRequest.Options( - apiKey = ApiKeyFixtures.FAKE_PUBLISHABLE_KEY, - stripeAccount = null - ) verify(paymentLauncher).confirm( eq(confirmPaymentIntentParams), @@ -563,7 +551,11 @@ internal class DefaultFlowControllerTest { ) ) - verify(paymentLauncher).confirm(any() as ConfirmPaymentIntentParams) + verify(paymentLauncher).confirm( + argWhere { params: ConfirmPaymentIntentParams -> + params.paymentMethodId == "pm_123456789" + } + ) } @Test @@ -611,13 +603,6 @@ internal class DefaultFlowControllerTest { @Test fun `onPaymentResult when canceled should invoke callback with Cancelled`() = testDispatcher.runBlockingTest { - whenever(flowResultProcessor.processResult(any())).thenReturn( - PaymentIntentResult( - PaymentIntentFixtures.CANCELLED, - StripeIntentResult.Outcome.CANCELED - ) - ) - var isReadyState = false flowController.configureWithPaymentIntent( PaymentSheetFixtures.CLIENT_SECRET