-
Notifications
You must be signed in to change notification settings - Fork 658
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
Keep reference to pending payment result #7704
Merged
tillh-stripe
merged 3 commits into
master
from
tillh/7590-3ds-not-working-when-activity-gets-recreated
Dec 8, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -55,6 +55,7 @@ import com.stripe.android.paymentsheet.state.GooglePayState | |
import com.stripe.android.paymentsheet.state.PaymentSheetLoader | ||
import com.stripe.android.paymentsheet.state.PaymentSheetState | ||
import com.stripe.android.paymentsheet.state.WalletsState | ||
import com.stripe.android.paymentsheet.state.asPaymentSheetLoadingException | ||
import com.stripe.android.paymentsheet.ui.HeaderTextFactory | ||
import com.stripe.android.paymentsheet.ui.ModifiableEditPaymentMethodViewInteractor | ||
import com.stripe.android.paymentsheet.ui.PrimaryButton | ||
|
@@ -180,6 +181,8 @@ internal class PaymentSheetViewModel @Inject internal constructor( | |
null -> GooglePayButtonType.Pay | ||
} | ||
|
||
private var pendingPaymentResult: InternalPaymentResult? = null | ||
|
||
@VisibleForTesting | ||
internal val googlePayLauncherConfig: GooglePayPaymentMethodLauncher.Config? = | ||
args.googlePayConfig?.let { config -> | ||
|
@@ -318,16 +321,27 @@ internal class PaymentSheetViewModel @Inject internal constructor( | |
} | ||
|
||
result.fold( | ||
onSuccess = { state -> | ||
handlePaymentSheetStateLoaded(state) | ||
}, | ||
onFailure = { error -> | ||
setStripeIntent(null) | ||
onFatal(error) | ||
} | ||
onSuccess = ::handlePaymentSheetStateLoaded, | ||
onFailure = ::handlePaymentSheetStateLoadFailure, | ||
) | ||
} | ||
|
||
private fun handlePaymentSheetStateLoadFailure(error: Throwable) { | ||
val pendingResult = pendingPaymentResult | ||
|
||
if (pendingResult is InternalPaymentResult.Completed) { | ||
// If we just received a transaction result after process death, we don't error. Instead, we dismiss | ||
// PaymentSheet and return a `Completed` result to the caller. | ||
val usedPaymentMethod = error.asPaymentSheetLoadingException.usedPaymentMethod | ||
handlePaymentCompleted(usedPaymentMethod, finishImmediately = true) | ||
} else { | ||
setStripeIntent(null) | ||
onFatal(error) | ||
} | ||
|
||
pendingPaymentResult = null | ||
} | ||
|
||
private fun handlePaymentSheetStateLoaded(state: PaymentSheetState.Full) { | ||
cbcEligibility = when (state.isEligibleForCardBrandChoice) { | ||
true -> CardBrandChoiceEligibility.Eligible( | ||
|
@@ -351,7 +365,10 @@ internal class PaymentSheetViewModel @Inject internal constructor( | |
|
||
linkHandler.setupLink(linkState) | ||
|
||
resetViewState() | ||
val pendingFailedPaymentResult = pendingPaymentResult as? InternalPaymentResult.Failed | ||
val errorMessage = pendingFailedPaymentResult?.throwable?.stripeErrorMessage(getApplication()) | ||
|
||
resetViewState(errorMessage) | ||
transitionToFirstScreen() | ||
} | ||
|
||
|
@@ -593,74 +610,83 @@ internal class PaymentSheetViewModel @Inject internal constructor( | |
} | ||
|
||
private fun onInternalPaymentResult(launcherResult: InternalPaymentResult) { | ||
viewModelScope.launch { | ||
runCatching { | ||
requireNotNull(stripeIntent.value) | ||
}.fold( | ||
onSuccess = { originalIntent -> | ||
when (launcherResult) { | ||
is InternalPaymentResult.Completed -> processPayment( | ||
stripeIntent = launcherResult.intent, | ||
paymentResult = PaymentResult.Completed | ||
) | ||
is InternalPaymentResult.Failed -> processPayment( | ||
stripeIntent = originalIntent, | ||
paymentResult = PaymentResult.Failed(launcherResult.throwable) | ||
) | ||
is InternalPaymentResult.Canceled -> processPayment( | ||
stripeIntent = originalIntent, | ||
paymentResult = PaymentResult.Canceled | ||
) | ||
} | ||
}, | ||
onFailure = ::onFatal | ||
) | ||
val intent = stripeIntent.value | ||
|
||
if (intent == null) { | ||
// We're recovering from process death. Wait for the pending payment result | ||
// to be handled after re-loading. | ||
pendingPaymentResult = launcherResult | ||
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. Should this live in the view model? Seems like we could model this to be opaque to the view model. 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. Will follow up with a better abstraction. |
||
return | ||
} | ||
|
||
when (launcherResult) { | ||
is InternalPaymentResult.Completed -> { | ||
processPayment(launcherResult.intent, PaymentResult.Completed) | ||
} | ||
is InternalPaymentResult.Failed -> { | ||
processPayment(intent, PaymentResult.Failed(launcherResult.throwable)) | ||
} | ||
is InternalPaymentResult.Canceled -> { | ||
processPayment(intent, PaymentResult.Canceled) | ||
} | ||
} | ||
} | ||
|
||
private fun handlePaymentFailed(error: Throwable) { | ||
eventReporter.onPaymentFailure( | ||
paymentSelection = selection.value, | ||
error = PaymentSheetConfirmationError.Stripe(error), | ||
) | ||
|
||
resetViewState( | ||
userErrorMessage = error.stripeErrorMessage(getApplication()) | ||
) | ||
} | ||
|
||
private fun handlePaymentCompleted(paymentMethod: PaymentMethod?, finishImmediately: Boolean) { | ||
eventReporter.onPaymentSuccess( | ||
paymentSelection = selection.value, | ||
deferredIntentConfirmationType = deferredIntentConfirmationType, | ||
) | ||
|
||
// Reset after sending event | ||
deferredIntentConfirmationType = null | ||
|
||
/* | ||
* Sets current selection as default payment method in future payment sheet usage. New payment | ||
* methods are only saved if the payment sheet is in setup mode, is in payment intent with setup | ||
* for usage, or the customer has requested the payment method be saved. | ||
*/ | ||
when (val currentSelection = selection.value) { | ||
is PaymentSelection.New -> paymentMethod.takeIf { | ||
currentSelection.canSave(args.initializationMode) | ||
}?.let { method -> | ||
PaymentSelection.Saved(method) | ||
} | ||
else -> currentSelection | ||
}?.let { | ||
prefsRepository.savePaymentSelection(it) | ||
} | ||
|
||
if (finishImmediately) { | ||
_paymentSheetResult.tryEmit(PaymentSheetResult.Completed) | ||
} else { | ||
viewState.value = PaymentSheetViewState.FinishProcessing { | ||
_paymentSheetResult.tryEmit(PaymentSheetResult.Completed) | ||
} | ||
} | ||
} | ||
|
||
private fun processPayment(stripeIntent: StripeIntent, paymentResult: PaymentResult) { | ||
when (paymentResult) { | ||
is PaymentResult.Completed -> { | ||
eventReporter.onPaymentSuccess( | ||
paymentSelection = selection.value, | ||
deferredIntentConfirmationType = deferredIntentConfirmationType, | ||
) | ||
|
||
// Reset after sending event | ||
deferredIntentConfirmationType = null | ||
|
||
/* | ||
* Sets current selection as default payment method in future payment sheet usage. New payment | ||
* methods are only saved if the payment sheet is in setup mode, is in payment intent with setup | ||
* for usage, or the customer has requested the payment method be saved. | ||
*/ | ||
when (val currentSelection = selection.value) { | ||
is PaymentSelection.New -> stripeIntent.paymentMethod.takeIf { | ||
currentSelection.canSave(args.initializationMode) | ||
}?.let { method -> | ||
PaymentSelection.Saved(method) | ||
} | ||
else -> currentSelection | ||
}?.let { | ||
prefsRepository.savePaymentSelection(it) | ||
} | ||
|
||
viewState.value = PaymentSheetViewState.FinishProcessing { | ||
_paymentSheetResult.tryEmit(PaymentSheetResult.Completed) | ||
} | ||
handlePaymentCompleted(paymentMethod = stripeIntent.paymentMethod, finishImmediately = false) | ||
} | ||
is PaymentResult.Failed -> { | ||
eventReporter.onPaymentFailure( | ||
paymentSelection = selection.value, | ||
error = PaymentSheetConfirmationError.Stripe(paymentResult.throwable), | ||
) | ||
|
||
resetViewState( | ||
userErrorMessage = paymentResult.throwable.stripeErrorMessage(getApplication()) | ||
) | ||
handlePaymentFailed(paymentResult.throwable) | ||
} | ||
is PaymentResult.Canceled -> { | ||
resetViewState(userErrorMessage = null) | ||
resetViewState() | ||
} | ||
} | ||
} | ||
|
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to set
pendingPaymentResult
tonull
after this? Or do we just assume the view model will go away?