-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa37860
commit 87ef2c7
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
paymentsheet/src/main/java/com/stripe/android/link/ui/cardedit/CardEditViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.stripe.android.link.ui.cardedit | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.stripe.android.cards.CardAccountRangeRepository | ||
import com.stripe.android.core.Logger | ||
import com.stripe.android.link.LinkActivityResult | ||
import com.stripe.android.link.LinkConfiguration | ||
import com.stripe.android.link.account.LinkAccountManager | ||
import com.stripe.android.link.model.LinkAccount | ||
import com.stripe.android.link.model.supportedPaymentMethodTypes | ||
import com.stripe.android.link.ui.paymentmenthod.Factory | ||
import com.stripe.android.model.ConsumerPaymentDetails | ||
import com.stripe.android.paymentsheet.analytics.code | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
internal class CardEditViewModel @Inject constructor( | ||
private val paymentDetailsId: String, | ||
private val linkAccount: LinkAccount, | ||
private val configuration: LinkConfiguration, | ||
private val linkAccountManager: LinkAccountManager, | ||
private val logger: Logger, | ||
private val cardAccountRangeRepositoryFactory: CardAccountRangeRepository.Factory, | ||
private val dismissWithResult: (LinkActivityResult) -> Unit | ||
) : ViewModel() { | ||
private val paymentMethodMetadata = Factory.paymentMethodMetadata(configuration) | ||
private val formHelper = Factory.formHelper( | ||
configuration = configuration, | ||
cardAccountRangeRepositoryFactory = cardAccountRangeRepositoryFactory, | ||
selectionUpdater = { | ||
// it.code() | ||
// updateSelection(it) | ||
} | ||
) | ||
|
||
init { | ||
viewModelScope.launch { | ||
loadCard() | ||
} | ||
} | ||
|
||
private suspend fun loadCard() { | ||
linkAccountManager.listPaymentDetails( | ||
paymentMethodTypes = configuration.stripeIntent.supportedPaymentMethodTypes(linkAccount) | ||
).fold( | ||
onSuccess = { paymentDetails -> | ||
val card = paymentDetails.paymentDetails | ||
.filterIsInstance<ConsumerPaymentDetails.Card>() | ||
.firstOrNull { it.id == paymentDetailsId } | ||
|
||
if (card == null) { | ||
logger.error("CardEditViewModel error: ", CardNotFoundException()) | ||
return@fold dismissWithResult(LinkActivityResult.Failed(CardNotFoundException())) | ||
} | ||
|
||
// val formElements = card.formElements() | ||
// _formElements.value = formElements | ||
}, | ||
onFailure = { e -> | ||
logger.error("CardEditViewModel error: ", e) | ||
dismissWithResult(LinkActivityResult.Failed(e)) | ||
} | ||
) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
paymentsheet/src/main/java/com/stripe/android/link/ui/cardedit/CardNotFoundException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.stripe.android.link.ui.cardedit | ||
|
||
internal class CardNotFoundException : IllegalArgumentException("card with id not found for link account") |