-
Notifications
You must be signed in to change notification settings - Fork 659
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
169b022
commit f243637
Showing
9 changed files
with
302 additions
and
51 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...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,94 @@ | ||
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 { | ||
val args = confirmationArgs(paymentDetails, linkAccount) | ||
confirmationHandler.start(args) | ||
val result = confirmationHandler.awaitResult() | ||
return transformResult(result) | ||
} | ||
|
||
private fun transformResult(result: ConfirmationHandler.Result?): Result { | ||
return when (result) { | ||
is ConfirmationHandler.Result.Canceled -> Result.Canceled | ||
is ConfirmationHandler.Result.Failed -> { | ||
logger.error( | ||
msg = "Failed to confirm payment", | ||
t = result.cause | ||
) | ||
Result.Failed(result.message) | ||
} | ||
is ConfirmationHandler.Result.Succeeded -> Result.Succeeded | ||
null -> { | ||
logger.error("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 ?: "" | ||
), | ||
shippingDetails = null | ||
) | ||
} | ||
|
||
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 | ||
) | ||
} | ||
} | ||
} |
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 | ||
} | ||
} | ||
|
||
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
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.