@stripe/stripe-js 2.2.1 → 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.1",
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.1",
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.1",
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.1",
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.1",
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
  };
@@ -0,0 +1,156 @@
1
+ export type ApplePayRecurringPaymentRequestIntervalUnit =
2
+ | 'year'
3
+ | 'month'
4
+ | 'day'
5
+ | 'hour'
6
+ | 'minute';
7
+
8
+ export interface ApplePayLineItem {
9
+ /**
10
+ * A short, localized description of the line item.
11
+ */
12
+ label: string;
13
+
14
+ /**
15
+ * The amount in the currency's subunit (e.g. cents, yen, etc.)
16
+ */
17
+ amount: number;
18
+ }
19
+
20
+ export type ApplePayRegularBilling = ApplePayLineItem & {
21
+ /**
22
+ * The date of the first payment.
23
+ */
24
+ recurringPaymentStartDate?: Date;
25
+
26
+ /**
27
+ * The date of the final payment.
28
+ */
29
+ recurringPaymentEndDate?: Date;
30
+
31
+ /**
32
+ * The amount of time — in calendar units, such as day, month, or year — that represents a fraction of the total payment interval.
33
+ */
34
+ recurringPaymentIntervalUnit?: ApplePayRecurringPaymentRequestIntervalUnit;
35
+
36
+ /**
37
+ * The number of interval units that make up the total payment interval.
38
+ */
39
+ recurringPaymentIntervalCount?: number;
40
+ };
41
+
42
+ export interface ApplePayRecurringPaymentRequest {
43
+ /**
44
+ * The description of the payment that the customer will see in their Apple Pay wallet.
45
+ */
46
+ paymentDescription: string;
47
+
48
+ /**
49
+ * The URL to manage items related to the recurring payment on your website.
50
+ */
51
+ managementURL: string;
52
+ regularBilling: ApplePayRegularBilling;
53
+
54
+ /**
55
+ * The billing agreement label that is displayed to the customer in the Apple Pay payment interface.
56
+ */
57
+ billingAgreement?: string;
58
+ }
59
+
60
+ export type ApplePayAutomaticReloadBilling = ApplePayLineItem & {
61
+ /**
62
+ * The balance an account reaches before the merchant applies the automatic reload amount.
63
+ */
64
+ automaticReloadPaymentThresholdAmount: number;
65
+ };
66
+
67
+ export interface ApplePayAutomaticReloadPaymentRequest {
68
+ /**
69
+ * The description of the payment that the customer will see in their Apple Pay wallet.
70
+ */
71
+ paymentDescription: string;
72
+
73
+ /**
74
+ * The URL to manage items related to the automatic reload payment on your website.
75
+ */
76
+ managementURL: string;
77
+ automaticReloadBilling: ApplePayAutomaticReloadBilling;
78
+
79
+ /**
80
+ * The billing agreement label that is displayed to the customer in the Apple Pay payment interface.
81
+ */
82
+ billingAgreement?: string;
83
+ }
84
+
85
+ export type ApplePayDeferredBilling = ApplePayLineItem & {
86
+ /**
87
+ * The date, in the future, of the payment.
88
+ */
89
+ deferredPaymentDate: Date;
90
+ };
91
+
92
+ export interface ApplePayDeferredPaymentRequest {
93
+ /**
94
+ * The description of the payment that the customer will see in their Apple Pay wallet.
95
+ */
96
+ paymentDescription: string;
97
+
98
+ /**
99
+ * The URL to manage items related to the deferred payment on your website.
100
+ */
101
+ managementURL: string;
102
+ deferredBilling: ApplePayDeferredBilling;
103
+
104
+ /**
105
+ * The billing agreement label that is displayed to the customer in the Apple Pay payment interface.
106
+ */
107
+ billingAgreement?: string;
108
+
109
+ /**
110
+ * The future date before which the customer can cancel the deferred payment for free.
111
+ */
112
+ freeCancellationDate?: Date;
113
+
114
+ /**
115
+ * The time zone of the free cancellation date.
116
+ *
117
+ * These are [tz](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) timezones such as `America/Los_Angeles`, `Europe/Dublin`, and `Asia/Singapore`.
118
+ */
119
+ freeCancellationDateTimeZone?: string;
120
+ }
121
+
122
+ export type ApplePayOption =
123
+ | {
124
+ recurringPaymentRequest: ApplePayRecurringPaymentRequest;
125
+ deferredPaymentRequest?: null;
126
+ automaticReloadPaymentRequest?: null;
127
+ }
128
+ | {
129
+ recurringPaymentRequest?: null;
130
+ deferredPaymentRequest: ApplePayDeferredPaymentRequest;
131
+ automaticReloadPaymentRequest?: null;
132
+ }
133
+ | {
134
+ recurringPaymentRequest?: null;
135
+ deferredPaymentRequest?: null;
136
+ automaticReloadPaymentRequest: ApplePayAutomaticReloadPaymentRequest;
137
+ }
138
+ | {
139
+ recurringPaymentRequest?: null;
140
+ deferredPaymentRequest?: null;
141
+ automaticReloadPaymentRequest?: null;
142
+ };
143
+
144
+ export type ApplePayUpdateOption =
145
+ | {
146
+ recurringPaymentRequest: ApplePayRecurringPaymentRequest;
147
+ automaticReloadPaymentRequest?: null;
148
+ }
149
+ | {
150
+ recurringPaymentRequest?: null;
151
+ automaticReloadPaymentRequest: ApplePayAutomaticReloadPaymentRequest;
152
+ }
153
+ | {
154
+ recurringPaymentRequest?: null;
155
+ automaticReloadPaymentRequest?: null;
156
+ };
@@ -1,5 +1,6 @@
1
1
  import {StripeElementBase} from './base';
