Skip to content
This repository was archived by the owner on Mar 27, 2021. It is now read-only.

Commit 91c465c

Browse files
Merge pull request #458 from stripe/dweedon/inject-elements
inject elements
2 parents 93e50bc + 9580eea commit 91c465c

File tree

10 files changed

+230
-132
lines changed

10 files changed

+230
-132
lines changed

CHANGELOG.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@
33
`react-stripe-elements` adheres to
44
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## v6.0.0 - 2019-11-05
7+
8+
### New Features
9+
10+
- `injectStripe` now injects a reference to the Elements instance created by
11+
`<Elements>` as the prop `elements`.
12+
13+
The primary reason you would want an Elements instance is to use
14+
[`elements.getElement()`](https://stripe.com/docs/stripe-js/reference#elements-get-element).
15+
which provides an easy way to get a reference to an Element. You will need to
16+
get a refrence to an Element to use
17+
[`confirmCardPayment`](https://stripe.com/docs/stripe-js/reference#stripe-confirm-card-payment),
18+
[`confirmCardSetup()`](https://stripe.com/docs/stripe-js/reference#stripe-confirm-card-setup),
19+
or
20+
[`createPaymentMethod()`](https://stripe.com/docs/stripe-js/reference#stripe-create-payment-method).
21+
22+
Note that the old API for `createPaymentMethod` will continue to work and
23+
provide automatic element injection, but we are updating documentation and
24+
examples to use the new argument shape:
25+
26+
```js
27+
// old shape with automatic element detection - still works
28+
this.props.stripe.createPaymentMethod('card').then(/* ... */);
29+
30+
// new shape without automatic element detection - recommended and will work with new non-card PaymentMethods
31+
this.props.stripe
32+
.createPaymentMethod({
33+
type: 'card',
34+
card: this.props.elements.getElement('card'),
35+
})
36+
.then(/* ... */);
37+
```
38+
39+
### Breaking Changes
40+
41+
- We have removed the `getElement` method on RSE components that we introduced
42+
in v5.1.0 in favor of the above change. Sorry for the churn.
43+
644
## v5.1.0 - 2019-10-22
745

846
### New Features
@@ -53,9 +91,11 @@
5391
): Promise<{error?: Object, setupIntent?: Object}>
5492
```
5593

56-
For more information, please review the Stripe Docs:
94+
````
95+
96+
For more information, please review the Stripe Docs:
5797
58-
- [`stripe.handleCardSetup`](https://stripe.com/docs/stripe-js/reference#stripe-handle-card-setup)
98+
- [`stripe.handleCardSetup`](https://stripe.com/docs/stripe-js/reference#stripe-handle-card-setup)
5999
60100
### Deprecations
61101
@@ -367,3 +407,4 @@ Initial release! Support for:
367407
- CardExpiryElement
368408
- CardCVCElement
369409
- PostalCodeElement
410+
````

README.md

Lines changed: 65 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -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
167166
form components in the `Elements` tree. The [Higher-Order Component][hoc]
168167
pattern in React can be unfamiliar to those who've never seen it before, so
169168
consider 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(
693669
Use `injectStripe` to wrap a component that needs to interact with `Stripe.js`
694670
to 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.
699674
2. Wrap that component by passing it to `injectStripe` so that it actually
700-
receives the `stripe` prop.
675+
receives the `stripe` and `elements` props.
701676
3. 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
740720
feature can not be used if the wrapped component is a stateless function
741721
component.
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
746726
type 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
785771
Stripe.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
787773
always be present.
788774

789775
## Troubleshooting

0 commit comments

Comments
 (0)