Create Token using Multiple Elements
Feature request or idea? Consider opening an API review!
Summary
Hello, we are looking to use multiple stripe elements in our react app for a customer to input their card details. We are async loading the stripe script on the component as to not have to load the stripe bundle on page load (accessing stripe elements is a rare case for our customers). Unfortunately so far it seems as though you can only pass in a single element to createToken, which doesn't really make sense if you are utilizing multiple elements such as CardNumberElement, CardExpiryElement and CardCVCElement.
We do not want to use injectStripe as we used to because it seems that you need to have the stripe bundle pre-loaded for that work, again, we are async loading the stripe script when the component is created:
componentWillReceiveProps(nextProps) {
const { isScriptLoaded, isScriptLoadSucceed } = nextProps
const { stripe } = this.state
if(!stripe && isScriptLoadSucceed) {
this.setState({ stripe: window.Stripe(StripePublishableKey) })
}
}
Can we get some help on how to best achieve this? Thanks.
Code Implementation
<StripeProvider stripe={stripe}>
<Elements>
<div className='card-entry-fields'>
<InputField custom title='Card Number' isFocused={this.state.numberFieldFocused}>
<CardNumberElement
style={ElementsStyle}
elementRef={ref => this.cardNumberRef = ref}
onFocus={() => this.setState({ numberFieldFocused: true })}
onBlur={() => this.setState({ numberFieldFocused: false })}
onChange={(numberField) => this.updateData({ numberField })}
/>
</InputField>
<div className='card-entry-fields-split-width'>
<InputField custom title='Expiry Date' isFocused={this.state.expiryFieldFocused}>
<CardExpiryElement
style={ElementsStyle}
elementRef={ref => this.cardExpiryRef = ref}
onFocus={() => this.setState({ expiryFieldFocused: true })}
onBlur={() => this.setState({ expiryFieldFocused: false })}
onChange={(expiryField) => this.updateData({ expiryField })}
/>
</InputField>
<InputField custom title='CVC Code' isFocused={this.state.cvcFieldFocused}>
<CardCVCElement
style={ElementsStyle}
elementRef={ref => this.cardCVCRef = ref}
onFocus={() => this.setState({ cvcFieldFocused: true })}
onBlur={() => this.setState({ cvcFieldFocused: false })}
onChange={(cvcField) => this.updateData({ cvcField })}
/>
</InputField>
</div>
</div>
</Elements>
</StripeProvider>
Create Token
async createToken() {
try {
let payload = await this.state.stripe.createToken(this.cardNumberRef, {
currency: 'usd'
})
return payload.token
} catch (e) {
console.error('Error creating card token', e)
throw e
}
}

Hey @luskin! It looks like you're not using the injectStripe higher-order component. When you use it like injectStripe(MyCheckoutPage), within the MyCheckoutPage component you'll have access to this.props.createToken().then(...), where you don't have to pass any arguments to createToken at all. Instead, react-stripe-elements automatically finds all the elements within the outer <Elements> ... </Elements wrapper.
See this section: https://github.com/stripe/react-stripe-elements#setting-up-your-payment-form-injectstripe
@jez Thanks for your reply. If you read my issue post carefully you will see multiple mentions as to why we opted out of using injectStripe.
@jez unless you know of a way to use injectStripe while also async loading the stripe script from the component and not included in the html header
injectStripe will inject the same stripe prop regardless of what options you're using on the StripeProvider.
For example, see the async demo: it uses async StripeProvider and also injectStripe:
https://github.com/stripe/react-stripe-elements/blob/master/demo/async/async.js#L46-L52
this.props.stripe will be null until Stripe.js loads. We recommend that you use this in your render function to disable the button, so that it's not possible for the submit handler to have been clicked before Stripe.js loads:
https://github.com/stripe/react-stripe-elements/blob/master/demo/async/async.js#L69
This was immensely useful to us. Thank you for the quick reply and solution @jez, we spend hours digging through the documentation but just couldn't quite get there.
No worrries, glad I could help!
Do you have any suggestions on how we could change the docs to make this more discoverable?
On Wed, Jan 31, 2018 at 11:50 AM Gregg Luskin [email protected] wrote:
This was immensely useful to us. Thank you for the quick reply and solution @jez https://github.com/jez, we spend hours digging through the documentation but just couldn't quite get there.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stripe/react-stripe-elements/issues/160#issuecomment-362049733, or mute the thread https://github.com/notifications/unsubscribe-auth/AdazsA_KUtcWHoZXDPa_tKIZ0_Vb1BB6ks5tQMQFgaJpZM4R0cD2 .
Going to reopen this, because we should fix this documentation issue for future users!
I'm going to close this since a PR for the docs improvements is now under review.