-
Notifications
You must be signed in to change notification settings - Fork 629
/
Stripe.kt
1907 lines (1834 loc) · 74.7 KB
/
Stripe.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.stripe.android
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.annotation.Size
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.stripe.android.core.ApiKeyValidator
import com.stripe.android.core.ApiVersion
import com.stripe.android.core.AppInfo
import com.stripe.android.core.Logger
import com.stripe.android.core.exception.APIConnectionException
import com.stripe.android.core.exception.APIException
import com.stripe.android.core.exception.AuthenticationException
import com.stripe.android.core.exception.InvalidRequestException
import com.stripe.android.core.exception.StripeException
import com.stripe.android.core.model.StripeFile
import com.stripe.android.core.model.StripeFileParams
import com.stripe.android.core.model.StripeModel
import com.stripe.android.core.networking.ApiRequest
import com.stripe.android.core.version.StripeSdkVersion
import com.stripe.android.exception.CardException
import com.stripe.android.hcaptcha.performPassiveHCaptcha
import com.stripe.android.model.AccountParams
import com.stripe.android.model.BankAccount
import com.stripe.android.model.BankAccountTokenParams
import com.stripe.android.model.Card
import com.stripe.android.model.CardParams
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmSetupIntentParams
import com.stripe.android.model.CvcTokenParams
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.PaymentMethodUpdateParams
import com.stripe.android.model.PersonTokenParams
import com.stripe.android.model.PiiTokenParams
import com.stripe.android.model.PossibleBrands
import com.stripe.android.model.RadarSession
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.Source
import com.stripe.android.model.SourceParams
import com.stripe.android.model.Token
import com.stripe.android.model.TokenParams
import com.stripe.android.model.WeChatPayNextAction
import com.stripe.android.networking.StripeApiRepository
import com.stripe.android.networking.StripeRepository
import com.stripe.android.view.AuthActivityStarterHost
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
/**
* Entry-point to the Stripe SDK.
*
* Supports asynchronous and synchronous methods to access the following Stripe APIs.
*
* * [Payment Intent API][PaymentIntent] - confirm and retrieve
* * [Setup Intents API][SetupIntent] - confirm and retrieve
* * [Payment Methods API][PaymentMethod] - create
* * [Sources API][Source] - create and retrieve
* * [Tokens API][Token] - create tokens for [Card], [BankAccount], [PiiTokenParams], and [AccountParams]
*
*/
class Stripe internal constructor(
internal val stripeRepository: StripeRepository,
internal val paymentController: PaymentController,
publishableKey: String,
internal val stripeAccountId: String? = null,
private val workContext: CoroutineContext = Dispatchers.IO
) {
internal val publishableKey: String = ApiKeyValidator().requireValid(publishableKey)
/**
* Constructor with publishable key and Stripe Connect account id.
*
* @param context Activity or application context
* @param publishableKey the client's publishable key
* @param stripeAccountId optional, the Stripe Connect account id to attach to [Stripe API requests](https://stripe.com/docs/connect/authentication#authentication-via-the-stripe-account-header)
* @param enableLogging enable logging in the Stripe and Stripe 3DS2 SDKs; disabled by default.
* It is recommended to disable logging in production. Logs can be accessed from the command line using
* `adb logcat -s StripeSdk`
* @param betas optional, set of beta flags to pass to the Stripe API. Setting this property is
* not sufficient to participate in a beta, and passing a beta you are not registered
* in will result in API errors.
*/
@JvmOverloads
constructor(
context: Context,
publishableKey: String,
stripeAccountId: String? = null,
enableLogging: Boolean = false,
betas: Set<StripeApiBeta> = emptySet()
) : this(
context.applicationContext,
StripeApiRepository(
context.applicationContext,
{ publishableKey },
appInfo,
Logger.getInstance(enableLogging),
betas = betas
),
ApiKeyValidator.get().requireValid(publishableKey),
stripeAccountId,
enableLogging
)
private constructor(
context: Context,
stripeRepository: StripeRepository,
publishableKey: String,
stripeAccountId: String?,
enableLogging: Boolean
) : this(
stripeRepository,
StripePaymentController(
context.applicationContext,
{ publishableKey },
stripeRepository,
enableLogging
),
publishableKey,
stripeAccountId
)
internal constructor(
stripeRepository: StripeRepository,
paymentController: PaymentController,
publishableKey: String,
stripeAccountId: String?
) : this(
stripeRepository,
paymentController,
publishableKey,
stripeAccountId,
Dispatchers.IO
)
//
// Payment Intents API - https://stripe.com/docs/api/payment_intents
//
/**
* Confirm and, if necessary, authenticate a [PaymentIntent].
* Used for [automatic confirmation](https://stripe.com/docs/payments/payment-intents/quickstart#automatic-confirmation-flow) flow.
*
* For confirmation attempts that require 3DS1 authentication, the following logic will
* be used:
* - Use [Custom Tabs](https://developer.chrome.com/docs/android/custom-tabs/overview/) if they
* are supported on the device.
* - Otherwise, use the device browser.
*
* @param activity the `Activity` that is launching the payment authentication flow
* @param confirmPaymentIntentParams [ConfirmPaymentIntentParams] used to confirm the
* [PaymentIntent]
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
@UiThread
fun confirmPayment(
activity: ComponentActivity,
confirmPaymentIntentParams: ConfirmPaymentIntentParams,
stripeAccountId: String? = this.stripeAccountId
) {
activity.lifecycleScope.launch {
paymentController.startConfirmAndAuth(
AuthActivityStarterHost.create(activity),
confirmPaymentIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
)
)
}
}
/**
* Confirm and authenticate a [PaymentIntent] using the Alipay SDK
* @see <a href="https://intl.alipay.com/docs/ac/app/sdk_integration">Alipay Documentation</a>
*
* @param confirmPaymentIntentParams [ConfirmPaymentIntentParams] used to confirm the
* [PaymentIntent]
* @param authenticator a [AlipayAuthenticator] used to interface with the Alipay SDK
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@JvmOverloads
fun confirmAlipayPayment(
confirmPaymentIntentParams: ConfirmPaymentIntentParams,
authenticator: AlipayAuthenticator,
stripeAccountId: String? = this.stripeAccountId,
callback: ApiResultCallback<PaymentIntentResult>
) {
executeAsyncForResult(callback) {
paymentController.confirmAndAuthenticateAlipay(
confirmPaymentIntentParams,
authenticator,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
)
)
}
}
/**
* Confirm a [PaymentIntent] for WeChat Pay. Extract params from [WeChatPayNextAction] to pass to WeChat Pay SDK.
* @see <a href="https://pay.weixin.qq.com/index.php/public/wechatpay">WeChat Pay Documentation</a>
*
* WeChat Pay API is still in beta, create a [Stripe] instance with [StripeApiBeta.WeChatPayV1] to enable this API.
*
* @param confirmPaymentIntentParams [ConfirmPaymentIntentParams] used to confirm the
* [PaymentIntent]
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the [Stripe] object, if specified.
* @param callback a [ApiResultCallback] to receive the result or error
*
* Possible callback errors:
* [AuthenticationException] failure to properly authenticate yourself (check your key)
* [InvalidRequestException] your request has invalid parameters
* [APIConnectionException] failure to connect to Stripe's API
* [APIException] any other type of problem (for instance, a temporary issue with Stripe's servers)
* [InvalidRequestException] if the payment intent's next action data is not for WeChat Pay
*
* To avoid interacting with WeChat Pay SDK directly, use WeChatPay module.
* @see <a href="https://github.com/stripe/stripe-android/tree/master/wechatpay">WeChatPay module</a>
*/
@JvmOverloads
@Deprecated("Use the WeChat Pay module instead.")
fun confirmWeChatPayPayment(
confirmPaymentIntentParams: ConfirmPaymentIntentParams,
stripeAccountId: String? = this.stripeAccountId,
callback: ApiResultCallback<WeChatPayNextAction>
) {
executeAsyncForResult(callback) {
paymentController.confirmWeChatPay(
confirmPaymentIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
)
)
}
}
/**
* Confirm and, if necessary, authenticate a [PaymentIntent].
* Used for [automatic confirmation](https://stripe.com/docs/payments/payment-intents/quickstart#automatic-confirmation-flow) flow.
*
* @param fragment the `Fragment` that is launching the payment authentication flow
* @param confirmPaymentIntentParams [ConfirmPaymentIntentParams] used to confirm the [PaymentIntent]
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
@UiThread
fun confirmPayment(
fragment: Fragment,
confirmPaymentIntentParams: ConfirmPaymentIntentParams,
stripeAccountId: String? = this.stripeAccountId
) {
fragment.lifecycleScope.launch {
paymentController.startConfirmAndAuth(
AuthActivityStarterHost.create(fragment),
confirmPaymentIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
)
)
}
}
/**
* Handle the [next_action](https://stripe.com/docs/api/payment_intents/object#payment_intent_object-next_action)
* for a previously confirmed [PaymentIntent].
*
* Used for [manual confirmation](https://stripe.com/docs/payments/payment-intents/quickstart#manual-confirmation-flow) flow.
*
* @param activity the `Activity` that is launching the payment authentication flow
* @param clientSecret the [client_secret](https://stripe.com/docs/api/payment_intents/object#payment_intent_object-client_secret)
* property of a confirmed [PaymentIntent] object
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
@UiThread
fun handleNextActionForPayment(
activity: ComponentActivity,
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId
) {
activity.lifecycleScope.launch {
paymentController.startAuth(
AuthActivityStarterHost.create(activity),
PaymentIntent.ClientSecret(clientSecret).value,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
PaymentController.StripeIntentType.PaymentIntent
)
}
}
/**
* Handle the [next_action](https://stripe.com/docs/api/payment_intents/object#payment_intent_object-next_action)
* for a previously confirmed [PaymentIntent].
*
* Used for [manual confirmation](https://stripe.com/docs/payments/accept-a-payment-synchronously) flow.
*
* @param fragment the `Fragment` that is launching the payment authentication flow
* @param clientSecret the [client_secret](https://stripe.com/docs/api/payment_intents/object#payment_intent_object-client_secret)
* property of a confirmed [PaymentIntent] object
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
@UiThread
fun handleNextActionForPayment(
fragment: Fragment,
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId
) {
fragment.lifecycleScope.launch {
paymentController.startAuth(
AuthActivityStarterHost.create(fragment),
PaymentIntent.ClientSecret(clientSecret).value,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
PaymentController.StripeIntentType.PaymentIntent
)
}
}
/**
* Check if the requestCode and [Intent] is for [PaymentIntentResult].
* The [Intent] should be retrieved from the result from `Activity#onActivityResult(int, int, Intent)}}`
* by [Activity] started with [confirmPayment] or [handleNextActionForPayment].
*
* @return whether the requestCode and intent is for [PaymentIntentResult].
*/
fun isPaymentResult(
requestCode: Int,
data: Intent?
): Boolean {
return data != null && paymentController.shouldHandlePaymentResult(requestCode, data)
}
/**
* Should be called via `Activity#onActivityResult(int, int, Intent)}}` to handle the
* result of a PaymentIntent automatic confirmation (see [confirmPayment]) or
* manual confirmation (see [handleNextActionForPayment]})
*/
@UiThread
fun onPaymentResult(
requestCode: Int,
data: Intent?,
callback: ApiResultCallback<PaymentIntentResult>
): Boolean {
return if (data != null && isPaymentResult(requestCode, data)) {
executeAsyncForResult(callback) {
paymentController.getPaymentIntentResult(data)
}
true
} else {
false
}
}
/**
* Retrieve a [PaymentIntent] asynchronously.
*
* See [Retrieve a PaymentIntent](https://stripe.com/docs/api/payment_intents/retrieve).
* `GET /v1/payment_intents/:id`
*
* @param clientSecret the client_secret with which to retrieve the [PaymentIntent]
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param expand Optional, a list of keys to expand on the returned `PaymentIntent` object.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@UiThread
@JvmOverloads
fun retrievePaymentIntent(
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId,
expand: List<String> = emptyList(),
callback: ApiResultCallback<PaymentIntent>
) {
executeAsyncForResult(callback) {
stripeRepository.retrievePaymentIntent(
clientSecret,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
expand,
)
}
}
/**
* Blocking method to retrieve a [PaymentIntent] object.
* Do not call this on the UI thread or your app will crash.
*
* See [Retrieve a PaymentIntent](https://stripe.com/docs/api/payment_intents/retrieve).
* `GET /v1/payment_intents/:id`
*
* @param clientSecret the client_secret with which to retrieve the [PaymentIntent]
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param expand Optional, a list of keys to expand on the returned `PaymentIntent` object.
* @return a [PaymentIntent] or `null` if a problem occurred
*/
@Throws(
APIException::class,
AuthenticationException::class,
InvalidRequestException::class,
APIConnectionException::class
)
@WorkerThread
@JvmOverloads
fun retrievePaymentIntentSynchronous(
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId,
expand: List<String> = emptyList(),
): PaymentIntent {
return runBlocking {
stripeRepository.retrievePaymentIntent(
PaymentIntent.ClientSecret(clientSecret).value,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
expand,
).getOrElse { throw StripeException.create(it) }
}
}
/**
* Blocking method to confirm a [PaymentIntent] object.
* Do not call this on the UI thread or your app will crash.
*
* See [Confirm a PaymentIntent](https://stripe.com/docs/api/payment_intents/confirm).
* `POST /v1/payment_intents/:id/confirm`
*
* @param confirmPaymentIntentParams a set of params with which to confirm the PaymentIntent
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
*
* @return a [PaymentIntent] or `null` if a problem occurred
*/
@Deprecated("use {@link #confirmPayment(Activity, ConfirmPaymentIntentParams)}")
@Throws(
AuthenticationException::class,
InvalidRequestException::class,
APIConnectionException::class,
APIException::class
)
@WorkerThread
@JvmOverloads
fun confirmPaymentIntentSynchronous(
confirmPaymentIntentParams: ConfirmPaymentIntentParams,
idempotencyKey: String? = null
): PaymentIntent {
return runBlocking {
stripeRepository.confirmPaymentIntent(
confirmPaymentIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
).getOrElse { throw StripeException.create(it) }
}
}
//
// Setup Intents API - https://stripe.com/docs/api/setup_intents
//
/**
* Confirm and, if necessary, authenticate a [SetupIntent].
*
* @param activity the `Activity` that is launching the payment authentication flow
* @param confirmSetupIntentParams a set of params with which to confirm the Setup Intent
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
fun confirmSetupIntent(
activity: ComponentActivity,
confirmSetupIntentParams: ConfirmSetupIntentParams,
stripeAccountId: String? = this.stripeAccountId
) {
activity.lifecycleScope.launch {
paymentController.startConfirmAndAuth(
AuthActivityStarterHost.create(activity),
confirmSetupIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
)
)
}
}
/**
* Confirm and, if necessary, authenticate a [SetupIntent].
*
* @param fragment the `Fragment` that is launching the payment authentication flow
* @param confirmSetupIntentParams a set of params with which to confirm the Setup Intent
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@UiThread
@JvmOverloads
fun confirmSetupIntent(
fragment: Fragment,
confirmSetupIntentParams: ConfirmSetupIntentParams,
stripeAccountId: String? = this.stripeAccountId
) {
fragment.lifecycleScope.launch {
paymentController.startConfirmAndAuth(
AuthActivityStarterHost.create(fragment),
confirmSetupIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
)
)
}
}
/**
* Handle [next_action](https://stripe.com/docs/api/setup_intents/object#setup_intent_object-next_action)
* for a previously confirmed [SetupIntent]. Used for manual confirmation flow.
*
* @param activity the `Activity` that is launching the payment authentication flow
* @param clientSecret the [client_secret](https://stripe.com/docs/api/setup_intents/object#setup_intent_object-client_secret)
* property of a confirmed [SetupIntent] object
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@UiThread
@JvmOverloads
fun handleNextActionForSetupIntent(
activity: ComponentActivity,
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId
) {
activity.lifecycleScope.launch {
paymentController.startAuth(
AuthActivityStarterHost.create(activity),
SetupIntent.ClientSecret(clientSecret).value,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
PaymentController.StripeIntentType.SetupIntent
)
}
}
/**
* Handle [next_action](https://stripe.com/docs/api/setup_intents/object#setup_intent_object-next_action)
* for a previously confirmed [SetupIntent]. Used for manual confirmation flow.
*
* @param fragment the `Fragment` launching the payment authentication flow
* @param clientSecret the [client_secret](https://stripe.com/docs/api/setup_intents/object#setup_intent_object-client_secret)
* property of a confirmed [SetupIntent] object
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@UiThread
@JvmOverloads
fun handleNextActionForSetupIntent(
fragment: Fragment,
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId
) {
fragment.lifecycleScope.launch {
paymentController.startAuth(
AuthActivityStarterHost.create(fragment),
SetupIntent.ClientSecret(clientSecret).value,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
PaymentController.StripeIntentType.SetupIntent
)
}
}
/**
* Check if the requestCode and [Intent] is for [SetupIntentResult].
* The [Intent] should be retrieved from the result from `Activity#onActivityResult(int, int, Intent)}}`
* by [Activity] started with [confirmSetupIntent].
*
* @return whether the requestCode and intent is for [SetupIntentResult].
*/
fun isSetupResult(
requestCode: Int,
data: Intent?
): Boolean {
return data != null && paymentController.shouldHandleSetupResult(requestCode, data)
}
/**
* Should be called via `Activity#onActivityResult(int, int, Intent)}}` to handle the
* result of a SetupIntent confirmation (see [confirmSetupIntent]).
*/
@UiThread
fun onSetupResult(
requestCode: Int,
data: Intent?,
callback: ApiResultCallback<SetupIntentResult>
): Boolean {
return if (data != null && isSetupResult(requestCode, data)) {
executeAsyncForResult(callback) {
paymentController.getSetupIntentResult(data)
}
true
} else {
false
}
}
/**
* Retrieve a [SetupIntent] asynchronously.
*
* See [Retrieve a SetupIntent](https://stripe.com/docs/api/setup_intents/retrieve).
* `GET /v1/setup_intents/:id`
*
* @param clientSecret the client_secret with which to retrieve the [SetupIntent]
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param expand Optional, a list of keys to expand on the returned `SetupIntent` object.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@Throws(
APIException::class,
AuthenticationException::class,
InvalidRequestException::class,
APIConnectionException::class
)
@WorkerThread
@JvmOverloads
fun retrieveSetupIntent(
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId,
expand: List<String> = emptyList(),
callback: ApiResultCallback<SetupIntent>
) {
executeAsyncForResult(callback) {
stripeRepository.retrieveSetupIntent(
clientSecret,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
expand,
)
}
}
/**
* Blocking method to retrieve a [SetupIntent] object.
* Do not call this on the UI thread or your app will crash.
*
* See [Retrieve a SetupIntent](https://stripe.com/docs/api/setup_intents/retrieve).
* `GET /v1/setup_intents/:id`
*
* @param clientSecret client_secret of the [SetupIntent] to retrieve
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param expand Optional, a list of keys to expand on the returned `SetupIntent` object.
* @return a [SetupIntent] or `null` if a problem occurred
*/
@Throws(
APIException::class,
AuthenticationException::class,
InvalidRequestException::class,
APIConnectionException::class
)
@WorkerThread
@JvmOverloads
fun retrieveSetupIntentSynchronous(
clientSecret: String,
stripeAccountId: String? = this.stripeAccountId,
expand: List<String> = emptyList(),
): SetupIntent {
return runBlocking {
stripeRepository.retrieveSetupIntent(
SetupIntent.ClientSecret(clientSecret).value,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId
),
expand,
).getOrElse { throw StripeException.create(it) }
}
}
/**
* Blocking method to confirm a [SetupIntent] object.
* Do not call this on the UI thread or your app will crash.
*
* See [Confirm a SetupIntent](https://stripe.com/docs/api/setup_intents/confirm).
* `POST /v1/setup_intents/:id/confirm`
*
* @param confirmSetupIntentParams a set of params with which to confirm the Setup Intent
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
*
* @return a [SetupIntent] or `null` if a problem occurred
*/
@Deprecated("use {@link #confirmSetupIntent(Activity, ConfirmSetupIntentParams)}")
@Throws(
AuthenticationException::class,
InvalidRequestException::class,
APIConnectionException::class,
APIException::class
)
@WorkerThread
@JvmOverloads
fun confirmSetupIntentSynchronous(
confirmSetupIntentParams: ConfirmSetupIntentParams,
idempotencyKey: String? = null
): SetupIntent {
return runBlocking {
stripeRepository.confirmSetupIntent(
confirmSetupIntentParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
).getOrElse { throw StripeException.create(it) }
}
}
//
// Payment Methods API - https://stripe.com/docs/api/payment_methods
//
/**
* Create a [PaymentMethod] asynchronously.
*
* See [Create a PaymentMethod](https://stripe.com/docs/api/payment_methods/create).
* `POST /v1/payment_methods`
*
* @param paymentMethodCreateParams the [PaymentMethodCreateParams] to be used
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@UiThread
@JvmOverloads
fun createPaymentMethod(
paymentMethodCreateParams: PaymentMethodCreateParams,
idempotencyKey: String? = null,
stripeAccountId: String? = this.stripeAccountId,
callback: ApiResultCallback<PaymentMethod>
) {
executeAsyncForResult(callback) {
stripeRepository.createPaymentMethod(
paymentMethodCreateParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
)
}
}
/**
* Update a [PaymentMethod] asynchronously.
*
* See [Update a PaymentMethod](https://stripe.com/docs/api/payment_methods/update).
* `POST /v1/payment_methods/:id`
*
* @param paymentMethodId the ID of the [PaymentMethod] to be updated
* @param paymentMethodUpdateParams the [PaymentMethodUpdateParams] to be used
* @param ephemeralKeySecret the Customer Ephemeral Key secret to be used
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@UiThread
@JvmOverloads
fun updatePaymentMethod(
paymentMethodId: String,
paymentMethodUpdateParams: PaymentMethodUpdateParams,
ephemeralKeySecret: String,
idempotencyKey: String? = null,
stripeAccountId: String? = this.stripeAccountId,
callback: ApiResultCallback<PaymentMethod>
) {
executeAsyncForResult(callback) {
stripeRepository.updatePaymentMethod(
paymentMethodId,
paymentMethodUpdateParams,
ApiRequest.Options(
apiKey = ephemeralKeySecret,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
)
}
}
/**
* Blocking method to create a [PaymentMethod] object.
* Do not call this on the UI thread or your app will crash.
*
* See [Create a PaymentMethod](https://stripe.com/docs/api/payment_methods/create).
* `POST /v1/payment_methods`
*
* @param paymentMethodCreateParams params with which to create the PaymentMethod
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*
* @return a [PaymentMethod] or `null` if a problem occurred
*/
@Throws(
APIException::class,
AuthenticationException::class,
InvalidRequestException::class,
APIConnectionException::class
)
@WorkerThread
@JvmOverloads
fun createPaymentMethodSynchronous(
paymentMethodCreateParams: PaymentMethodCreateParams,
idempotencyKey: String? = null,
stripeAccountId: String? = this.stripeAccountId
): PaymentMethod {
return runBlocking {
stripeRepository.createPaymentMethod(
paymentMethodCreateParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
).getOrElse { throw StripeException.create(it) }
}
}
//
// Sources API - https://stripe.com/docs/api/sources
//
/**
* Authenticate a [Source] that requires user action via a redirect (i.e. [Source.flow] is
* [Source.Flow.Redirect].
*
* The result of this operation will be returned via `Activity#onActivityResult(int, int, Intent)}}`
*
* @param activity the `Activity` that is launching the [Source] authentication flow
* @param source the [Source] to confirm
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
fun authenticateSource(
activity: ComponentActivity,
source: Source,
stripeAccountId: String? = this.stripeAccountId
) {
activity.lifecycleScope.launch {
paymentController.startAuthenticateSource(
AuthActivityStarterHost.create(activity),
source,
ApiRequest.Options(publishableKey, stripeAccountId)
)
}
}
/**
* Authenticate a [Source] that requires user action via a redirect (i.e. [Source.flow] is
* [Source.Flow.Redirect].
*
* The result of this operation will be returned via `Activity#onActivityResult(int, int, Intent)}}`
*
* @param fragment the `Fragment` that is launching the [Source] authentication flow
* @param source the [Source] to confirm
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*/
@JvmOverloads
fun authenticateSource(
fragment: Fragment,
source: Source,
stripeAccountId: String? = this.stripeAccountId
) {
fragment.lifecycleScope.launchWhenCreated {
paymentController.startAuthenticateSource(
AuthActivityStarterHost.create(fragment),
source,
ApiRequest.Options(publishableKey, stripeAccountId)
)
}
}
/**
* Check if the requestCode and [Intent] is for [Source] authentication.
* The [Intent] should be retrieved from the result from `Activity#onActivityResult(int, int, Intent)}}`
* by [Activity] started with [authenticateSource].
*
* @return whether the requestCode and intent is for [Source] authentication
*/
fun isAuthenticateSourceResult(
requestCode: Int,
data: Intent?
): Boolean {
return data != null && paymentController.shouldHandleSourceResult(requestCode, data)
}
/**
* The result of a call to [authenticateSource].
*
* Use [isAuthenticateSourceResult] before calling this method.
*/
fun onAuthenticateSourceResult(
data: Intent,
callback: ApiResultCallback<Source>
) {
executeAsyncForResult(callback) {
paymentController.getAuthenticateSourceResult(data)
}
}
/**
* Create a [Source] asynchronously.
*
* See [Create a source](https://stripe.com/docs/api/sources/create).
* `POST /v1/sources`
*
* @param sourceParams the [SourceParams] to be used
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
* @param callback a [ApiResultCallback] to receive the result or error
*/
@UiThread
@JvmOverloads
fun createSource(
sourceParams: SourceParams,
idempotencyKey: String? = null,
stripeAccountId: String? = this.stripeAccountId,
callback: ApiResultCallback<Source>
) {
executeAsyncForResult(callback) {
stripeRepository.createSource(
sourceParams,
ApiRequest.Options(
apiKey = publishableKey,
stripeAccount = stripeAccountId,
idempotencyKey = idempotencyKey
)
)
}
}
/**
* Blocking method to create a [Source] object.
* Do not call this on the UI thread or your app will crash.
*
* See [Create a source](https://stripe.com/docs/api/sources/create).
* `POST /v1/sources`
*
* @param params a set of [SourceParams] with which to create the source
* @param idempotencyKey optional, see [Idempotent Requests](https://stripe.com/docs/api/idempotent_requests)
* @param stripeAccountId Optional, the Connect account to associate with this request.
* By default, will use the Connect account that was used to instantiate the `Stripe` object, if specified.
*
* @return a [Source], or `null` if a problem occurred
*
* @throws AuthenticationException failure to properly authenticate yourself (check your key)
* @throws InvalidRequestException your request has invalid parameters
* @throws APIConnectionException failure to connect to Stripe's API
* @throws APIException any other type of problem (for instance, a temporary issue with
* Stripe's servers