stripe-pwa-elements 4.0.1 → 4.0.2
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/cjs/factory-tenFFIul.js +1200 -0
- package/dist/cjs/{factory-Cd2VI_N2.js → i18n-FhEOc8vz.js} +6 -1149
- package/dist/cjs/ion-icon_2.cjs.entry.js +723 -0
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/stripe-address-element.cjs.entry.js +5 -4
- package/dist/cjs/stripe-card-element-modal.cjs.entry.js +3 -2
- package/dist/cjs/{stripe-card-element_2.cjs.entry.js → stripe-card-element.cjs.entry.js} +8 -93
- package/dist/cjs/stripe-currency-selector.cjs.entry.js +4 -3
- package/dist/cjs/stripe-elements.cjs.js +1 -1
- package/dist/cjs/stripe-express-checkout-element.cjs.entry.js +24 -5
- package/dist/cjs/stripe-link-authentication-element.cjs.entry.js +4 -3
- package/dist/cjs/stripe-payment-element.cjs.entry.js +12 -124
- package/dist/cjs/stripe-payment-element.helpers-DukP33RI.js +122 -0
- package/dist/cjs/stripe-payment-request-button.cjs.entry.js +2 -2
- package/dist/cjs/stripe-payment-sheet.cjs.entry.js +506 -0
- package/dist/collection/collection-manifest.json +2 -1
- package/dist/collection/components/stripe-card-element/stripe-card-element.js +3 -2
- package/dist/collection/components/stripe-card-element-modal/stripe-card-element-modal.js +4 -3
- package/dist/collection/components/stripe-express-checkout-element/stripe-express-checkout-element.js +18 -0
- package/dist/collection/components/stripe-modal/stripe-modal.css +23 -1
- package/dist/collection/components/stripe-modal/stripe-modal.js +130 -1
- package/dist/collection/components/stripe-payment-request-button/stripe-payment-request-button.js +3 -3
- package/dist/collection/components/stripe-payment-sheet/payment-sheet-state.js +179 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.css +68 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.helpers.js +90 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.js +544 -0
- package/dist/collection/services/express-checkout-element-manager.js +36 -2
- package/dist/collection/services/stripe-service.js +20 -6
- package/dist/collection/utils/focus.js +76 -0
- package/dist/collection/utils/i18n.js +6 -0
- package/dist/components/factory.js +1 -1
- package/dist/components/i18n.js +1 -0
- package/dist/components/stripe-address-element.js +1 -1
- package/dist/components/stripe-card-element-modal.js +1 -1
- package/dist/components/stripe-card-element2.js +1 -1
- package/dist/components/stripe-currency-selector.js +1 -1
- package/dist/components/stripe-express-checkout-element.js +1 -1
- package/dist/components/stripe-link-authentication-element.js +1 -1
- package/dist/components/stripe-modal2.js +1 -1
- package/dist/components/stripe-payment-element.helpers.js +1 -0
- package/dist/components/stripe-payment-element.js +1 -1
- package/dist/components/stripe-payment-request-button2.js +1 -1
- package/dist/components/stripe-payment-sheet.d.ts +11 -0
- package/dist/components/stripe-payment-sheet.js +1 -0
- package/dist/esm/factory-Cd6OGTWN.js +1198 -0
- package/dist/esm/{factory-VTa1Gc94.js → i18n-B6JoQetF.js} +7 -1149
- package/dist/esm/ion-icon_2.entry.js +720 -0
- package/dist/esm/loader.js +1 -1
- package/dist/esm/stripe-address-element.entry.js +2 -1
- package/dist/esm/stripe-card-element-modal.entry.js +3 -2
- package/dist/esm/{stripe-card-element_2.entry.js → stripe-card-element.entry.js} +6 -90
- package/dist/esm/stripe-currency-selector.entry.js +2 -1
- package/dist/esm/stripe-elements.js +1 -1
- package/dist/esm/stripe-express-checkout-element.entry.js +20 -1
- package/dist/esm/stripe-link-authentication-element.entry.js +2 -1
- package/dist/esm/stripe-payment-element.entry.js +4 -116
- package/dist/esm/stripe-payment-element.helpers-CipV87b8.js +117 -0
- package/dist/esm/stripe-payment-request-button.entry.js +2 -2
- package/dist/esm/stripe-payment-sheet.entry.js +504 -0
- package/dist/stripe-elements/{p-ae0c336e.entry.js → p-4891edf0.entry.js} +1 -1
- package/dist/stripe-elements/p-5338bc5e.entry.js +1 -0
- package/dist/stripe-elements/p-6a9c8b7a.entry.js +1 -0
- package/dist/stripe-elements/p-7fe4a287.entry.js +1 -0
- package/dist/stripe-elements/p-915b86ed.entry.js +1 -0
- package/dist/stripe-elements/p-B6JoQetF.js +1 -0
- package/dist/stripe-elements/p-C7MQk9FK.js +1 -0
- package/dist/stripe-elements/p-DFAhQS0n.js +1 -0
- package/dist/stripe-elements/p-b06a254f.entry.js +1 -0
- package/dist/stripe-elements/p-b91c4fa7.entry.js +1 -0
- package/dist/stripe-elements/p-ba791c8f.entry.js +1 -0
- package/dist/stripe-elements/p-e7372ca7.entry.js +1 -0
- package/dist/stripe-elements/p-f892e375.entry.js +1 -0
- package/dist/stripe-elements/stripe-elements.esm.js +1 -1
- package/dist/types/components/stripe-card-element/stripe-card-element.d.ts +2 -1
- package/dist/types/components/stripe-card-element-modal/stripe-card-element-modal.d.ts +2 -1
- package/dist/types/components/stripe-express-checkout-element/stripe-express-checkout-element.d.ts +5 -0
- package/dist/types/components/stripe-modal/stripe-modal.d.ts +43 -0
- package/dist/types/components/stripe-payment-request-button/stripe-payment-request-button.d.ts +1 -1
- package/dist/types/components/stripe-payment-sheet/payment-sheet-state.d.ts +104 -0
- package/dist/types/components/stripe-payment-sheet/stripe-payment-sheet.d.ts +112 -0
- package/dist/types/components/stripe-payment-sheet/stripe-payment-sheet.helpers.d.ts +95 -0
- package/dist/types/components.d.ts +169 -10
- package/dist/types/index.d.ts +2 -0
- package/dist/types/services/interfaces.d.ts +1 -0
- package/dist/types/services/stripe-service.d.ts +7 -2
- package/dist/types/utils/focus.d.ts +15 -0
- package/package.json +5 -3
- package/readme.md +42 -6
- package/dist/cjs/ion-icon.cjs.entry.js +0 -431
- package/dist/components/utils.js +0 -1
- package/dist/esm/ion-icon.entry.js +0 -429
- package/dist/stripe-elements/p-1bd46766.entry.js +0 -1
- package/dist/stripe-elements/p-1c6e0a45.entry.js +0 -1
- package/dist/stripe-elements/p-3f42ca0c.entry.js +0 -1
- package/dist/stripe-elements/p-5cf62945.entry.js +0 -1
- package/dist/stripe-elements/p-74de1556.entry.js +0 -1
- package/dist/stripe-elements/p-76e57d78.entry.js +0 -1
- package/dist/stripe-elements/p-840d499f.entry.js +0 -1
- package/dist/stripe-elements/p-CrjrbGJ3.js +0 -1
- package/dist/stripe-elements/p-cfd8e081.entry.js +0 -1
|
@@ -0,0 +1,1200 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./index-Dkb0SxZc.js');
|
|
4
|
+
var index$1 = require('./index-CYbA1N_k.js');
|
|
5
|
+
var i18n = require('./i18n-FhEOc8vz.js');
|
|
6
|
+
|
|
7
|
+
const appendToMap = (map, propName, value) => {
|
|
8
|
+
let refs = map.get(propName);
|
|
9
|
+
if (!refs) {
|
|
10
|
+
refs = [];
|
|
11
|
+
map.set(propName, refs);
|
|
12
|
+
}
|
|
13
|
+
if (!refs.some((ref) => ref.deref() === value)) {
|
|
14
|
+
refs.push(new WeakRef(value));
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const debounce = (fn, ms) => {
|
|
18
|
+
let timeoutId;
|
|
19
|
+
return (...args) => {
|
|
20
|
+
if (timeoutId) {
|
|
21
|
+
clearTimeout(timeoutId);
|
|
22
|
+
}
|
|
23
|
+
timeoutId = setTimeout(() => {
|
|
24
|
+
timeoutId = 0;
|
|
25
|
+
fn(...args);
|
|
26
|
+
}, ms);
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check if a possible element isConnected.
|
|
32
|
+
* The property might not be there, so we check for it.
|
|
33
|
+
*
|
|
34
|
+
* We want it to return true if isConnected is not a property,
|
|
35
|
+
* otherwise we would remove these elements and would not update.
|
|
36
|
+
*
|
|
37
|
+
* Better leak in Edge than to be useless.
|
|
38
|
+
*/
|
|
39
|
+
const isConnected = (maybeElement) => !('isConnected' in maybeElement) || maybeElement.isConnected;
|
|
40
|
+
const cleanupElements = debounce((map) => {
|
|
41
|
+
for (let key of map.keys()) {
|
|
42
|
+
const refs = map.get(key).filter((ref) => {
|
|
43
|
+
const elm = ref.deref();
|
|
44
|
+
return elm && isConnected(elm);
|
|
45
|
+
});
|
|
46
|
+
map.set(key, refs);
|
|
47
|
+
}
|
|
48
|
+
}, 2_000);
|
|
49
|
+
const core = index.StencilCore;
|
|
50
|
+
const forceUpdate = core.forceUpdate;
|
|
51
|
+
const getRenderingRef = core.getRenderingRef;
|
|
52
|
+
const stencilSubscription = () => {
|
|
53
|
+
if (typeof getRenderingRef !== 'function' || typeof forceUpdate !== 'function') {
|
|
54
|
+
// If we are not in a stencil project, we do nothing.
|
|
55
|
+
// This function is not really exported by @stencil/core.
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
const ensureForceUpdate = forceUpdate;
|
|
59
|
+
const ensureGetRenderingRef = getRenderingRef;
|
|
60
|
+
const elmsToUpdate = new Map();
|
|
61
|
+
return {
|
|
62
|
+
dispose: () => elmsToUpdate.clear(),
|
|
63
|
+
get: (propName) => {
|
|
64
|
+
const elm = ensureGetRenderingRef();
|
|
65
|
+
if (elm) {
|
|
66
|
+
appendToMap(elmsToUpdate, propName, elm);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
set: (propName) => {
|
|
70
|
+
const refs = elmsToUpdate.get(propName);
|
|
71
|
+
if (refs) {
|
|
72
|
+
const nextRefs = refs.filter((ref) => {
|
|
73
|
+
const elm = ref.deref();
|
|
74
|
+
if (!elm)
|
|
75
|
+
return false;
|
|
76
|
+
return ensureForceUpdate(elm);
|
|
77
|
+
});
|
|
78
|
+
elmsToUpdate.set(propName, nextRefs);
|
|
79
|
+
}
|
|
80
|
+
cleanupElements(elmsToUpdate);
|
|
81
|
+
},
|
|
82
|
+
reset: () => {
|
|
83
|
+
elmsToUpdate.forEach((refs) => {
|
|
84
|
+
refs.forEach((ref) => {
|
|
85
|
+
const elm = ref.deref();
|
|
86
|
+
if (elm)
|
|
87
|
+
ensureForceUpdate(elm);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
cleanupElements(elmsToUpdate);
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const unwrap = (val) => (typeof val === 'function' ? val() : val);
|
|
96
|
+
const createObservableMap = (defaultState, shouldUpdate = (a, b) => a !== b) => {
|
|
97
|
+
const resolveDefaultState = () => (unwrap(defaultState) ?? {});
|
|
98
|
+
const initialState = resolveDefaultState();
|
|
99
|
+
let states = new Map(Object.entries(initialState));
|
|
100
|
+
const proxyAvailable = typeof Proxy !== 'undefined';
|
|
101
|
+
const plainState = proxyAvailable ? null : {};
|
|
102
|
+
const handlers = {
|
|
103
|
+
dispose: [],
|
|
104
|
+
get: [],
|
|
105
|
+
set: [],
|
|
106
|
+
reset: [],
|
|
107
|
+
};
|
|
108
|
+
// Track onChange listeners to enable removeListener functionality
|
|
109
|
+
const changeListeners = new Map();
|
|
110
|
+
const reset = () => {
|
|
111
|
+
// When resetting the state, the default state may be a function - unwrap it to invoke it.
|
|
112
|
+
// otherwise, the state won't be properly reset
|
|
113
|
+
states = new Map(Object.entries(resolveDefaultState()));
|
|
114
|
+
if (!proxyAvailable) {
|
|
115
|
+
syncPlainStateKeys();
|
|
116
|
+
}
|
|
117
|
+
handlers.reset.forEach((cb) => cb());
|
|
118
|
+
};
|
|
119
|
+
const dispose = () => {
|
|
120
|
+
// Call first dispose as resetting the state would
|
|
121
|
+
// cause less updates ;)
|
|
122
|
+
handlers.dispose.forEach((cb) => cb());
|
|
123
|
+
reset();
|
|
124
|
+
};
|
|
125
|
+
const get = (propName) => {
|
|
126
|
+
handlers.get.forEach((cb) => cb(propName));
|
|
127
|
+
return states.get(propName);
|
|
128
|
+
};
|
|
129
|
+
const set = (propName, value) => {
|
|
130
|
+
const oldValue = states.get(propName);
|
|
131
|
+
if (shouldUpdate(value, oldValue, propName)) {
|
|
132
|
+
states.set(propName, value);
|
|
133
|
+
if (!proxyAvailable) {
|
|
134
|
+
ensurePlainProperty(propName);
|
|
135
|
+
}
|
|
136
|
+
handlers.set.forEach((cb) => cb(propName, value, oldValue));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const state = (proxyAvailable
|
|
140
|
+
? new Proxy(initialState, {
|
|
141
|
+
get(_, propName) {
|
|
142
|
+
return get(propName);
|
|
143
|
+
},
|
|
144
|
+
ownKeys(_) {
|
|
145
|
+
return Array.from(states.keys());
|
|
146
|
+
},
|
|
147
|
+
getOwnPropertyDescriptor() {
|
|
148
|
+
return {
|
|
149
|
+
enumerable: true,
|
|
150
|
+
configurable: true,
|
|
151
|
+
};
|
|
152
|
+
},
|
|
153
|
+
has(_, propName) {
|
|
154
|
+
return states.has(propName);
|
|
155
|
+
},
|
|
156
|
+
set(_, propName, value) {
|
|
157
|
+
set(propName, value);
|
|
158
|
+
return true;
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
: (() => {
|
|
162
|
+
syncPlainStateKeys();
|
|
163
|
+
return plainState;
|
|
164
|
+
})());
|
|
165
|
+
const on = (eventName, callback) => {
|
|
166
|
+
handlers[eventName].push(callback);
|
|
167
|
+
return () => {
|
|
168
|
+
removeFromArray(handlers[eventName], callback);
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
const onChange = (propName, cb) => {
|
|
172
|
+
const setHandler = (key, newValue) => {
|
|
173
|
+
if (key === propName) {
|
|
174
|
+
cb(newValue);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const resetHandler = () => {
|
|
178
|
+
const snapshot = resolveDefaultState();
|
|
179
|
+
cb(snapshot[propName]);
|
|
180
|
+
};
|
|
181
|
+
// Register the handlers
|
|
182
|
+
const unSet = on('set', setHandler);
|
|
183
|
+
const unReset = on('reset', resetHandler);
|
|
184
|
+
// Track the relationship between the user callback and internal handlers
|
|
185
|
+
changeListeners.set(cb, { setHandler, resetHandler, propName });
|
|
186
|
+
return () => {
|
|
187
|
+
unSet();
|
|
188
|
+
unReset();
|
|
189
|
+
changeListeners.delete(cb);
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
const use = (...subscriptions) => {
|
|
193
|
+
const unsubs = subscriptions.reduce((unsubs, subscription) => {
|
|
194
|
+
if (subscription.set) {
|
|
195
|
+
unsubs.push(on('set', subscription.set));
|
|
196
|
+
}
|
|
197
|
+
if (subscription.get) {
|
|
198
|
+
unsubs.push(on('get', subscription.get));
|
|
199
|
+
}
|
|
200
|
+
if (subscription.reset) {
|
|
201
|
+
unsubs.push(on('reset', subscription.reset));
|
|
202
|
+
}
|
|
203
|
+
if (subscription.dispose) {
|
|
204
|
+
unsubs.push(on('dispose', subscription.dispose));
|
|
205
|
+
}
|
|
206
|
+
return unsubs;
|
|
207
|
+
}, []);
|
|
208
|
+
return () => unsubs.forEach((unsub) => unsub());
|
|
209
|
+
};
|
|
210
|
+
const forceUpdate = (key) => {
|
|
211
|
+
const oldValue = states.get(key);
|
|
212
|
+
handlers.set.forEach((cb) => cb(key, oldValue, oldValue));
|
|
213
|
+
};
|
|
214
|
+
const removeListener = (propName, listener) => {
|
|
215
|
+
const listenerInfo = changeListeners.get(listener);
|
|
216
|
+
if (listenerInfo && listenerInfo.propName === propName) {
|
|
217
|
+
// Remove the specific handlers that were created for this listener
|
|
218
|
+
removeFromArray(handlers.set, listenerInfo.setHandler);
|
|
219
|
+
removeFromArray(handlers.reset, listenerInfo.resetHandler);
|
|
220
|
+
changeListeners.delete(listener);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
function ensurePlainProperty(key) {
|
|
224
|
+
if (proxyAvailable || !plainState) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (Object.prototype.hasOwnProperty.call(plainState, key)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
Object.defineProperty(plainState, key, {
|
|
231
|
+
configurable: true,
|
|
232
|
+
enumerable: true,
|
|
233
|
+
get() {
|
|
234
|
+
return get(key);
|
|
235
|
+
},
|
|
236
|
+
set(value) {
|
|
237
|
+
set(key, value);
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
function syncPlainStateKeys() {
|
|
242
|
+
if (proxyAvailable || !plainState) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const knownKeys = new Set(states.keys());
|
|
246
|
+
for (const key of Object.keys(plainState)) {
|
|
247
|
+
if (!knownKeys.has(key)) {
|
|
248
|
+
delete plainState[key];
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const key of knownKeys) {
|
|
252
|
+
ensurePlainProperty(key);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
state,
|
|
257
|
+
get,
|
|
258
|
+
set,
|
|
259
|
+
on,
|
|
260
|
+
onChange,
|
|
261
|
+
use,
|
|
262
|
+
dispose,
|
|
263
|
+
reset,
|
|
264
|
+
forceUpdate,
|
|
265
|
+
removeListener,
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
const removeFromArray = (array, item) => {
|
|
269
|
+
const index = array.indexOf(item);
|
|
270
|
+
if (index >= 0) {
|
|
271
|
+
array[index] = array[array.length - 1];
|
|
272
|
+
array.length--;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const createStore = (defaultState, shouldUpdate) => {
|
|
277
|
+
const map = createObservableMap(defaultState, shouldUpdate);
|
|
278
|
+
map.use(stencilSubscription());
|
|
279
|
+
return map;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Core Stripe Service
|
|
284
|
+
* Manages Stripe.js initialization and Elements instance only
|
|
285
|
+
* Does NOT manage specific element types (Card, Payment, etc.)
|
|
286
|
+
*/
|
|
287
|
+
class StripeServiceClass {
|
|
288
|
+
constructor() {
|
|
289
|
+
this.store = createStore({
|
|
290
|
+
loadStripeStatus: '',
|
|
291
|
+
applicationName: 'stripe-pwa-elements',
|
|
292
|
+
isCheckoutSession: false,
|
|
293
|
+
});
|
|
294
|
+
/** Incremented on every init so that a slower, older call cannot overwrite newer state */
|
|
295
|
+
this.initGeneration = 0;
|
|
296
|
+
this.changeListeners = new Map();
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Get the reactive state object
|
|
300
|
+
* Components can use this directly in render() for automatic reactivity
|
|
301
|
+
*/
|
|
302
|
+
get state() {
|
|
303
|
+
return this.store.state;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Register a callback for when a specific state property changes
|
|
307
|
+
* @param key - The state property to watch
|
|
308
|
+
* @param callback - Function to call when the property changes
|
|
309
|
+
* @returns Unsubscribe function
|
|
310
|
+
*/
|
|
311
|
+
onChange(key, callback) {
|
|
312
|
+
const dispose = this.store.onChange(key, callback);
|
|
313
|
+
// Track listeners for cleanup
|
|
314
|
+
if (!this.changeListeners.has(key)) {
|
|
315
|
+
this.changeListeners.set(key, []);
|
|
316
|
+
}
|
|
317
|
+
this.changeListeners.get(key).push(dispose);
|
|
318
|
+
return dispose;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Initialize Stripe.js client with Payment Intent mode
|
|
322
|
+
* @param publishableKey - Your Stripe publishable API key
|
|
323
|
+
* @param options - Optional configuration. `clientSecret` is the PaymentIntent / SetupIntent client secret;
|
|
324
|
+
* `elementsOptions` (mode / amount / currency) is used to create Elements when there is no client secret.
|
|
325
|
+
* `clientSecret` takes priority when both are given.
|
|
326
|
+
*/
|
|
327
|
+
async initialize(publishableKey, options) {
|
|
328
|
+
const elementsOptionsKey = (options === null || options === void 0 ? void 0 : options.elementsOptions) != null ? JSON.stringify(options.elementsOptions) : undefined;
|
|
329
|
+
// Capture the previous config before overwriting it, so we can detect real changes
|
|
330
|
+
const isUpToDate = this.store.state.stripe != null &&
|
|
331
|
+
!this.store.state.isCheckoutSession &&
|
|
332
|
+
this.store.state.publishableKey === publishableKey &&
|
|
333
|
+
this.store.state.stripeAccount === (options === null || options === void 0 ? void 0 : options.stripeAccount) &&
|
|
334
|
+
this.intentClientSecret === (options === null || options === void 0 ? void 0 : options.clientSecret) &&
|
|
335
|
+
this.elementsOptionsKey === elementsOptionsKey;
|
|
336
|
+
// Update configuration
|
|
337
|
+
this.store.state.publishableKey = publishableKey;
|
|
338
|
+
this.store.state.isCheckoutSession = false;
|
|
339
|
+
this.intentClientSecret = options === null || options === void 0 ? void 0 : options.clientSecret;
|
|
340
|
+
this.elementsOptionsKey = elementsOptionsKey;
|
|
341
|
+
if ((options === null || options === void 0 ? void 0 : options.stripeAccount) != null && (options === null || options === void 0 ? void 0 : options.stripeAccount) !== '') {
|
|
342
|
+
this.store.state.stripeAccount = options.stripeAccount;
|
|
343
|
+
}
|
|
344
|
+
if ((options === null || options === void 0 ? void 0 : options.applicationName) != null && (options === null || options === void 0 ? void 0 : options.applicationName) !== '') {
|
|
345
|
+
this.store.state.applicationName = options.applicationName;
|
|
346
|
+
}
|
|
347
|
+
// If Stripe.js already loaded with same config, do nothing.
|
|
348
|
+
// Return before bumping the generation, so an in-flight init for this same config is not discarded.
|
|
349
|
+
if (isUpToDate) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const generation = ++this.initGeneration;
|
|
353
|
+
this.store.state.loadStripeStatus = 'loading';
|
|
354
|
+
try {
|
|
355
|
+
const stripe = await index$1.loadStripe(publishableKey, {
|
|
356
|
+
stripeAccount: options === null || options === void 0 ? void 0 : options.stripeAccount,
|
|
357
|
+
});
|
|
358
|
+
if (generation !== this.initGeneration) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
// Register app info
|
|
362
|
+
stripe.registerAppInfo({
|
|
363
|
+
name: this.store.state.applicationName,
|
|
364
|
+
});
|
|
365
|
+
this.store.state.stripe = stripe;
|
|
366
|
+
// Elements must be created with a client secret, or with mode / amount / currency, depending on the element type
|
|
367
|
+
if ((options === null || options === void 0 ? void 0 : options.clientSecret) != null && options.clientSecret !== '') {
|
|
368
|
+
this.store.state.elements = stripe.elements({ clientSecret: options.clientSecret });
|
|
369
|
+
}
|
|
370
|
+
else if ((options === null || options === void 0 ? void 0 : options.elementsOptions) != null) {
|
|
371
|
+
this.store.state.elements = stripe.elements(options.elementsOptions);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
this.store.state.elements = stripe.elements();
|
|
375
|
+
}
|
|
376
|
+
this.store.state.checkout = undefined;
|
|
377
|
+
this.store.state.loadStripeStatus = 'success';
|
|
378
|
+
}
|
|
379
|
+
catch (e) {
|
|
380
|
+
console.error('Failed to load Stripe.js:', e);
|
|
381
|
+
this.store.state.loadStripeStatus = 'failure';
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Initialize Stripe.js client with Checkout Session mode
|
|
386
|
+
* @param publishableKey - Your Stripe publishable API key
|
|
387
|
+
* @param checkoutSessionClientSecret - The client secret from Checkout Session
|
|
388
|
+
* @param options - Optional configuration (`clientSecret` is the PaymentIntent / SetupIntent client secret)
|
|
389
|
+
*/
|
|
390
|
+
async initializeWithCheckoutSession(publishableKey, checkoutSessionClientSecret, options) {
|
|
391
|
+
const generation = ++this.initGeneration;
|
|
392
|
+
// Update configuration
|
|
393
|
+
this.store.state.publishableKey = publishableKey;
|
|
394
|
+
this.store.state.isCheckoutSession = true;
|
|
395
|
+
if ((options === null || options === void 0 ? void 0 : options.stripeAccount) != null && (options === null || options === void 0 ? void 0 : options.stripeAccount) !== '') {
|
|
396
|
+
this.store.state.stripeAccount = options.stripeAccount;
|
|
397
|
+
}
|
|
398
|
+
if ((options === null || options === void 0 ? void 0 : options.applicationName) != null && (options === null || options === void 0 ? void 0 : options.applicationName) !== '') {
|
|
399
|
+
this.store.state.applicationName = options.applicationName;
|
|
400
|
+
}
|
|
401
|
+
this.store.state.loadStripeStatus = 'loading';
|
|
402
|
+
try {
|
|
403
|
+
const stripe = await index$1.loadStripe(publishableKey, {
|
|
404
|
+
stripeAccount: options === null || options === void 0 ? void 0 : options.stripeAccount,
|
|
405
|
+
});
|
|
406
|
+
if (generation !== this.initGeneration) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
// Register app info
|
|
410
|
+
stripe.registerAppInfo({
|
|
411
|
+
name: this.store.state.applicationName,
|
|
412
|
+
});
|
|
413
|
+
this.store.state.stripe = stripe;
|
|
414
|
+
// Initialize Checkout instance instead of Elements
|
|
415
|
+
const checkout = stripe.initCheckoutElementsSdk({
|
|
416
|
+
clientSecret: checkoutSessionClientSecret,
|
|
417
|
+
elementsOptions: options === null || options === void 0 ? void 0 : options.elementsOptions,
|
|
418
|
+
});
|
|
419
|
+
this.store.state.checkout = checkout;
|
|
420
|
+
this.store.state.elements = undefined;
|
|
421
|
+
this.store.state.loadStripeStatus = 'success';
|
|
422
|
+
}
|
|
423
|
+
catch (e) {
|
|
424
|
+
console.error('Failed to initialize Stripe Checkout:', e);
|
|
425
|
+
this.store.state.loadStripeStatus = 'failure';
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Get the current Stripe instance
|
|
430
|
+
*/
|
|
431
|
+
getStripe() {
|
|
432
|
+
return this.store.state.stripe;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Get the current Elements instance
|
|
436
|
+
*/
|
|
437
|
+
getElements() {
|
|
438
|
+
return this.store.state.elements;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Get the current Checkout instance (for Checkout Session mode)
|
|
442
|
+
*/
|
|
443
|
+
getCheckout() {
|
|
444
|
+
return this.store.state.checkout;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Reset the service to initial state
|
|
448
|
+
* Useful for testing and cleanup
|
|
449
|
+
*/
|
|
450
|
+
reset() {
|
|
451
|
+
this.store.reset();
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Dispose of all listeners and reset state
|
|
455
|
+
* Should be called in test cleanup
|
|
456
|
+
*/
|
|
457
|
+
dispose() {
|
|
458
|
+
// Dispose all onChange listeners
|
|
459
|
+
this.changeListeners.forEach(listeners => {
|
|
460
|
+
listeners.forEach(dispose => dispose());
|
|
461
|
+
});
|
|
462
|
+
this.changeListeners.clear();
|
|
463
|
+
// Dispose the store
|
|
464
|
+
this.store.dispose();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Singleton instance of StripeService (for backward compatibility)
|
|
469
|
+
* New code should use ServiceFactory for better testability
|
|
470
|
+
*
|
|
471
|
+
* @deprecated Use ServiceFactory.createStripeService() for better DI support
|
|
472
|
+
*/
|
|
473
|
+
new StripeServiceClass();
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Wait for an element to appear in the DOM
|
|
477
|
+
* @param containerElement - Parent element to search in
|
|
478
|
+
* @param selector - CSS selector
|
|
479
|
+
* @param timeout - Timeout in milliseconds (default: 5000ms)
|
|
480
|
+
* @returns Promise that resolves to the found element or rejects on timeout
|
|
481
|
+
*/
|
|
482
|
+
function findElement(containerElement, selector, timeout = 5000) {
|
|
483
|
+
return new Promise((resolve, reject) => {
|
|
484
|
+
const elem = containerElement.querySelector(selector);
|
|
485
|
+
if (elem != null) {
|
|
486
|
+
return resolve(elem);
|
|
487
|
+
}
|
|
488
|
+
const timeoutId = setTimeout(() => {
|
|
489
|
+
observer.disconnect();
|
|
490
|
+
reject(new Error(`Element "${selector}" not found within ${timeout}ms`));
|
|
491
|
+
}, timeout);
|
|
492
|
+
const observer = new MutationObserver(() => {
|
|
493
|
+
const elem = containerElement.querySelector(selector);
|
|
494
|
+
if (elem != null) {
|
|
495
|
+
clearTimeout(timeoutId);
|
|
496
|
+
observer.disconnect();
|
|
497
|
+
resolve(elem);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
observer.observe(containerElement, {
|
|
501
|
+
childList: true,
|
|
502
|
+
subtree: true,
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Card Element Manager
|
|
509
|
+
* Manages Stripe Card Elements (cardNumber, cardExpiry, cardCvc)
|
|
510
|
+
* with dependency injection of StripeService
|
|
511
|
+
*/
|
|
512
|
+
class CardElementManager {
|
|
513
|
+
/**
|
|
514
|
+
* Constructor with dependency injection
|
|
515
|
+
* @param stripeService - Injected Stripe service instance
|
|
516
|
+
*/
|
|
517
|
+
constructor(stripeService) {
|
|
518
|
+
this.stripeService = stripeService;
|
|
519
|
+
this.store = createStore({
|
|
520
|
+
errorMessage: '',
|
|
521
|
+
errorSource: undefined,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Get current card element state (reactive)
|
|
526
|
+
*/
|
|
527
|
+
getState() {
|
|
528
|
+
return this.store.state;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Initialize and mount card elements to DOM
|
|
532
|
+
* @param containerElement - Parent element containing mount points
|
|
533
|
+
* @returns Promise resolving to card element instances
|
|
534
|
+
*/
|
|
535
|
+
async initialize(containerElement) {
|
|
536
|
+
const elements = this.stripeService.getElements();
|
|
537
|
+
if (elements == null) {
|
|
538
|
+
throw new Error('StripeService not initialized. Call StripeService.initialize() first.');
|
|
539
|
+
}
|
|
540
|
+
// Unmount if already initialized
|
|
541
|
+
if (this.cardElements != null) {
|
|
542
|
+
this.unmount();
|
|
543
|
+
}
|
|
544
|
+
// Create and mount card number element
|
|
545
|
+
const cardNumber = elements.create('cardNumber', {
|
|
546
|
+
placeholder: i18n.i18n.t('Card Number'),
|
|
547
|
+
});
|
|
548
|
+
const cardNumberElement = await findElement(containerElement, '#card-number');
|
|
549
|
+
cardNumber.mount(cardNumberElement);
|
|
550
|
+
cardNumber.on('change', this.handleCardError('cardNumber'));
|
|
551
|
+
// Create and mount card expiry element
|
|
552
|
+
const cardExpiry = elements.create('cardExpiry');
|
|
553
|
+
const cardExpiryElement = await findElement(containerElement, '#card-expiry');
|
|
554
|
+
cardExpiry.mount(cardExpiryElement);
|
|
555
|
+
cardExpiry.on('change', this.handleCardError('cardExpiry'));
|
|
556
|
+
// Create and mount card CVC element
|
|
557
|
+
const cardCVC = elements.create('cardCvc');
|
|
558
|
+
const cardCVCElement = await findElement(containerElement, '#card-cvc');
|
|
559
|
+
cardCVC.mount(cardCVCElement);
|
|
560
|
+
cardCVC.on('change', this.handleCardError('cardCvc'));
|
|
561
|
+
this.cardElements = {
|
|
562
|
+
cardNumber,
|
|
563
|
+
cardExpiry,
|
|
564
|
+
cardCVC,
|
|
565
|
+
};
|
|
566
|
+
return this.cardElements;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Get mounted card element instances
|
|
570
|
+
*/
|
|
571
|
+
getElements() {
|
|
572
|
+
return this.cardElements;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Set error message (triggers reactivity)
|
|
576
|
+
*/
|
|
577
|
+
setError(message) {
|
|
578
|
+
this.store.set('errorMessage', message);
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Clear error message (triggers reactivity)
|
|
582
|
+
*/
|
|
583
|
+
clearError() {
|
|
584
|
+
this.store.set('errorMessage', '');
|
|
585
|
+
this.store.set('errorSource', undefined);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Unmount all card elements
|
|
589
|
+
*/
|
|
590
|
+
unmount() {
|
|
591
|
+
var _a, _b, _c;
|
|
592
|
+
if (this.cardElements == null) {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
(_a = this.cardElements.cardNumber) === null || _a === void 0 ? void 0 : _a.unmount();
|
|
596
|
+
(_b = this.cardElements.cardExpiry) === null || _b === void 0 ? void 0 : _b.unmount();
|
|
597
|
+
(_c = this.cardElements.cardCVC) === null || _c === void 0 ? void 0 : _c.unmount();
|
|
598
|
+
this.cardElements = undefined;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Handle card element errors
|
|
602
|
+
* Only clears error if it's from the same element type
|
|
603
|
+
* Uses store.set() to trigger reactivity
|
|
604
|
+
*/
|
|
605
|
+
handleCardError(elementType) {
|
|
606
|
+
return ({ error }) => {
|
|
607
|
+
if (error) {
|
|
608
|
+
this.store.set('errorMessage', error.message);
|
|
609
|
+
this.store.set('errorSource', elementType);
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
// Only clear error if it originated from the same element
|
|
613
|
+
if (this.store.state.errorSource === elementType) {
|
|
614
|
+
this.store.set('errorMessage', '');
|
|
615
|
+
this.store.set('errorSource', undefined);
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Payment Element Manager
|
|
623
|
+
* Manages Stripe Payment Element (unified payment form)
|
|
624
|
+
* with dependency injection of StripeService
|
|
625
|
+
* Supports both Payment Intent mode and Checkout Session mode
|
|
626
|
+
*/
|
|
627
|
+
class PaymentElementManager {
|
|
628
|
+
/**
|
|
629
|
+
* Constructor with dependency injection
|
|
630
|
+
* @param stripeService - Injected Stripe service instance
|
|
631
|
+
*/
|
|
632
|
+
constructor(stripeService) {
|
|
633
|
+
this.stripeService = stripeService;
|
|
634
|
+
this.store = createStore({
|
|
635
|
+
errorMessage: '',
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Get current payment element state (reactive)
|
|
640
|
+
*/
|
|
641
|
+
getState() {
|
|
642
|
+
return this.store.state;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Initialize and mount payment element to DOM
|
|
646
|
+
* Supports both Payment Intent mode and Checkout Session mode
|
|
647
|
+
* @param containerElement - Parent element containing mount point
|
|
648
|
+
* @param options - Optional payment element options
|
|
649
|
+
* @returns Promise resolving to payment element instance
|
|
650
|
+
*/
|
|
651
|
+
async initialize(containerElement, options) {
|
|
652
|
+
const isCheckoutSession = this.stripeService.state.isCheckoutSession;
|
|
653
|
+
// Unmount if already initialized
|
|
654
|
+
if (this.paymentElement != null) {
|
|
655
|
+
this.unmount();
|
|
656
|
+
}
|
|
657
|
+
if (isCheckoutSession) {
|
|
658
|
+
// Checkout Session mode: use checkout.createPaymentElement()
|
|
659
|
+
const checkout = this.stripeService.getCheckout();
|
|
660
|
+
if (checkout == null) {
|
|
661
|
+
throw new Error('StripeService not initialized with Checkout Session. Call StripeService.initializeWithCheckoutSession() first.');
|
|
662
|
+
}
|
|
663
|
+
// Create payment element using Checkout instance
|
|
664
|
+
this.paymentElement = checkout.createPaymentElement(options);
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
// Payment Intent mode: use elements.create('payment')
|
|
668
|
+
const elements = this.stripeService.getElements();
|
|
669
|
+
if (elements == null) {
|
|
670
|
+
throw new Error('StripeService not initialized. Call StripeService.initialize() first.');
|
|
671
|
+
}
|
|
672
|
+
// Create payment element with options
|
|
673
|
+
const elementOptions = options || {};
|
|
674
|
+
// Create the payment element
|
|
675
|
+
this.paymentElement = elements.create('payment', elementOptions);
|
|
676
|
+
}
|
|
677
|
+
// Find mount point and mount
|
|
678
|
+
const paymentElementContainer = await findElement(containerElement, '#payment-element');
|
|
679
|
+
this.paymentElement.mount(paymentElementContainer);
|
|
680
|
+
// Note: Payment Element errors are handled during submission
|
|
681
|
+
// Use setError() method to manually set errors if needed
|
|
682
|
+
return this.paymentElement;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Get mounted payment element instance
|
|
686
|
+
*/
|
|
687
|
+
getElement() {
|
|
688
|
+
return this.paymentElement;
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Set error message (triggers reactivity)
|
|
692
|
+
*/
|
|
693
|
+
setError(message) {
|
|
694
|
+
this.store.set('errorMessage', message);
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Clear error message (triggers reactivity)
|
|
698
|
+
*/
|
|
699
|
+
clearError() {
|
|
700
|
+
this.store.set('errorMessage', '');
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Unmount payment element
|
|
704
|
+
*/
|
|
705
|
+
unmount() {
|
|
706
|
+
if (this.paymentElement == null) {
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
this.paymentElement.unmount();
|
|
710
|
+
this.paymentElement = undefined;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Address Element Manager
|
|
716
|
+
* Manages Stripe Address Element
|
|
717
|
+
* with dependency injection of StripeService
|
|
718
|
+
*/
|
|
719
|
+
class AddressElementManager {
|
|
720
|
+
/**
|
|
721
|
+
* Constructor with dependency injection
|
|
722
|
+
* @param stripeService - Injected Stripe service instance
|
|
723
|
+
*/
|
|
724
|
+
constructor(stripeService) {
|
|
725
|
+
this.stripeService = stripeService;
|
|
726
|
+
this.store = createStore({
|
|
727
|
+
errorMessage: '',
|
|
728
|
+
isComplete: false,
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Get current address element state (reactive)
|
|
733
|
+
*/
|
|
734
|
+
getState() {
|
|
735
|
+
return this.store.state;
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Initialize and mount address element to DOM
|
|
739
|
+
* @param containerElement - Parent element containing mount point
|
|
740
|
+
* @param options - Address element options
|
|
741
|
+
* @returns Promise resolving to address element instance
|
|
742
|
+
*/
|
|
743
|
+
async initialize(containerElement, options) {
|
|
744
|
+
const elements = this.stripeService.getElements();
|
|
745
|
+
if (elements == null) {
|
|
746
|
+
throw new Error('StripeService not initialized. Call StripeService.initialize() first.');
|
|
747
|
+
}
|
|
748
|
+
// Unmount if already initialized
|
|
749
|
+
if (this.addressElement != null) {
|
|
750
|
+
this.unmount();
|
|
751
|
+
}
|
|
752
|
+
// Create and mount address element
|
|
753
|
+
this.addressElement = elements.create('address', options || { mode: 'billing' });
|
|
754
|
+
const addressElementContainer = await findElement(containerElement, '#address-element');
|
|
755
|
+
this.addressElement.mount(addressElementContainer);
|
|
756
|
+
// Listen for changes
|
|
757
|
+
this.addressElement.on('change', event => {
|
|
758
|
+
// StripeAddressElementChangeEvent doesn't have an error property
|
|
759
|
+
// Errors are handled through the complete property and validation
|
|
760
|
+
this.store.set('errorMessage', '');
|
|
761
|
+
this.store.set('isComplete', event.complete);
|
|
762
|
+
});
|
|
763
|
+
return this.addressElement;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Get mounted address element instance
|
|
767
|
+
*/
|
|
768
|
+
getElement() {
|
|
769
|
+
return this.addressElement;
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Set error message (triggers reactivity)
|
|
773
|
+
*/
|
|
774
|
+
setError(message) {
|
|
775
|
+
this.store.set('errorMessage', message);
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Clear error message (triggers reactivity)
|
|
779
|
+
*/
|
|
780
|
+
clearError() {
|
|
781
|
+
this.store.set('errorMessage', '');
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Unmount address element
|
|
785
|
+
*/
|
|
786
|
+
unmount() {
|
|
787
|
+
if (this.addressElement == null) {
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
this.addressElement.unmount();
|
|
791
|
+
this.addressElement = undefined;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Link Authentication Element Manager
|
|
797
|
+
* Manages Stripe Link Authentication Element for email collection and Link authentication
|
|
798
|
+
* with dependency injection of StripeService
|
|
799
|
+
*/
|
|
800
|
+
class LinkAuthenticationElementManager {
|
|
801
|
+
/**
|
|
802
|
+
* Constructor with dependency injection
|
|
803
|
+
* @param stripeService - Injected Stripe service instance
|
|
804
|
+
*/
|
|
805
|
+
constructor(stripeService) {
|
|
806
|
+
this.stripeService = stripeService;
|
|
807
|
+
this.store = createStore({
|
|
808
|
+
errorMessage: '',
|
|
809
|
+
email: undefined,
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Get current link authentication element state (reactive)
|
|
814
|
+
*/
|
|
815
|
+
getState() {
|
|
816
|
+
return this.store.state;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Register state change listener
|
|
820
|
+
* @param key - State property to watch
|
|
821
|
+
* @param callback - Callback function invoked when property changes
|
|
822
|
+
* @returns Unsubscribe function
|
|
823
|
+
*/
|
|
824
|
+
onChange(key, callback) {
|
|
825
|
+
return this.store.onChange(key, callback);
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Initialize and mount link authentication element to DOM
|
|
829
|
+
* @param containerElement - Parent element containing mount point
|
|
830
|
+
* @param options - Optional configuration for Link Authentication Element
|
|
831
|
+
* @returns Promise resolving to link authentication element instance
|
|
832
|
+
*/
|
|
833
|
+
async initialize(containerElement, options) {
|
|
834
|
+
const elements = this.stripeService.getElements();
|
|
835
|
+
if (elements == null) {
|
|
836
|
+
throw new Error('StripeService not initialized. Call StripeService.initialize() first.');
|
|
837
|
+
}
|
|
838
|
+
// Unmount if already initialized
|
|
839
|
+
if (this.linkAuthenticationElement != null) {
|
|
840
|
+
this.unmount();
|
|
841
|
+
}
|
|
842
|
+
// Create link authentication element
|
|
843
|
+
const linkAuthentication = elements.create('linkAuthentication', options);
|
|
844
|
+
const linkAuthenticationElementDiv = await findElement(containerElement, '#link-authentication-element');
|
|
845
|
+
linkAuthentication.mount(linkAuthenticationElementDiv);
|
|
846
|
+
// Listen for change events
|
|
847
|
+
linkAuthentication.on('change', event => {
|
|
848
|
+
var _a;
|
|
849
|
+
// Unconditionally set email from event.value.email
|
|
850
|
+
// This handles all cases: valid email, empty string, or undefined
|
|
851
|
+
this.store.set('email', (_a = event.value) === null || _a === void 0 ? void 0 : _a.email);
|
|
852
|
+
});
|
|
853
|
+
this.linkAuthenticationElement = linkAuthentication;
|
|
854
|
+
return this.linkAuthenticationElement;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Get mounted link authentication element instance
|
|
858
|
+
*/
|
|
859
|
+
getElement() {
|
|
860
|
+
return this.linkAuthenticationElement;
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Set error message (triggers reactivity)
|
|
864
|
+
*/
|
|
865
|
+
setError(message) {
|
|
866
|
+
this.store.set('errorMessage', message);
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Clear error message (triggers reactivity)
|
|
870
|
+
*/
|
|
871
|
+
clearError() {
|
|
872
|
+
this.store.set('errorMessage', '');
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Unmount link authentication element
|
|
876
|
+
*/
|
|
877
|
+
unmount() {
|
|
878
|
+
if (this.linkAuthenticationElement == null) {
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
this.linkAuthenticationElement.unmount();
|
|
882
|
+
this.linkAuthenticationElement = undefined;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Currency Selector Element Manager
|
|
888
|
+
* Manages Stripe Currency Selector Element
|
|
889
|
+
* with dependency injection of StripeService
|
|
890
|
+
*/
|
|
891
|
+
class CurrencySelectorElementManager {
|
|
892
|
+
/**
|
|
893
|
+
* Constructor with dependency injection
|
|
894
|
+
* @param stripeService - Injected Stripe service instance
|
|
895
|
+
*/
|
|
896
|
+
constructor(stripeService) {
|
|
897
|
+
this.stripeService = stripeService;
|
|
898
|
+
this.store = createStore({
|
|
899
|
+
errorMessage: '',
|
|
900
|
+
selectedCurrency: undefined,
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Get current currency selector element state (reactive)
|
|
905
|
+
*/
|
|
906
|
+
getState() {
|
|
907
|
+
return this.store.state;
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Initialize and mount currency selector element to DOM
|
|
911
|
+
* @param containerElement - Parent element containing mount point
|
|
912
|
+
* @param options - Currency selector element options
|
|
913
|
+
* @returns Promise resolving to currency selector element instance
|
|
914
|
+
* Note: StripeCurrencySelectorElementOptions is not yet exported in @stripe/stripe-js v8.6.0
|
|
915
|
+
*/
|
|
916
|
+
async initialize(containerElement, options) {
|
|
917
|
+
const elements = this.stripeService.getElements();
|
|
918
|
+
if (elements == null) {
|
|
919
|
+
throw new Error('StripeService not initialized. Call StripeService.initialize() first.');
|
|
920
|
+
}
|
|
921
|
+
// Unmount if already initialized
|
|
922
|
+
if (this.currencySelectorElement != null) {
|
|
923
|
+
this.unmount();
|
|
924
|
+
}
|
|
925
|
+
// Find mount point first to avoid element leak if findElement throws
|
|
926
|
+
const currencySelectorElementContainer = await findElement(containerElement, '#currency-selector');
|
|
927
|
+
// Create and mount currency selector element
|
|
928
|
+
// Note: 'currencySelector' type is not yet in @stripe/stripe-js v8.6.0 type definitions
|
|
929
|
+
// but is documented in Stripe's Currency Selector Element documentation
|
|
930
|
+
this.currencySelectorElement = elements.create('currencySelector', options || {});
|
|
931
|
+
this.currencySelectorElement.mount(currencySelectorElementContainer);
|
|
932
|
+
// Listen for changes
|
|
933
|
+
// Note: The 'change' event type is not yet in @stripe/stripe-js type definitions
|
|
934
|
+
// but is documented in Stripe's Currency Selector Element documentation
|
|
935
|
+
this.currencySelectorElement.on('change', (event) => {
|
|
936
|
+
var _a;
|
|
937
|
+
// Clear error on change
|
|
938
|
+
this.store.set('errorMessage', '');
|
|
939
|
+
// Update selected currency if available
|
|
940
|
+
if ((_a = event === null || event === void 0 ? void 0 : event.value) === null || _a === void 0 ? void 0 : _a.currency) {
|
|
941
|
+
this.store.set('selectedCurrency', event.value.currency);
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
return this.currencySelectorElement;
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* Get mounted currency selector element instance
|
|
948
|
+
*/
|
|
949
|
+
getElement() {
|
|
950
|
+
return this.currencySelectorElement;
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* Set error message (triggers reactivity)
|
|
954
|
+
*/
|
|
955
|
+
setError(message) {
|
|
956
|
+
this.store.set('errorMessage', message);
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Clear error message (triggers reactivity)
|
|
960
|
+
*/
|
|
961
|
+
clearError() {
|
|
962
|
+
this.store.set('errorMessage', '');
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Unmount currency selector element
|
|
966
|
+
*/
|
|
967
|
+
unmount() {
|
|
968
|
+
if (this.currencySelectorElement == null) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
this.currencySelectorElement.unmount();
|
|
972
|
+
this.currencySelectorElement = undefined;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
var __rest = (undefined && undefined.__rest) || function (s, e) {
|
|
977
|
+
var t = {};
|
|
978
|
+
for (var p in s)
|
|
979
|
+
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
980
|
+
t[p] = s[p];
|
|
981
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
982
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
983
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
984
|
+
t[p[i]] = s[p[i]];
|
|
985
|
+
}
|
|
986
|
+
return t;
|
|
987
|
+
};
|
|
988
|
+
/**
|
|
989
|
+
* Express Checkout Element Manager
|
|
990
|
+
* Manages Stripe Express Checkout Element
|
|
991
|
+
* with dependency injection of StripeService
|
|
992
|
+
*/
|
|
993
|
+
class ExpressCheckoutElementManager {
|
|
994
|
+
/**
|
|
995
|
+
* Constructor with dependency injection
|
|
996
|
+
* @param stripeService - Injected Stripe service instance
|
|
997
|
+
*/
|
|
998
|
+
constructor(stripeService) {
|
|
999
|
+
this.stripeService = stripeService;
|
|
1000
|
+
this.store = createStore({
|
|
1001
|
+
errorMessage: '',
|
|
1002
|
+
isReady: false,
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Get current express checkout element state (reactive)
|
|
1007
|
+
*/
|
|
1008
|
+
getState() {
|
|
1009
|
+
return this.store.state;
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Initialize and mount express checkout element to DOM
|
|
1013
|
+
* @param containerElement - Parent element containing mount point
|
|
1014
|
+
* @param options - Express Checkout Element configuration options
|
|
1015
|
+
* @param eventHandlers - Event handlers for element events
|
|
1016
|
+
* @returns Promise resolving to express checkout element instance
|
|
1017
|
+
*/
|
|
1018
|
+
async initialize(containerElement, options, eventHandlers) {
|
|
1019
|
+
const elements = this.stripeService.getElements();
|
|
1020
|
+
if (elements == null) {
|
|
1021
|
+
throw new Error('StripeService not initialized. Call StripeService.initialize() first.');
|
|
1022
|
+
}
|
|
1023
|
+
// Unmount if already initialized
|
|
1024
|
+
if (this.expressCheckoutElement != null) {
|
|
1025
|
+
this.unmount();
|
|
1026
|
+
}
|
|
1027
|
+
// mode / amount / currency belong to stripe.elements(), not to elements.create('expressCheckout')
|
|
1028
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1029
|
+
const restOptions = __rest(options, ["mode", "amount", "currency"]);
|
|
1030
|
+
const createOptions = Object.assign({}, restOptions);
|
|
1031
|
+
// Convert buttonHeight from string to number if present
|
|
1032
|
+
if (createOptions.buttonHeight && typeof createOptions.buttonHeight === 'string') {
|
|
1033
|
+
const height = parseInt(createOptions.buttonHeight.replace('px', ''), 10);
|
|
1034
|
+
if (!isNaN(height)) {
|
|
1035
|
+
createOptions.buttonHeight = height;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
// Create express checkout element
|
|
1039
|
+
// Note: 'expressCheckout' is not yet in the official Stripe type definitions
|
|
1040
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1041
|
+
const expressCheckout = elements.create('expressCheckout', createOptions);
|
|
1042
|
+
// Find the mount point
|
|
1043
|
+
const mountPoint = await findElement(containerElement, '#express-checkout-element');
|
|
1044
|
+
// Set up event handlers before mounting
|
|
1045
|
+
if ((eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onConfirm) != null) {
|
|
1046
|
+
expressCheckout.on('confirm', eventHandlers.onConfirm);
|
|
1047
|
+
}
|
|
1048
|
+
if ((eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onClick) != null) {
|
|
1049
|
+
expressCheckout.on('click', eventHandlers.onClick);
|
|
1050
|
+
}
|
|
1051
|
+
if ((eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onCancel) != null) {
|
|
1052
|
+
expressCheckout.on('cancel', eventHandlers.onCancel);
|
|
1053
|
+
}
|
|
1054
|
+
if ((eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onShippingAddressChange) != null) {
|
|
1055
|
+
expressCheckout.on('shippingaddresschange', eventHandlers.onShippingAddressChange);
|
|
1056
|
+
}
|
|
1057
|
+
if ((eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onShippingRateChange) != null) {
|
|
1058
|
+
expressCheckout.on('shippingratechange', eventHandlers.onShippingRateChange);
|
|
1059
|
+
}
|
|
1060
|
+
// Add ready event handler
|
|
1061
|
+
expressCheckout.on('ready', () => {
|
|
1062
|
+
this.store.set('isReady', true);
|
|
1063
|
+
});
|
|
1064
|
+
// Mount the element
|
|
1065
|
+
expressCheckout.mount(mountPoint);
|
|
1066
|
+
this.expressCheckoutElement = expressCheckout;
|
|
1067
|
+
return this.expressCheckoutElement;
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Get mounted express checkout element instance
|
|
1071
|
+
*/
|
|
1072
|
+
getElement() {
|
|
1073
|
+
return this.expressCheckoutElement;
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Update element options dynamically
|
|
1077
|
+
* @param options - Partial options to update
|
|
1078
|
+
*/
|
|
1079
|
+
update(options) {
|
|
1080
|
+
var _a;
|
|
1081
|
+
if (this.expressCheckoutElement == null) {
|
|
1082
|
+
console.warn('Express Checkout Element not initialized. Cannot update options.');
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
// mode / amount / currency are Elements group options, so they are updated with elements.update()
|
|
1086
|
+
const { mode, amount, currency } = options, elementOptions = __rest(options, ["mode", "amount", "currency"]);
|
|
1087
|
+
const groupOptions = {};
|
|
1088
|
+
if (mode !== undefined) {
|
|
1089
|
+
groupOptions.mode = mode;
|
|
1090
|
+
}
|
|
1091
|
+
if (amount !== undefined) {
|
|
1092
|
+
groupOptions.amount = amount;
|
|
1093
|
+
}
|
|
1094
|
+
if (currency !== undefined) {
|
|
1095
|
+
groupOptions.currency = currency;
|
|
1096
|
+
}
|
|
1097
|
+
if (Object.keys(groupOptions).length > 0) {
|
|
1098
|
+
(_a = this.stripeService.getElements()) === null || _a === void 0 ? void 0 : _a.update(groupOptions);
|
|
1099
|
+
}
|
|
1100
|
+
// Convert buttonHeight from string to number if present
|
|
1101
|
+
const updateOptions = Object.assign({}, elementOptions);
|
|
1102
|
+
if (updateOptions.buttonHeight && typeof updateOptions.buttonHeight === 'string') {
|
|
1103
|
+
const height = parseInt(updateOptions.buttonHeight.replace('px', ''), 10);
|
|
1104
|
+
if (!isNaN(height)) {
|
|
1105
|
+
updateOptions.buttonHeight = height;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
if (Object.keys(updateOptions).length === 0) {
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1112
|
+
this.expressCheckoutElement.update(updateOptions);
|
|
1113
|
+
}
|
|
1114
|
+
/**
|
|
1115
|
+
* Set error message (triggers reactivity)
|
|
1116
|
+
*/
|
|
1117
|
+
setError(message) {
|
|
1118
|
+
this.store.set('errorMessage', message);
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Clear error message (triggers reactivity)
|
|
1122
|
+
*/
|
|
1123
|
+
clearError() {
|
|
1124
|
+
this.store.set('errorMessage', '');
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Unmount express checkout element
|
|
1128
|
+
*/
|
|
1129
|
+
unmount() {
|
|
1130
|
+
if (this.expressCheckoutElement == null) {
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
this.expressCheckoutElement.unmount();
|
|
1134
|
+
this.expressCheckoutElement = undefined;
|
|
1135
|
+
this.store.set('isReady', false);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* Service Factory
|
|
1141
|
+
* Creates service instances with proper dependency injection
|
|
1142
|
+
* Can be mocked in tests for better testability
|
|
1143
|
+
*/
|
|
1144
|
+
class ServiceFactory {
|
|
1145
|
+
/**
|
|
1146
|
+
* Create a new StripeService instance
|
|
1147
|
+
*/
|
|
1148
|
+
createStripeService() {
|
|
1149
|
+
return new StripeServiceClass();
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Create a new CardElementManager instance
|
|
1153
|
+
* @param stripeService - Injected StripeService dependency
|
|
1154
|
+
*/
|
|
1155
|
+
createCardElementManager(stripeService) {
|
|
1156
|
+
return new CardElementManager(stripeService);
|
|
1157
|
+
}
|
|
1158
|
+
/**
|
|
1159
|
+
* Create a new PaymentElementManager instance
|
|
1160
|
+
* @param stripeService - Injected StripeService dependency
|
|
1161
|
+
*/
|
|
1162
|
+
createPaymentElementManager(stripeService) {
|
|
1163
|
+
return new PaymentElementManager(stripeService);
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Create a new AddressElementManager instance
|
|
1167
|
+
* @param stripeService - Injected StripeService dependency
|
|
1168
|
+
*/
|
|
1169
|
+
createAddressElementManager(stripeService) {
|
|
1170
|
+
return new AddressElementManager(stripeService);
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Create a new LinkAuthenticationElementManager instance
|
|
1174
|
+
* @param stripeService - Injected StripeService dependency
|
|
1175
|
+
*/
|
|
1176
|
+
createLinkAuthenticationElementManager(stripeService) {
|
|
1177
|
+
return new LinkAuthenticationElementManager(stripeService);
|
|
1178
|
+
}
|
|
1179
|
+
/**
|
|
1180
|
+
* Create a new CurrencySelectorElementManager instance
|
|
1181
|
+
* @param stripeService - Injected StripeService dependency
|
|
1182
|
+
*/
|
|
1183
|
+
createCurrencySelectorElementManager(stripeService) {
|
|
1184
|
+
return new CurrencySelectorElementManager(stripeService);
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Create a new ExpressCheckoutElementManager instance
|
|
1188
|
+
* @param stripeService - Injected StripeService dependency
|
|
1189
|
+
*/
|
|
1190
|
+
createExpressCheckoutElementManager(stripeService) {
|
|
1191
|
+
return new ExpressCheckoutElementManager(stripeService);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Default factory instance
|
|
1196
|
+
* Can be replaced with a mock in tests
|
|
1197
|
+
*/
|
|
1198
|
+
const serviceFactory = new ServiceFactory();
|
|
1199
|
+
|
|
1200
|
+
exports.serviceFactory = serviceFactory;
|