-
Notifications
You must be signed in to change notification settings - Fork 658
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
[Android] Replicate other platforms polling logic across the AuthFlow #6919
Changes from all commits
990a144
cb60298
8356625
e2c2630
25e7e2a
139b41e
56dbd4e
7b3a4d5
3517bb5
808b45f
82d9402
acb5c28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.stripe.android.financialconnections.domain | ||
|
||
import com.stripe.android.financialconnections.FinancialConnectionsSheet | ||
import com.stripe.android.financialconnections.model.PartnerAccountsList | ||
import com.stripe.android.financialconnections.repository.FinancialConnectionsAccountsRepository | ||
import javax.inject.Inject | ||
|
||
internal class FetchNetworkedAccounts @Inject constructor( | ||
private val repository: FinancialConnectionsAccountsRepository, | ||
private val configuration: FinancialConnectionsSheet.Configuration | ||
) { | ||
|
||
suspend operator fun invoke( | ||
consumerSessionClientSecret: String, | ||
): PartnerAccountsList = repository.getNetworkedAccounts( | ||
clientSecret = configuration.financialConnectionsSessionClientSecret, | ||
consumerSessionClientSecret = consumerSessionClientSecret | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,21 @@ import com.stripe.android.financialconnections.FinancialConnectionsSheet | |
import com.stripe.android.financialconnections.exception.AccountLoadError | ||
import com.stripe.android.financialconnections.exception.AccountNoneEligibleForPaymentMethodError | ||
import com.stripe.android.financialconnections.features.common.getBusinessName | ||
import com.stripe.android.financialconnections.model.FinancialConnectionsAuthorizationSession | ||
import com.stripe.android.financialconnections.model.FinancialConnectionsInstitution | ||
import com.stripe.android.financialconnections.model.FinancialConnectionsSessionManifest | ||
import com.stripe.android.financialconnections.model.PartnerAccountsList | ||
import com.stripe.android.financialconnections.repository.FinancialConnectionsAccountsRepository | ||
import com.stripe.android.financialconnections.utils.PollTimingOptions | ||
import com.stripe.android.financialconnections.utils.retryOnException | ||
import com.stripe.android.financialconnections.utils.shouldRetry | ||
import javax.inject.Inject | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* Polls accounts from backend after authorization session completes. | ||
* | ||
* Will retry upon 202 backend responses every [POLLING_TIME_MS] up to [MAX_TRIES] | ||
* Will retry upon 202 backend responses. | ||
*/ | ||
internal class PollAuthorizationSessionAccounts @Inject constructor( | ||
private val repository: FinancialConnectionsAccountsRepository, | ||
|
@@ -27,66 +30,80 @@ internal class PollAuthorizationSessionAccounts @Inject constructor( | |
suspend operator fun invoke( | ||
canRetry: Boolean, | ||
manifest: FinancialConnectionsSessionManifest | ||
): PartnerAccountsList { | ||
return retryOnException( | ||
times = MAX_TRIES, | ||
delayMilliseconds = POLLING_TIME_MS, | ||
): PartnerAccountsList = try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try now wraps the |
||
val activeAuthSession = requireNotNull(manifest.activeAuthSession) | ||
retryOnException( | ||
PollTimingOptions( | ||
initialDelayMs = activeAuthSession.flow.toPollIntervalMs(), | ||
), | ||
retryCondition = { exception -> exception.shouldRetry } | ||
) { | ||
try { | ||
val authSession = requireNotNull(manifest.activeAuthSession) | ||
val accounts = repository.postAuthorizationSessionAccounts( | ||
clientSecret = configuration.financialConnectionsSessionClientSecret, | ||
sessionId = authSession.id | ||
) | ||
if (accounts.data.isEmpty()) { | ||
throw AccountLoadError( | ||
institution = requireNotNull(manifest.activeInstitution), | ||
allowManualEntry = manifest.allowManualEntry, | ||
canRetry = canRetry, | ||
stripeException = APIException() | ||
) | ||
} else { | ||
accounts | ||
} | ||
} catch (@Suppress("SwallowedException") e: StripeException) { | ||
throw e.toDomainException( | ||
institution = manifest.activeInstitution, | ||
businessName = manifest.getBusinessName(), | ||
val accounts = repository.postAuthorizationSessionAccounts( | ||
clientSecret = configuration.financialConnectionsSessionClientSecret, | ||
sessionId = activeAuthSession.id | ||
) | ||
if (accounts.data.isEmpty()) { | ||
throw AccountLoadError( | ||
institution = requireNotNull(manifest.activeInstitution), | ||
allowManualEntry = manifest.allowManualEntry, | ||
canRetry = canRetry, | ||
allowManualEntry = manifest.allowManualEntry | ||
stripeException = APIException() | ||
) | ||
} else { | ||
accounts | ||
} | ||
} | ||
} catch (@Suppress("SwallowedException") e: StripeException) { | ||
throw e.toDomainException( | ||
institution = manifest.activeInstitution, | ||
businessName = manifest.getBusinessName(), | ||
canRetry = canRetry, | ||
allowManualEntry = manifest.allowManualEntry | ||
) | ||
} | ||
} | ||
|
||
private fun StripeException.toDomainException( | ||
institution: FinancialConnectionsInstitution?, | ||
businessName: String?, | ||
canRetry: Boolean, | ||
allowManualEntry: Boolean | ||
): StripeException = | ||
when { | ||
institution == null -> this | ||
stripeError?.extraFields?.get("reason") == "no_supported_payment_method_type_accounts_found" -> | ||
AccountNoneEligibleForPaymentMethodError( | ||
accountsCount = stripeError?.extraFields?.get("total_accounts_count")?.toInt() | ||
?: 0, | ||
institution = institution, | ||
merchantName = businessName ?: "", | ||
stripeException = this | ||
) | ||
|
||
else -> AccountLoadError( | ||
allowManualEntry = allowManualEntry, | ||
private fun StripeException.toDomainException( | ||
institution: FinancialConnectionsInstitution?, | ||
businessName: String?, | ||
canRetry: Boolean, | ||
allowManualEntry: Boolean | ||
): StripeException = | ||
when { | ||
institution == null -> this | ||
stripeError?.extraFields?.get("reason") == "no_supported_payment_method_type_accounts_found" -> | ||
AccountNoneEligibleForPaymentMethodError( | ||
accountsCount = stripeError?.extraFields?.get("total_accounts_count")?.toInt() | ||
?: 0, | ||
institution = institution, | ||
canRetry = canRetry, | ||
merchantName = businessName ?: "", | ||
stripeException = this | ||
) | ||
|
||
else -> AccountLoadError( | ||
allowManualEntry = allowManualEntry, | ||
institution = institution, | ||
canRetry = canRetry, | ||
stripeException = this | ||
) | ||
} | ||
|
||
private fun FinancialConnectionsAuthorizationSession.Flow?.toPollIntervalMs(): Long { | ||
val defaultInitialPollDelay: Long = 1.75.seconds.inWholeMilliseconds | ||
return when (this) { | ||
FinancialConnectionsAuthorizationSession.Flow.TESTMODE, | ||
FinancialConnectionsAuthorizationSession.Flow.TESTMODE_OAUTH, | ||
FinancialConnectionsAuthorizationSession.Flow.TESTMODE_OAUTH_WEBVIEW, | ||
FinancialConnectionsAuthorizationSession.Flow.FINICITY_CONNECT_V2_LITE -> { | ||
// Post auth flow, Finicity non-OAuth account retrieval latency is extremely quick - p90 < 1sec. | ||
0 | ||
} | ||
|
||
FinancialConnectionsAuthorizationSession.Flow.MX_CONNECT -> { | ||
// 10 account retrieval latency on MX non-OAuth sessions is currently 460 ms | ||
0.5.seconds.inWholeMilliseconds | ||
} | ||
|
||
private companion object { | ||
private const val POLLING_TIME_MS = 2000L | ||
private const val MAX_TRIES = 10 | ||
else -> defaultInitialPollDelay | ||
Comment on lines
+91
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replicated from iOS. Each flow has an optimal initial delay. |
||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just renamed from PollNetworkedAccounts. No polling needed here.