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

Fetch available incentives after linking bank account #9735

Draft
wants to merge 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,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,26 @@ internal class RealCreateInstantDebitsResult @Inject constructor(
)
}

val incentiveEligibilitySessionId = elementsSessionContext?.incentiveEligibilitySession?.id

val eligibleForIncentive = if (incentiveEligibilitySessionId != null) {
val availableIncentives = runCatching {
consumerRepository.updateAvailableIncentives(
sessionId = incentiveEligibilitySessionId,
consumerSessionClientSecret = clientSecret,
)
}.getOrNull()

availableIncentives?.data?.isNotEmpty() == true
} 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,
): 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,
): UpdateAvailableIncentives {
return consumersApiService.updateAvailableIncentives(
sessionId = sessionId,
consumerSessionClientSecret = consumerSessionClientSecret,
requestSurface = requestSurface,
requestOptions = provideApiRequestOptions(useConsumerPublishableKey = true),
).getOrThrow()
}

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,69 @@ 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(
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 +282,7 @@ class RealCreateInstantDebitsResultTest {
private fun makeElementsSessionContext(
linkMode: LinkMode?,
billingDetails: BillingDetails? = null,
incentiveEligibilitySession: IncentiveEligibilitySession? = null,
): ElementsSessionContext {
return ElementsSessionContext(
amount = 100L,
Expand All @@ -223,7 +294,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
Loading