-
Notifications
You must be signed in to change notification settings - Fork 659
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
Link Cvc & Expiry recollection #9880
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModelProvider | |
import androidx.lifecycle.viewModelScope | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import com.stripe.android.common.exception.stripeErrorMessage | ||
import com.stripe.android.core.Logger | ||
import com.stripe.android.core.strings.resolvableString | ||
import com.stripe.android.link.LinkActivityResult | ||
|
@@ -14,14 +15,25 @@ import com.stripe.android.link.account.LinkAccountManager | |
import com.stripe.android.link.injection.NativeLinkComponent | ||
import com.stripe.android.link.model.LinkAccount | ||
import com.stripe.android.link.model.supportedPaymentMethodTypes | ||
import com.stripe.android.model.CardBrand | ||
import com.stripe.android.model.ConsumerPaymentDetails | ||
import com.stripe.android.model.ConsumerPaymentDetailsUpdateParams | ||
import com.stripe.android.model.PaymentIntent | ||
import com.stripe.android.model.PaymentMethod | ||
import com.stripe.android.model.PaymentMethodCreateParams | ||
import com.stripe.android.model.SetupIntent | ||
import com.stripe.android.model.StripeIntent | ||
import com.stripe.android.ui.core.Amount | ||
import com.stripe.android.ui.core.FieldValuesToParamsMapConverter | ||
import com.stripe.android.ui.core.R | ||
import com.stripe.android.ui.core.elements.CvcController | ||
import com.stripe.android.ui.core.elements.createExpiryDateFormFieldValues | ||
import com.stripe.android.uicore.elements.DateConfig | ||
import com.stripe.android.uicore.elements.SimpleTextFieldController | ||
import com.stripe.android.uicore.utils.mapAsStateFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
@@ -48,8 +60,33 @@ internal class WalletViewModel @Inject constructor( | |
|
||
val uiState: StateFlow<WalletUiState> = _uiState | ||
|
||
val expiryDateController = SimpleTextFieldController( | ||
textFieldConfig = DateConfig() | ||
) | ||
val cvcController = CvcController( | ||
cardBrandFlow = uiState.mapAsStateFlow { | ||
(it.selectedItem as? ConsumerPaymentDetails.Card)?.brand ?: CardBrand.Unknown | ||
} | ||
) | ||
|
||
init { | ||
loadPaymentDetails() | ||
|
||
viewModelScope.launch { | ||
expiryDateController.formFieldValue.collectLatest { input -> | ||
_uiState.update { | ||
it.copy(expiryDateInput = input) | ||
} | ||
} | ||
} | ||
|
||
viewModelScope.launch { | ||
cvcController.formFieldValue.collectLatest { input -> | ||
_uiState.update { | ||
it.copy(cvcInput = input) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun loadPaymentDetails() { | ||
|
@@ -84,12 +121,66 @@ internal class WalletViewModel @Inject constructor( | |
fun onItemSelected(item: ConsumerPaymentDetails.PaymentDetails) { | ||
if (item == uiState.value.selectedItem) return | ||
|
||
expiryDateController.onRawValueChange("") | ||
cvcController.onRawValueChange("") | ||
|
||
_uiState.update { | ||
it.copy(selectedItem = item) | ||
} | ||
} | ||
|
||
fun onPrimaryButtonClicked() = Unit | ||
fun onPrimaryButtonClicked() { | ||
val paymentDetail = _uiState.value.selectedItem ?: return | ||
_uiState.update { | ||
it.copy(isProcessing = true) | ||
} | ||
|
||
viewModelScope.launch { | ||
performPaymentConfirmation(paymentDetail) | ||
} | ||
} | ||
|
||
private suspend fun performPaymentConfirmation( | ||
selectedPaymentDetails: ConsumerPaymentDetails.PaymentDetails, | ||
) { | ||
val card = selectedPaymentDetails as? ConsumerPaymentDetails.Card | ||
val isExpired = card != null && card.isExpired | ||
|
||
if (isExpired) { | ||
performPaymentDetailsUpdate(selectedPaymentDetails).fold( | ||
onSuccess = { result -> | ||
val updatedPaymentDetails = result.paymentDetails.single { | ||
it.id == selectedPaymentDetails.id | ||
} | ||
performPaymentConfirmation(updatedPaymentDetails) | ||
}, | ||
onFailure = { error -> | ||
_uiState.update { | ||
it.copy( | ||
alertMessage = error.stripeErrorMessage(), | ||
isProcessing = false | ||
) | ||
} | ||
} | ||
) | ||
} else { | ||
// Confirm payment with LinkConfirmationHandler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
private suspend fun performPaymentDetailsUpdate( | ||
selectedPaymentDetails: ConsumerPaymentDetails.PaymentDetails | ||
): Result<ConsumerPaymentDetails> { | ||
val paymentMethodCreateParams = uiState.value.toPaymentMethodCreateParams() | ||
|
||
val updateParams = ConsumerPaymentDetailsUpdateParams( | ||
id = selectedPaymentDetails.id, | ||
isDefault = selectedPaymentDetails.isDefault, | ||
cardPaymentMethodCreateParamsMap = paymentMethodCreateParams.toParamMap() | ||
) | ||
|
||
return linkAccountManager.updatePaymentDetails(updateParams) | ||
} | ||
|
||
fun onPayAnotherWayClicked() { | ||
dismissWithResult(LinkActivityResult.Canceled(LinkActivityResult.Canceled.Reason.PayAnotherWay)) | ||
|
@@ -129,3 +220,12 @@ internal class WalletViewModel @Inject constructor( | |
} | ||
} | ||
} | ||
|
||
private fun WalletUiState.toPaymentMethodCreateParams(): PaymentMethodCreateParams { | ||
val expiryDateValues = createExpiryDateFormFieldValues(expiryDateInput) | ||
return FieldValuesToParamsMapConverter.transformToPaymentMethodCreateParams( | ||
fieldValuePairs = expiryDateValues, | ||
code = PaymentMethod.Type.Card.code, | ||
requiresMandate = false | ||
) | ||
} | ||
Comment on lines
+224
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Spotted by Graphite Reviewer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cvc is not part of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
isMissingExpiryDateInput
check should only depend onexpiryDateInput.isComplete
. IncludingcvcInput.isComplete
in this check is incorrect since it's meant to validate expiry date completeness only. The check should be simplified to:Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expired cards requires both updated expiryDate and cvc