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

Launch Link Eagerly with new ConfirmationDefinition #9940

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions paymentsheet/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@
<item name="android:windowBackground">@color/stripe_link_window_background</item>
<item name="windowNoTitle">true</item>
</style>

<style name="StripeLinkTranslucentTheme" parent="@android:style/Theme.Translucent">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
9 changes: 9 additions & 0 deletions paymentsheet/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
android:autoRemoveFromRecents="true"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize|smallestScreenSize" />

<activity
android:name="com.stripe.android.link.express.LinkExpressActivity"
android:theme="@style/StripeLinkTranslucentTheme"
android:exported="false"
android:label="@string/stripe_link"
android:windowSoftInputMode="adjustResize"
android:autoRemoveFromRecents="true"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize|smallestScreenSize" />

<activity
android:name="com.stripe.android.link.LinkForegroundActivity"
android:autoRemoveFromRecents="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.stripe.android.core.utils.FeatureFlags
import com.stripe.android.link.model.LinkAccount
import javax.inject.Inject

/**
Expand Down Expand Up @@ -34,7 +35,8 @@ internal class LinkActivityContract @Inject internal constructor(
}

data class Args internal constructor(
internal val configuration: LinkConfiguration
internal val configuration: LinkConfiguration,
internal val linkAccount: LinkAccount?
)

data class Result(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ internal class LinkActivityViewModel @Inject constructor(
.stripeAccountIdProvider { args.stripeAccountId }
.savedStateHandle(handle)
.context(app)
.linkAccount(args.linkAccount)
.build()
.viewModel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.link

import com.stripe.android.link.injection.LinkComponent
import com.stripe.android.link.model.AccountStatus
import com.stripe.android.link.model.LinkAccount
import com.stripe.android.link.ui.inline.UserInput
import com.stripe.android.model.ConsumerSession
import com.stripe.android.model.PaymentMethodCreateParams
Expand All @@ -22,6 +23,8 @@ internal interface LinkConfigurationCoordinator {

fun getAccountStatusFlow(configuration: LinkConfiguration): Flow<AccountStatus>

fun getLinkAccountFlow(configuration: LinkConfiguration): StateFlow<LinkAccount?>

suspend fun signInWithUserInput(
configuration: LinkConfiguration,
userInput: UserInput
Expand Down Expand Up @@ -62,6 +65,10 @@ internal class RealLinkConfigurationCoordinator @Inject internal constructor(
override fun getAccountStatusFlow(configuration: LinkConfiguration): Flow<AccountStatus> =
getLinkPaymentLauncherComponent(configuration).linkAccountManager.accountStatus

override fun getLinkAccountFlow(configuration: LinkConfiguration): StateFlow<LinkAccount?> {
return getLinkPaymentLauncherComponent(configuration).linkAccountManager.linkAccount
}

/**
* Trigger Link sign in with the input collected from the user inline in PaymentSheet, whether
* it's a new or existing account.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.activity.result.ActivityResultRegistry
import com.stripe.android.link.LinkActivityResult.PaymentMethodObtained
import com.stripe.android.link.account.LinkStore
import com.stripe.android.link.injection.LinkAnalyticsComponent
import com.stripe.android.link.model.LinkAccount
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -72,9 +73,11 @@ internal class LinkPaymentLauncher @Inject internal constructor(
*/
fun present(
configuration: LinkConfiguration,
linkAccount: LinkAccount?
) {
val args = LinkActivityContract.Args(
configuration,
configuration = configuration,
linkAccount = linkAccount
)
linkActivityResultLauncher?.launch(args)
analyticsHelper.onLinkLaunched()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ internal class NativeLinkActivityContract @Inject constructor() :
args = NativeLinkArgs(
configuration = input.configuration,
stripeAccountId = paymentConfiguration.stripeAccountId,
publishableKey = paymentConfiguration.publishableKey
publishableKey = paymentConfiguration.publishableKey,
linkAccount = input.linkAccount
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.stripe.android.link

import android.os.Parcelable
import com.stripe.android.link.model.LinkAccount
import kotlinx.parcelize.Parcelize

@Parcelize
internal data class NativeLinkArgs(
val configuration: LinkConfiguration,
val publishableKey: String,
val stripeAccountId: String?
val stripeAccountId: String?,
val linkAccount: LinkAccount?
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ internal class DefaultLinkAccountManager @Inject constructor(
return newAccount
}

internal fun setAccount(linkAccount: LinkAccount?) {
_linkAccount.value = linkAccount
}

override fun setLinkAccountFromLookupResult(
lookup: ConsumerSessionLookup,
startSession: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.stripe.android.link.express

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.VisibleForTesting
import androidx.compose.ui.window.Dialog
import androidx.core.os.bundleOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavHostController
import com.stripe.android.core.Logger
import com.stripe.android.link.NoArgsException
import com.stripe.android.link.theme.DefaultLinkTheme
import com.stripe.android.link.ui.verification.VerificationScreen
import com.stripe.android.link.ui.verification.VerificationViewModel
import com.stripe.android.paymentsheet.BuildConfig

class LinkExpressActivity : ComponentActivity() {
internal var viewModel: VerificationViewModel? = null

@VisibleForTesting
internal lateinit var navController: NavHostController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

try {
viewModel = ViewModelProvider(
owner = this,
factory = VerificationViewModel.factory(
onVerificationSucceeded = { linkAccount ->
dismissWithResult(LinkExpressResult.Authenticated(linkAccount))
},
onChangeEmailClicked = {},
onDismissClicked = {
dismissWithResult(LinkExpressResult.Canceled)
}
)
)[VerificationViewModel::class.java]
} catch (e: NoArgsException) {
Logger.getInstance(BuildConfig.DEBUG).error("Failed to create VerificationViewModel", e)
setResult(Activity.RESULT_CANCELED)
finish()
}

val vm = viewModel ?: return

setContent {
Dialog(
onDismissRequest = {
dismissWithResult(LinkExpressResult.Canceled)
}
) {
DefaultLinkTheme {
VerificationScreen(vm)
}
}
}
}

private fun dismissWithResult(result: LinkExpressResult) {
val bundle = bundleOf(
LinkExpressContract.EXTRA_RESULT to result
)
[email protected](
RESULT_COMPLETE,
intent.putExtras(bundle)
)
[email protected]()
}

companion object {
internal const val EXTRA_ARGS = "link_express_args"
internal const val RESULT_COMPLETE = 57576

internal fun createIntent(
context: Context,
args: LinkExpressArgs
): Intent {
return Intent(context, LinkExpressActivity::class.java)
.putExtra(EXTRA_ARGS, args)
}

internal fun getArgs(savedStateHandle: SavedStateHandle): LinkExpressArgs? {
return savedStateHandle.get<LinkExpressArgs>(EXTRA_ARGS)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.stripe.android.link.express

import android.os.Parcelable
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.model.LinkAccount
import kotlinx.parcelize.Parcelize

@Parcelize
internal data class LinkExpressArgs(
val configuration: LinkConfiguration,
val publishableKey: String,
val stripeAccountId: String?,
val linkAccount: LinkAccount?
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.stripe.android.link.express

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.os.BundleCompat
import com.stripe.android.PaymentConfiguration
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.model.LinkAccount
import javax.inject.Inject

internal class LinkExpressContract @Inject constructor() :
ActivityResultContract<LinkExpressContract.Args, LinkExpressResult>() {

override fun createIntent(context: Context, input: Args): Intent {
val paymentConfiguration = PaymentConfiguration.getInstance(context)
return LinkExpressActivity.createIntent(
context = context,
args = LinkExpressArgs(
configuration = input.configuration,
stripeAccountId = paymentConfiguration.stripeAccountId,
publishableKey = paymentConfiguration.publishableKey,
linkAccount = input.linkAccount
)
)
}

override fun parseResult(resultCode: Int, intent: Intent?): LinkExpressResult {
return when (resultCode) {
LinkExpressActivity.RESULT_COMPLETE -> {
val result = intent?.extras?.let {
BundleCompat.getParcelable(it, EXTRA_RESULT, LinkExpressResult::class.java)
}
return result ?: LinkExpressResult.Canceled
}
Activity.RESULT_CANCELED -> {
LinkExpressResult.Canceled
}
else -> LinkExpressResult.Canceled
}
}

data class Args(
val configuration: LinkConfiguration,
val linkAccount: LinkAccount
)

companion object {
internal const val EXTRA_RESULT =
"com.stripe.android.link.express.LinkExpressContract.extra_result"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.stripe.android.link.express

import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.model.LinkAccount
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
internal class LinkExpressLauncher @Inject constructor(
private val linkExpressContract: LinkExpressContract
) {
private var linkActivityResultLauncher:
ActivityResultLauncher<LinkExpressContract.Args>? = null

fun register(
activityResultRegistry: ActivityResultRegistry,
callback: (LinkExpressResult) -> Unit,
) {
linkActivityResultLauncher = activityResultRegistry.register(
"LinkPaymentLauncher",
linkExpressContract,
) { linkExpressResult ->
handleActivityResult(linkExpressResult, callback)
}
}

fun register(
activityResultCaller: ActivityResultCaller,
callback: (LinkExpressResult) -> Unit,
) {
linkActivityResultLauncher = activityResultCaller.registerForActivityResult(
linkExpressContract
) { linkExpressContract ->
handleActivityResult(linkExpressContract, callback)
}
}

private fun handleActivityResult(
linkExpressResult: LinkExpressResult,
nextStep: (LinkExpressResult) -> Unit
) {
nextStep(linkExpressResult)
}

fun unregister() {
linkActivityResultLauncher?.unregister()
linkActivityResultLauncher = null
}

/**
* Launch the Link UI to process a payment.
*
* @param configuration The payment and customer settings
*/
fun present(
configuration: LinkConfiguration,
linkAccount: LinkAccount
) {
val args = LinkExpressContract.Args(
configuration = configuration,
linkAccount = linkAccount
)
linkActivityResultLauncher?.launch(args)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.stripe.android.link.express

import android.os.Parcelable
import com.stripe.android.link.model.LinkAccount
import kotlinx.parcelize.Parcelize

@Parcelize
internal sealed interface LinkExpressResult : Parcelable {
@Parcelize
data class Authenticated(val linkAccount: LinkAccount) : LinkExpressResult

@Parcelize
data object Canceled : LinkExpressResult

@Parcelize
data class Failed(val error: Throwable) : LinkExpressResult
}
Loading
Loading