forked from tipsi/tipsi-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStripeModule.java
765 lines (652 loc) · 27.1 KB
/
StripeModule.java
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
package com.gettipsi.stripe;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.gettipsi.stripe.dialog.AddCardDialogFragment;
import com.gettipsi.stripe.util.ArgCheck;
import com.gettipsi.stripe.util.Converters;
import com.gettipsi.stripe.util.Fun0;
import com.google.android.gms.wallet.WalletConstants;
import com.stripe.android.ApiResultCallback;
import com.stripe.android.AppInfo;
import com.stripe.android.PaymentIntentResult;
import com.stripe.android.SetupIntentResult;
import com.stripe.android.SourceCallback;
import com.stripe.android.Stripe;
import com.stripe.android.TokenCallback;
import com.stripe.android.model.Address;
import com.stripe.android.model.ConfirmPaymentIntentParams;
import com.stripe.android.model.ConfirmSetupIntentParams;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.PaymentMethodCreateParams;
import com.stripe.android.model.Source;
import com.stripe.android.model.Source.SourceStatus;
import com.stripe.android.model.SourceParams;
import com.stripe.android.model.StripeIntent;
import com.stripe.android.model.Token;
import java.util.HashMap;
import java.util.Map;
import static com.gettipsi.stripe.Errors.AUTHENTICATION_FAILED;
import static com.gettipsi.stripe.Errors.CANCELLED;
import static com.gettipsi.stripe.Errors.FAILED;
import static com.gettipsi.stripe.Errors.UNEXPECTED;
import static com.gettipsi.stripe.Errors.getDescription;
import static com.gettipsi.stripe.Errors.getErrorCode;
import static com.gettipsi.stripe.Errors.toErrorCode;
import static com.gettipsi.stripe.util.Converters.convertPaymentIntentResultToWritableMap;
import static com.gettipsi.stripe.util.Converters.convertPaymentMethodToWritableMap;
import static com.gettipsi.stripe.util.Converters.convertSetupIntentResultToWritableMap;
import static com.gettipsi.stripe.util.Converters.convertSourceToWritableMap;
import static com.gettipsi.stripe.util.Converters.convertTokenToWritableMap;
import static com.gettipsi.stripe.util.Converters.createBankAccount;
import static com.gettipsi.stripe.util.Converters.createCard;
import static com.gettipsi.stripe.util.Converters.getBooleanOrNull;
import static com.gettipsi.stripe.util.Converters.getMapOrNull;
import static com.gettipsi.stripe.util.Converters.getStringOrNull;
import static com.gettipsi.stripe.util.InitializationOptions.ANDROID_PAY_MODE_KEY;
import static com.gettipsi.stripe.util.InitializationOptions.ANDROID_PAY_MODE_PRODUCTION;
import static com.gettipsi.stripe.util.InitializationOptions.ANDROID_PAY_MODE_TEST;
import static com.gettipsi.stripe.util.InitializationOptions.PUBLISHABLE_KEY;
import static com.stripe.android.model.StripeIntent.Status.Canceled;
import static com.stripe.android.model.StripeIntent.Status.RequiresAction;
import static com.stripe.android.model.StripeIntent.Status.RequiresCapture;
import static com.stripe.android.model.StripeIntent.Status.RequiresConfirmation;
import static com.stripe.android.model.StripeIntent.Status.Succeeded;
public class StripeModule extends ReactContextBaseJavaModule {
private static final String MODULE_NAME = StripeModule.class.getSimpleName();
// If you change these, make sure to also change:
// ios/TPSStripe/TPSStripeManager
// Relevant Docs:
// - https://stripe.dev/stripe-ios/docs/Classes/STPAppInfo.html https://stripe.dev/stripe-android/com/stripe/android/AppInfo.html
// - https://stripe.com/docs/building-plugins#setappinfo
private static final String APP_INFO_NAME = "tipsi-stripe";
private static final String APP_INFO_URL = "https://github.com/tipsi/tipsi-stripe";
private static final String APP_INFO_VERSION = "8.x";
public static final String CLIENT_SECRET = "clientSecret";
private static StripeModule sInstance = null;
public static StripeModule getInstance() {
return sInstance;
}
public Stripe getStripe() {
return mStripe;
}
@Nullable
private Promise mCreateSourcePromise;
@Nullable
private Source mCreatedSource;
private String mPublicKey;
private Stripe mStripe;
private PayFlow mPayFlow;
private ReadableMap mErrorCodes;
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
boolean handled = getPayFlow().onActivityResult(activity, requestCode, resultCode, data);
if (!handled) {
super.onActivityResult(activity, requestCode, resultCode, data);
}
}
};
public StripeModule(ReactApplicationContext reactContext) {
super(reactContext);
// Add the listener for `onActivityResult`
reactContext.addActivityEventListener(mActivityEventListener);
sInstance = this;
}
@Override
public String getName() {
return MODULE_NAME;
}
@ReactMethod
public void init(@NonNull ReadableMap options, @NonNull ReadableMap errorCodes) {
ArgCheck.nonNull(options);
String newPubKey = Converters.getStringOrNull(options, PUBLISHABLE_KEY);
String newAndroidPayMode = Converters.getStringOrNull(options, ANDROID_PAY_MODE_KEY);
if (newPubKey != null && !TextUtils.equals(newPubKey, mPublicKey)) {
ArgCheck.notEmptyString(newPubKey);
mPublicKey = newPubKey;
Stripe.setAppInfo(AppInfo.create(APP_INFO_NAME, APP_INFO_VERSION, APP_INFO_URL));
mStripe = new Stripe(getReactApplicationContext(), mPublicKey);
getPayFlow().setPublishableKey(mPublicKey);
}
if (newAndroidPayMode != null) {
ArgCheck.isTrue(ANDROID_PAY_MODE_TEST.equals(newAndroidPayMode) || ANDROID_PAY_MODE_PRODUCTION.equals(newAndroidPayMode));
getPayFlow().setEnvironment(androidPayModeToEnvironment(newAndroidPayMode));
}
if (mErrorCodes == null) {
mErrorCodes = errorCodes;
getPayFlow().setErrorCodes(errorCodes);
}
}
private PayFlow getPayFlow() {
if (mPayFlow == null) {
mPayFlow = PayFlow.create(
new Fun0<Activity>() { public Activity call() {
return getCurrentActivity();
}}
);
}
return mPayFlow;
}
private static int androidPayModeToEnvironment(@NonNull String androidPayMode) {
ArgCheck.notEmptyString(androidPayMode);
return ANDROID_PAY_MODE_TEST.equals(androidPayMode.toLowerCase()) ? WalletConstants.ENVIRONMENT_TEST : WalletConstants.ENVIRONMENT_PRODUCTION;
}
@ReactMethod
public void deviceSupportsAndroidPay(final Promise promise) {
getPayFlow().deviceSupportsAndroidPay(false, promise);
}
@ReactMethod
public void canMakeAndroidPayPayments(final Promise promise) {
getPayFlow().deviceSupportsAndroidPay(true, promise);
}
@ReactMethod
public void setStripeAccount(final String stripeAccount) {
ArgCheck.notEmptyString(mPublicKey);
if (stripeAccount == null) {
mStripe = new Stripe(getReactApplicationContext(), mPublicKey);
} else {
mStripe = new Stripe(getReactApplicationContext(), mPublicKey, stripeAccount);
}
}
@ReactMethod
public void createTokenWithCard(final ReadableMap cardData, final Promise promise) {
try {
ArgCheck.nonNull(mStripe);
ArgCheck.notEmptyString(mPublicKey);
mStripe.createToken(
createCard(cardData),
mPublicKey,
new TokenCallback() {
public void onSuccess(Token token) {
promise.resolve(convertTokenToWritableMap(token));
}
public void onError(Exception error) {
error.printStackTrace();
promise.reject(toErrorCode(error), error.getMessage());
}
});
} catch (Exception e) {
promise.reject(toErrorCode(e), e.getMessage());
}
}
@ReactMethod
public void createTokenWithBankAccount(final ReadableMap accountData, final Promise promise) {
try {
ArgCheck.nonNull(mStripe);
ArgCheck.notEmptyString(mPublicKey);
mStripe.createBankAccountToken(
createBankAccount(accountData),
mPublicKey,
null,
new TokenCallback() {
public void onSuccess(Token token) {
promise.resolve(convertTokenToWritableMap(token));
}
public void onError(Exception error) {
error.printStackTrace();
promise.reject(toErrorCode(error), error.getMessage());
}
});
} catch (Exception e) {
promise.reject(toErrorCode(e), e.getMessage());
}
}
@ReactMethod
public void paymentRequestWithCardForm(ReadableMap params, final Promise promise) {
Activity currentActivity = getCurrentActivity();
try {
ArgCheck.nonNull(currentActivity);
ArgCheck.notEmptyString(mPublicKey);
final AddCardDialogFragment cardDialog = AddCardDialogFragment.newInstance(
getErrorCode(mErrorCodes, "cancelled"),
getDescription(mErrorCodes, "cancelled")
);
cardDialog.setPromise(promise);
cardDialog.show(currentActivity.getFragmentManager(), "AddNewCard");
} catch (Exception e) {
promise.reject(toErrorCode(e), e.getMessage());
}
}
@ReactMethod
public void paymentRequestWithAndroidPay(final ReadableMap payParams, final Promise promise) {
getPayFlow().paymentRequestWithAndroidPay(payParams, promise);
}
private void attachPaymentResultActivityListener(final Promise promise) {
ActivityEventListener ael = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity a, int requestCode, int resultCode, Intent data) {
final ActivityEventListener ael = this;
mStripe.onPaymentResult(requestCode, data, new ApiResultCallback<PaymentIntentResult>() {
@Override
public void onSuccess(@NonNull PaymentIntentResult result) {
getReactApplicationContext().removeActivityEventListener(ael);
StripeIntent.Status resultingStatus = result.getIntent().getStatus();
if (Succeeded.equals(resultingStatus) ||
RequiresCapture.equals(resultingStatus) ||
RequiresConfirmation.equals(resultingStatus)) {
promise.resolve(convertPaymentIntentResultToWritableMap(result));
} else {
if (Canceled.equals(resultingStatus) ||
RequiresAction.equals(resultingStatus)
) {
promise.reject(CANCELLED, CANCELLED); // TODO - normalize the message
} else {
promise.reject(FAILED, FAILED);
}
}
}
@Override
public void onError(@NonNull Exception e) {
getReactApplicationContext().removeActivityEventListener(ael);
e.printStackTrace();
promise.reject(toErrorCode(e), e.getMessage());
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
onActivityResult(null, requestCode, resultCode, data);
}
};
getReactApplicationContext().addActivityEventListener(ael);
}
private void attachSetupResultActivityListener(final Promise promise) {
ActivityEventListener ael = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity a, int requestCode, int resultCode, Intent data) {
final ActivityEventListener ael = this;
mStripe.onSetupResult(requestCode, data, new ApiResultCallback<SetupIntentResult>() {
@Override
public void onSuccess(@NonNull SetupIntentResult result) {
getReactApplicationContext().removeActivityEventListener(ael);
try {
switch (result.getIntent().getStatus()) {
case Canceled:
// The Setup Intent was canceled, so reject the promise with a predefined code.
promise.reject(CANCELLED, "The SetupIntent was canceled by the user.");
break;
case RequiresAction:
case RequiresPaymentMethod:
promise.reject(AUTHENTICATION_FAILED, "The user failed authentication.");
break;
case Succeeded:
promise.resolve(convertSetupIntentResultToWritableMap(result));
break;
case RequiresCapture:
case RequiresConfirmation:
default:
promise.reject(UNEXPECTED, "Unexpected state");
}
} catch (Exception e) {
promise.reject(UNEXPECTED, "Unexpected error");
}
}
@Override
public void onError(@NonNull Exception e) {
getReactApplicationContext().removeActivityEventListener(ael);
e.printStackTrace();
promise.reject(toErrorCode(e), e.getMessage());
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
onActivityResult(null, requestCode, resultCode, data);
}
};
getReactApplicationContext().addActivityEventListener(ael);
}
@ReactMethod
public void confirmPaymentIntent(final ReadableMap options, final Promise promise) {
attachPaymentResultActivityListener(promise);
Activity activity = getCurrentActivity();
if (activity != null) {
mStripe.confirmPayment(activity, extractConfirmPaymentIntentParams(options));
}
}
@ReactMethod
public void authenticatePaymentIntent(final ReadableMap options, final Promise promise) {
attachPaymentResultActivityListener(promise);
String clientSecret = options.getString(CLIENT_SECRET);
Activity activity = getCurrentActivity();
if (activity != null) {
mStripe.authenticatePayment(activity, clientSecret);
}
}
@ReactMethod
public void confirmSetupIntent(final ReadableMap options, final Promise promise) {
attachSetupResultActivityListener(promise);
Activity activity = getCurrentActivity();
if (activity != null) {
mStripe.confirmSetupIntent(activity, extractConfirmSetupIntentParams(options));
}
}
@ReactMethod
public void authenticateSetupIntent(final ReadableMap options, final Promise promise) {
attachSetupResultActivityListener(promise);
String clientSecret = options.getString(CLIENT_SECRET);
Activity activity = getCurrentActivity();
if (activity != null) {
mStripe.authenticateSetup(activity, clientSecret);
}
}
@ReactMethod
public void createPaymentMethod(final ReadableMap options, final Promise promise) {
PaymentMethodCreateParams pmcp = extractPaymentMethodCreateParams(options);
mStripe.createPaymentMethod(pmcp, new ApiResultCallback<PaymentMethod>() {
@Override
public void onError(Exception error) {
promise.reject(toErrorCode(error), error.getMessage());
}
@Override
public void onSuccess(PaymentMethod paymentMethod) {
promise.resolve(convertPaymentMethodToWritableMap(paymentMethod));
}
});
}
@ReactMethod
public void createSourceWithParams(final ReadableMap options, final Promise promise) {
SourceParams sourceParams = extractSourceParams(options);
ArgCheck.nonNull(sourceParams);
mStripe.createSource(sourceParams, new SourceCallback() {
@Override
public void onError(Exception error) {
promise.reject(toErrorCode(error));
}
@Override
public void onSuccess(Source source) {
if (Source.SourceFlow.REDIRECT.equals(source.getFlow())) {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
promise.reject(
getErrorCode(mErrorCodes, "activityUnavailable"),
getDescription(mErrorCodes, "activityUnavailable")
);
} else {
mCreateSourcePromise = promise;
mCreatedSource = source;
String redirectUrl = source.getRedirect().getUrl();
Intent browserIntent = new Intent(currentActivity, OpenBrowserActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
.putExtra(OpenBrowserActivity.EXTRA_URL, redirectUrl);
currentActivity.startActivity(browserIntent);
}
} else {
promise.resolve(convertSourceToWritableMap(source));
}
}
});
}
private ConfirmSetupIntentParams extractConfirmSetupIntentParams(final ReadableMap options) {
ReadableMap paymentMethod = getMapOrNull(options, "paymentMethod");
String paymentMethodId = getStringOrNull(options, "paymentMethodId");
String returnURL = getStringOrNull(options, "returnURL");
String clientSecret = options.getString("clientSecret");
ConfirmSetupIntentParams csip = null;
if (returnURL == null) {
returnURL = "stripejs://use_stripe_sdk/return_url";
}
if (paymentMethod != null) {
csip = ConfirmSetupIntentParams.create(extractPaymentMethodCreateParams(paymentMethod),
clientSecret, returnURL);
} else if (paymentMethodId != null) {
csip = ConfirmSetupIntentParams.create(paymentMethodId, clientSecret, returnURL);
}
ArgCheck.nonNull(csip);
csip.withShouldUseStripeSdk(true);
return csip;
}
private ConfirmPaymentIntentParams extractConfirmPaymentIntentParams(final ReadableMap options) {
ConfirmPaymentIntentParams cpip = null;
String clientSecret = options.getString("clientSecret");
ReadableMap paymentMethod = getMapOrNull(options, "paymentMethod");
String paymentMethodId = getStringOrNull(options,"paymentMethodId");
// ReadableMap source = options.getMap("source");
// String sourceId = getStringOrNull(options,"sourceId");
String returnURL = getStringOrNull(options, "returnURL");
if (returnURL == null) {
returnURL = "stripejs://use_stripe_sdk/return_url";
}
boolean savePaymentMethod = getBooleanOrNull(options, "savePaymentMethod", false);
// TODO support extra params in each of the create methods below
Map<String, Object> extraParams = null;
// Create with Payment Method
if (paymentMethod != null) {
PaymentMethodCreateParams pmcp = extractPaymentMethodCreateParams(paymentMethod);
cpip = ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(pmcp, clientSecret, returnURL, savePaymentMethod, extraParams);
// Create with Payment Method ID
} else if (paymentMethodId != null) {
cpip = ConfirmPaymentIntentParams.createWithPaymentMethodId(paymentMethodId, clientSecret, returnURL, savePaymentMethod, extraParams);
// Create with Source
/**
Support for creating a Source while confirming a PaymentIntent is not being included
at this time, however, for compatibility with existing saved Sources, you can still confirm
a payment intent using a pre-existing Source by specifying its 'sourceId', as shown in the next
branch
*/
/*
} else if (source != null) {
SourceParams sourceParams = extractSourceParams(source);
cpip = ConfirmPaymentIntentParams.createWithSourceParams(sourceParams, clientSecret, returnURL, savePaymentMethod, extraParams);
*/
// Create with Source ID
/**
If you have a sourceId, pass it into the paymentMethodId parameter instead!
The payment_method parameter of a payment intent is fully compatible with Sources.
Reference: https://stripe.com/docs/api/payment_intents/confirm#confirm_payment_intent-payment_method
*/
/*
} else if (sourceId != null) {
cpip = ConfirmPaymentIntentParams.createWithSourceId(sourceId, clientSecret, returnURL, savePaymentMethod, extraParams);
*/
/**
This branch can be used if the client secret refers to a payment intent that already
has payment method information and just needs to be confirmed.
*/
} else {
cpip = ConfirmPaymentIntentParams.create(clientSecret, returnURL);
}
cpip.withShouldUseStripeSdk(true);
return cpip;
}
private PaymentMethodCreateParams extractPaymentMethodCreateParams(final ReadableMap options) {
ReadableMap cardParams = getMapOrNull(options, "card");
ReadableMap billingDetailsParams = getMapOrNull(options, "billingDetails");
ReadableMap metadataParams = getMapOrNull(options, "metadata");
PaymentMethodCreateParams.Card card = null;
PaymentMethod.BillingDetails billingDetails = null;
Address address = null;
Map<String, String> metadata = new HashMap<>();
if (metadataParams != null) {
ReadableMapKeySetIterator iter = metadataParams.keySetIterator();
while (iter.hasNextKey()) {
String key = iter.nextKey();
metadata.put(key, metadataParams.getString(key));
}
}
if (billingDetailsParams != null) {
ReadableMap addressParams = getMapOrNull(options, "address");
if (addressParams != null) {
address = new Address.Builder().
setCity(getStringOrNull(addressParams, "city")).
setCountry(addressParams.getString("country")).
setLine1(getStringOrNull(addressParams, "line1")).
setLine2(getStringOrNull(addressParams, "line2")).
setPostalCode(getStringOrNull(addressParams, "postalCode")).
setState(getStringOrNull(addressParams, "state")).
build();
}
billingDetails = new PaymentMethod.BillingDetails.Builder().
setAddress(address).
setEmail(getStringOrNull(billingDetailsParams, "email")).
setName(getStringOrNull(billingDetailsParams,"name")).
setPhone(getStringOrNull(billingDetailsParams,"phone")).
build();
}
if (cardParams != null) {
String token = getStringOrNull(cardParams, "token");
if (token != null) {
card = PaymentMethodCreateParams.Card.create(token);
} else {
card = new PaymentMethodCreateParams.Card.Builder().
setCvc(cardParams.getString("cvc")).
setExpiryMonth(cardParams.getInt("expMonth")).
setExpiryYear(cardParams.getInt("expYear")).
setNumber(cardParams.getString("number")).
build();
}
}
return PaymentMethodCreateParams.create(
card,
billingDetails,
metadata
);
}
private SourceParams extractSourceParams(final ReadableMap options) {
String sourceType = options.getString("type");
SourceParams sourceParams = null;
switch (sourceType) {
case "alipay":
sourceParams = SourceParams.createAlipaySingleUseParams(
options.getInt("amount"),
options.getString("currency"),
getStringOrNull(options, "name"),
getStringOrNull(options, "email"),
options.getString("returnURL"));
break;
case "bancontact":
sourceParams = SourceParams.createBancontactParams(
options.getInt("amount"),
options.getString("name"),
options.getString("returnURL"),
getStringOrNull(options, "statementDescriptor"),
options.getString("preferredLanguage"));
break;
case "giropay":
sourceParams = SourceParams.createGiropayParams(
options.getInt("amount"),
options.getString("name"),
options.getString("returnURL"),
getStringOrNull(options, "statementDescriptor"));
break;
case "ideal":
sourceParams = SourceParams.createIdealParams(
options.getInt("amount"),
options.getString("name"),
options.getString("returnURL"),
getStringOrNull(options, "statementDescriptor"),
getStringOrNull(options, "bank"));
break;
case "sepaDebit":
sourceParams = SourceParams.createSepaDebitParams(
options.getString("name"),
options.getString("iban"),
getStringOrNull(options, "addressLine1"),
options.getString("city"),
options.getString("postalCode"),
options.getString("country"));
break;
case "sofort":
sourceParams = SourceParams.createSofortParams(
options.getInt("amount"),
options.getString("returnURL"),
options.getString("country"),
getStringOrNull(options, "statementDescriptor"));
break;
case "threeDSecure":
sourceParams = SourceParams.createThreeDSecureParams(
options.getInt("amount"),
options.getString("currency"),
options.getString("returnURL"),
options.getString("card"));
break;
case "card":
sourceParams = SourceParams.createCardParams(Converters.createCard(options));
break;
}
return sourceParams;
}
void processRedirect(@Nullable Uri redirectData) {
if (mCreatedSource == null || mCreateSourcePromise == null) {
return;
}
if (redirectData == null) {
mCreateSourcePromise.reject(
getErrorCode(mErrorCodes, "redirectCancelled"),
getDescription(mErrorCodes, "redirectCancelled")
);
mCreatedSource = null;
mCreateSourcePromise = null;
return;
}
final String clientSecret = redirectData.getQueryParameter("client_secret");
if (!mCreatedSource.getClientSecret().equals(clientSecret)) {
mCreateSourcePromise.reject(
getErrorCode(mErrorCodes, "redirectNoSource"),
getDescription(mErrorCodes, "redirectNoSource")
);
mCreatedSource = null;
mCreateSourcePromise = null;
return;
}
final String sourceId = redirectData.getQueryParameter("source");
if (!mCreatedSource.getId().equals(sourceId)) {
mCreateSourcePromise.reject(
getErrorCode(mErrorCodes, "redirectWrongSourceId"),
getDescription(mErrorCodes, "redirectWrongSourceId")
);
mCreatedSource = null;
mCreateSourcePromise = null;
return;
}
final Promise promise = mCreateSourcePromise;
// Nulls those properties to avoid processing them twice
mCreatedSource = null;
mCreateSourcePromise = null;
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Source source = null;
try {
source = mStripe.retrieveSourceSynchronous(sourceId, clientSecret);
} catch (Exception e) {
return null;
}
switch (source.getStatus()) {
case SourceStatus.CHARGEABLE:
case SourceStatus.CONSUMED:
promise.resolve(convertSourceToWritableMap(source));
break;
case SourceStatus.CANCELED:
promise.reject(
getErrorCode(mErrorCodes, "redirectCancelled"),
getDescription(mErrorCodes, "redirectCancelled")
);
break;
case SourceStatus.PENDING:
case SourceStatus.FAILED:
default:
promise.reject(
getErrorCode(mErrorCodes, "redirectFailed"),
getDescription(mErrorCodes, "redirectFailed")
);
}
return null;
}
}.execute();
}
}