-
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
d833049
commit 6b20df7
Showing
17 changed files
with
330 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.
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
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 | ||
|
||
/** | ||
* 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) | ||
} | ||
|
||
@OptIn(ExperimentalPaymentSheetDecouplingApi::class) | ||
@Composable | ||
private fun UpdateIntentConfirmationInterceptor( | ||
createIntentCallback: CreateIntentCallback, | ||
) { | ||
LaunchedEffect(createIntentCallback) { | ||
IntentConfirmationInterceptor.createIntentCallback = createIntentCallback | ||
} | ||
} |
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.