react-stripe-elements icon indicating copy to clipboard operation
react-stripe-elements copied to clipboard

Create Token using Multiple Elements

Open luskin opened this issue 8 years ago • 8 comments

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
    }
  }

screen shot 2018-01-31 at 10 32 20 am

luskin avatar Jan 31 '18 18:01 luskin

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 avatar Jan 31 '18 19:01 jez

@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.

luskin avatar Jan 31 '18 19:01 luskin

@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

luskin avatar Jan 31 '18 19:01 luskin

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

jez avatar Jan 31 '18 19:01 jez

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.

luskin avatar Jan 31 '18 19:01 luskin

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 .

jez-stripe avatar Jan 31 '18 20:01 jez-stripe

Going to reopen this, because we should fix this documentation issue for future users!

michelle avatar Feb 01 '18 01:02 michelle

I'm going to close this since a PR for the docs improvements is now under review.

asolove-stripe avatar Sep 10 '18 19:09 asolove-stripe