2
2
  import {StripeError} from '../stripe';
3
+ import {ApplePayOption, ApplePayUpdateOption} from './apple-pay';
3
4
 
4
5
  export type StripeExpressCheckoutElement = StripeElementBase & {
5
6
  /**
@@ -362,29 +363,6 @@ export interface StripeExpressCheckoutElementReadyEvent {
362
363
  availablePaymentMethods: undefined | AvailablePaymentMethods;
363
364
  }
364
365
 
365
- export type RecurringPaymentIntervalUnit =
366
- | 'year'
367
- | 'month'
368
- | 'day'
369
- | 'hour'
370
- | 'minute';
371
-
372
- export type ApplePayOption = {
373
- recurringPaymentRequest?: {
374
- paymentDescription: string;
375
- managementURL: string;
376
- regularBilling: {
377
- amount: number;
378
- label: string;
379
- recurringPaymentStartDate?: Date;
380
- recurringPaymentEndDate?: Date;
381
- recurringPaymentIntervalUnit?: RecurringPaymentIntervalUnit;
382
- recurringPaymentIntervalCount?: number;
383
- };
384
- billingAgreement?: string;
385
- };
386
- };
387
-
388
366
  export type ClickResolveDetails = {
389
367
  /**
390
368
  * An array of two-letter ISO country codes representing which countries
@@ -451,6 +429,7 @@ export interface StripeExpressCheckoutElementConfirmEvent {
451
429
  export type ChangeResolveDetails = {
452
430
  lineItems?: Array<LineItem>;
453
431
  shippingRates?: Array<ShippingRate>;
432
+ applePay?: ApplePayUpdateOption;
454
433
  };
455
434
 
456
435
  export interface StripeExpressCheckoutElementShippingAddressChangeEvent {
@@ -1,5 +1,6 @@
1
1
  import {StripeElementBase} from './base';
2
2
  import {StripeError} from '../stripe';
3
+ import {ApplePayOption} from './apple-pay';
3
4
 
4
5
  export type StripePaymentElement = StripeElementBase & {
5
6
  /**
@@ -239,6 +240,11 @@ export interface StripePaymentElementOptions {
239
240
  * Specify a layout to use when rendering a Payment Element.
240
241
  */
241
242
  layout?: Layout | LayoutObject;
243
+
244
+ /**
245
+ * Specify the options to be used when the Apple Pay payment interface opens.
246
+ */
247
+ applePay?: ApplePayOption;
242
248
  }
243
249
 
244
250
  export interface StripePaymentElementChangeEvent {
@@ -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 =
@@ -1,4 +1,5 @@
1
1
  import {Token, PaymentMethod, Source} from '../api';
2
+ import {ApplePayOption, ApplePayUpdateOption} from './elements/apple-pay';
2
3
 
3
4
  export interface PaymentRequest {
4
5
  /**
@@ -154,6 +155,11 @@ export interface PaymentRequestUpdateOptions {
154
155
  */
155
156
 
156
157
  shippingOptions?: PaymentRequestShippingOption[];
158
+
159
+ /**
160
+ * Specify the options to be used when the Apple Pay payment interface opens.
161
+ */
162
+ applePay?: ApplePayOption;
157
163
  }
158
164
 
159
165
  /**
@@ -226,6 +232,11 @@ export interface PaymentRequestOptions {
226
232
  */
227
233
  disableWallets?: PaymentRequestWallet[];
228
234
 
235
+ /**
236
+ * Specify the options to be used when the Apple Pay payment interface opens.
237
+ */
238
+ applePay?: ApplePayOption;
239
+
229
240
  /**
230
241
  * @deprecated
231
242
  * Use disableWallets instead.
@@ -497,6 +508,11 @@ export interface PaymentRequestUpdateDetails {
497
508
  * The first shipping option listed appears in the browser payment interface as the default option.
498
509
  */
499
510
  shippingOptions?: PaymentRequestShippingOption[];
511
+
512
+ /**
513
+ * Specify new options to refresh the Apple Pay payment interface.
514
+ */
515
+ applePay?: ApplePayUpdateOption;
500
516
  }
501
517
 
502
518
  export interface PaymentRequestShippingOptionEvent {