@stripe/stripe-js 2.2.2 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/pure.esm.js CHANGED
@@ -54,7 +54,7 @@ var registerWrapper = function registerWrapper(stripe, startTime) {
54
54
 
55
55
  stripe._registerWrapper({
56
56
  name: 'stripe-js',
57
- version: "2.2.2",
57
+ version: "2.4.0",
58
58
  startTime: startTime
59
59
  });
60
60
  };
package/dist/pure.js CHANGED
@@ -58,7 +58,7 @@ var registerWrapper = function registerWrapper(stripe, startTime) {
58
58
 
59
59
  stripe._registerWrapper({
60
60
  name: 'stripe-js',
61
- version: "2.2.2",
61
+ version: "2.4.0",
62
62
  startTime: startTime
63
63
  });
64
64
  };
@@ -38,7 +38,7 @@ var registerWrapper = function registerWrapper(stripe, startTime) {
38
38
 
39
39
  stripe._registerWrapper({
40
40
  name: 'stripe-js',
41
- version: "2.2.2",
41
+ version: "2.4.0",
42
42
  startTime: startTime
43
43
  });
44
44
  };
@@ -130,15 +130,29 @@ var initStripe = function initStripe(maybeStripe, args, startTime) {
130
130
  return stripe;
131
131
  }; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
132
132
 
133
+ var stripePromise$1;
134
+ var loadCalled = false;
135
+
136
+ var getStripePromise = function getStripePromise() {
137
+ if (stripePromise$1) {
138
+ return stripePromise$1;
139
+ }
140
+
141
+ stripePromise$1 = loadScript(null)["catch"](function (error) {
142
+ // clear cache on error
143
+ stripePromise$1 = null;
144
+ return Promise.reject(error);
145
+ });
146
+ return stripePromise$1;
147
+ }; // Execute our own script injection after a tick to give users time to do their
133
148
  // own script injection.
134
149
 
