-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
remember
methods for PaymentSheet
integration in Compose
- Loading branch information
1 parent
c23a5fa
commit f7e25b1
Showing
16 changed files
with
399 additions
and
101 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
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
16 changes: 0 additions & 16 deletions
16
paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetActivityStarter.kt
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetCompose.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,106 @@ | ||
package com.stripe.android.paymentsheet | ||
|
||
import android.app.Application | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import com.stripe.android.CreateIntentCallback | ||
import com.stripe.android.CreateIntentCallbackForServerSideConfirmation | ||
import com.stripe.android.ExperimentalPaymentSheetDecouplingApi | ||
import com.stripe.android.IntentConfirmationInterceptor | ||
|
||
/** | ||
* Creates a [PaymentSheet] that is remembered across compositions. | ||
* | ||
* This *must* be called unconditionally, as part of the initialization path. | ||
* | ||
* @param paymentResultCallback Called with the result of the payment after [PaymentSheet] is dismissed. | ||
*/ | ||
@Composable | ||
fun rememberPaymentSheet( | ||
paymentResultCallback: PaymentSheetResultCallback, | ||
): PaymentSheet { | ||
val onResult by rememberUpdatedState(newValue = paymentResultCallback::onPaymentSheetResult) | ||
|
||
val activityResultLauncher = rememberLauncherForActivityResult( | ||
contract = PaymentSheetContractV2(), | ||
onResult = onResult, | ||
) | ||
|
||
val context = LocalContext.current | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
return remember { | ||
val launcher = DefaultPaymentSheetLauncher( | ||
activityResultLauncher = activityResultLauncher, | ||
application = context.applicationContext as Application, | ||
lifecycleOwner = lifecycleOwner, | ||
) | ||
PaymentSheet(launcher) | ||
} | ||
} | ||
|
||
/** | ||
* Creates a [PaymentSheet] that is remembered across compositions. Use this method when you intend | ||
* to create the [com.stripe.android.model.PaymentIntent] or [com.stripe.android.model.SetupIntent] | ||
* on your server. | ||
* | ||
* This *must* be called unconditionally, as part of the initialization path. | ||
* | ||
* @param createIntentCallback Called when the customer confirms the payment or setup. | ||
* @param paymentResultCallback Called with the result of the payment after [PaymentSheet] is dismissed. | ||
*/ | ||
@ExperimentalPaymentSheetDecouplingApi | ||
@Composable | ||
fun rememberPaymentSheet( | ||
createIntentCallback: CreateIntentCallback, | ||
paymentResultCallback: PaymentSheetResultCallback, | ||
): PaymentSheet { | ||
UpdateIntentConfirmationInterceptor(createIntentCallback) | ||
return rememberPaymentSheet(paymentResultCallback) | ||
} | ||
|
||
/** | ||
* Creates a [PaymentSheet] that is remembered across compositions. Use this method when you intend | ||
* to create and confirm the [com.stripe.android.model.PaymentIntent] or | ||
* [com.stripe.android.model.SetupIntent] on your server. | ||
* | ||
* This *must* be called unconditionally, as part of the initialization path. | ||
* | ||
* @param createIntentCallback Called when the customer confirms the payment or setup. | ||
* @param paymentResultCallback Called with the result of the payment after [PaymentSheet] is dismissed. | ||
*/ | ||
@ExperimentalPaymentSheetDecouplingApi | ||
@Composable | ||
fun rememberPaymentSheet( | ||
createIntentCallback: CreateIntentCallbackForServerSideConfirmation, | ||
paymentResultCallback: PaymentSheetResultCallback, | ||
): PaymentSheet { | ||
UpdateIntentConfirmationInterceptor(createIntentCallback) | ||
return rememberPaymentSheet(paymentResultCallback) | ||
} | ||
|
||
@OptIn(ExperimentalPaymentSheetDecouplingApi::class) | ||
@Composable | ||
private fun UpdateIntentConfirmationInterceptor( | ||
createIntentCallback: CreateIntentCallback, | ||
) { | ||
LaunchedEffect(createIntentCallback) { | ||
IntentConfirmationInterceptor.createIntentCallback = createIntentCallback | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalPaymentSheetDecouplingApi::class) | ||
@Composable | ||
private fun UpdateIntentConfirmationInterceptor( | ||
createIntentCallbackForServerSideConfirmation: CreateIntentCallbackForServerSideConfirmation, | ||
) { | ||
LaunchedEffect(createIntentCallbackForServerSideConfirmation) { | ||
IntentConfirmationInterceptor.createIntentCallback = createIntentCallbackForServerSideConfirmation | ||
} | ||
} |
Oops, something went wrong.