@stripe/connect-js 2.0.5 → 3.0.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/README.md +24 -19
- package/dist/connect.esm.js +34 -18
- package/dist/connect.js +34 -18
- package/dist/index.d.ts +2 -0
- package/dist/pure.d.ts +2 -2
- package/dist/pure.esm.js +35 -18
- package/dist/pure.js +35 -18
- package/dist/shared.d.ts +13 -4
- package/package.json +1 -1
- package/pure.d.ts +2 -2
- package/src/index.ts +10 -6
- package/src/pure.test.ts +11 -4
- package/src/pure.ts +11 -3
- package/src/shared.ts +52 -24
- package/types/index.d.ts +4 -2
- package/types/shared.d.ts +30 -24
- package/dist/src/index.d.ts +0 -3
package/README.md
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
The [Connect.js library](https://stripe.com/docs/connect/get-started-connect-embedded-components) and its supporting API allows you to add connected account dashboard functionality to your website.
|
|
4
4
|
This NPM package contains initialization logic for Connect embedded components along with related types.
|
|
5
5
|
|
|
6
|
-
Calling `
|
|
6
|
+
Calling `loadConnectAndInitialize` always loads the latest version of Connect.js, regardless of which version of `@stripe/connect-js` you use. Updates for this package only impact tooling around the `loadConnectAndInitialize` helper itself and the TypeScript type definitions provided for Connect.js. Updates do not affect runtime availability of features of Connect.js.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
The embedded onboarding component is generally available now. Please refer to our [documentation](https://stripe.com/docs/connect/get-started-connect-embedded-components#account-onboarding) for more information.
|
|
9
|
+
|
|
10
|
+
Note: Majority of Connect embedded components are currently still in beta. Please [contact us](https://stripe.com/docs/connect/get-started-connect-embedded-components#access) to request beta access.
|
|
9
11
|
|
|
10
12
|
## Installation
|
|
11
13
|
|
|
@@ -17,25 +19,25 @@ npm install @stripe/connect-js
|
|
|
17
19
|
|
|
18
20
|
## Documentation
|
|
19
21
|
|
|
20
|
-
- [Connect embedded components](https://stripe.com/docs/connect/get-started-connect-embedded-
|
|
21
|
-
- [Quickstart guide](https://stripe.com/docs/connect/connect-embedded-
|
|
22
|
+
- [Connect embedded components](https://stripe.com/docs/connect/get-started-connect-embedded-components)
|
|
23
|
+
- [Quickstart guide for GA](https://stripe.com/docs/connect/connect-embedded-components/quickstart)
|
|
24
|
+
- [Quickstart guide for beta](https://stripe.com/docs/connect/connect-embedded-components/beta-quickstart)
|
|
22
25
|
|
|
23
26
|
## Usage
|
|
24
27
|
|
|
25
|
-
### `
|
|
26
|
-
|
|
27
|
-
This function returns a `Promise` that resolves with a newly created `StripeConnect`
|
|
28
|
-
object once Connect.js has loaded. If necessary, it will load Connect.js for you by inserting the Connect.js script tag.
|
|
28
|
+
### `loadConnectAndInitialize`
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
This synchronous function takes in a publishable key, a function to retrieve the client secret returned from the [Account Session API](https://stripe.com/docs/api/account_sessions/create), and other [initialization parameters](https://stripe.com/docs/connect/get-started-connect-embedded-components#configuring-connect-js). It returns a `StripeConnectInstance`. If necessary, it will load Connect.js for you by inserting the Connect.js script tag.
|
|
31
31
|
|
|
32
32
|
```js
|
|
33
|
-
import {
|
|
33
|
+
import { loadConnectAndInitialize } from "@stripe/connect-js";
|
|
34
|
+
const fetchClientSecret = async () => {
|
|
35
|
+
// Fetch the AccountSession client secret by making an API call to your service
|
|
36
|
+
};
|
|
34
37
|
|
|
35
|
-
const
|
|
36
|
-
const connectInstance = stripeConnect.initialize({
|
|
38
|
+
const instance = loadConnectAndInitialize({
|
|
37
39
|
publishableKey: "{{pk test123}}",
|
|
38
|
-
|
|
40
|
+
fetchClientSecret: fetchClientSecret
|
|
39
41
|
});
|
|
40
42
|
```
|
|
41
43
|
|
|
@@ -62,22 +64,25 @@ import "@stripe/connect-js";
|
|
|
62
64
|
|
|
63
65
|
Manually add the Connect.js script tag to the `<head>` of each page on your site.
|
|
64
66
|
If an existing script tag is already present, this module will not insert a new
|
|
65
|
-
one. When you call `
|
|
67
|
+
one. When you call `loadConnectAndInitialize`, it will use the existing script tag.
|
|
66
68
|
|
|
67
69
|
```html
|
|
68
70
|
<!-- Somewhere in your site's <head> -->
|
|
69
71
|
<script src="https://connect-js.stripe.com/v0.1/connect.js" async></script>
|
|
70
72
|
```
|
|
71
73
|
|
|
72
|
-
### Importing `
|
|
74
|
+
### Importing `loadConnectAndInitialize` without side effects
|
|
73
75
|
|
|
74
|
-
If you would like to use `
|
|
75
|
-
Connect.js script until `
|
|
76
|
+
If you would like to use `loadConnectAndInitialize` in your application, but defer loading the
|
|
77
|
+
Connect.js script until `loadConnectAndInitialize` is first called, use the alternative
|
|
76
78
|
`@stripe/connect-js/pure` import path:
|
|
77
79
|
|
|
78
80
|
```js
|
|
79
|
-
import {
|
|
81
|
+
import { loadConnectAndInitialize } from "@stripe/connect-js/pure";
|
|
80
82
|
|
|
81
83
|
// Connect.js will not be loaded until `loadConnect` is called
|
|
82
|
-
const
|
|
84
|
+
const instance = loadConnectAndInitialize({
|
|
85
|
+
publishableKey: "{{pk test123}}",
|
|
86
|
+
fetchClientSecret: fetchClientSecret
|
|
87
|
+
});
|
|
83
88
|
```
|
package/dist/connect.esm.js
CHANGED
|
@@ -59,13 +59,38 @@ const loadScript = () => {
|
|
|
59
59
|
});
|
|
60
60
|
return stripePromise;
|
|
61
61
|
};
|
|
62
|
-
const initStripeConnect =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
const initStripeConnect = (stripePromise, initParams) => {
|
|
63
|
+
const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
|
|
64
|
+
return {
|
|
65
|
+
create: tagName => {
|
|
66
|
+
let htmlName = componentNameMapping[tagName];
|
|
67
|
+
if (!htmlName) {
|
|
68
|
+
htmlName = tagName;
|
|
69
|
+
}
|
|
70
|
+
const element = document.createElement(htmlName);
|
|
71
|
+
stripeConnectInstance.then(instance => {
|
|
72
|
+
element.setConnector(instance.connect);
|
|
73
|
+
});
|
|
74
|
+
return element;
|
|
75
|
+
},
|
|
76
|
+
update: updateOptions => {
|
|
77
|
+
stripeConnectInstance.then(instance => {
|
|
78
|
+
instance.update(updateOptions);
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
debugInstance: () => {
|
|
82
|
+
return stripeConnectInstance;
|
|
83
|
+
},
|
|
84
|
+
logout: () => {
|
|
85
|
+
return stripeConnectInstance.then(instance => {
|
|
86
|
+
instance.logout();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
67
90
|
};
|
|
68
91
|
const createWrapper = stripeConnect => {
|
|
92
|
+
window.StripeConnect = window.StripeConnect || {};
|
|
93
|
+
window.StripeConnect.optimizedLoading = true;
|
|
69
94
|
const wrapper = {
|
|
70
95
|
initialize: params => {
|
|
71
96
|
var _a;
|
|
@@ -75,19 +100,10 @@ const createWrapper = stripeConnect => {
|
|
|
75
100
|
sdk: true,
|
|
76
101
|
sdkOptions: {
|
|
77
102
|
// This will be replaced by the npm package version when bundling
|
|
78
|
-
sdkVersion: "
|
|
103
|
+
sdkVersion: "3.0.0"
|
|
79
104
|
}
|
|
80
105
|
})
|
|
81
106
|
}));
|
|
82
|
-
// We wrap create so we can map its different strings to supported components
|
|
83
|
-
const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
|
|
84
|
-
stripeConnectInstance.create = tagName => {
|
|
85
|
-
let htmlName = componentNameMapping[tagName];
|
|
86
|
-
if (!htmlName) {
|
|
87
|
-
htmlName = tagName;
|
|
88
|
-
}
|
|
89
|
-
return oldCreate(htmlName);
|
|
90
|
-
};
|
|
91
107
|
return stripeConnectInstance;
|
|
92
108
|
}
|
|
93
109
|
};
|
|
@@ -103,9 +119,9 @@ stripePromise$1.catch(err => {
|
|
|
103
119
|
console.warn(err);
|
|
104
120
|
}
|
|
105
121
|
});
|
|
106
|
-
const
|
|
122
|
+
const loadConnectAndInitialize = initParams => {
|
|
107
123
|
loadCalled = true;
|
|
108
|
-
return stripePromise$1
|
|
124
|
+
return initStripeConnect(stripePromise$1, initParams);
|
|
109
125
|
};
|
|
110
126
|
|
|
111
|
-
export {
|
|
127
|
+
export { loadConnectAndInitialize };
|
package/dist/connect.js
CHANGED
|
@@ -63,13 +63,38 @@ const loadScript = () => {
|
|
|
63
63
|
});
|
|
64
64
|
return stripePromise;
|
|
65
65
|
};
|
|
66
|
-
const initStripeConnect =
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
const initStripeConnect = (stripePromise, initParams) => {
|
|
67
|
+
const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
|
|
68
|
+
return {
|
|
69
|
+
create: tagName => {
|
|
70
|
+
let htmlName = componentNameMapping[tagName];
|
|
71
|
+
if (!htmlName) {
|
|
72
|
+
htmlName = tagName;
|
|
73
|
+
}
|
|
74
|
+
const element = document.createElement(htmlName);
|
|
75
|
+
stripeConnectInstance.then(instance => {
|
|
76
|
+
element.setConnector(instance.connect);
|
|
77
|
+
});
|
|
78
|
+
return element;
|
|
79
|
+
},
|
|
80
|
+
update: updateOptions => {
|
|
81
|
+
stripeConnectInstance.then(instance => {
|
|
82
|
+
instance.update(updateOptions);
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
debugInstance: () => {
|
|
86
|
+
return stripeConnectInstance;
|
|
87
|
+
},
|
|
88
|
+
logout: () => {
|
|
89
|
+
return stripeConnectInstance.then(instance => {
|
|
90
|
+
instance.logout();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
71
94
|
};
|
|
72
95
|
const createWrapper = stripeConnect => {
|
|
96
|
+
window.StripeConnect = window.StripeConnect || {};
|
|
97
|
+
window.StripeConnect.optimizedLoading = true;
|
|
73
98
|
const wrapper = {
|
|
74
99
|
initialize: params => {
|
|
75
100
|
var _a;
|
|
@@ -79,19 +104,10 @@ const createWrapper = stripeConnect => {
|
|
|
79
104
|
sdk: true,
|
|
80
105
|
sdkOptions: {
|
|
81
106
|
// This will be replaced by the npm package version when bundling
|
|
82
|
-
sdkVersion: "
|
|
107
|
+
sdkVersion: "3.0.0"
|
|
83
108
|
}
|
|
84
109
|
})
|
|
85
110
|
}));
|
|
86
|
-
// We wrap create so we can map its different strings to supported components
|
|
87
|
-
const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
|
|
88
|
-
stripeConnectInstance.create = tagName => {
|
|
89
|
-
let htmlName = componentNameMapping[tagName];
|
|
90
|
-
if (!htmlName) {
|
|
91
|
-
htmlName = tagName;
|
|
92
|
-
}
|
|
93
|
-
return oldCreate(htmlName);
|
|
94
|
-
};
|
|
95
111
|
return stripeConnectInstance;
|
|
96
112
|
}
|
|
97
113
|
};
|
|
@@ -107,9 +123,9 @@ stripePromise$1.catch(err => {
|
|
|
107
123
|
console.warn(err);
|
|
108
124
|
}
|
|
109
125
|
});
|
|
110
|
-
const
|
|
126
|
+
const loadConnectAndInitialize = initParams => {
|
|
111
127
|
loadCalled = true;
|
|
112
|
-
return stripePromise$1
|
|
128
|
+
return initStripeConnect(stripePromise$1, initParams);
|
|
113
129
|
};
|
|
114
130
|
|
|
115
|
-
exports.
|
|
131
|
+
exports.loadConnectAndInitialize = loadConnectAndInitialize;
|
package/dist/index.d.ts
ADDED
package/dist/pure.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const
|
|
1
|
+
import { LoadConnectAndInitialize } from "./shared";
|
|
2
|
+
export declare const loadConnectAndInitialize: LoadConnectAndInitialize;
|
package/dist/pure.esm.js
CHANGED
|
@@ -59,13 +59,38 @@ const loadScript = () => {
|
|
|
59
59
|
});
|
|
60
60
|
return stripePromise;
|
|
61
61
|
};
|
|
62
|
-
const initStripeConnect =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
const initStripeConnect = (stripePromise, initParams) => {
|
|
63
|
+
const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
|
|
64
|
+
return {
|
|
65
|
+
create: tagName => {
|
|
66
|
+
let htmlName = componentNameMapping[tagName];
|
|
67
|
+
if (!htmlName) {
|
|
68
|
+
htmlName = tagName;
|
|
69
|
+
}
|
|
70
|
+
const element = document.createElement(htmlName);
|
|
71
|
+
stripeConnectInstance.then(instance => {
|
|
72
|
+
element.setConnector(instance.connect);
|
|
73
|
+
});
|
|
74
|
+
return element;
|
|
75
|
+
},
|
|
76
|
+
update: updateOptions => {
|
|
77
|
+
stripeConnectInstance.then(instance => {
|
|
78
|
+
instance.update(updateOptions);
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
debugInstance: () => {
|
|
82
|
+
return stripeConnectInstance;
|
|
83
|
+
},
|
|
84
|
+
logout: () => {
|
|
85
|
+
return stripeConnectInstance.then(instance => {
|
|
86
|
+
instance.logout();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
67
90
|
};
|
|
68
91
|
const createWrapper = stripeConnect => {
|
|
92
|
+
window.StripeConnect = window.StripeConnect || {};
|
|
93
|
+
window.StripeConnect.optimizedLoading = true;
|
|
69
94
|
const wrapper = {
|
|
70
95
|
initialize: params => {
|
|
71
96
|
var _a;
|
|
@@ -75,27 +100,19 @@ const createWrapper = stripeConnect => {
|
|
|
75
100
|
sdk: true,
|
|
76
101
|
sdkOptions: {
|
|
77
102
|
// This will be replaced by the npm package version when bundling
|
|
78
|
-
sdkVersion: "
|
|
103
|
+
sdkVersion: "3.0.0"
|
|
79
104
|
}
|
|
80
105
|
})
|
|
81
106
|
}));
|
|
82
|
-
// We wrap create so we can map its different strings to supported components
|
|
83
|
-
const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
|
|
84
|
-
stripeConnectInstance.create = tagName => {
|
|
85
|
-
let htmlName = componentNameMapping[tagName];
|
|
86
|
-
if (!htmlName) {
|
|
87
|
-
htmlName = tagName;
|
|
88
|
-
}
|
|
89
|
-
return oldCreate(htmlName);
|
|
90
|
-
};
|
|
91
107
|
return stripeConnectInstance;
|
|
92
108
|
}
|
|
93
109
|
};
|
|
94
110
|
return wrapper;
|
|
95
111
|
};
|
|
96
112
|
|
|
97
|
-
const
|
|
98
|
-
|
|
113
|
+
const loadConnectAndInitialize = initParams => {
|
|
114
|
+
const maybeConnect = loadScript();
|
|
115
|
+
return initStripeConnect(maybeConnect, initParams);
|
|
99
116
|
};
|
|
100
117
|
|
|
101
|
-
export {
|
|
118
|
+
export { loadConnectAndInitialize };
|
package/dist/pure.js
CHANGED
|
@@ -63,13 +63,38 @@ const loadScript = () => {
|
|
|
63
63
|
});
|
|
64
64
|
return stripePromise;
|
|
65
65
|
};
|
|
66
|
-
const initStripeConnect =
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
const initStripeConnect = (stripePromise, initParams) => {
|
|
67
|
+
const stripeConnectInstance = stripePromise.then(wrapper => wrapper.initialize(initParams));
|
|
68
|
+
return {
|
|
69
|
+
create: tagName => {
|
|
70
|
+
let htmlName = componentNameMapping[tagName];
|
|
71
|
+
if (!htmlName) {
|
|
72
|
+
htmlName = tagName;
|
|
73
|
+
}
|
|
74
|
+
const element = document.createElement(htmlName);
|
|
75
|
+
stripeConnectInstance.then(instance => {
|
|
76
|
+
element.setConnector(instance.connect);
|
|
77
|
+
});
|
|
78
|
+
return element;
|
|
79
|
+
},
|
|
80
|
+
update: updateOptions => {
|
|
81
|
+
stripeConnectInstance.then(instance => {
|
|
82
|
+
instance.update(updateOptions);
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
debugInstance: () => {
|
|
86
|
+
return stripeConnectInstance;
|
|
87
|
+
},
|
|
88
|
+
logout: () => {
|
|
89
|
+
return stripeConnectInstance.then(instance => {
|
|
90
|
+
instance.logout();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
71
94
|
};
|
|
72
95
|
const createWrapper = stripeConnect => {
|
|
96
|
+
window.StripeConnect = window.StripeConnect || {};
|
|
97
|
+
window.StripeConnect.optimizedLoading = true;
|
|
73
98
|
const wrapper = {
|
|
74
99
|
initialize: params => {
|
|
75
100
|
var _a;
|
|
@@ -79,27 +104,19 @@ const createWrapper = stripeConnect => {
|
|
|
79
104
|
sdk: true,
|
|
80
105
|
sdkOptions: {
|
|
81
106
|
// This will be replaced by the npm package version when bundling
|
|
82
|
-
sdkVersion: "
|
|
107
|
+
sdkVersion: "3.0.0"
|
|
83
108
|
}
|
|
84
109
|
})
|
|
85
110
|
}));
|
|
86
|
-
// We wrap create so we can map its different strings to supported components
|
|
87
|
-
const oldCreate = stripeConnectInstance.create.bind(stripeConnectInstance);
|
|
88
|
-
stripeConnectInstance.create = tagName => {
|
|
89
|
-
let htmlName = componentNameMapping[tagName];
|
|
90
|
-
if (!htmlName) {
|
|
91
|
-
htmlName = tagName;
|
|
92
|
-
}
|
|
93
|
-
return oldCreate(htmlName);
|
|
94
|
-
};
|
|
95
111
|
return stripeConnectInstance;
|
|
96
112
|
}
|
|
97
113
|
};
|
|
98
114
|
return wrapper;
|
|
99
115
|
};
|
|
100
116
|
|
|
101
|
-
const
|
|
102
|
-
|
|
117
|
+
const loadConnectAndInitialize = initParams => {
|
|
118
|
+
const maybeConnect = loadScript();
|
|
119
|
+
return initStripeConnect(maybeConnect, initParams);
|
|
103
120
|
};
|
|
104
121
|
|
|
105
|
-
exports.
|
|
122
|
+
exports.loadConnectAndInitialize = loadConnectAndInitialize;
|
package/dist/shared.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type
|
|
1
|
+
import { IStripeConnectInitParams, StripeConnectInstance, ConnectElementTagName } from "../types";
|
|
2
|
+
export type LoadConnectAndInitialize = (initParams: IStripeConnectInitParams) => StripeConnectInstance;
|
|
3
|
+
type ConnectElementHTMLName = "stripe-connect-account-onboarding";
|
|
4
|
+
export declare const componentNameMapping: Record<ConnectElementTagName, ConnectElementHTMLName>;
|
|
5
|
+
type StripeConnectInstanceExtended = StripeConnectInstance & {
|
|
6
|
+
debugInstance: () => Promise<StripeConnectInstance>;
|
|
7
|
+
};
|
|
8
|
+
interface StripeConnectWrapper {
|
|
9
|
+
initialize: (params: IStripeConnectInitParams) => StripeConnectInstance;
|
|
10
|
+
}
|
|
3
11
|
export declare const findScript: () => HTMLScriptElement | null;
|
|
4
|
-
export declare const loadScript: () => Promise<StripeConnectWrapper
|
|
5
|
-
export declare const initStripeConnect: (
|
|
12
|
+
export declare const loadScript: () => Promise<StripeConnectWrapper>;
|
|
13
|
+
export declare const initStripeConnect: (stripePromise: Promise<StripeConnectWrapper>, initParams: IStripeConnectInitParams) => StripeConnectInstanceExtended;
|
|
14
|
+
export {};
|
package/package.json
CHANGED
package/pure.d.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IStripeConnectInitParams, StripeConnectInstance } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
loadScript,
|
|
4
|
+
initStripeConnect,
|
|
5
|
+
LoadConnectAndInitialize
|
|
6
|
+
} from "./shared";
|
|
2
7
|
|
|
3
8
|
// Execute our own script injection after a tick to give users time to do their
|
|
4
9
|
// own script injection.
|
|
@@ -12,10 +17,9 @@ stripePromise.catch((err: Error) => {
|
|
|
12
17
|
}
|
|
13
18
|
});
|
|
14
19
|
|
|
15
|
-
export const
|
|
20
|
+
export const loadConnectAndInitialize: LoadConnectAndInitialize = (
|
|
21
|
+
initParams: IStripeConnectInitParams
|
|
22
|
+
): StripeConnectInstance => {
|
|
16
23
|
loadCalled = true;
|
|
17
|
-
|
|
18
|
-
return stripePromise.then(maybeConnect => initStripeConnect(maybeConnect));
|
|
24
|
+
return initStripeConnect(stripePromise, initParams);
|
|
19
25
|
};
|
|
20
|
-
|
|
21
|
-
export {};
|
package/src/pure.test.ts
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
2
|
|
|
3
|
+
import { IStripeConnectInitParams } from "../types";
|
|
3
4
|
import { SCRIPT_SELECTOR } from "./utils/jestHelpers";
|
|
4
5
|
|
|
5
6
|
describe("pure module", () => {
|
|
6
|
-
test("does not inject the script if
|
|
7
|
+
test("does not inject the script if loadConnectAndInitialize is not called", async () => {
|
|
7
8
|
require("./pure");
|
|
8
9
|
|
|
9
10
|
expect(document.querySelector(SCRIPT_SELECTOR)).toBe(null);
|
|
10
11
|
});
|
|
11
12
|
|
|
12
|
-
test("it injects the script if
|
|
13
|
-
const {
|
|
14
|
-
|
|
13
|
+
test("it injects the script if loadConnectAndInitialize is called", async () => {
|
|
14
|
+
const { loadConnectAndInitialize } = require("./pure");
|
|
15
|
+
const mockInitParams: IStripeConnectInitParams = {
|
|
16
|
+
publishableKey: "pk_123",
|
|
17
|
+
fetchClientSecret: async () => {
|
|
18
|
+
return "secret_123";
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
loadConnectAndInitialize(mockInitParams);
|
|
15
22
|
|
|
16
23
|
expect(document.querySelector(SCRIPT_SELECTOR)).not.toBe(null);
|
|
17
24
|
});
|
package/src/pure.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IStripeConnectInitParams, StripeConnectInstance } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
loadScript,
|
|
4
|
+
initStripeConnect,
|
|
5
|
+
LoadConnectAndInitialize
|
|
6
|
+
} from "./shared";
|
|
2
7
|
|
|
3
|
-
export const
|
|
4
|
-
|
|
8
|
+
export const loadConnectAndInitialize: LoadConnectAndInitialize = (
|
|
9
|
+
initParams: IStripeConnectInitParams
|
|
10
|
+
): StripeConnectInstance => {
|
|
11
|
+
const maybeConnect = loadScript();
|
|
12
|
+
return initStripeConnect(maybeConnect, initParams);
|
|
5
13
|
};
|
package/src/shared.ts
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
|
-
StripeConnectWrapper,
|
|
3
2
|
IStripeConnectInitParams,
|
|
4
|
-
|
|
3
|
+
StripeConnectInstance,
|
|
4
|
+
ConnectElementTagName,
|
|
5
|
+
ConnectHTMLElementRecord
|
|
5
6
|
} from "../types";
|
|
6
7
|
|
|
7
|
-
export type
|
|
8
|
+
export type LoadConnectAndInitialize = (
|
|
9
|
+
initParams: IStripeConnectInitParams
|
|
10
|
+
) => StripeConnectInstance;
|
|
8
11
|
|
|
9
12
|
type ConnectElementHTMLName = "stripe-connect-account-onboarding";
|
|
10
13
|
|
|
11
|
-
const componentNameMapping: Record<
|
|
14
|
+
export const componentNameMapping: Record<
|
|
12
15
|
ConnectElementTagName,
|
|
13
16
|
ConnectElementHTMLName
|
|
14
17
|
> = {
|
|
15
18
|
"account-onboarding": "stripe-connect-account-onboarding"
|
|
16
19
|
};
|
|
17
20
|
|
|
21
|
+
type StripeConnectInstanceExtended = StripeConnectInstance & {
|
|
22
|
+
debugInstance: () => Promise<StripeConnectInstance>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
interface StripeConnectWrapper {
|
|
26
|
+
initialize: (params: IStripeConnectInitParams) => StripeConnectInstance;
|
|
27
|
+
}
|
|
28
|
+
|
|
18
29
|
const EXISTING_SCRIPT_MESSAGE =
|
|
19
30
|
"loadConnect was called but an existing Connect.js script already exists in the document; existing script parameters will be used";
|
|
20
31
|
const V0_URL = "https://connect-js.stripe.com/v1.0/connect.js";
|
|
@@ -44,13 +55,13 @@ const injectScript = (): HTMLScriptElement => {
|
|
|
44
55
|
|
|
45
56
|
let stripePromise: Promise<StripeConnectWrapper> | null = null;
|
|
46
57
|
|
|
47
|
-
export const loadScript = (): Promise<StripeConnectWrapper
|
|
58
|
+
export const loadScript = (): Promise<StripeConnectWrapper> => {
|
|
48
59
|
// Ensure that we only attempt to load Connect.js at most once
|
|
49
60
|
if (stripePromise !== null) {
|
|
50
61
|
return stripePromise;
|
|
51
62
|
}
|
|
52
63
|
|
|
53
|
-
stripePromise = new Promise((resolve, reject) => {
|
|
64
|
+
stripePromise = new Promise<StripeConnectWrapper>((resolve, reject) => {
|
|
54
65
|
if (typeof window === "undefined") {
|
|
55
66
|
reject(
|
|
56
67
|
"ConnectJS won't load when rendering code in the server - it can only be loaded on a browser. This error is expected when loading ConnectJS in SSR environments, like NextJS. It will have no impact in the UI, however if you wish to avoid it, you can switch to the `pure` version of the connect.js loader: https://github.com/stripe/connect-js#importing-loadconnect-without-side-effects."
|
|
@@ -98,16 +109,45 @@ export const loadScript = (): Promise<StripeConnectWrapper | null> => {
|
|
|
98
109
|
};
|
|
99
110
|
|
|
100
111
|
export const initStripeConnect = (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
112
|
+
stripePromise: Promise<StripeConnectWrapper>,
|
|
113
|
+
initParams: IStripeConnectInitParams
|
|
114
|
+
): StripeConnectInstanceExtended => {
|
|
115
|
+
const stripeConnectInstance = stripePromise.then(wrapper =>
|
|
116
|
+
wrapper.initialize(initParams)
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
create: tagName => {
|
|
121
|
+
let htmlName = componentNameMapping[tagName];
|
|
122
|
+
if (!htmlName) {
|
|
123
|
+
htmlName = (tagName as unknown) as ConnectElementHTMLName;
|
|
124
|
+
}
|
|
125
|
+
const element = document.createElement(htmlName);
|
|
126
|
+
stripeConnectInstance.then(instance => {
|
|
127
|
+
(element as any).setConnector((instance as any).connect);
|
|
128
|
+
});
|
|
106
129
|
|
|
107
|
-
|
|
130
|
+
return element as ConnectHTMLElementRecord[typeof tagName];
|
|
131
|
+
},
|
|
132
|
+
update: updateOptions => {
|
|
133
|
+
stripeConnectInstance.then(instance => {
|
|
134
|
+
instance.update(updateOptions);
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
debugInstance: () => {
|
|
138
|
+
return stripeConnectInstance;
|
|
139
|
+
},
|
|
140
|
+
logout: () => {
|
|
141
|
+
return stripeConnectInstance.then(instance => {
|
|
142
|
+
instance.logout();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
108
146
|
};
|
|
109
147
|
|
|
110
148
|
const createWrapper = (stripeConnect: any) => {
|
|
149
|
+
(window as any).StripeConnect = (window as any).StripeConnect || {};
|
|
150
|
+
(window as any).StripeConnect.optimizedLoading = true;
|
|
111
151
|
const wrapper: StripeConnectWrapper = {
|
|
112
152
|
initialize: (params: IStripeConnectInitParams) => {
|
|
113
153
|
const metaOptions = (params as any).metaOptions ?? {};
|
|
@@ -122,18 +162,6 @@ const createWrapper = (stripeConnect: any) => {
|
|
|
122
162
|
}
|
|
123
163
|
}
|
|
124
164
|
});
|
|
125
|
-
|
|
126
|
-
// We wrap create so we can map its different strings to supported components
|
|
127
|
-
const oldCreate = stripeConnectInstance.create.bind(
|
|
128
|
-
stripeConnectInstance
|
|
129
|
-
);
|
|
130
|
-
stripeConnectInstance.create = (tagName: ConnectElementTagName) => {
|
|
131
|
-
let htmlName = componentNameMapping[tagName];
|
|
132
|
-
if (!htmlName) {
|
|
133
|
-
htmlName = tagName as ConnectElementHTMLName;
|
|
134
|
-
}
|
|
135
|
-
return oldCreate(htmlName);
|
|
136
|
-
};
|
|
137
165
|
return stripeConnectInstance;
|
|
138
166
|
}
|
|
139
167
|
};
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IStripeConnectInitParams, StripeConnectInstance } from "../types";
|
|
2
2
|
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const loadConnectAndInitialize: (
|
|
4
|
+
initParams: IStripeConnectInitParams
|
|
5
|
+
) => StripeConnectInstance;
|
|
4
6
|
export * from "./shared";
|
package/types/shared.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export declare type
|
|
1
|
+
export declare type LoadConnectAndInitialize = (
|
|
2
|
+
initParams: IStripeConnectInitParams
|
|
3
|
+
) => StripeConnectInstance;
|
|
2
4
|
|
|
3
5
|
export declare type OverlayOption = "dialog" | "drawer";
|
|
4
6
|
|
|
@@ -335,9 +337,10 @@ export interface IStripeConnectInitParams {
|
|
|
335
337
|
publishableKey: string;
|
|
336
338
|
|
|
337
339
|
/**
|
|
338
|
-
*
|
|
340
|
+
* Function that fetches client secret
|
|
341
|
+
* @returns A promise that resolves with a new client secret.
|
|
339
342
|
*/
|
|
340
|
-
|
|
343
|
+
fetchClientSecret: () => Promise<string>;
|
|
341
344
|
|
|
342
345
|
/**
|
|
343
346
|
* Appearance options for the Connect instance.
|
|
@@ -345,24 +348,33 @@ export interface IStripeConnectInitParams {
|
|
|
345
348
|
*/
|
|
346
349
|
appearance?: AppearanceOptions;
|
|
347
350
|
|
|
348
|
-
/**
|
|
349
|
-
* Callback function that returns a new client secret. Used to support long running sessions and called on account session expiry.
|
|
350
|
-
*/
|
|
351
|
-
refreshClientSecret?: () => Promise<string>;
|
|
352
|
-
|
|
353
351
|
/**
|
|
354
352
|
* The locale to use for the Connect instance.
|
|
355
353
|
*/
|
|
356
354
|
locale?: string;
|
|
357
355
|
}
|
|
358
356
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
357
|
+
type ConnectHTMLElementRecordFallback = {
|
|
358
|
+
[key in string]: HTMLElement | null;
|
|
359
|
+
};
|
|
360
|
+
type ConnectHTMLElementRecordBase = {
|
|
361
|
+
[tagName in ConnectElementTagName]: HTMLElement;
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
interface ConnectHTMLElementRecord
|
|
365
|
+
extends ConnectHTMLElementRecordBase,
|
|
366
|
+
ConnectHTMLElementRecordFallback {
|
|
367
|
+
"account-onboarding": HTMLElement & {
|
|
368
|
+
setFullTermsOfServiceUrl: (termOfServiceUrl: string | undefined) => void;
|
|
369
|
+
setRecipientTermsOfServiceUrl: (
|
|
370
|
+
recipientTermsOfServiceUrl: string | undefined
|
|
371
|
+
) => void;
|
|
372
|
+
setPrivacyPolicyUrl: (privacyPolicyUrl: string | undefined) => void;
|
|
373
|
+
setSkipTermsOfServiceCollection: (
|
|
374
|
+
skipTermsOfServiceCollection: boolean | undefined
|
|
375
|
+
) => void;
|
|
376
|
+
setOnExit: (listener: () => void) => void;
|
|
377
|
+
};
|
|
366
378
|
}
|
|
367
379
|
|
|
368
380
|
export interface StripeConnectInstance {
|
|
@@ -371,7 +383,9 @@ export interface StripeConnectInstance {
|
|
|
371
383
|
* @tagName Name of the Connect component to create.
|
|
372
384
|
* @returns An HTML component corresponding to that connect component
|
|
373
385
|
*/
|
|
374
|
-
create:
|
|
386
|
+
create: <T extends ConnectElementTagName>(
|
|
387
|
+
tagName: T
|
|
388
|
+
) => ConnectHTMLElementRecord[T];
|
|
375
389
|
|
|
376
390
|
/**
|
|
377
391
|
* Updates the Connect instance with new parameters.
|
|
@@ -390,11 +404,3 @@ export interface StripeConnectInstance {
|
|
|
390
404
|
* Tagnames to be used with the `create` method of the Connect instance.
|
|
391
405
|
*/
|
|
392
406
|
export type ConnectElementTagName = "account-onboarding";
|
|
393
|
-
|
|
394
|
-
export declare const findScript: () => HTMLScriptElement | null;
|
|
395
|
-
|
|
396
|
-
export declare const loadScript: () => Promise<any | null>;
|
|
397
|
-
|
|
398
|
-
export declare const initStripeConnect: (
|
|
399
|
-
stripeConnectPromise: StripeConnectWrapper | null
|
|
400
|
-
) => any | null;
|
package/dist/src/index.d.ts
DELETED