-
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.
Use ConfirmationHandler in embedded. (#9750)
- Loading branch information
1 parent
51f98a2
commit 54b60fe
Showing
13 changed files
with
403 additions
and
28 deletions.
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
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
72 changes: 72 additions & 0 deletions
72
...et/src/main/java/com/stripe/android/paymentelement/embedded/EmbeddedConfirmationHelper.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,72 @@ | ||
package com.stripe.android.paymentelement.embedded | ||
|
||
import androidx.activity.result.ActivityResultCaller | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import com.stripe.android.common.model.asCommonConfiguration | ||
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata | ||
import com.stripe.android.paymentelement.EmbeddedPaymentElement | ||
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi | ||
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler | ||
import com.stripe.android.paymentelement.confirmation.toConfirmationOption | ||
import com.stripe.android.paymentsheet.model.PaymentSelection | ||
import com.stripe.android.paymentsheet.state.PaymentElementLoader | ||
import kotlinx.coroutines.launch | ||
|
||
@ExperimentalEmbeddedPaymentElementApi | ||
internal class EmbeddedConfirmationHelper( | ||
private val confirmationHandler: ConfirmationHandler, | ||
private val resultCallback: EmbeddedPaymentElement.ResultCallback, | ||
private val activityResultCaller: ActivityResultCaller, | ||
private val lifecycleOwner: LifecycleOwner, | ||
private val confirmationStateSupplier: () -> State?, | ||
) { | ||
init { | ||
confirmationHandler.register( | ||
activityResultCaller = activityResultCaller, | ||
lifecycleOwner = lifecycleOwner | ||
) | ||
|
||
lifecycleOwner.lifecycleScope.launch { | ||
confirmationHandler.state.collect { state -> | ||
when (state) { | ||
is ConfirmationHandler.State.Complete -> { | ||
resultCallback.onResult(state.result.asEmbeddedResult()) | ||
} | ||
is ConfirmationHandler.State.Confirming, ConfirmationHandler.State.Idle -> Unit | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun confirm() { | ||
confirmationArgs()?.let { confirmationArgs -> | ||
confirmationHandler.start(confirmationArgs) | ||
} ?: run { | ||
resultCallback.onResult( | ||
EmbeddedPaymentElement.Result.Failed(IllegalStateException("Not in a state that's confirmable.")) | ||
) | ||
} | ||
} | ||
|
||
private fun confirmationArgs(): ConfirmationHandler.Args? { | ||
val loadedState = confirmationStateSupplier() ?: return null | ||
val confirmationOption = loadedState.selection?.toConfirmationOption( | ||
initializationMode = loadedState.initializationMode, | ||
configuration = loadedState.configuration.asCommonConfiguration(), | ||
appearance = loadedState.configuration.appearance, | ||
) ?: return null | ||
|
||
return ConfirmationHandler.Args( | ||
intent = loadedState.paymentMethodMetadata.stripeIntent, | ||
confirmationOption = confirmationOption, | ||
) | ||
} | ||
|
||
data class State( | ||
val paymentMethodMetadata: PaymentMethodMetadata, | ||
val selection: PaymentSelection?, | ||
val initializationMode: PaymentElementLoader.InitializationMode, | ||
val configuration: EmbeddedPaymentElement.Configuration, | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
paymentsheet/src/main/java/com/stripe/android/paymentelement/embedded/EmbeddedResultKtx.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,20 @@ | ||
package com.stripe.android.paymentelement.embedded | ||
|
||
import com.stripe.android.paymentelement.EmbeddedPaymentElement | ||
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi | ||
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler | ||
|
||
@ExperimentalEmbeddedPaymentElementApi | ||
internal fun ConfirmationHandler.Result.asEmbeddedResult(): EmbeddedPaymentElement.Result { | ||
return when (this) { | ||
is ConfirmationHandler.Result.Canceled -> { | ||
EmbeddedPaymentElement.Result.Canceled() | ||
} | ||
is ConfirmationHandler.Result.Failed -> { | ||
EmbeddedPaymentElement.Result.Failed(cause) | ||
} | ||
is ConfirmationHandler.Result.Succeeded -> { | ||
EmbeddedPaymentElement.Result.Completed() | ||
} | ||
} | ||
} |
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.