Skip to content

Commit

Permalink
Formally delete StopgapGlobalState
Browse files Browse the repository at this point in the history
This commit finally passes down the relevant EnvConfig
info to the PinSubmitters allowing us to delete the
global state we had been using before and eliminating
the chance for bugs, which we've experienced in the
past

Signed-off-by: Devin Morgan <[email protected]>
  • Loading branch information
devinmorgan committed Jun 10, 2024
1 parent 7fc4401 commit 8b4f924
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 252 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.joinforage.forage.android.core.ui.element
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.joinforage.forage.android.core.services.vault.StopgapGlobalState
import com.joinforage.forage.android.core.services.EnvConfig
import com.joinforage.forage.android.core.ui.element.state.ElementState

/**
Expand Down Expand Up @@ -34,11 +34,6 @@ abstract class AbstractForageElement<T : ElementState>(
// update the forage config
this._forageConfig = forageConfig

// TODO: 9/20/23: This is a temporary workaround and is
// not meant to stick around. See this doc for more details
// https://www.notion.so/joinforage/226d8ee6f8294d2694b1bb451791960b
StopgapGlobalState.forageConfig = forageConfig

// there are a number of side effect operations that we
// need to run as soon as a ForageElement has access to
// ForageConfig data. However, we don't want to run these
Expand All @@ -60,4 +55,5 @@ abstract class AbstractForageElement<T : ElementState>(
internal fun getForageConfig(): ForageConfig? {
return _forageConfig
}
internal fun getEnvConfig(): EnvConfig = EnvConfig.fromForageConfig(_forageConfig)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.joinforage.forage.android.ecom.services.vault.bt
import com.basistheory.android.service.BasisTheoryElements
import com.basistheory.android.service.ProxyRequest
import com.basistheory.android.view.TextElement
import com.joinforage.forage.android.core.services.EnvConfig
import com.joinforage.forage.android.core.services.ForageConstants
import com.joinforage.forage.android.core.services.VaultType
import com.joinforage.forage.android.core.services.forageapi.encryptkey.EncryptionKeys
import com.joinforage.forage.android.core.services.forageapi.network.ForageApiResponse
import com.joinforage.forage.android.core.services.forageapi.paymentmethod.PaymentMethod
import com.joinforage.forage.android.core.services.telemetry.Log
import com.joinforage.forage.android.core.services.vault.AbstractVaultSubmitter
import com.joinforage.forage.android.core.services.vault.StopgapGlobalState
import com.joinforage.forage.android.core.services.vault.VaultProxyRequest
import com.joinforage.forage.android.core.services.vault.VaultSubmitterParams
import com.joinforage.forage.android.core.ui.element.ForagePinElement
Expand All @@ -19,8 +19,8 @@ internal typealias BasisTheoryResponse = Result<Any?>

internal class BasisTheoryPinSubmitter(
foragePinEditText: ForagePinElement,
private val envConfig: EnvConfig,
logger: Log,
private val buildVaultProvider: () -> BasisTheoryElements = { buildBt() }
) : AbstractVaultSubmitter(
foragePinEditText = foragePinEditText,
logger = logger
Expand All @@ -40,11 +40,13 @@ internal class BasisTheoryPinSubmitter(
encryptionKey = encryptionKey,
vaultToken = vaultToken
)
.setHeader(ForageConstants.Headers.BT_PROXY_KEY, PROXY_ID)
.setHeader(ForageConstants.Headers.BT_PROXY_KEY, envConfig.btProxyID)
.setHeader(ForageConstants.Headers.CONTENT_TYPE, "application/json")

override suspend fun submitProxyRequest(vaultProxyRequest: VaultProxyRequest): ForageApiResponse<String> {
val bt = buildVaultProvider()
val bt = BasisTheoryElements.builder()
.apiKey(envConfig.btAPIKey)
.build()

val proxyRequest: ProxyRequest = ProxyRequest().apply {
headers = vaultProxyRequest.headers
Expand All @@ -64,17 +66,4 @@ internal class BasisTheoryPinSubmitter(

override fun getVaultToken(paymentMethod: PaymentMethod): String? = pickVaultTokenByIndex(paymentMethod, 1)

companion object {
// this code assumes that .setForageConfig() has been called
// on a Forage***EditText before PROXY_ID or API_KEY get
// referenced
private val PROXY_ID = StopgapGlobalState.envConfig.btProxyID
private val API_KEY = StopgapGlobalState.envConfig.btAPIKey

private fun buildBt(): BasisTheoryElements {
return BasisTheoryElements.builder()
.apiKey(API_KEY)
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.joinforage.forage.android.ecom.services.vault.vgs

import android.content.Context
import com.joinforage.forage.android.core.services.VaultType
import com.joinforage.forage.android.core.services.forageapi.encryptkey.EncryptionKeys
import com.joinforage.forage.android.core.services.EnvConfig
import com.joinforage.forage.android.core.services.forageapi.network.ForageApiError
import com.joinforage.forage.android.core.services.forageapi.network.ForageApiResponse
import com.joinforage.forage.android.core.services.forageapi.network.ForageError
import com.joinforage.forage.android.core.services.forageapi.network.UnknownErrorApiResponse
import com.joinforage.forage.android.core.services.forageapi.paymentmethod.PaymentMethod
import com.joinforage.forage.android.core.services.telemetry.Log
import com.joinforage.forage.android.core.services.vault.AbstractVaultSubmitter
import com.joinforage.forage.android.core.services.vault.StopgapGlobalState
import com.joinforage.forage.android.core.services.vault.VaultProxyRequest
import com.joinforage.forage.android.core.ui.element.ForagePinElement
import com.verygoodsecurity.vgscollect.VGSCollectLogger
Expand All @@ -25,8 +24,8 @@ import kotlin.coroutines.suspendCoroutine

internal class VgsPinSubmitter(
foragePinEditText: ForagePinElement,
private val envConfig: EnvConfig,
logger: Log,
private val buildVaultProvider: (context: Context) -> VGSCollect = { buildVGSCollect(foragePinEditText.context) }
) : AbstractVaultSubmitter(
foragePinEditText = foragePinEditText,
logger = logger
Expand All @@ -35,7 +34,11 @@ internal class VgsPinSubmitter(
override suspend fun submitProxyRequest(
vaultProxyRequest: VaultProxyRequest
): ForageApiResponse<String> = suspendCoroutine { continuation ->
val vgsCollect = buildVaultProvider(foragePinEditText.context)
VGSCollectLogger.isEnabled = false
val vgsCollect = VGSCollect
.Builder(foragePinEditText.context, envConfig.vgsVaultId)
.setEnvironment(envConfig.vgsVaultType)
.create()
vgsCollect.bindView(foragePinEditText.getTextElement() as VGSEditText)

vgsCollect.addOnResponseListeners(object : VgsCollectResponseListener {
Expand Down Expand Up @@ -66,20 +69,6 @@ internal class VgsPinSubmitter(
override fun getVaultToken(paymentMethod: PaymentMethod): String? =
pickVaultTokenByIndex(paymentMethod, 0)

companion object {
// this code assumes that .setForageConfig() has been called
// on a Forage***EditText before PROXY_ID or API_KEY get
// referenced
private val VAULT_ID = StopgapGlobalState.envConfig.vgsVaultId
private val VGS_ENVIRONMENT = StopgapGlobalState.envConfig.vgsVaultType

private fun buildVGSCollect(context: Context): VGSCollect {
VGSCollectLogger.isEnabled = false
return VGSCollect.Builder(context, VAULT_ID)
.setEnvironment(VGS_ENVIRONMENT)
.create()
}
}

fun toVaultErrorOrNull(vaultResponse: VGSResponse?): ForageApiResponse.Failure? {
if (vaultResponse == null) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ internal class BTVaultWrapper @JvmOverloads constructor(
logger: Log
): AbstractVaultSubmitter = BasisTheoryPinSubmitter(
foragePinElement,
foragePinElement.getEnvConfig(),
logger
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ internal class VGSVaultWrapper @JvmOverloads constructor(
logger: Log
): AbstractVaultSubmitter = VgsPinSubmitter(
foragePinElement,
foragePinElement.getEnvConfig(),
logger
)

Expand Down

This file was deleted.

0 comments on commit 8b4f924

Please sign in to comment.