-
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
fbc3daf
commit 1b50c6d
Showing
6 changed files
with
400 additions
and
1 deletion.
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
118 changes: 118 additions & 0 deletions
118
...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,118 @@ | ||
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.PaymentIntent | ||
import com.stripe.android.model.PaymentMethodCreateParams | ||
import com.stripe.android.model.SetupIntent | ||
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 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 = initializationMode(), | ||
shippingDetails = configuration.shippingDetails | ||
) | ||
} | ||
|
||
private fun initializationMode(): PaymentElementLoader.InitializationMode { | ||
val clientSecret = configuration.stripeIntent.clientSecret ?: throw NO_CLIENT_SECRET_FOUND | ||
return when (configuration.stripeIntent) { | ||
is PaymentIntent -> { | ||
PaymentElementLoader.InitializationMode.PaymentIntent(clientSecret) | ||
} | ||
is SetupIntent -> { | ||
PaymentElementLoader.InitializationMode.SetupIntent(clientSecret) | ||
} | ||
} | ||
} | ||
|
||
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.") | ||
} | ||
} |
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.