-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentLauncher.kt
171 lines (156 loc) · 6 KB
/
PaymentLauncher.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.stripe.android.payments.paymentlauncher
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.RestrictTo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.fragment.app.Fragment
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmSetupIntentParams
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.SetupIntent
import com.stripe.android.utils.rememberActivityOrNull
/**
* API to confirm and handle next actions for [PaymentIntent] and [SetupIntent].
*/
interface PaymentLauncher {
/**
* Confirms and, if necessary, authenticates a [PaymentIntent].
*/
fun confirm(params: ConfirmPaymentIntentParams)
/**
* Confirms and, if necessary, authenticates a [SetupIntent].
*/
fun confirm(params: ConfirmSetupIntentParams)
/**
* Fetches a [PaymentIntent] and handles its next action.
*/
fun handleNextActionForPaymentIntent(clientSecret: String)
/**
* Fetches a [SetupIntent] and handles its next action.
*/
fun handleNextActionForSetupIntent(clientSecret: String)
/**
* Callback to notify when the intent is confirmed and next action handled.
*/
fun interface PaymentResultCallback {
fun onPaymentResult(paymentResult: PaymentResult)
}
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun interface InternalPaymentResultCallback {
fun onPaymentResult(launcherResult: InternalPaymentResult)
}
companion object {
/**
* Create a [PaymentLauncher] instance with [ComponentActivity].
*
* This API registers an [ActivityResultLauncher] into the [ComponentActivity], it needs
* to be called before the [ComponentActivity] is created.
*/
@JvmStatic
@JvmOverloads
fun create(
activity: ComponentActivity,
publishableKey: String,
stripeAccountId: String? = null,
callback: PaymentResultCallback
) = PaymentLauncherFactory(
activity,
callback.toInternalResultCallback()
).create(publishableKey, stripeAccountId)
/**
* Create a [PaymentLauncher] instance with [Fragment].
*
* This API registers an [ActivityResultLauncher] into the [Fragment]'s hosting Activity, it
* needs to be called before the [Fragment] is created.
*/
@JvmStatic
@JvmOverloads
fun create(
fragment: Fragment,
publishableKey: String,
stripeAccountId: String? = null,
callback: PaymentResultCallback
) = PaymentLauncherFactory(
fragment,
callback.toInternalResultCallback()
).create(publishableKey, stripeAccountId)
/**
* Create a [PaymentLauncher] used for Jetpack Compose.
*
* This API uses Compose specific API [rememberLauncherForActivityResult] to register a
* [ActivityResultLauncher] into current activity, it should be called as part of Compose
* initialization path.
* This method creates a new PaymentLauncher object every time it is called, even during
* recompositions.
*/
@Deprecated(
message = "Use rememberPaymentLauncher() instead",
replaceWith = ReplaceWith(
expression = "rememberPaymentLauncher(publishableKey, stripeAccountId, callback)",
),
)
@Composable
fun createForCompose(
publishableKey: String,
stripeAccountId: String? = null,
callback: PaymentResultCallback
): PaymentLauncher {
return rememberPaymentLauncher(publishableKey, stripeAccountId, callback)
}
/**
* Create a [PaymentLauncher] used for Jetpack Compose.
*
* This API uses Compose specific API [rememberLauncherForActivityResult] to register a
* [ActivityResultLauncher] into current activity, it should be called as part of Compose
* initialization path.
* The PaymentLauncher created is remembered across recompositions. Recomposition will
* always return the value produced by composition.
*/
@Deprecated(
message = "Use rememberPaymentLauncher() instead",
replaceWith = ReplaceWith(
expression = "rememberPaymentLauncher(publishableKey, stripeAccountId, callback)",
),
)
@Composable
fun rememberLauncher(
publishableKey: String,
stripeAccountId: String? = null,
callback: PaymentResultCallback
): PaymentLauncher {
return rememberPaymentLauncher(publishableKey, stripeAccountId, callback)
}
}
}
/**
* Creates a [PaymentLauncher] that is remembered across compositions.
*
* This *must* be called unconditionally, as part of the initialization path.
*
* @param publishableKey The publishable key to use
* @param stripeAccountId Optional, the Connect account to associate with this request
* @param callback Called with the result of the payment operation
*/
@Composable
fun rememberPaymentLauncher(
publishableKey: String,
stripeAccountId: String? = null,
callback: PaymentLauncher.PaymentResultCallback
): PaymentLauncher {
val activity = rememberActivityOrNull()
val launcherCallback = remember(callback) {
callback.toInternalResultCallback()
}
val activityResultLauncher = rememberLauncherForActivityResult(
PaymentLauncherContract(),
launcherCallback::onPaymentResult
)
return remember(publishableKey, stripeAccountId) {
PaymentLauncherFactory(
hostActivityLauncher = activityResultLauncher,
statusBarColor = activity?.window?.statusBarColor,
).create(publishableKey, stripeAccountId)
}
}