@@ -44,7 +44,6 @@ styles, fonts).
4444 - [ Available components] ( #available-components )
4545 - [ Props shape] ( #props-shape-2 )
4646 - [ Using ` onReady ` ] ( #using-onready )
47- - [ Using ` getElement ` ] ( #using-getelement )
4847 - [ ` injectStripe ` HOC] ( #injectstripe-hoc )
4948 - [ Example] ( #example )
5049- [ Troubleshooting] ( #troubleshooting )
@@ -167,19 +166,13 @@ Use the `injectStripe` [Higher-Order Component][hoc] (HOC) to build your payment
167166form components in the ` Elements ` tree. The [ Higher-Order Component] [ hoc ]
168167pattern in React can be unfamiliar to those who've never seen it before, so
169168consider reading up before continuing. The ` injectStripe ` HOC provides the
170- ` this.props.stripe ` property that manages your ` Elements ` groups. Within an
171- injected component, you can call any of the following:
172-
173- - ` this.props.stripe.createPaymentMethod `
174- - ` this.props.stripe.createToken `
175- - ` this.props.stripe.createSource `
176- - ` this.props.stripe.handleCardPayment `
177- - ` this.props.stripe.handleCardSetup `
178-
179- Calling any of these methods will collect data from the appropriate Element and
180- use it to submit payment data to Stripe.
169+ ` this.props.stripe ` and ` this.props.elements ` properties that manage your
170+ ` Elements ` groups. Within an injected component, you can call any of the methods
171+ on the [ Stripe] [ stripe ] or [ Elements] [ elements ] objects.
181172
182173[ hoc ] : https://facebook.github.io/react/docs/higher-order-components.html
174+ [ stripe ] : https://stripe.com/docs/stripe-js/reference#the-stripe-object
175+ [ elements ] : https://stripe.com/docs/stripe-js/reference#the-elements-object
183176
184177> :warning : NOTE ` injectStripe ` cannot be used on the same element that renders
185178> the ` Elements ` component; it must be used on the child component of
@@ -200,29 +193,49 @@ class CheckoutForm extends React.Component {
200193 // We don't want to let default form submission happen here, which would refresh the page.
201194 ev .preventDefault ();
202195
203- // Within the context of `Elements`, this call to createPaymentMethod knows from which Element to
204- // create the PaymentMethod, since there's only one in this group.
196+ // Use Elements to get a reference to the Card Element mounted somewhere
197+ // in your <Elements> tree. Elements will know how to find your Card Element
198+ // becase only one is allowed.
199+ // See our getElement documentation for more:
200+ // https://stripe.com/docs/stripe-js/reference#elements-get-element
201+ const cardElement = this .props .elements .getElement (' card' );
202+
203+ // From here we cal call createPaymentMethod to create a PaymentMethod
205204 // See our createPaymentMethod documentation for more:
206205 // https://stripe.com/docs/stripe-js/reference#stripe-create-payment-method
207206 this .props .stripe
208- .createPaymentMethod (' card' , {billing_details: {name: ' Jenny Rosen' }})
207+ .createPaymentMethod ({
208+ type: ' card' ,
209+ card: cardElement,
210+ billing_details: {name: ' Jenny Rosen' },
211+ })
209212 .then (({paymentMethod}) => {
210213 console .log (' Received Stripe PaymentMethod:' , paymentMethod);
211214 });
212215
213- // You can also use handleCardPayment with the PaymentIntents API automatic confirmation flow.
214- // See our handleCardPayment documentation for more:
215- // https://stripe.com/docs/stripe-js/reference#stripe-handle-card-payment
216- this .props .stripe .handleCardPayment (' {PAYMENT_INTENT_CLIENT_SECRET}' , data);
216+ // You can also use confirmCardPayment with the PaymentIntents API automatic confirmation flow.
217+ // See our confirmCardPayment documentation for more:
218+ // https://stripe.com/docs/stripe-js/reference#stripe-confirm-card-payment
219+ this .props .stripe .confirmCardPayment (' {PAYMENT_INTENT_CLIENT_SECRET}' , {
220+ payment_method: {
221+ card: cardElement,
222+ },
223+ });
217224
218- // You can also use handleCardSetup with the SetupIntents API.
219- // See our handleCardSetup documentation for more:
220- // https://stripe.com/docs/stripe-js/reference#stripe-handle-card-setup
221- this .props .stripe .handleCardSetup (' {PAYMENT_INTENT_CLIENT_SECRET}' , data);
225+ // You can also use confirmCardSetup with the SetupIntents API.
226+ // See our confirmCardSetup documentation for more:
227+ // https://stripe.com/docs/stripe-js/reference#stripe-confirm-card-setup
228+ this .props .stripe .confirmCardSetpu (' {PAYMENT_INTENT_CLIENT_SECRET}' , {
229+ payment_method: {
230+ card: cardElement,
231+ },
232+ });
222233
223234 // You can also use createToken to create tokens.
224235 // See our tokens documentation for more:
225236 // https://stripe.com/docs/stripe-js/reference#stripe-create-token
237+ // With createToken, you will not need to pass in the reference to
238+ // the Element. It will be inferred automatically.
226239 this .props .stripe .createToken ({type: ' card' , name: ' Jenny Rosen' });
227240 // token type can optionally be inferred if there is only one Element
228241 // with which to create tokens
@@ -231,6 +244,8 @@ class CheckoutForm extends React.Component {
231244 // You can also use createSource to create Sources.
232245 // See our Sources documentation for more:
233246 // https://stripe.com/docs/stripe-js/reference#stripe-create-source
247+ // With createSource, you will not need to pass in the reference to
248+ // the Element. It will be inferred automatically.
234249 this .props .stripe .createSource ({
235250 type: ' card' ,
236251 owner: {
@@ -640,45 +655,6 @@ export default CardSection;
640655
641656[ element ] : https://stripe.com/docs/stripe-js/reference#other-methods
642657
643- #### Using ` getElement `
644-
645- In addition to ` onReady ` , you can also access to the underlying Element by using
646- a [ ref] to the access the component instance and calling ` getElement() ` .
647-
648- Note that if you are async loading Stripe.js, until it loads ` getElement ` will
649- return ` null ` .
650-
651- ``` jsx
652- // CardSection.js
653- import React from ' react' ;
654- import {CardElement } from ' react-stripe-elements' ;
655-
656- class CardSection extends React .Component {
657- ref = React .createRef ();
658-
659- focus () {
660- const element = this .ref .current .getElement ();
661-
662- if (element) {
663- element .focus ();
664- }
665- }
666-
667- render = () => {
668- return (
669- < label>
670- Card details
671- < CardElement ref= {this .ref } / >
672- < / label>
673- );
674- };
675- }
676-
677- export default CardSection ;
678- ```
679-
680- [ ref ] : https://reactjs.org/docs/refs-and-the-dom.html
681-
682658### ` injectStripe ` HOC
683659
684660``` jsx
@@ -693,14 +669,13 @@ function injectStripe(
693669Use `injectStripe` to wrap a component that needs to interact with `Stripe.js`
694670to create sources or tokens.
695671
696- 1. First, create a component that accepts the `stripe` prop and calls
697- `this.props.stripe.createToken` or `this.props.stripe.createSource` when
698- necessary.
672+ 1. First, create a component that accepts the `stripe` prop and calls one of
673+ the Stripe or Elements methods when necessary.
6996742. Wrap that component by passing it to `injectStripe` so that it actually
700- receives the `stripe` prop .
675+ receives the `stripe` and `elements` props .
7016763. Render the component that `injectStripe` returns.
702677
703- ### Example
678+ #### Example
704679
705680```jsx
706681// 1. Create a component that uses this.props.stripe:
@@ -709,7 +684,12 @@ class CheckoutForm extends React.Component {
709684 /* ... */
710685 }
711686 onCompleteCheckout () {
712- this .props .stripe .createPaymentMethod (' card' ).then (/* ... */ );
687+ this .props .stripe
688+ .createPaymentMethod ({
689+ type: ' card' ,
690+ card: this .props .stripe .getElement (' card' ),
691+ })
692+ .then (/* ... */ );
713693 }
714694}
715695
@@ -740,10 +720,16 @@ available with the `getWrappedInstance()` method of the wrapper component. This
740720feature can not be used if the wrapped component is a stateless function
741721component.
742722
743- Within the wrapped component, the ` stripe ` prop has the type:
723+ Within the wrapped component, the ` stripe ` and ` elements ` props have the type:
744724
745725``` jsx
746726type FactoryProps = {
727+ elements: null | {
728+ getElement : (type : string ) => Element | null ,
729+ // For more detail and documentation on other methods available on
730+ // the `elements` object, please refer to our official documentation:
731+ // https://stripe.com/docs/elements/reference#the-elements-object
732+ },
747733 stripe: null | {
748734 createToken : (tokenData : {type?: string}) => Promise < {
749735 token?: Object ,
@@ -754,36 +740,36 @@ type FactoryProps = {
754740 error?: Object ,
755741 }> ,
756742 createPaymentMethod: (
757- type: string,
758- paymentMethodData?: Object
743+ paymentMethodData: Object
759744 ) => Promise < {
760745 paymentMethod?: Object ,
761746 error?: Object ,
762747 }> ,
763- handleCardPayment : (
748+ confirmCardPayment : (
764749 clientSecret: string,
765- paymentMethodData ?: Object
750+ paymentIntentData ?: Object
766751 ) => Promise < {
767752 paymentIntent?: Object ,
768753 error?: Object ,
769754 }> ,
770- handleCardSetup : (
755+ confirmCardSetup : (
771756 clientSecret: string,
772- paymentMethodData ?: Object
757+ paymentIntentData ?: Object
773758 ) => Promise < {
774759 setupIntent?: Object ,
775760 error?: Object ,
776761 }> ,
777- // and other functions available on the `stripe` object,
778- // as officially documented here: https://stripe.com/docs/elements/reference#the-stripe-object
762+ // For more detail and documentation on other methods available on
763+ // the `stripe` object, please refer to our official documentation:
764+ // https://stripe.com/docs/elements/reference#the-stripe-object
779765 },
780766};
781767```
782768
783- The ` stripe ` prop can only be ` null ` if you are using one of the
769+ The ` stripe ` and ` elements ` props can only be ` null ` if you are using one of the
784770[ Advanced integrations] ( #advanced-integrations ) mentioned above, like loading
785771Stripe.js asynchronously or providing an existing instance. If you are using a
786- basic integration where you pass in an api key to ` <StripeProvider/> ` , it will
772+ basic integration where you pass in an api key to ` <StripeProvider/> ` , they will
787773always be present.
788774
789775## Troubleshooting
0 commit comments