-
Notifications
You must be signed in to change notification settings - Fork 943
/
stripe.duck.js
732 lines (641 loc) · 22.5 KB
/
stripe.duck.js
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
import config from '../config';
import { storableError } from '../util/errors';
import * as log from '../util/log';
// ================ Action types ================ //
export const STRIPE_ACCOUNT_CREATE_REQUEST = 'app/stripe/STRIPE_ACCOUNT_CREATE_REQUEST';
export const STRIPE_ACCOUNT_CREATE_SUCCESS = 'app/stripe/STRIPE_ACCOUNT_CREATE_SUCCESS';
export const STRIPE_ACCOUNT_CREATE_ERROR = 'app/stripe/STRIPE_ACCOUNT_CREATE_ERROR';
export const STRIPE_ACCOUNT_CLEAR_ERROR = 'app/stripe/STRIPE_ACCOUNT_CLEAR_ERROR';
export const ACCOUNT_OPENER_CREATE_REQUEST = 'app/stripe/ACCOUNT_OPENER_CREATE_REQUEST';
export const ACCOUNT_OPENER_CREATE_SUCCESS = 'app/stripe/ACCOUNT_OPENER_CREATE_SUCCESS';
export const ACCOUNT_OPENER_CREATE_ERROR = 'app/stripe/ACCOUNT_OPENER_CREATE_ERROR';
export const PERSON_CREATE_REQUEST = 'app/stripe/PERSON_CREATE_REQUEST';
export const PERSON_CREATE_SUCCESS = 'app/stripe/PERSON_CREATE_SUCCESS';
export const PERSON_CREATE_ERROR = 'app/stripe/PERSON_CREATE_ERROR';
export const CLEAR_PAYMENT_TOKEN = 'app/stripe/CLEAR_PAYMENT_TOKEN';
export const HANDLE_CARD_PAYMENT_REQUEST = 'app/stripe/HANDLE_CARD_PAYMENT_REQUEST';
export const HANDLE_CARD_PAYMENT_SUCCESS = 'app/stripe/HANDLE_CARD_PAYMENT_SUCCESS';
export const HANDLE_CARD_PAYMENT_ERROR = 'app/stripe/HANDLE_CARD_PAYMENT_ERROR';
export const HANDLE_CARD_SETUP_REQUEST = 'app/stripe/HANDLE_CARD_SETUP_REQUEST';
export const HANDLE_CARD_SETUP_SUCCESS = 'app/stripe/HANDLE_CARD_SETUP_SUCCESS';
export const HANDLE_CARD_SETUP_ERROR = 'app/stripe/HANDLE_CARD_SETUP_ERROR';
export const CLEAR_HANDLE_CARD_PAYMENT = 'app/stripe/CLEAR_HANDLE_CARD_PAYMENT';
export const RETRIEVE_PAYMENT_INTENT_REQUEST = 'app/stripe/RETRIEVE_PAYMENT_INTENT_REQUEST';
export const RETRIEVE_PAYMENT_INTENT_SUCCESS = 'app/stripe/RETRIEVE_PAYMENT_INTENT_SUCCESS';
export const RETRIEVE_PAYMENT_INTENT_ERROR = 'app/stripe/RETRIEVE_PAYMENT_INTENT_ERROR';
// ================ Reducer ================ //
const initialState = {
createStripeAccountInProgress: false,
createStripeAccountError: null,
createAccountOpenerInProgress: false,
createAccountOpenerError: false,
personAccountOpener: null,
persons: [],
stripeAccount: null,
stripeAccountFetched: false,
handleCardPaymentInProgress: false,
handleCardPaymentError: null,
handleCardSetupInProgress: false,
handleCardSetupError: null,
paymentIntent: null,
setupIntent: null,
retrievePaymentIntentInProgress: false,
retrievePaymentIntentError: null,
};
export default function reducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case STRIPE_ACCOUNT_CREATE_REQUEST:
return { ...state, createStripeAccountError: null, createStripeAccountInProgress: true };
case STRIPE_ACCOUNT_CREATE_SUCCESS:
return {
...state,
createStripeAccountInProgress: false,
stripeAccount: payload,
stripeAccountFetched: true,
};
case STRIPE_ACCOUNT_CREATE_ERROR:
console.error(payload);
return { ...state, createStripeAccountError: payload, createStripeAccountInProgress: false };
case STRIPE_ACCOUNT_CLEAR_ERROR:
return { ...initialState };
case ACCOUNT_OPENER_CREATE_REQUEST:
return {
...state,
createAccountOpenerError: null,
createAccountOpenerInProgress: true,
};
case ACCOUNT_OPENER_CREATE_SUCCESS:
return { ...state, createAccountOpenerInProgress: false, personAccountOpener: payload };
case ACCOUNT_OPENER_CREATE_ERROR:
console.error(payload);
return { ...state, createAccountOpenerError: payload, createAccountOpenerInProgress: false };
case PERSON_CREATE_REQUEST:
return {
...state,
persons: [
...state.persons,
{
...payload,
createStripePersonError: null,
createStripePersonInProgress: true,
},
],
};
case PERSON_CREATE_SUCCESS:
return {
...state,
persons: state.persons.map(p => {
return p.personToken === payload.personToken
? { ...payload, createStripePersonInProgress: false }
: p;
}),
};
case PERSON_CREATE_ERROR:
console.error(payload);
return {
...state,
persons: state.persons.map(p => {
return p.personToken === payload.personToken
? { ...p, createStripePersonInProgress: false, createStripePersonError: payload.error }
: p;
}),
};
case HANDLE_CARD_PAYMENT_REQUEST:
return {
...state,
handleCardPaymentError: null,
handleCardPaymentInProgress: true,
};
case HANDLE_CARD_PAYMENT_SUCCESS:
return { ...state, paymentIntent: payload, handleCardPaymentInProgress: false };
case HANDLE_CARD_PAYMENT_ERROR:
console.error(payload);
return { ...state, handleCardPaymentError: payload, handleCardPaymentInProgress: false };
case HANDLE_CARD_SETUP_REQUEST:
return {
...state,
handleCardSetupError: null,
handleCardSetupInProgress: true,
};
case HANDLE_CARD_SETUP_SUCCESS:
return { ...state, setupIntent: payload, handleCardSetupInProgress: false };
case HANDLE_CARD_SETUP_ERROR:
console.error(payload);
return { ...state, handleCardSetupError: payload, handleCardSetupInProgress: false };
case CLEAR_HANDLE_CARD_PAYMENT:
return {
...state,
handleCardPaymentInProgress: false,
handleCardPaymentError: null,
paymentIntent: null,
};
case RETRIEVE_PAYMENT_INTENT_REQUEST:
return {
...state,
retrievePaymentIntentError: null,
retrievePaymentIntentInProgress: true,
};
case RETRIEVE_PAYMENT_INTENT_SUCCESS:
return { ...state, paymentIntent: payload, retrievePaymentIntentInProgress: false };
case RETRIEVE_PAYMENT_INTENT_ERROR:
console.error(payload);
return {
...state,
retrievePaymentIntentError: payload,
retrievePaymentIntentInProgress: false,
};
default:
return state;
}
}
// ================ Action creators ================ //
export const stripeAccountCreateRequest = () => ({ type: STRIPE_ACCOUNT_CREATE_REQUEST });
export const stripeAccountCreateSuccess = stripeAccount => ({
type: STRIPE_ACCOUNT_CREATE_SUCCESS,
payload: stripeAccount,
});
export const stripeAccountCreateError = e => ({
type: STRIPE_ACCOUNT_CREATE_ERROR,
payload: e,
error: true,
});
export const stripeAccountClearError = () => ({
type: STRIPE_ACCOUNT_CLEAR_ERROR,
});
export const accountOpenerCreateRequest = personToken => ({
type: ACCOUNT_OPENER_CREATE_REQUEST,
payload: personToken,
});
export const accountOpenerCreateSuccess = payload => ({
type: ACCOUNT_OPENER_CREATE_SUCCESS,
payload,
});
export const accountOpenerCreateError = payload => ({
type: ACCOUNT_OPENER_CREATE_ERROR,
payload,
error: true,
});
export const personCreateRequest = personToken => ({
type: PERSON_CREATE_REQUEST,
payload: personToken,
});
export const personCreateSuccess = payload => ({
type: PERSON_CREATE_SUCCESS,
payload,
});
export const personCreateError = payload => ({
type: PERSON_CREATE_ERROR,
payload,
error: true,
});
export const handleCardPaymentRequest = () => ({
type: HANDLE_CARD_PAYMENT_REQUEST,
});
export const handleCardPaymentSuccess = payload => ({
type: HANDLE_CARD_PAYMENT_SUCCESS,
payload,
});
export const handleCardPaymentError = payload => ({
type: HANDLE_CARD_PAYMENT_ERROR,
payload,
error: true,
});
export const handleCardSetupRequest = () => ({
type: HANDLE_CARD_SETUP_REQUEST,
});
export const handleCardSetupSuccess = payload => ({
type: HANDLE_CARD_SETUP_SUCCESS,
payload,
});
export const handleCardSetupError = payload => ({
type: HANDLE_CARD_SETUP_ERROR,
payload,
error: true,
});
export const initializeCardPaymentData = () => ({
type: CLEAR_HANDLE_CARD_PAYMENT,
});
export const retrievePaymentIntentRequest = () => ({
type: RETRIEVE_PAYMENT_INTENT_REQUEST,
});
export const retrievePaymentIntentSuccess = payload => ({
type: RETRIEVE_PAYMENT_INTENT_SUCCESS,
payload,
});
export const retrievePaymentIntentError = payload => ({
type: RETRIEVE_PAYMENT_INTENT_ERROR,
payload,
error: true,
});
// ================ Thunks ================ //
// Util: rename address fields to match Stripe API specifications
const formatAddress = address => {
const { city, streetAddress, postalCode, state, province } = address;
const cityMaybe = city ? { city } : {};
const streetAddressMaybe = streetAddress ? { line1: streetAddress } : {};
const postalCodeMaybe = postalCode ? { postal_code: postalCode } : {};
const stateMaybe = state ? { state } : province ? { state: province } : {};
return {
...cityMaybe,
...streetAddressMaybe,
...postalCodeMaybe,
...stateMaybe,
};
};
// Util: rename personToken params to match Stripe API specifications
const personTokenParams = (personData, companyConfig) => {
const {
isAccountOpener,
fname: firstName,
lname: lastName,
birthDate,
address,
personalIdNumber,
email,
phone,
role,
ownershipPercentage,
title,
} = personData;
const addressMaybe = address ? { address: formatAddress(address) } : {};
const emailMaybe = email ? { email } : {};
const phoneMaybe = phone ? { phone } : {};
const personalIdNumberRequired = companyConfig && companyConfig.personalIdNumberRequired;
const ssnLast4Required = companyConfig && companyConfig.ssnLast4Required;
const idNumberMaybe = ssnLast4Required
? { ssn_last_4: personalIdNumber }
: personalIdNumberRequired
? { id_number: personalIdNumber }
: {};
const accountOpenerMaybe = isAccountOpener ? { account_opener: true } : {};
const jobTitleMaybe = title ? { title } : {};
const ownerMaybe = role && role.find(r => r === 'owner') ? { owner: true } : {};
const ownershipPercentageMaybe = ownershipPercentage
? { percent_ownership: Number.parseFloat(ownershipPercentage) }
: {};
const relationshipMaybe =
isAccountOpener || title || role
? {
relationship: {
...accountOpenerMaybe,
...jobTitleMaybe,
...ownerMaybe,
...ownershipPercentageMaybe,
},
}
: {};
return {
person: {
first_name: firstName,
last_name: lastName,
dob: birthDate,
...addressMaybe,
...idNumberMaybe,
...emailMaybe,
...phoneMaybe,
...relationshipMaybe,
},
};
};
const createStripePerson = (personParams, companyConfig, stripe) => (dispatch, getState, sdk) => {
const { isAccountOpener } = personParams;
let personToken = 'no-token';
return stripe
.createToken('person', personTokenParams(personParams, companyConfig))
.then(response => {
personToken = response.token.id;
// Request to create person in progress
// Account opener is mandatory for all - so it's handled separately
const createPersonRequest = isAccountOpener
? accountOpenerCreateRequest
: personCreateRequest;
dispatch(createPersonRequest({ personToken }));
return sdk.stripePersons.create({ personToken }, { expand: true });
})
.then(response => {
// Stripe person created successfully
const createPersonSuccess = isAccountOpener
? accountOpenerCreateSuccess
: personCreateSuccess;
dispatch(createPersonSuccess({ personToken, stripePerson: response.data.data }));
return response;
})
.catch(err => {
const e = storableError(err);
// Stripe person creation failed
const createPersonError = isAccountOpener ? accountOpenerCreateError : personCreateError;
dispatch(createPersonError({ personToken, error: e }));
const stripeMessage =
e.apiErrors && e.apiErrors.length > 0 && e.apiErrors[0].meta
? e.apiErrors[0].meta.stripeMessage
: null;
log.error(err, 'create-stripe-person-failed', { stripeMessage });
throw e;
});
};
// accountData should be either individual or company
const bankAccountTokenParams = accountData => accountData.bankAccountToken;
const businessProfileParams = (accountData, accountConfig) => {
const businessProfileRequired =
accountConfig && accountConfig.mccForUS && accountConfig.businessURL;
const { mcc, url } =
accountData && accountData.businessProfile ? accountData.businessProfile : {};
const hasInformation = mcc && url;
return businessProfileRequired && hasInformation
? {
businessProfileMCC: mcc,
businessProfileURL: url,
}
: {};
};
// Util: rename accountToken params to match Stripe API specifications
const accountTokenParamsForCompany = company => {
const { address, name, phone, taxId } = company;
const addressMaybe = address ? { address: formatAddress(address) } : {};
const phoneMaybe = phone ? { phone } : {};
return {
business_type: 'company',
company: {
name,
tax_id: taxId,
...addressMaybe,
...phoneMaybe,
},
tos_shown_and_accepted: true,
};
};
export const createStripeCompanyAccount = (payoutDetails, companyConfig, stripe) => (
dispatch,
getState,
sdk
) => {
const { company, country, accountOpener, persons = [] } = payoutDetails;
const state = getState();
let stripeAccount =
state.stripe && state.stripe.stripeAccount ? state.stripe.stripeAccount : null;
dispatch(stripeAccountCreateRequest());
const createPersons = () => {
return Promise.all([
dispatch(
createStripePerson({ ...accountOpener, isAccountOpener: true }, companyConfig, stripe)
),
...persons.map(p => dispatch(createStripePerson(p, companyConfig, stripe))),
]);
};
// If stripeAccount exists, stripePersons call must have failed.
// Retry person creation
if (stripeAccount) {
return createPersons()
.then(response => {
// Return created stripe account from this thunk function
return stripeAccount;
})
.catch(err => {
const e = storableError(err);
dispatch(stripeAccountCreateError(e));
const stripeMessage =
e.apiErrors && e.apiErrors.length > 0 && e.apiErrors[0].meta
? e.apiErrors[0].meta.stripeMessage
: null;
log.error(err, 'create-stripe-company-persons-failed', { stripeMessage });
throw e;
});
}
return stripe
.createToken('account', accountTokenParamsForCompany(company))
.then(response => {
const accountToken = response.token.id;
const bankAccountToken = bankAccountTokenParams(company);
const stripeAccountParams = {
accountToken,
bankAccountToken,
country,
...businessProfileParams(company, companyConfig),
};
return sdk.stripeAccount.create(stripeAccountParams, { expand: true });
})
.then(response => {
stripeAccount = response;
dispatch(stripeAccountCreateSuccess(response.data.data));
return createPersons();
})
.then(response => {
// Return created stripe account from this thunk function
return stripeAccount;
})
.catch(err => {
const e = storableError(err);
dispatch(stripeAccountCreateError(e));
const stripeMessage =
e.apiErrors && e.apiErrors.length > 0 && e.apiErrors[0].meta
? e.apiErrors[0].meta.stripeMessage
: null;
const errorFlag = !stripeAccount
? 'create-stripe-company-account-failed'
: 'create-stripe-company-persons-failed';
log.error(err, errorFlag, { stripeMessage });
throw e;
});
};
const accountTokenParamsForIndividual = (individual, individualConfig) => {
const {
fname: firstName,
lname: lastName,
birthDate,
address,
phone,
email,
personalIdNumber,
} = individual;
const addressMaybe = address ? { address: formatAddress(address) } : {};
const dobMaybe = birthDate ? { dob: birthDate } : {};
const emailMaybe = email ? { email } : {};
const phoneMaybe = phone ? { phone } : {};
const personalIdNumberRequired = individualConfig && individualConfig.personalIdNumberRequired;
const ssnLast4Required = individualConfig && individualConfig.ssnLast4Required;
const idNumberMaybe = ssnLast4Required
? { ssn_last_4: personalIdNumber }
: personalIdNumberRequired
? { id_number: personalIdNumber }
: {};
return {
business_type: 'individual',
individual: {
first_name: firstName,
last_name: lastName,
...dobMaybe,
...addressMaybe,
...emailMaybe,
...phoneMaybe,
...idNumberMaybe,
},
tos_shown_and_accepted: true,
};
};
export const createStripeIndividualAccount = (payoutDetails, individualConfig, stripe) => (
dispatch,
getState,
sdk
) => {
const { country, individual } = payoutDetails;
let stripeAccount;
dispatch(stripeAccountCreateRequest());
return stripe
.createToken('account', accountTokenParamsForIndividual(individual, individualConfig))
.then(response => {
const accountToken = response.token.id;
const bankAccountToken = bankAccountTokenParams(individual);
const stripeAccountParams = {
accountToken,
bankAccountToken,
country,
...businessProfileParams(individual, individualConfig),
};
return sdk.stripeAccount.create(stripeAccountParams, { expand: true });
})
.then(response => {
stripeAccount = response;
dispatch(stripeAccountCreateSuccess(response.data.data));
return stripeAccount;
})
.catch(err => {
const e = storableError(err);
dispatch(stripeAccountCreateError(e));
const stripeMessage =
e.apiErrors && e.apiErrors.length > 0 && e.apiErrors[0].meta
? e.apiErrors[0].meta.stripeMessage
: null;
log.error(err, 'create-stripe-individual-account-failed', { stripeMessage });
throw e;
});
};
export const createStripeAccount = payoutDetails => (dispatch, getState, sdk) => {
if (typeof window === 'undefined' || !window.Stripe) {
throw new Error('Stripe must be loaded for submitting PayoutPreferences');
}
const stripe = window.Stripe(config.stripe.publishableKey);
const country = payoutDetails.country;
const countryConfig = config.stripe.supportedCountries.find(c => c.code === country);
const individualConfig = countryConfig.individualConfig;
const companyConfig = countryConfig.companyConfig;
if (payoutDetails.accountType === 'individual') {
return dispatch(createStripeIndividualAccount(payoutDetails, individualConfig, stripe));
} else {
return dispatch(createStripeCompanyAccount(payoutDetails, companyConfig, stripe));
}
};
export const retrievePaymentIntent = params => dispatch => {
const { stripe, stripePaymentIntentClientSecret } = params;
dispatch(retrievePaymentIntentRequest());
return stripe
.retrievePaymentIntent(stripePaymentIntentClientSecret)
.then(response => {
if (response.error) {
return Promise.reject(response);
} else {
dispatch(retrievePaymentIntentSuccess(response.paymentIntent));
return response;
}
})
.catch(err => {
// Unwrap Stripe error.
const e = err.error || storableError(err);
dispatch(retrievePaymentIntentError(e));
// Log error
const { code, doc_url, message, payment_intent } = err.error || {};
const loggableError = err.error
? {
code,
message,
doc_url,
paymentIntentStatus: payment_intent
? payment_intent.status
: 'no payment_intent included',
}
: e;
log.error(loggableError, 'stripe-retrieve-payment-intent-failed', {
stripeMessage: loggableError.message,
});
throw err;
});
};
export const handleCardPayment = params => dispatch => {
// It's required to use the same instance of Stripe as where the card has been created
// so that's why Stripe needs to be passed here and we can't create a new instance.
const { stripe, card, paymentParams, stripePaymentIntentClientSecret } = params;
const transactionId = params.orderId;
dispatch(handleCardPaymentRequest());
// When using default payment method, card (aka Stripe Element) is not needed.
// We also set paymentParams.payment_method already in Flex API side,
// when request-payment transition is made - so there's no need for paymentParams
const args = card
? [stripePaymentIntentClientSecret, card, paymentParams]
: [stripePaymentIntentClientSecret];
return stripe
.handleCardPayment(...args)
.then(response => {
if (response.error) {
return Promise.reject(response);
} else {
dispatch(handleCardPaymentSuccess(response));
return { ...response, transactionId };
}
})
.catch(err => {
// Unwrap Stripe error.
const e = err.error || storableError(err);
dispatch(handleCardPaymentError(e));
// Log error
const containsPaymentIntent = err.error && err.error.payment_intent;
const { code, doc_url, message, payment_intent } = containsPaymentIntent ? err.error : {};
const loggableError = containsPaymentIntent
? {
code,
message,
doc_url,
paymentIntentStatus: payment_intent.status,
}
: e;
log.error(loggableError, 'stripe-handle-card-payment-failed', {
stripeMessage: loggableError.message,
});
throw e;
});
};
export const handleCardSetup = params => dispatch => {
// It's required to use the same instance of Stripe as where the card has been created
// so that's why Stripe needs to be passed here and we can't create a new instance.
const { stripe, card, setupIntentClientSecret, paymentParams } = params;
dispatch(handleCardSetupRequest());
return stripe
.handleCardSetup(setupIntentClientSecret, card, paymentParams)
.then(response => {
if (response.error) {
return Promise.reject(response);
} else {
dispatch(handleCardSetupSuccess(response));
return response;
}
})
.catch(err => {
// Unwrap Stripe error.
const e = err.error || storableError(err);
dispatch(handleCardSetupError(e));
// Log error
const containsSetupIntent = err.error && err.error.setup_intent;
const { code, doc_url, message, setup_intent } = containsSetupIntent ? err.error : {};
const loggableError = containsSetupIntent
? {
code,
message,
doc_url,
paymentIntentStatus: setup_intent.status,
}
: e;
log.error(loggableError, 'stripe-handle-card-setup-failed', {
stripeMessage: loggableError.message,
});
throw e;
});
};