Skip to content

Commit

Permalink
Fetch available incentives after linking bank account (#9735)
Browse files Browse the repository at this point in the history
* Fetch available incentives after linking bank account

* Update tests

* Update API

* Remove unused `Serializable` annotation

* Make minor code tweak

* Propagate `Result<T>` to call-site
  • Loading branch information
tillh-stripe authored Jan 13, 2025
1 parent 0087322 commit b67e9c7
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
instantDebits = InstantDebitsResult(
encodedPaymentMethod = paymentMethod,
last4 = url.getQueryParameter(QUERY_PARAM_LAST4),
bankName = url.getQueryParameter(QUERY_BANK_NAME)
bankName = url.getQueryParameter(QUERY_BANK_NAME),
// TODO(tillh-stripe): Pull this from the URL
eligibleForIncentive = false,
),
financialConnectionsSession = null,
token = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,24 @@ internal class RealCreateInstantDebitsResult @Inject constructor(
)
}

val incentiveEligibilitySessionId = elementsSessionContext?.incentiveEligibilitySession?.id

val eligibleForIncentive = if (incentiveEligibilitySessionId != null) {
consumerRepository.updateAvailableIncentives(
sessionId = incentiveEligibilitySessionId,
consumerSessionClientSecret = clientSecret,
).map {
it.data.isNotEmpty()
}.getOrDefault(false)
} else {
false
}

