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

Commit f29ea72

Browse files
updates to types, demos, and readme
1 parent 6f24bd1 commit f29ea72

File tree

5 files changed

+114
-59
lines changed

5 files changed

+114
-59
lines changed

CHANGELOG.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ get a refrence to an Element to use
1919
or
2020
[`createPaymentMethod()`](https://stripe.com/docs/stripe-js/reference#stripe-create-payment-method).
2121

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+
2239
### Breaking Changes
2340

2441
- We have removed the `getElement` method on RSE components that we introduced
@@ -74,9 +91,11 @@ or
7491
): Promise<{error?: Object, setupIntent?: Object}>
7592
```
7693

77-
For more information, please review the Stripe Docs:
94+
````
95+
96+
For more information, please review the Stripe Docs:
7897
79-
- [`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)
8099
81100
### Deprecations
82101
@@ -388,3 +407,4 @@ Initial release! Support for:
388407
- CardExpiryElement
389408
- CardCVCElement
390409
- PostalCodeElement
410+
````

README.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ styles, fonts).
2929
- [The Stripe context (`StripeProvider`)](#the-stripe-context-stripeprovider)
3030
- [Element groups (`Elements`)](#element-groups-elements)
3131
- [Setting up your payment form (`injectStripe`)](#setting-up-your-payment-form-injectstripe)
32-
- [Using Stripe.js methods that do not have automatic element detection](#using-methods-that-do-not-have-automatic-element-detection)
3332
- [Using individual `*Element` components](#using-individual-element-components)
3433
- [Using the `PaymentRequestButtonElement`](#using-the-paymentrequestbuttonelement)
3534
- [Advanced integrations](#advanced-integrations)
@@ -197,6 +196,8 @@ class CheckoutForm extends React.Component {
197196
// Use Elements to get a reference to the Card Element mounted somewhere
198197
// in your <Elements> tree. Elements will know how to find your Card Element
199198
// becase only one is allowed.
199+
// See our getElement documentation for more:
200+
// https://stripe.com/docs/stripe-js/reference#elements-get-element
200201
const cardElement = this.props.elements.getElement('card');
201202

202203
// From here we cal call createPaymentMethod to create a PaymentMethod
@@ -223,8 +224,8 @@ class CheckoutForm extends React.Component {
223224

224225
// You can also use confirmCardSetup with the SetupIntents API.
225226
// See our confirmCardSetup documentation for more:
226-
// https://stripe.com/docs/stripe-js/reference#stripe-handle-card-setup
227-
this.props.stripe.handleCardSetup('{PAYMENT_INTENT_CLIENT_SECRET}', {
227+
// https://stripe.com/docs/stripe-js/reference#stripe-confirm-card-setup
228+
this.props.stripe.confirmCardSetpu('{PAYMENT_INTENT_CLIENT_SECRET}', {
228229
payment_method: {
229230
card: cardElement,
230231
},
@@ -234,7 +235,7 @@ class CheckoutForm extends React.Component {
234235
// See our tokens documentation for more:
235236
// https://stripe.com/docs/stripe-js/reference#stripe-create-token
236237
// With createToken, you will not need to pass in the reference to
237-
// the Card Element. It will be inferred automatically.
238+
// the Element. It will be inferred automatically.
238239
this.props.stripe.createToken({type: 'card', name: 'Jenny Rosen'});
239240
// token type can optionally be inferred if there is only one Element
240241
// with which to create tokens
@@ -244,7 +245,7 @@ class CheckoutForm extends React.Component {
244245
// See our Sources documentation for more:
245246
// https://stripe.com/docs/stripe-js/reference#stripe-create-source
246247
// With createSource, you will not need to pass in the reference to
247-
// the Card Element. It will be inferred automatically.
248+
// the Element. It will be inferred automatically.
248249
this.props.stripe.createSource({
249250
type: 'card',
250251
owner: {
@@ -668,14 +669,13 @@ function injectStripe(
668669
Use `injectStripe` to wrap a component that needs to interact with `Stripe.js`
669670
to create sources or tokens.
670671

671-
1. First, create a component that accepts the `stripe` prop and calls
672-
`this.props.stripe.createToken` or `this.props.stripe.createSource` when
673-
necessary.
672+
1. First, create a component that accepts the `stripe` prop and calls one of
673+
the Stripe or Elements methods when necessary.
674674
2. Wrap that component by passing it to `injectStripe` so that it actually
675675
receives the `stripe` and `elements` props.
676676
3. Render the component that `injectStripe` returns.
677677

678-
### Example
678+
#### Example
679679

680680
```jsx
681681
// 1. Create a component that uses this.props.stripe:
@@ -684,7 +684,12 @@ class CheckoutForm extends React.Component {
684684
/* ... */
685685
}
686686
onCompleteCheckout() {
687-
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(/* ... */);
688693
}
689694
}
690695

@@ -715,10 +720,15 @@ available with the `getWrappedInstance()` method of the wrapper component. This
715720
feature can not be used if the wrapped component is a stateless function
716721
component.
717722

718-
Within the wrapped component, the `stripe` prop has the type:
723+
Within the wrapped component, the `stripe` and `elements` props have the type:
719724

720725
```jsx
721726
type FactoryProps = {
727+
elements: null | {
728+
getElement: (type: string) => Element | null,
729+
// and other functions available on the `elements` object,
730+
// as officially documented here: https://stripe.com/docs/elements/reference#the-elements-object
731+
},
722732
stripe: null | {
723733
createToken: (tokenData: {type?: string}) => Promise<{
724734
token?: Object,
@@ -729,8 +739,7 @@ type FactoryProps = {
729739
error?: Object,
730740
}>,
731741
createPaymentMethod: (
732-
type: string,
733-
paymentMethodData?: Object
742+
paymentMethodData: Object
734743
) => Promise<{
735744
paymentMethod?: Object,
736745
error?: Object,
@@ -755,10 +764,7 @@ type FactoryProps = {
755764
};
756765
```
757766

758-
In addition to the `stripe` prop, `injectStripe` also injects a reference to the
759-
Elements instance as the `elements` prop.
760-
761-
The `stripe` and `elements` prop can only be `null` if you are using one of the
767+
The `stripe` and `elements` props can only be `null` if you are using one of the
762768
[Advanced integrations](#advanced-integrations) mentioned above, like loading
763769
Stripe.js asynchronously or providing an existing instance. If you are using a
764770
basic integration where you pass in an api key to `<StripeProvider/>`, they will
@@ -805,14 +811,9 @@ reach all components.
805811
2. You can use the [`pure: false`][pure-false] option for redux-connect:
806812

807813
```jsx
808-
const Component = connect(
809-
mapStateToProps,
810-
mapDispatchToProps,
811-
mergeProps,
812-
{
813-
pure: false,
814-
}
815-
)(injectStripe(_CardForm));
814+
const Component = connect(mapStateToProps, mapDispatchToProps, mergeProps, {
815+
pure: false,
816+
})(injectStripe(_CardForm));
816817
```
817818

818819
[pure-false]:

demo/intents/index.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,27 @@ class _CreatePaymentMethod extends React.Component<
6363

6464
handleSubmit = (ev) => {
6565
ev.preventDefault();
66-
if (this.props.stripe) {
67-
this.props.stripe.createPaymentMethod('card').then((payload) => {
68-
if (payload.error) {
69-
this.setState({
70-
error: `Failed to create PaymentMethod: ${payload.error.message}`,
71-
processing: false,
72-
});
73-
console.log('[error]', payload.error);
74-
} else {
75-
this.setState({
76-
message: `Created PaymentMethod: ${payload.paymentMethod.id}`,
77-
processing: false,
78-
});
79-
console.log('[paymentMethod]', payload.paymentMethod);
80-
}
81-
});
66+
if (this.props.stripe && this.props.elements) {
67+
this.props.stripe
68+
.createPaymentMethod({
69+
type: 'card',
70+
card: this.props.elements.getElement('card'),
71+
})
72+
.then((payload) => {
73+
if (payload.error) {
74+
this.setState({
75+
error: `Failed to create PaymentMethod: ${payload.error.message}`,
76+
processing: false,
77+
});
78+
console.log('[error]', payload.error);
79+
} else {
80+
this.setState({
81+
message: `Created PaymentMethod: ${payload.paymentMethod.id}`,
82+
processing: false,
83+
});
84+
console.log('[paymentMethod]', payload.paymentMethod);
85+
}
86+
});
8287
this.setState({processing: true});
8388
} else {
8489
console.log("Stripe.js hasn't loaded yet.");
@@ -149,9 +154,13 @@ class _HandleCardPayment extends React.Component<
149154

150155
handleSubmit = (ev) => {
151156
ev.preventDefault();
152-
if (this.props.stripe) {
157+
if (this.props.stripe && this.props.elements) {
153158
this.props.stripe
154-
.handleCardPayment(this.state.clientSecret)
159+
.confirmCardPayment(this.state.clientSecret, {
160+
payment_method: {
161+
card: this.props.elements.getElement('card'),
162+
},
163+
})
155164
.then((payload) => {
156165
if (payload.error) {
157166
this.setState({
@@ -162,9 +171,7 @@ class _HandleCardPayment extends React.Component<
162171
} else {
163172
this.setState({
164173
succeeded: true,
165-
message: `Charge succeeded! PaymentIntent is in state: ${
166-
payload.paymentIntent.status
167-
}`,
174+
message: `Charge succeeded! PaymentIntent is in state: ${payload.paymentIntent.status}`,
168175
});
169176
console.log('[PaymentIntent]', payload.paymentIntent);
170177
}
@@ -179,7 +186,7 @@ class _HandleCardPayment extends React.Component<
179186
return (
180187
<form onSubmit={this.handleSubmit}>
181188
<label>
182-
stripe.handleCardPayment
189+
stripe.confirmCardPayment
183190
<CardElement
184191
onBlur={handleBlur}
185192
onChange={handleChange}
@@ -239,9 +246,13 @@ class _HandleCardSetup extends React.Component<
239246

240247
handleSubmit = (ev) => {
241248
ev.preventDefault();
242-
if (this.props.stripe) {
249+
if (this.props.stripe && this.props.elements) {
243250
this.props.stripe
244-
.handleCardSetup(this.state.clientSecret)
251+
.confirmCardSetup(this.state.clientSecret, {
252+
payment_method: {
253+
card: this.props.elements.getElement('card'),
254+
},
255+
})
245256
.then((payload) => {
246257
if (payload.error) {
247258
this.setState({
@@ -252,9 +263,7 @@ class _HandleCardSetup extends React.Component<
252263
} else {
253264
this.setState({
254265
succeeded: true,
255-
message: `Setup succeeded! SetupIntent is in state: ${
256-
payload.setupIntent.status
257-
}`,
266+
message: `Setup succeeded! SetupIntent is in state: ${payload.setupIntent.status}`,
258267
});
259268
console.log('[SetupIntent]', payload.setupIntent);
260269
}
@@ -269,7 +278,7 @@ class _HandleCardSetup extends React.Component<
269278
return (
270279
<form onSubmit={this.handleSubmit}>
271280
<label>
272-
stripe.handleCardSetup
281+
stripe.confirmCardSetup
273282
<CardElement
274283
onBlur={handleBlur}
275284
onChange={handleChange}

src/components/inject.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ type WrappedStripeShape = {
2222
createPaymentMethod: Function,
2323
handleCardPayment: Function,
2424
handleCardSetup: Function,
25+
confirmCardPayment: Function,
26+
confirmCardSetup: Function,
2527
};
2628

2729
type State = {stripe: WrappedStripeShape | null};
2830

29-
export type InjectedProps = {stripe: WrappedStripeShape | null};
31+
export type InjectedProps = {
32+
stripe: WrappedStripeShape | null,
33+
elements: ElementsShape | null,
34+
};
3035

3136
// react-redux does a bunch of stuff with pure components / checking if it needs to re-render.
3237
// not sure if we need to do the same.
@@ -226,6 +231,10 @@ Please be sure the component that calls createSource or createToken is within an
226231
elementOrData?: mixed,
227232
maybeData?: mixed
228233
) => {
234+
if (paymentMethodType && typeof paymentMethodType === 'object') {
235+
return stripe.createPaymentMethod(paymentMethodType);
236+
}
237+
229238
if (!paymentMethodType || typeof paymentMethodType !== 'string') {
230239
throw new Error(
231240
`Invalid PaymentMethod type passed to createPaymentMethod. Expected a string, got ${typeof paymentMethodType}.`

src/decls/Stripe.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ declare type ElementShape = {
1313

1414
declare type ElementsShape = {
1515
create: (type: string, options: MixedObject) => ElementShape,
16+
getElement: (type: string) => null | ElementShape,
1617
};
1718

19+
type ConfirmSetupFn = (
20+
clientSecret: string,
21+
options?: mixed
22+
) => Promise<{setupIntent?: MixedObject, error?: MixedObject}>;
23+
24+
type ConfirmPaymentFn = (
25+
clientSecret: string,
26+
options?: mixed
27+
) => Promise<{paymentIntent?: MixedObject, error?: MixedObject}>;
28+
1829
declare type StripeShape = {
1930
elements: (options: MixedObject) => ElementsShape,
2031
createSource: (
@@ -26,9 +37,9 @@ declare type StripeShape = {
2637
options: mixed
2738
) => Promise<{token?: MixedObject, error?: MixedObject}>,
2839
createPaymentMethod: (
29-
type: string,
30-
element: ElementShape | MixedObject,
31-
data: mixed
40+
type: mixed,
41+
element?: ElementShape | MixedObject,
42+
data?: mixed
3243
) => Promise<{paymentMethod?: MixedObject, error?: MixedObject}>,
3344
handleCardPayment: (
3445
clientSecret: string,
@@ -40,4 +51,9 @@ declare type StripeShape = {
4051
element: mixed,
4152
options: mixed
4253
) => Promise<{setupIntent?: MixedObject, error?: MixedObject}>,
54+
confirmCardPayment: ConfirmPaymentFn,
55+
confirmCardSetup: ConfirmSetupFn,
56+
confirmIdealPayment: ConfirmPaymentFn,
57+
confirmSepaDebitPayment: ConfirmPaymentFn,
58+
confirmSepaDebitSetup: ConfirmSetupFn,
4359
};

0 commit comments

Comments
 (0)