-
Notifications
You must be signed in to change notification settings - Fork 662
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 Confirmation Handler #9865
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ff01f2
Link Confirmation Handler
toluo-stripe 20e039d
Fix savedStateHandle test issue
toluo-stripe a71adc6
Remove unneeded return statement
toluo-stripe 27d901e
Add setup intent case and update tests
toluo-stripe bf6452c
Remove function parameter
toluo-stripe 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
107 changes: 107 additions & 0 deletions
107
...heet/src/main/java/com/stripe/android/link/confirmation/DefaultLinkConfirmationHandler.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,107 @@ | ||||||
package com.stripe.android.link.confirmation | ||||||
|
||||||
import com.stripe.android.core.Logger | ||||||
import com.stripe.android.core.strings.resolvableString | ||||||
import com.stripe.android.link.LinkConfiguration | ||||||
import com.stripe.android.link.model.LinkAccount | ||||||
import com.stripe.android.model.ConsumerPaymentDetails | ||||||
import com.stripe.android.model.PaymentMethodCreateParams | ||||||
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler | ||||||
import com.stripe.android.paymentelement.confirmation.PaymentMethodConfirmationOption | ||||||
import com.stripe.android.paymentsheet.PaymentSheet | ||||||
import com.stripe.android.paymentsheet.R | ||||||
import com.stripe.android.paymentsheet.state.PaymentElementLoader | ||||||
import javax.inject.Inject | ||||||
|
||||||
internal class DefaultLinkConfirmationHandler @Inject constructor( | ||||||
private val configuration: LinkConfiguration, | ||||||
private val logger: Logger, | ||||||
private val confirmationHandler: ConfirmationHandler | ||||||
) : LinkConfirmationHandler { | ||||||
override suspend fun confirm( | ||||||
paymentDetails: ConsumerPaymentDetails.PaymentDetails, | ||||||
linkAccount: LinkAccount | ||||||
): Result { | ||||||
return kotlin.runCatching { | ||||||
val args = confirmationArgs(paymentDetails, linkAccount) | ||||||
confirmationHandler.start(args) | ||||||
val result = confirmationHandler.awaitResult() | ||||||
transformResult(result) | ||||||
}.getOrElse { error -> | ||||||
logger.error( | ||||||
msg = "DefaultLinkConfirmationHandler: Failed to confirm payment", | ||||||
t = error | ||||||
) | ||||||
Result.Failed(R.string.stripe_something_went_wrong.resolvableString) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun transformResult(result: ConfirmationHandler.Result?): Result { | ||||||
return when (result) { | ||||||
is ConfirmationHandler.Result.Canceled -> Result.Canceled | ||||||
is ConfirmationHandler.Result.Failed -> { | ||||||
logger.error( | ||||||
msg = "DefaultLinkConfirmationHandler: Failed to confirm payment", | ||||||
t = result.cause | ||||||
) | ||||||
Result.Failed(result.message) | ||||||
} | ||||||
is ConfirmationHandler.Result.Succeeded -> Result.Succeeded | ||||||
null -> { | ||||||
logger.error("DefaultLinkConfirmationHandler: Payment confirmation returned null") | ||||||
Result.Failed(R.string.stripe_something_went_wrong.resolvableString) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private fun confirmationArgs( | ||||||
paymentDetails: ConsumerPaymentDetails.PaymentDetails, | ||||||
linkAccount: LinkAccount | ||||||
): ConfirmationHandler.Args { | ||||||
return ConfirmationHandler.Args( | ||||||
intent = configuration.stripeIntent, | ||||||
confirmationOption = PaymentMethodConfirmationOption.New( | ||||||
createParams = createPaymentMethodCreateParams( | ||||||
selectedPaymentDetails = paymentDetails, | ||||||
linkAccount = linkAccount | ||||||
), | ||||||
optionsParams = null, | ||||||
shouldSave = false | ||||||
), | ||||||
appearance = PaymentSheet.Appearance(), | ||||||
initializationMode = PaymentElementLoader.InitializationMode.PaymentIntent( | ||||||
clientSecret = configuration.stripeIntent.clientSecret | ||||||
?: throw NO_CLIENT_SECRET_FOUND | ||||||
), | ||||||
shippingDetails = configuration.shippingDetails | ||||||
) | ||||||
} | ||||||
|
||||||
private fun createPaymentMethodCreateParams( | ||||||
selectedPaymentDetails: ConsumerPaymentDetails.PaymentDetails, | ||||||
linkAccount: LinkAccount, | ||||||
): PaymentMethodCreateParams { | ||||||
return PaymentMethodCreateParams.createLink( | ||||||
paymentDetailsId = selectedPaymentDetails.id, | ||||||
consumerSessionClientSecret = linkAccount.clientSecret, | ||||||
extraParams = emptyMap(), | ||||||
) | ||||||
} | ||||||
|
||||||
class Factory @Inject constructor( | ||||||
private val configuration: LinkConfiguration, | ||||||
private val logger: Logger, | ||||||
) : LinkConfirmationHandler.Factory { | ||||||
override fun create(confirmationHandler: ConfirmationHandler): LinkConfirmationHandler { | ||||||
return DefaultLinkConfirmationHandler( | ||||||
confirmationHandler = confirmationHandler, | ||||||
logger = logger, | ||||||
configuration = configuration | ||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
companion object { | ||||||
val NO_CLIENT_SECRET_FOUND = IllegalStateException("no client secret found") | ||||||
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.
Suggested change
|
||||||
} | ||||||
} |
23 changes: 23 additions & 0 deletions
23
paymentsheet/src/main/java/com/stripe/android/link/confirmation/LinkConfirmationHandler.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,23 @@ | ||
package com.stripe.android.link.confirmation | ||
|
||
import com.stripe.android.core.strings.ResolvableString | ||
import com.stripe.android.link.model.LinkAccount | ||
import com.stripe.android.model.ConsumerPaymentDetails | ||
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler | ||
|
||
internal interface LinkConfirmationHandler { | ||
suspend fun confirm( | ||
paymentDetails: ConsumerPaymentDetails.PaymentDetails, | ||
linkAccount: LinkAccount | ||
): Result | ||
|
||
fun interface Factory { | ||
fun create(confirmationHandler: ConfirmationHandler): LinkConfirmationHandler | ||
} | ||
} | ||
|
||
internal sealed interface Result { | ||
data object Succeeded : Result | ||
data object Canceled : Result | ||
data class Failed(val message: ResolvableString) : Result | ||
} |
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.
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.
Is it possible to write this this way? Or why do we need to specify
kotlin
here?