@@ -8,25 +8,55 @@ import com.stripe.android.paymentsheet.PaymentSheet
8
8
import com.stripe.android.paymentsheet.PaymentSheetResult
9
9
import model.InitialiseParams
10
10
11
+ /* *
12
+ * Class responsible for initializing Stripe SDK and handling payment-related operations.
13
+ */
11
14
class InitializeStripe {
12
15
16
+ // Private variables for storing activity, context, and publishable key.
13
17
private var _activity : ComponentActivity ? = null
14
18
private var _context : Context ? = null
15
19
private var _publishableKey : String? = null
20
+
21
+ /* *
22
+ * Instance of [Stripe] used for interacting with Stripe SDK.
23
+ */
16
24
lateinit var stripe: Stripe
25
+
26
+ /* *
27
+ * Instance of [PaymentSheet] used for managing Stripe payment sheets.
28
+ */
17
29
lateinit var paymentSheet: PaymentSheet
18
- var clientSecret = " "
19
30
20
- private var paymentResultCallback : PaymentResult ? = null
31
+ var clientSecret = " "
21
32
33
+ /* *
34
+ * Callback interface for payment result handling.
35
+ */
36
+ private var paymentResultCallback: PaymentResult ? = null
22
37
38
+ /* *
39
+ * Interface definition for payment result callbacks.
40
+ */
23
41
interface PaymentResult {
42
+ /* *
43
+ * Called when payment succeeds.
44
+ * @param status A map containing payment status details.
45
+ */
24
46
fun onSuccess (status : Map <String , Any ?>)
47
+
48
+ /* *
49
+ * Called when payment fails.
50
+ * @param throwable An exception containing the error details.
51
+ */
25
52
fun onFailure (throwable : Throwable )
26
53
}
27
54
28
-
29
- // Initialize Stripe only once
55
+ /* *
56
+ * Initializes the Stripe SDK with the provided parameters. Ensures initialization happens only once.
57
+ *
58
+ * @param initialObject An instance of [InitialiseParams] containing initialization details.
59
+ */
30
60
fun initializeStripe (initialObject : InitialiseParams ) {
31
61
if (_activity == null && _context == null && _publishableKey == null ) {
32
62
_activity = initialObject.androidActivity as ComponentActivity
@@ -40,20 +70,30 @@ class InitializeStripe {
40
70
}
41
71
}
42
72
43
-
73
+ /* *
74
+ * Initializes the Stripe Payment Sheet with the provided parameters.
75
+ *
76
+ * @param initialiseParams An instance of [InitialiseParams] containing initialization details.
77
+ */
44
78
fun initialisePaymentSheet (initialiseParams : InitialiseParams ) {
45
- if ((initialiseParams.androidContext != null ) && (initialiseParams.androidActivity != null )) {
79
+ if ((initialiseParams.androidContext != null ) && (initialiseParams.androidActivity != null )) {
46
80
PaymentConfiguration .init (
47
81
context = initialiseParams.androidContext as Context ,
48
82
publishableKey = initialiseParams.publishableKey
49
83
)
50
84
51
85
paymentSheet =
52
86
PaymentSheet (initialiseParams.androidActivity as ComponentActivity ) { paymentSheet ->
53
- onPaymentSheetResult(paymentSheet)
87
+ onPaymentSheetResult(paymentSheet)
54
88
}
55
89
}
56
90
}
91
+
92
+ /* *
93
+ * Handles the result of a payment sheet operation.
94
+ *
95
+ * @param paymentSheetResult The result of the payment sheet operation.
96
+ */
57
97
private fun onPaymentSheetResult (paymentSheetResult : PaymentSheetResult ) {
58
98
when (paymentSheetResult) {
59
99
is PaymentSheetResult .Canceled -> {
@@ -67,19 +107,29 @@ class InitializeStripe {
67
107
}
68
108
69
109
is PaymentSheetResult .Completed -> {
70
- // TODO: Handle paymentSheetResult
110
+ // TODO: Handle paymentSheetResult
71
111
paymentResultCallback?.onSuccess(emptyMap())
72
112
paymentResultCallback = null
73
113
}
74
114
}
75
115
}
76
116
117
+ /* *
118
+ * Sets the callback for handling payment results.
119
+ *
120
+ * @param callback An implementation of [PaymentResult] interface.
121
+ */
77
122
fun setPaymentResultCallback (callback : InitializeStripe .PaymentResult ) {
78
123
paymentResultCallback = callback
79
124
}
80
-
81
125
}
82
126
127
+ /* *
128
+ * Singleton object for managing a single instance of [InitializeStripe].
129
+ */
83
130
object SingletonStripeInitialization {
131
+ /* *
132
+ * Single instance of [InitializeStripe].
133
+ */
84
134
val StripeInstanse = InitializeStripe ()
85
- }
135
+ }
0 commit comments