@stripe/stripe-js 2.2.2 → 2.3.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.3.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.3.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.3.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.3.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.3.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
  };
@@ -796,6 +796,13 @@ export interface StripeElementsOptionsMode extends BaseStripeElementsOptions {
796
796
  * Either use mode or clientSecret when creating an Elements group
797
797
  */
798
798
  clientSecret?: never;
799
+
800
+ /**
801
+ * The external payment methods to be displayed in the Payment Element that you are already integrated with.
802
+ *
803
+ * @docs https://stripe.com/docs/js/elements_object/create#stripe_elements-options-externalPaymentMethodTypes
804
+ */
805
+ externalPaymentMethodTypes?: string[];
799
806
  }
800
807
 
801
808
  export type StripeElementsOptions =