Skip to content

Commit

Permalink
Call stripe.createToken before saving those tokens to Flex API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnito committed May 17, 2018
1 parent be68b65 commit 81490c5
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/containers/PayoutPreferencesPage/PayoutPreferencesPage.duck.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { omitBy, isUndefined } from 'lodash';
import { fetchCurrentUser, createStripeAccount } from '../../ducks/user.duck';
import config from '../../config';

// ================ Action types ================ //

Expand Down Expand Up @@ -52,6 +53,12 @@ export const savePayoutDetailsSuccess = () => ({
// ================ Thunks ================ //

export const savePayoutDetails = values => (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);

dispatch(savePayoutDetailsRequest());
const {
firstName,
Expand All @@ -63,22 +70,32 @@ export const savePayoutDetails = values => (dispatch, getState, sdk) => {
city,
bankAccountToken,
} = values;

const address = {
country,
city,
addressLine: streetAddress,
postalCode,
line1: streetAddress,
postal_code: postalCode,
};

// Params for Stripe SDK
const params = {
firstName,
lastName,
birthDate,
bankAccountToken,
address: omitBy(address, isUndefined),
legal_entity: {
first_name: firstName,
last_name: lastName,
address: omitBy(address, isUndefined),
dob: birthDate,
},
tos_shown_and_accepted: true,
};
return dispatch(createStripeAccount(params))

return stripe
.createToken('account', params)
.then(response => {
const accountToken = response.token.id;
return dispatch(createStripeAccount({ accountToken, bankAccountToken, country }));
})
.then(() => dispatch(savePayoutDetailsSuccess()))
.catch(() => dispatch(savePayoutDetailsError()));
.catch(e => dispatch(savePayoutDetailsError()));
};

export const loadData = () => (dispatch, getState, sdk) => {
Expand Down

0 comments on commit 81490c5

Please sign in to comment.