Skip to content

Commit

Permalink
add trigger for 3ds flow for post campaign payments (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgriego authored Jun 10, 2024
1 parent fc9b18c commit 6a4f779
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,6 @@ class ProjectPageActivity :
val userEmail = latePledgeCheckoutUIState.userEmail
val checkoutLoading = latePledgeCheckoutUIState.isLoading

LaunchedEffect(Unit) {
latePledgeCheckoutViewModel.paymentRequiresAction.collect {
stripeNextAction(it)
}
}

latePledgeCheckoutViewModel.provideErrorAction { message ->
showToastError(message)
}
Expand Down Expand Up @@ -1219,6 +1213,7 @@ class ProjectPageActivity :
binding.pledgeContainerCompose,
getString(R.string.general_error_oops)
)
latePledgeCheckoutViewModel.onNewCardFailed()
}

is PaymentSheetResult.Failed -> {
Expand All @@ -1228,6 +1223,7 @@ class ProjectPageActivity :
binding.pledgeContainerCompose,
errorMessage
)
latePledgeCheckoutViewModel.onNewCardFailed()
}

is PaymentSheetResult.Completed -> {
Expand Down Expand Up @@ -1279,11 +1275,12 @@ class ProjectPageActivity :
object : ApiResultCallback<PaymentIntentResult> {
override fun onSuccess(result: PaymentIntentResult) {
if (result.outcome == StripeIntentResult.Outcome.SUCCEEDED) {
// Go to thanks page
latePledgeCheckoutViewModel.completeOnSessionCheckoutFor3DS()
} else showToastError()
}

override fun onError(e: Exception) {
latePledgeCheckoutViewModel.clear3DSValues()
showToastError()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
private var newStoredCard: StoredCard? = null
private var errorAction: (message: String?) -> Unit = {}

private var clientSecretFor3DSVerification: String = ""
private var selectedCardFor3DSVerification: StoredCard? = null

private var selectedReward: Reward? = null
private var mutableLatePledgeCheckoutUIState = MutableStateFlow(LatePledgeCheckoutUIState())

Expand Down Expand Up @@ -149,6 +152,12 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
}
}

fun onNewCardFailed() {
viewModelScope.launch {
emitCurrentState()
}
}

private suspend fun refreshUserCards() {
apolloClient.getStoredCards()
.asFlow().onStart {
Expand Down Expand Up @@ -241,7 +250,12 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
val withSDK =
intentParams.withShouldUseStripeSdk(shouldUseStripeSdk = true)
val paymentIntent = stripe.confirmPaymentIntent(withSDK)
if (paymentIntent.lastPaymentError.isNotNull()) {
if (paymentIntent.requiresAction()) {
clientSecretFor3DSVerification = clientSecret
selectedCardFor3DSVerification = selectedCard
mutablePaymentRequiresAction.emit(clientSecret)
emitCurrentState()
} else if (paymentIntent.lastPaymentError.isNotNull()) {
// Display error with lastPaymentError.message
emitCurrentState()
errorAction.invoke(paymentIntent.lastPaymentError?.message)
Expand Down Expand Up @@ -270,6 +284,40 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
}.collect()
}

fun completeOnSessionCheckoutFor3DS() {
viewModelScope.launch {
if (clientSecretFor3DSVerification.isNotEmpty() && selectedCardFor3DSVerification.isNotNull()) {
apolloClient.completeOnSessionCheckout(
checkoutId = checkoutId ?: "",
paymentIntentClientSecret = clientSecretFor3DSVerification,
paymentSourceId = if (selectedCardFor3DSVerification == newStoredCard) null else selectedCardFor3DSVerification?.id() ?: "",
paymentSourceReusable = true
).asFlow().map { iDRequiresActionPair ->
if (iDRequiresActionPair.second) {
mutablePaymentRequiresAction.emit(clientSecretFor3DSVerification)
} else {
mutableOnPledgeSuccessAction.emit(true)
}
}.onStart {
emitCurrentState(isLoading = true)
}.onCompletion {
emitCurrentState()
}.catch {
errorAction.invoke(null)
clear3DSValues()
}.collect()
} else {
errorAction.invoke(null)
clear3DSValues()
}
}
}

fun clear3DSValues() {
clientSecretFor3DSVerification = ""
selectedCardFor3DSVerification = null
}

private fun createCheckoutData(shippingAmount: Double, total: Double, bonusAmount: Double?, checkout: Checkout? = null): CheckoutData {
return CheckoutData.builder()
.amount(total)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class LoginToutScreenTest : KSRobolectricTestCase() {
touPpCookieDisclaimer.assertIsDisplayed()
}

@Test
fun testComponentsVisible_featureFlag_On() {
composeTestRule.setContent {
KSTheme {
Expand Down

0 comments on commit 6a4f779

Please sign in to comment.