-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathPaymentSheetFragment.kt
530 lines (477 loc) · 21.9 KB
/
PaymentSheetFragment.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package com.reactnativestripesdk
import android.app.Activity
import android.app.Application
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Base64
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.DrawableCompat
import androidx.fragment.app.Fragment
import com.facebook.react.bridge.*
import com.reactnativestripesdk.addresssheet.AddressSheetView
import com.reactnativestripesdk.utils.*
import com.reactnativestripesdk.utils.createError
import com.reactnativestripesdk.utils.createResult
import com.stripe.android.ExperimentalAllowsRemovalOfLastSavedPaymentMethodApi
import com.stripe.android.paymentsheet.*
import kotlinx.coroutines.CompletableDeferred
import java.io.ByteArrayOutputStream
import kotlin.Exception
@OptIn(ExperimentalAllowsRemovalOfLastSavedPaymentMethodApi::class)
class PaymentSheetFragment(
private val context: ReactApplicationContext,
private val initPromise: Promise
) : Fragment() {
private var paymentSheet: PaymentSheet? = null
private var flowController: PaymentSheet.FlowController? = null
private var paymentIntentClientSecret: String? = null
private var setupIntentClientSecret: String? = null
private var intentConfiguration: PaymentSheet.IntentConfiguration? = null
private lateinit var paymentSheetConfiguration: PaymentSheet.Configuration
private var confirmPromise: Promise? = null
private var presentPromise: Promise? = null
private var paymentSheetTimedOut = false
internal var paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return FrameLayout(requireActivity()).also {
it.visibility = View.GONE
}
}
@OptIn(ExperimentalPaymentMethodLayoutApi::class)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val merchantDisplayName = arguments?.getString("merchantDisplayName").orEmpty()
if (merchantDisplayName.isEmpty()) {
initPromise.resolve(createError(ErrorType.Failed.toString(), "merchantDisplayName cannot be empty or null."))
return
}
val primaryButtonLabel = arguments?.getString("primaryButtonLabel")
val googlePayConfig = buildGooglePayConfig(arguments?.getBundle("googlePay"))
val allowsDelayedPaymentMethods = arguments?.getBoolean("allowsDelayedPaymentMethods")
val billingDetailsBundle = arguments?.getBundle("defaultBillingDetails")
val billingConfigParams = arguments?.getBundle("billingDetailsCollectionConfiguration")
val paymentMethodOrder = arguments?.getStringArrayList("paymentMethodOrder")
val allowsRemovalOfLastSavedPaymentMethod = arguments?.getBoolean("allowsRemovalOfLastSavedPaymentMethod", true) ?: true
paymentIntentClientSecret = arguments?.getString("paymentIntentClientSecret").orEmpty()
setupIntentClientSecret = arguments?.getString("setupIntentClientSecret").orEmpty()
intentConfiguration = try {
buildIntentConfiguration(arguments?.getBundle("intentConfiguration"))
} catch (error: PaymentSheetException) {
initPromise.resolve(createError(ErrorType.Failed.toString(), error))
return
}
val appearance = try {
buildPaymentSheetAppearance(arguments?.getBundle("appearance"), context)
} catch (error: PaymentSheetAppearanceException) {
initPromise.resolve(createError(ErrorType.Failed.toString(), error))
return
}
val customerConfiguration = try {
buildCustomerConfiguration(arguments)
} catch (error: PaymentSheetException) {
initPromise.resolve(createError(ErrorType.Failed.toString(), error))
return
}
val shippingDetails = arguments?.getBundle("defaultShippingDetails")?.let {
AddressSheetView.buildAddressDetails(it)
}
val paymentOptionCallback = PaymentOptionCallback { paymentOption ->
val result = paymentOption?.let {
val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
val imageString = getBase64FromBitmap(bitmap)
val option: WritableMap = WritableNativeMap()
option.putString("label", it.label)
option.putString("image", imageString)
createResult("paymentOption", option)
} ?: run {
if (paymentSheetTimedOut) {
paymentSheetTimedOut = false
createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out")
} else {
createError(PaymentSheetErrorType.Canceled.toString(), "The payment option selection flow has been canceled")
}
}
presentPromise?.resolve(result)
}
val paymentResultCallback = PaymentSheetResultCallback { paymentResult ->
if (paymentSheetTimedOut) {
paymentSheetTimedOut = false
resolvePaymentResult(createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out"))
} else {
when (paymentResult) {
is PaymentSheetResult.Canceled -> {
resolvePaymentResult(createError(PaymentSheetErrorType.Canceled.toString(), "The payment flow has been canceled"))
}
is PaymentSheetResult.Failed -> {
resolvePaymentResult(createError(PaymentSheetErrorType.Failed.toString(), paymentResult.error))
}
is PaymentSheetResult.Completed -> {
resolvePaymentResult(WritableNativeMap())
// Remove the fragment now, we can be sure it won't be needed again if an intent is successful
removeFragment(context)
paymentSheet = null
flowController = null
}
}
}
}
val createIntentCallback = CreateIntentCallback { paymentMethod, shouldSavePaymentMethod ->
val stripeSdkModule: StripeSdkModule? = context.getNativeModule(StripeSdkModule::class.java)
if (stripeSdkModule == null || stripeSdkModule.eventListenerCount == 0) {
return@CreateIntentCallback CreateIntentResult.Failure(
cause = Exception("Tried to call confirmHandler, but no callback was found. Please file an issue: https://github.com/stripe/stripe-react-native/issues"),
displayMessage = "An unexpected error occurred"
)
}
val params = Arguments.createMap().apply {
putMap("paymentMethod", mapFromPaymentMethod(paymentMethod))
putBoolean("shouldSavePaymentMethod", shouldSavePaymentMethod)
}
stripeSdkModule.sendEvent(context, "onConfirmHandlerCallback", params)
val resultFromJavascript = paymentSheetIntentCreationCallback.await()
// reset the completable
paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
return@CreateIntentCallback resultFromJavascript.getString("clientSecret")?.let {
CreateIntentResult.Success(clientSecret = it)
} ?: run {
val errorMap = resultFromJavascript.getMap("error")
CreateIntentResult.Failure(
cause = Exception(errorMap?.getString("message")),
displayMessage = errorMap?.getString("localizedMessage")
)
}
}
val billingDetailsConfig = PaymentSheet.BillingDetailsCollectionConfiguration(
name = mapToCollectionMode(billingConfigParams?.getString("name")),
phone = mapToCollectionMode(billingConfigParams?.getString("phone")),
email = mapToCollectionMode(billingConfigParams?.getString("email")),
address = mapToAddressCollectionMode(billingConfigParams?.getString("address")),
attachDefaultsToPaymentMethod = billingConfigParams?.getBoolean("attachDefaultsToPaymentMethod")
?: false
)
var defaultBillingDetails: PaymentSheet.BillingDetails? = null
if (billingDetailsBundle != null) {
val addressBundle = billingDetailsBundle.getBundle("address")
val address = PaymentSheet.Address(
addressBundle?.getString("city"),
addressBundle?.getString("country"),
addressBundle?.getString("line1"),
addressBundle?.getString("line2"),
addressBundle?.getString("postalCode"),
addressBundle?.getString("state"))
defaultBillingDetails = PaymentSheet.BillingDetails(
address,
billingDetailsBundle.getString("email"),
billingDetailsBundle.getString("name"),
billingDetailsBundle.getString("phone"))
}
val configurationBuilder = PaymentSheet.Configuration.Builder(merchantDisplayName)
.allowsDelayedPaymentMethods(allowsDelayedPaymentMethods ?: false)
.defaultBillingDetails(defaultBillingDetails)
.customer(customerConfiguration)
.googlePay(googlePayConfig)
.appearance(appearance)
.shippingDetails(shippingDetails)
.billingDetailsCollectionConfiguration(billingDetailsConfig)
.preferredNetworks(mapToPreferredNetworks(arguments?.getIntegerArrayList("preferredNetworks")))
.allowsRemovalOfLastSavedPaymentMethod(allowsRemovalOfLastSavedPaymentMethod)
primaryButtonLabel?.let {
configurationBuilder.primaryButtonLabel(it)
}
paymentMethodOrder?.let {
configurationBuilder.paymentMethodOrder(it)
}
configurationBuilder.paymentMethodLayout(
mapToPaymentMethodLayout(arguments?.getString("paymentMethodLayout"))
)
paymentSheetConfiguration = configurationBuilder.build()
if (arguments?.getBoolean("customFlow") == true) {
flowController = if (intentConfiguration != null) {
PaymentSheet.FlowController.create(
this,
paymentOptionCallback = paymentOptionCallback,
createIntentCallback = createIntentCallback,
paymentResultCallback = paymentResultCallback
)
} else {
PaymentSheet.FlowController.create(
this,
paymentOptionCallback = paymentOptionCallback,
paymentResultCallback = paymentResultCallback
)
}
configureFlowController()
} else {
paymentSheet = if (intentConfiguration != null) {
PaymentSheet(
this,
createIntentCallback = createIntentCallback,
paymentResultCallback = paymentResultCallback
)
} else {
PaymentSheet(
this,
callback = paymentResultCallback
)
}
initPromise.resolve(WritableNativeMap())
}
}
fun present(promise: Promise) {
this.presentPromise = promise
if(paymentSheet != null) {
if (!paymentIntentClientSecret.isNullOrEmpty()) {
paymentSheet?.presentWithPaymentIntent(paymentIntentClientSecret!!, paymentSheetConfiguration)
} else if (!setupIntentClientSecret.isNullOrEmpty()) {
paymentSheet?.presentWithSetupIntent(setupIntentClientSecret!!, paymentSheetConfiguration)
} else if (intentConfiguration != null) {
paymentSheet?.presentWithIntentConfiguration(
intentConfiguration = intentConfiguration!!,
configuration = paymentSheetConfiguration
)
}
} else if(flowController != null) {
flowController?.presentPaymentOptions()
} else {
promise.resolve(createMissingInitError())
}
}
fun presentWithTimeout(timeout: Long, promise: Promise) {
var paymentSheetActivity: Activity? = null
val activityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
paymentSheetActivity = activity
}
override fun onActivityStarted(activity: Activity) {}
override fun onActivityResumed(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {
paymentSheetActivity = null
context.currentActivity?.application?.unregisterActivityLifecycleCallbacks(this)
}
}
Handler(Looper.getMainLooper()).postDelayed({
paymentSheetActivity?.let {
it.finish()
paymentSheetTimedOut = true
}
}, timeout)
context.currentActivity?.application?.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
this.present(promise)
}
fun confirmPayment(promise: Promise) {
this.confirmPromise = promise
flowController?.confirm()
}
private fun configureFlowController() {
val onFlowControllerConfigure = PaymentSheet.FlowController.ConfigCallback { _, _ ->
val result = flowController?.getPaymentOption()?.let {
val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
val imageString = getBase64FromBitmap(bitmap)
val option: WritableMap = WritableNativeMap()
option.putString("label", it.label)
option.putString("image", imageString)
createResult("paymentOption", option)
} ?: run {
WritableNativeMap()
}
initPromise.resolve(result)
}
if (!paymentIntentClientSecret.isNullOrEmpty()) {
flowController?.configureWithPaymentIntent(
paymentIntentClientSecret = paymentIntentClientSecret!!,
configuration = paymentSheetConfiguration,
callback = onFlowControllerConfigure
)
} else if (!setupIntentClientSecret.isNullOrEmpty()) {
flowController?.configureWithSetupIntent(
setupIntentClientSecret = setupIntentClientSecret!!,
configuration = paymentSheetConfiguration,
callback = onFlowControllerConfigure
)
} else if (intentConfiguration != null) {
flowController?.configureWithIntentConfiguration(
intentConfiguration = intentConfiguration!!,
configuration = paymentSheetConfiguration,
callback = onFlowControllerConfigure
)
} else {
initPromise.resolve(createError(ErrorType.Failed.toString(), "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration` is required"))
return
}
}
private fun resolvePaymentResult(map: WritableMap) {
confirmPromise?.let {
it.resolve(map)
confirmPromise = null
} ?: run {
presentPromise?.resolve(map)
}
}
companion object {
internal const val TAG = "payment_sheet_launch_fragment"
private val mapIntToButtonType = mapOf(
1 to PaymentSheet.GooglePayConfiguration.ButtonType.Buy,
6 to PaymentSheet.GooglePayConfiguration.ButtonType.Book,
5 to PaymentSheet.GooglePayConfiguration.ButtonType.Checkout,
4 to PaymentSheet.GooglePayConfiguration.ButtonType.Donate,
11 to PaymentSheet.GooglePayConfiguration.ButtonType.Order,
1000 to PaymentSheet.GooglePayConfiguration.ButtonType.Pay,
7 to PaymentSheet.GooglePayConfiguration.ButtonType.Subscribe,
1001 to PaymentSheet.GooglePayConfiguration.ButtonType.Plain,
)
internal fun createMissingInitError(): WritableMap {
return createError(PaymentSheetErrorType.Failed.toString(), "No payment sheet has been initialized yet. You must call `initPaymentSheet` before `presentPaymentSheet`.")
}
internal fun buildGooglePayConfig(params: Bundle?): PaymentSheet.GooglePayConfiguration? {
if (params == null) {
return null
}
val countryCode = params.getString("merchantCountryCode").orEmpty()
val currencyCode = params.getString("currencyCode").orEmpty()
val testEnv = params.getBoolean("testEnv")
val amount = params.getString("amount")?.toLongOrNull()
val label = params.getString("label")
val buttonType = mapIntToButtonType.get(params.getInt("buttonType")) ?: PaymentSheet.GooglePayConfiguration.ButtonType.Pay
return PaymentSheet.GooglePayConfiguration(
environment = if (testEnv) PaymentSheet.GooglePayConfiguration.Environment.Test else PaymentSheet.GooglePayConfiguration.Environment.Production,
countryCode = countryCode,
currencyCode = currencyCode,
amount = amount,
label = label,
buttonType = buttonType
)
}
@Throws(PaymentSheetException::class)
private fun buildIntentConfiguration(intentConfigurationParams: Bundle?): PaymentSheet.IntentConfiguration? {
if (intentConfigurationParams == null) {
return null
}
val modeParams = intentConfigurationParams.getBundle("mode")
?: throw PaymentSheetException("If `intentConfiguration` is provided, `intentConfiguration.mode` is required")
return PaymentSheet.IntentConfiguration(
mode = buildIntentConfigurationMode(modeParams),
paymentMethodTypes = intentConfigurationParams.getStringArrayList("paymentMethodTypes")?.toList() ?: emptyList(),
)
}
private fun buildIntentConfigurationMode(modeParams: Bundle): PaymentSheet.IntentConfiguration.Mode {
val currencyCode = modeParams.getString("currencyCode")
?: throw PaymentSheetException("You must provide a value to intentConfiguration.mode.currencyCode")
return if (modeParams.containsKey("amount")) {
PaymentSheet.IntentConfiguration.Mode.Payment(
amount = modeParams.getInt("amount").toLong(),
currency = currencyCode,
setupFutureUse = mapToSetupFutureUse(modeParams.getString("setupFutureUsage")),
captureMethod = mapToCaptureMethod(modeParams.getString("captureMethod")),
)
} else {
val setupFutureUsage = mapToSetupFutureUse(modeParams.getString("setupFutureUsage"))
?: throw PaymentSheetException("You must provide a value to intentConfiguration.mode.setupFutureUsage")
PaymentSheet.IntentConfiguration.Mode.Setup(
currency = currencyCode,
setupFutureUse = setupFutureUsage
)
}
}
@OptIn(ExperimentalCustomerSessionApi::class)
@Throws(PaymentSheetException::class)
private fun buildCustomerConfiguration(bundle: Bundle?): PaymentSheet.CustomerConfiguration? {
val customerId = bundle?.getString("customerId").orEmpty()
val customerEphemeralKeySecret = bundle?.getString("customerEphemeralKeySecret").orEmpty()
val customerSessionClientSecret = bundle?.getString("customerSessionClientSecret").orEmpty()
return if (customerSessionClientSecret.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
throw PaymentSheetException("`customerEphemeralKeySecret` and `customerSessionClientSecret` cannot both be set")
} else if (customerId.isNotEmpty() && customerSessionClientSecret.isNotEmpty()) {
PaymentSheet.CustomerConfiguration.createWithCustomerSession(
id = customerId,
clientSecret = customerSessionClientSecret
)
}
else if (customerId.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
PaymentSheet.CustomerConfiguration(
id = customerId,
ephemeralKeySecret = customerEphemeralKeySecret
)
} else null
}
}
}
fun getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap? {
val drawable = AppCompatResources.getDrawable(context!!, drawableId) ?: return null
return getBitmapFromDrawable(drawable)
}
fun getBitmapFromDrawable(drawable: Drawable): Bitmap? {
val drawableCompat = DrawableCompat.wrap(drawable).mutate()
if (drawableCompat.intrinsicWidth <= 0 || drawableCompat.intrinsicHeight <= 0) {
return null
}
val bitmap = Bitmap.createBitmap(drawableCompat.intrinsicWidth, drawableCompat.intrinsicHeight, Bitmap.Config.ARGB_8888)
bitmap.eraseColor(Color.WHITE)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
fun getBase64FromBitmap(bitmap: Bitmap?): String? {
if (bitmap == null) {
return null
}
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
val imageBytes: ByteArray = stream.toByteArray()
return Base64.encodeToString(imageBytes, Base64.DEFAULT)
}
fun mapToCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode {
return when (str) {
"automatic" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Automatic
"never" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Never
"always" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
else -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Automatic
}
}
fun mapToPaymentMethodLayout(str: String?): PaymentSheet.PaymentMethodLayout {
return when (str) {
"Horizontal" -> PaymentSheet.PaymentMethodLayout.Horizontal
"Vertical" -> PaymentSheet.PaymentMethodLayout.Vertical
else -> PaymentSheet.PaymentMethodLayout.Automatic
}
}
fun mapToAddressCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode {
return when (str) {
"automatic" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
"never" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Never
"full" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Full
else -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
}
}
fun mapToSetupFutureUse(type: String?): PaymentSheet.IntentConfiguration.SetupFutureUse? {
return when (type) {
"OffSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OffSession
"OnSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OnSession
else -> null
}
}
fun mapToCaptureMethod(type: String?): PaymentSheet.IntentConfiguration.CaptureMethod {
return when (type) {
"Automatic" -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
"Manual" -> PaymentSheet.IntentConfiguration.CaptureMethod.Manual
"AutomaticAsync" -> PaymentSheet.IntentConfiguration.CaptureMethod.AutomaticAsync
else -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
}
}