135
- var stripePromise$1 = Promise.resolve().then(function () {
136
- return loadScript(null);
137
- });
138
- var loadCalled = false;
139
- stripePromise$1["catch"](function (err) {
150
+
151
+ Promise.resolve().then(function () {
152
+ return getStripePromise();
153
+ })["catch"](function (error) {
140
154
  if (!loadCalled) {
141
- console.warn(err);
155
+ console.warn(error);
142
156
  }
143
157
  });
144
158
  var loadStripe = function loadStripe() {
@@ -147,8 +161,9 @@ var loadStripe = function loadStripe() {
147
161
  }
148
162
 
149
163
  loadCalled = true;
150
- var startTime = Date.now();
151
- return stripePromise$1.then(function (maybeStripe) {
164
+ var startTime = Date.now(); // if previous attempts are unsuccessful, will re-load script
165
+
166
+ return getStripePromise().then(function (maybeStripe) {
152
167
  return initStripe(maybeStripe, args, startTime);
153
168
  });
154
169
  };
package/dist/stripe.js CHANGED
@@ -42,7 +42,7 @@ var registerWrapper = function registerWrapper(stripe, startTime) {
42
42
 
43
43
  stripe._registerWrapper({
44
44
  name: 'stripe-js',
45
- version: "2.2.2",
45
+ version: "2.4.0",
46
46
  startTime: startTime
47
47
  });
48
48
  };
@@ -134,15 +134,29 @@ var initStripe = function initStripe(maybeStripe, args, startTime) {
134
134
  return stripe;
135
135
  }; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
136
136
 
137
+ var stripePromise$1;
138
+ var loadCalled = false;
139
+
140
+ var getStripePromise = function getStripePromise() {
141
+ if (stripePromise$1) {
142
+ return stripePromise$1;
143
+ }
144
+
145
+ stripePromise$1 = loadScript(null)["catch"](function (error) {
146
+ // clear cache on error
147
+ stripePromise$1 = null;
148
+ return Promise.reject(error);
149
+ });
150
+ return stripePromise$1;
151
+ }; // Execute our own script injection after a tick to give users time to do their
137
152
  // own script injection.
138
153
 
139
- var stripePromise$1 = Promise.resolve().then(function () {
140
- return loadScript(null);
141
- });
142
- var loadCalled = false;
143
- stripePromise$1["catch"](function (err) {
154
+
155
+ Promise.resolve().then(function () {
156
+ return getStripePromise();
157
+ })["catch"](function (error) {
144
158
  if (!loadCalled) {
145
- console.warn(err);
159
+ console.warn(error);
146
160
  }
147
161
  });
148
162
  var loadStripe = function loadStripe() {
@@ -151,8 +165,9 @@ var loadStripe = function loadStripe() {
151
165
  }
152
166
 
153
167
  loadCalled = true;
154
- var startTime = Date.now();
155
- return stripePromise$1.then(function (maybeStripe) {
168
+ var startTime = Date.now(); // if previous attempts are unsuccessful, will re-load script
169
+
170
+ return getStripePromise().then(function (maybeStripe) {
156
171
  return initStripe(maybeStripe, args, startTime);
157
172
  });
158
173
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stripe/stripe-js",
3
- "version": "2.2.2",
3
+ "version": "2.4.0",
4
4
  "description": "Stripe.js loading utility",
5
5
  "repository": "github:stripe/stripe-js",
6
6
  "main": "dist/stripe.js",
package/src/index.test.ts CHANGED
@@ -150,6 +150,68 @@ describe('Stripe module loader', () => {
150
150
  new Error('Stripe.js not available')
151
151
  );
152
152
  });
153
+
154
+ it('rejects on first load, and succeeds on second load resolving with Stripe object', async () => {
155
+ const {loadStripe} = require(requirePath);
156
+
157
+ // start of first load, expect load failure
158
+ let stripePromise = loadStripe('pk_test_foo');
159
+
160
+ await Promise.resolve();
161
+ dispatchScriptEvent('error');
162
+
163
+ await expect(stripePromise).rejects.toEqual(
164
+ new Error('Failed to load Stripe.js')
165
+ );
166
+
167
+ expect(console.warn).not.toHaveBeenCalled();
168
+
169
+ // start of second load, expect successful load
170
+ stripePromise = loadStripe('pk_test_foo');
171
+
172
+ await new Promise((resolve) => setTimeout(resolve));
173
+ window.Stripe = jest.fn((key) => ({key})) as any;
174
+ dispatchScriptEvent('load');
175
+
176
+ return expect(stripePromise).resolves.toEqual({key: 'pk_test_foo'});
177
+ });
178
+
179
+ it('rejects on first load and second load but succeeds on third load resolving with Stripe object', async () => {
180
+ const {loadStripe} = require(requirePath);
181
+
182
+ // start of first load, expect load failure
183
+ let stripePromise = loadStripe('pk_test_foo');
184
+
185
+ await Promise.resolve();
186
+ dispatchScriptEvent('error');
187
+
188
+ await expect(stripePromise).rejects.toEqual(
189
+ new Error('Failed to load Stripe.js')
190
+ );
191
+
192
+ expect(console.warn).not.toHaveBeenCalled();
193
+
194
+ // start of second load, expect load failure
195
+ stripePromise = loadStripe('pk_test_foo');
196
+
197
+ await Promise.resolve();
198
+ dispatchScriptEvent('error');
199
+
200
+ await expect(stripePromise).rejects.toEqual(
201
+ new Error('Failed to load Stripe.js')
202
+ );
203
+
204
+ expect(console.warn).not.toHaveBeenCalled();
205
+
206
+ // start of third load, expect success
207
+ stripePromise = loadStripe('pk_test_foo');
208
+
209
+ await new Promise((resolve) => setTimeout(resolve));
210
+ window.Stripe = jest.fn((key) => ({key})) as any;
211
+ dispatchScriptEvent('load');
212
+
213
+ return expect(stripePromise).resolves.toEqual({key: 'pk_test_foo'});
214
+ });
153
215
  });
154
216
 
155
217
  describe('loadStripe (index.ts)', () => {
package/src/index.ts CHANGED
@@ -1,22 +1,38 @@
1
+ import {StripeConstructor} from '../types';
1
2
  import {loadScript, initStripe, LoadStripe} from './shared';
2
3
 
3
- // Execute our own script injection after a tick to give users time to do their
4
- // own script injection.
5
- const stripePromise = Promise.resolve().then(() => loadScript(null));
6
-
4
+ let stripePromise: Promise<StripeConstructor | null> | null;
7
5
  let loadCalled = false;
8
6
 
9
- stripePromise.catch((err: Error) => {
10
- if (!loadCalled) {
11
- console.warn(err);
7
+ const getStripePromise: () => Promise<StripeConstructor | null> = () => {
8
+ if (stripePromise) {
9
+ return stripePromise;
12
10
  }
13
- });
11
+
12
+ stripePromise = loadScript(null).catch((error) => {
13
+ // clear cache on error
14
+ stripePromise = null;
15
+ return Promise.reject(error);
16
+ });
17
+ return stripePromise;
18
+ };
19
+
20
+ // Execute our own script injection after a tick to give users time to do their
21
+ // own script injection.
22
+ Promise.resolve()
23
+ .then(() => getStripePromise())
24
+ .catch((error) => {
25
+ if (!loadCalled) {
26
+ console.warn(error);
27
+ }
28
+ });
14
29
 
15
30
  export const loadStripe: LoadStripe = (...args) => {
16
31
  loadCalled = true;
17
32
  const startTime = Date.now();
18
33
 
19
- return stripePromise.then((maybeStripe) =>
34
+ // if previous attempts are unsuccessful, will re-load script
35
+ return getStripePromise().then((maybeStripe) =>
20
36
  initStripe(maybeStripe, args, startTime)
21
37
  );
22
38
  };
@@ -4,6 +4,7 @@ import {
4
4
  StripeElementClasses,
5
5
  StripeElementChangeEvent,
6
6
  } from './base';
7
+ import {CardNetworkBrand} from '../elements-group';
7
8
 
8
9
  export type StripeCardNumberElement = StripeElementBase & {
9
10
  /**
@@ -109,7 +110,7 @@ export type StripeCardNumberElement = StripeElementBase & {
109
110
  * The styles of an `Element` can be dynamically changed using `element.update`.
110
111
  * This method can be used to simulate CSS media queries that automatically adjust the size of elements when viewed on different devices.
111
112
  */
112
- update(options: Partial<StripeCardNumberElementOptions>): void;
113
+ update(options: Partial<StripeCardNumberElementUpdateOptions>): void;
113
114
  };
114
115
 
115
116
  export interface StripeCardNumberElementOptions {
@@ -121,7 +122,47 @@ export interface StripeCardNumberElementOptions {
121
122
 
122
123
  /**
123
124
  * Applies a disabled state to the Element such that user input is not accepted.
124
- * Default is false.
125
+ * Default is `false`.
126
+ */
127
+ disabled?: boolean;
128
+
129
+ /**
130
+ * Show a card brand icon in the Element.
131
+ * Default is `false`.
132
+ */
133
+ showIcon?: boolean;
134
+
135
+ /**
136
+ * Appearance of the brand icon in the Element.
137
+ */
138
+ iconStyle?: 'default' | 'solid';
139
+
140
+ /**
141
+ * Hides and disables the Link Button in the Card Element.
142
+ * Default is `false`.
143
+ */
144
+ disableLink?: boolean;
145
+
146
+ /**
147
+ * Specifies a network preference for Card Brand Choice. The first network in the array which is a valid
148
+ * network on the entered card will be selected as the default in the Card Brand Choice dropdown upon
149
+ * entry of a co-branded card.
150
+ *
151
+ * Default is an empty array, meaning no default selection will be made in the Card Brand choice dropdown.
152
+ */
153
+ preferredNetwork?: Array<CardNetworkBrand>;
154
+ }
155
+
156
+ export interface StripeCardNumberElementUpdateOptions {
157
+ classes?: StripeElementClasses;
158
+
159
+ style?: StripeElementStyle;
160
+
161
+ placeholder?: string;
162
+
163
+ /**
164
+ * Applies a disabled state to the Element such that user input is not accepted.
165
+ * Default is `false`.
125
166
  */
126
167
  disabled?: boolean;
127
168
 
@@ -4,6 +4,7 @@ import {
4
4
  StripeElementClasses,
5
5
  StripeElementChangeEvent,
6
6
  } from './base';
7
+ import {CardNetworkBrand} from '../elements-group';
7
8
 
8
9
  export type StripeCardElement = StripeElementBase & {
9
10
  /**
@@ -143,15 +144,24 @@ export interface StripeCardElementOptions {
143
144
 
144
145
  /**
145
146
  * Applies a disabled state to the Element such that user input is not accepted.
146
- * Default is false.
147
+ * Default is `false`.
147
148
  */
148
149
  disabled?: boolean;
149
150
 
150
151
  /**
151
152
  * Hides and disables the Link Button in the Card Element.
152
- * Default is false.
153
+ * Default is `false`.
153
154
  */
154
155
  disableLink?: boolean;
156
+
157
+ /**
158
+ * Specifies a network preference for Card Brand Choice. The first network in the array which is a valid
159
+ * network on the entered card will be selected as the default in the Card Brand Choice dropdown upon
160
+ * entry of a co-branded card.
161
+ *
162
+ * Default is an empty array, meaning no default selection will be made in the Card Brand choice dropdown.
163
+ */
164
+ preferredNetwork?: Array<CardNetworkBrand>;
155
165
  }
156
166
 
157
167
  export interface StripeCardElementUpdateOptions {
@@ -185,7 +195,7 @@ export interface StripeCardElementUpdateOptions {
185
195
 
186
196
  /**
187
197
  * Applies a disabled state to the Element such that user input is not accepted.
188
- * Default is false.
198
+ * Default is `false`.
189
199
  */
190
200
  disabled?: boolean;
191
201
  }
@@ -617,6 +617,26 @@ export type StripeElementLocale =
617
617
  | 'zh-HK'
618
618
  | 'zh-TW';
619
619
 
620
+ export type CardNetworkBrand =
621
+ | 'accel'
622
+ | 'amex'
623
+ | 'carnet'
624
+ | 'cartes_bancaires'
625
+ | 'diners'
626
+ | 'discover'
627
+ | 'eftpos_au'
628
+ | 'elo'
629
+ | 'girocard'
630
+ | 'interac'
631
+ | 'jcb'
632
+ | 'mastercard'
633
+ | 'nyce'
634
+ | 'pulse'
635
+ | 'rupay'
636
+ | 'star'
637
+ | 'unionpay'
638
+ | 'visa';
639
+
620
640
  type PaymentMethodOptions = {
621
641
  card?: {require_cvc_recollection?: boolean};
622
642
  us_bank_account?: {
@@ -796,6 +816,13 @@ export interface StripeElementsOptionsMode extends BaseStripeElementsOptions {
796
816
  * Either use mode or clientSecret when creating an Elements group
797
817
  */
798
818
  clientSecret?: never;
819
+
820
+ /**
821
+ * The external payment methods to be displayed in the Payment Element that you are already integrated with.
822
+ *
823
+ * @docs https://stripe.com/docs/js/elements_object/create#stripe_elements-options-externalPaymentMethodTypes
824
+ */
825
+ externalPaymentMethodTypes?: string[];
799
826
  }
800
827
 
801
828
  export type StripeElementsOptions =