Skip to content

Commit

Permalink
Use a builder to create the embedded payment element. (#9799)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Dec 18, 2024
1 parent 3b4b419 commit f2eeb95
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ internal class EmbeddedPlaygroundActivity : AppCompatActivity(), ExternalPayment
return
}

val embeddedBuilder = EmbeddedPaymentElement.Builder(
createIntentCallback = { _, _ ->
CreateIntentResult.Success(playgroundState.clientSecret)
},
resultCallback = ::handleEmbeddedResult,
).externalPaymentMethodConfirmHandler(this)
setContent {
val embeddedPaymentElement = rememberEmbeddedPaymentElement(
createIntentCallback = { _, _ ->
CreateIntentResult.Success(playgroundState.clientSecret)
},
externalPaymentMethodConfirmHandler = this,
resultCallback = ::handleEmbeddedResult,
)
val embeddedPaymentElement = rememberEmbeddedPaymentElement(embeddedBuilder)

LaunchedEffect(embeddedPaymentElement) {
embeddedPaymentElement.configure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.stripe.android.model.SetupIntent
import com.stripe.android.paymentelement.confirmation.intent.IntentConfirmationInterceptor
import com.stripe.android.paymentelement.embedded.EmbeddedConfirmationHelper
import com.stripe.android.paymentelement.embedded.SharedPaymentElementViewModel
import com.stripe.android.paymentsheet.CreateIntentCallback
import com.stripe.android.paymentsheet.ExternalPaymentMethodConfirmHandler
import com.stripe.android.paymentsheet.ExternalPaymentMethodInterceptor
import com.stripe.android.paymentsheet.PaymentSheet
Expand Down Expand Up @@ -85,6 +86,34 @@ class EmbeddedPaymentElement private constructor(
sharedViewModel.clearPaymentOption()
}

/**
* Builder used in the creation of the [EmbeddedPaymentElement].
*
* Creation can be completed with [rememberEmbeddedPaymentElement].
*/
@ExperimentalEmbeddedPaymentElementApi
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class Builder(
/**
* Called when the customer confirms the payment or setup.
*/
internal val createIntentCallback: CreateIntentCallback,
/**
* Called with the result of the payment.
*/
internal val resultCallback: ResultCallback,
) {
internal var externalPaymentMethodConfirmHandler: ExternalPaymentMethodConfirmHandler? = null
private set

/**
* Called when a user confirms payment for an external payment method.
*/
fun externalPaymentMethodConfirmHandler(handler: ExternalPaymentMethodConfirmHandler) = apply {
this.externalPaymentMethodConfirmHandler = handler
}
}

/** Configuration for [EmbeddedPaymentElement] **/
@Parcelize
@Poko
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,32 @@ import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import com.stripe.android.common.ui.PaymentElementActivityResultCaller
import com.stripe.android.common.ui.UpdateExternalPaymentMethodConfirmHandler
import com.stripe.android.common.ui.UpdateIntentConfirmationInterceptor
import com.stripe.android.paymentsheet.CreateIntentCallback
import com.stripe.android.paymentsheet.ExternalPaymentMethodConfirmHandler
import com.stripe.android.utils.rememberActivity

/**
* Creates an [EmbeddedPaymentElement] that is remembered across compositions.
*
* This *must* be called unconditionally, as part of the initialization path.
*
* @param createIntentCallback Called when the customer confirms the payment or setup.
* @param externalPaymentMethodConfirmHandler Called when a user confirms payment for an external payment method.
* @param resultCallback Called with the result of the payment.
*/
@ExperimentalEmbeddedPaymentElementApi
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Composable
fun rememberEmbeddedPaymentElement(
createIntentCallback: CreateIntentCallback,
externalPaymentMethodConfirmHandler: ExternalPaymentMethodConfirmHandler? = null,
resultCallback: EmbeddedPaymentElement.ResultCallback,
builder: EmbeddedPaymentElement.Builder
): EmbeddedPaymentElement {
val viewModelStoreOwner = requireNotNull(LocalViewModelStoreOwner.current) {
"EmbeddedPaymentElement must have a ViewModelStoreOwner."
}

UpdateExternalPaymentMethodConfirmHandler(externalPaymentMethodConfirmHandler)
UpdateIntentConfirmationInterceptor(createIntentCallback)
UpdateExternalPaymentMethodConfirmHandler(builder.externalPaymentMethodConfirmHandler)
UpdateIntentConfirmationInterceptor(builder.createIntentCallback)

val lifecycleOwner = LocalLifecycleOwner.current
val activityResultRegistryOwner = requireNotNull(LocalActivityResultRegistryOwner.current) {
"EmbeddedPaymentElement must have an ActivityResultRegistryOwner."
}

val onResult by rememberUpdatedState(newValue = resultCallback::onResult)
val onResult by rememberUpdatedState(newValue = builder.resultCallback::onResult)

val activity = rememberActivity {
"EmbeddedPaymentElement must be created in the context of an Activity."
Expand Down

0 comments on commit f2eeb95

Please sign in to comment.