return InstantDebitsResult(
encodedPaymentMethod = paymentMethod,
bankName = paymentDetails.bankName,
last4 = paymentDetails.last4,
eligibleForIncentive = eligibleForIncentive,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ internal sealed class FinancialConnectionsSheetActivityResult : Parcelable {
internal data class InstantDebitsResult(
val encodedPaymentMethod: String,
val last4: String?,
val bankName: String?
val bankName: String?,
val eligibleForIncentive: Boolean,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class FinancialConnectionsSheetForInstantDebitsContract :
encodedPaymentMethod = instantDebits.encodedPaymentMethod,
last4 = instantDebits.last4,
bankName = instantDebits.bankName,
eligibleForIncentive = instantDebits.eligibleForIncentive,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import kotlinx.parcelize.Parcelize
sealed class FinancialConnectionsSheetInstantDebitsResult : Parcelable {
/**
* The customer completed the connections session.
* @param paymentMethodId The payment method id, that can be used to confirm the payment.
* @param last4 The last 4 digits of the bank account.
* @param bankName The name of the bank.
*/
@Parcelize
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
data class Completed(
val encodedPaymentMethod: String,
val last4: String?,
val bankName: String?
val bankName: String?,
val eligibleForIncentive: Boolean,
) : FinancialConnectionsSheetInstantDebitsResult()

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.stripe.android.model.ConsumerSessionSignup
import com.stripe.android.model.ConsumerSignUpConsentAction.EnteredPhoneNumberClickedSaveToLink
import com.stripe.android.model.CustomEmailType
import com.stripe.android.model.SharePaymentDetails
import com.stripe.android.model.UpdateAvailableIncentives
import com.stripe.android.model.VerificationType
import com.stripe.android.repository.ConsumersApiService
import kotlinx.coroutines.sync.Mutex
Expand Down Expand Up @@ -69,6 +70,11 @@ internal interface FinancialConnectionsConsumerSessionRepository {
billingPhone: String?,
): SharePaymentDetails

suspend fun updateAvailableIncentives(
sessionId: String,
consumerSessionClientSecret: String,
): Result<UpdateAvailableIncentives>

companion object {
operator fun invoke(
consumersApiService: ConsumersApiService,
Expand Down Expand Up @@ -238,6 +244,18 @@ private class FinancialConnectionsConsumerSessionRepositoryImpl(
).getOrThrow()
}

override suspend fun updateAvailableIncentives(
sessionId: String,
consumerSessionClientSecret: String,
): Result<UpdateAvailableIncentives> {
return consumersApiService.updateAvailableIncentives(
sessionId = sessionId,
consumerSessionClientSecret = consumerSessionClientSecret,
requestSurface = requestSurface,
requestOptions = provideApiRequestOptions(useConsumerPublishableKey = true),
)
}

private suspend fun postConsumerSession(
email: String,
clientSecret: String
Expand Down Expand Up @@ -266,6 +284,9 @@ private class FinancialConnectionsConsumerSessionRepositoryImpl(
signup: ConsumerSessionSignup,
) {
logger.debug("SYNC_CACHE: updating local consumer session from signUp")
consumerSessionRepository.storeNewConsumerSession(signup.consumerSession, signup.publishableKey)
consumerSessionRepository.storeNewConsumerSession(
consumerSession = signup.consumerSession,
publishableKey = signup.publishableKey,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.stripe.android.financialconnections.domain

import com.google.common.truth.Truth.assertThat
import com.stripe.android.financialconnections.FinancialConnectionsSheet.ElementsSessionContext
import com.stripe.android.financialconnections.FinancialConnectionsSheet.ElementsSessionContext.BillingDetails
import com.stripe.android.financialconnections.repository.CachedConsumerSession
import com.stripe.android.financialconnections.repository.FinancialConnectionsConsumerSessionRepository
import com.stripe.android.financialconnections.repository.FinancialConnectionsRepository
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.IncentiveEligibilitySession
import com.stripe.android.model.LinkConsumerIncentive
import com.stripe.android.model.LinkMode
import com.stripe.android.model.SharePaymentDetails
import com.stripe.android.model.UpdateAvailableIncentives
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever

class RealCreateInstantDebitsResultTest {

Expand Down Expand Up @@ -167,6 +174,71 @@ class RealCreateInstantDebitsResultTest {
)
}

@Test
fun `Skips checking available incentives if not incentive eligible`() = runTest {
val consumerRepository = makeConsumerSessionRepository()
val repository = makeRepository()

val createInstantDebitResult = RealCreateInstantDebitsResult(
consumerRepository = consumerRepository,
repository = repository,
consumerSessionProvider = { makeCachedConsumerSession() },
elementsSessionContext = makeElementsSessionContext(
linkMode = LinkMode.LinkPaymentMethod,
incentiveEligibilitySession = null,
),
)

val result = createInstantDebitResult("bank_account_id_001")

verify(consumerRepository, never()).updateAvailableIncentives(
sessionId = any(),
consumerSessionClientSecret = any(),
)

assertThat(result.eligibleForIncentive).isFalse()
}

@Test
fun `Checks available incentives if eligible`() = runTest {
val consumerRepository = makeConsumerSessionRepository()
val repository = makeRepository()
val consumerSession = makeCachedConsumerSession()

val createInstantDebitResult = RealCreateInstantDebitsResult(
consumerRepository = consumerRepository,
repository = repository,
consumerSessionProvider = { consumerSession },
elementsSessionContext = makeElementsSessionContext(
linkMode = LinkMode.LinkPaymentMethod,
incentiveEligibilitySession = IncentiveEligibilitySession.PaymentIntent("pi_123"),
),
)

whenever(
consumerRepository.updateAvailableIncentives(
sessionId = eq("pi_123"),
consumerSessionClientSecret = eq("clientSecret"),
)
).thenReturn(
Result.success(
UpdateAvailableIncentives(
data = listOf(
LinkConsumerIncentive(
incentiveParams = LinkConsumerIncentive.IncentiveParams(
paymentMethod = "link_instant_debits",
),
incentiveDisplayText = "$5",
)
)
)
)
)

val result = createInstantDebitResult("bank_account_id_001")
assertThat(result.eligibleForIncentive).isTrue()
}

private fun makeConsumerSessionRepository(): FinancialConnectionsConsumerSessionRepository {
val consumerPaymentDetails = ConsumerPaymentDetails(
paymentDetails = listOf(
Expand Down Expand Up @@ -212,6 +284,7 @@ class RealCreateInstantDebitsResultTest {
private fun makeElementsSessionContext(
linkMode: LinkMode?,
billingDetails: BillingDetails? = null,
incentiveEligibilitySession: IncentiveEligibilitySession? = null,
): ElementsSessionContext {
return ElementsSessionContext(
amount = 100L,
Expand All @@ -223,7 +296,7 @@ class RealCreateInstantDebitsResultTest {
phone = null,
phoneCountryCode = null,
),
incentiveEligibilitySession = null,
incentiveEligibilitySession = incentiveEligibilitySession,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FinancialConnectionsSheetForInstantDebitsLauncherTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "1234",
bankName = "Bank of America",
eligibleForIncentive = false,
)
)

Expand All @@ -46,6 +47,7 @@ class FinancialConnectionsSheetForInstantDebitsLauncherTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "1234",
bankName = "Bank of America",
eligibleForIncentive = false,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ internal class FinancialConnectionsSheetNativeViewModelTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "4242",
bankName = "Stripe Bank",
eligibleForIncentive = false,
)
},
)
Expand All @@ -339,6 +340,7 @@ internal class FinancialConnectionsSheetNativeViewModelTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "4242",
bankName = "Stripe Bank",
eligibleForIncentive = false,
),
)
)
Expand Down Expand Up @@ -382,6 +384,7 @@ internal class FinancialConnectionsSheetNativeViewModelTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "4242",
bankName = "Stripe Bank",
eligibleForIncentive = false,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ sealed interface CollectBankAccountForInstantDebitsResult : Parcelable {
val intent: StripeIntent?,
val paymentMethod: PaymentMethod,
val last4: String?,
val bankName: String?
val bankName: String?,
val eligibleForIncentive: Boolean,
) : CollectBankAccountForInstantDebitsResult

@Parcelize
Expand Down Expand Up @@ -51,7 +52,8 @@ internal fun CollectBankAccountResultInternal.toInstantDebitsResult(): CollectBa
intent = response.intent,
paymentMethod = response.instantDebitsData.paymentMethod,
last4 = response.instantDebitsData.last4,
bankName = response.instantDebitsData.bankName
bankName = response.instantDebitsData.bankName,
eligibleForIncentive = response.instantDebitsData.eligibleForIncentive,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ data class CollectBankAccountResponseInternal(
data class InstantDebitsData(
val paymentMethod: PaymentMethod,
val last4: String?,
val bankName: String?
val bankName: String?,
val eligibleForIncentive: Boolean,
) : StripeModel
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ internal class CollectBankAccountViewModel @Inject constructor(
paymentMethod = it,
last4 = result.last4,
bankName = result.bankName,
eligibleForIncentive = result.eligibleForIncentive,
)
},
)
Expand Down
8 changes: 8 additions & 0 deletions payments-model/api/payments-model.api
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ public final class com/stripe/android/model/TokenizationMethod : java/lang/Enum
public static fun values ()[Lcom/stripe/android/model/TokenizationMethod;
}

public final class com/stripe/android/model/UpdateAvailableIncentives$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/model/UpdateAvailableIncentives;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/model/UpdateAvailableIncentives;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/payments/model/BuildConfig {
public static final field BUILD_TYPE Ljava/lang/String;
public static final field DEBUG Z
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.stripe.android.model

import androidx.annotation.RestrictTo
import com.stripe.android.core.model.StripeModel
import kotlinx.parcelize.Parcelize

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Parcelize
data class UpdateAvailableIncentives(
val data: List<LinkConsumerIncentive>,
) : StripeModel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.stripe.android.model.parsers

import androidx.annotation.RestrictTo
import com.stripe.android.core.model.parsers.ModelJsonParser
import com.stripe.android.model.UpdateAvailableIncentives
import org.json.JSONObject

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
object UpdateAvailableIncentivesJsonParser : ModelJsonParser<UpdateAvailableIncentives> {

override fun parse(json: JSONObject): UpdateAvailableIncentives? {
val incentives = json.optJSONArray("data")?.let { data ->
List(data.length()) { index ->
LinkConsumerIncentiveJsonParser.parse(data.getJSONObject(index))
}
}

return incentives?.let {
UpdateAvailableIncentives(data = it.filterNotNull())
}
}
}
Loading

0 comments on commit b67e9c7

Please sign in to comment.