@benbraide/inlinejs-stripe 2.2.0 → 2.3.1

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.
@@ -1,449 +0,0 @@
1
- import { FindComponentById, AddDirectiveHandler, CreateDirectiveHandlerCallback, EvaluateLater, GetGlobal, JournalError, JournalTry, AddChanges, BuildGetterProxyOptions, BuildProxyOptions, CreateInplaceProxy, BindEvent, ResolveOptions } from "@benbraide/inlinejs";
2
- const StripeSpecialKeys = ['submit', 'save', 'name', 'email', 'phone', 'address'];
3
- const StripeKeys = {
4
- 'number': 'cardNumber',
5
- 'expiry': 'cardExpiry',
6
- 'cvc': 'cardCvc',
7
- 'postal': 'postalCode',
8
- 'zip': 'postalCode',
9
- };
10
- const StripeDirectiveName = 'stripe';
11
- let StripePublicKey = '';
12
- let StripeUrl = 'https://js.stripe.com/v3/';
13
- let StripeStyles = null;
14
- let StripeClasses = null;
15
- export const StripeDirectiveHandler = CreateDirectiveHandlerCallback(StripeDirectiveName, ({ componentId, component, contextElement, expression, argKey, argOptions }) => {
16
- var _a;
17
- if (BindEvent({ contextElement, expression,
18
- component: (component || componentId),
19
- key: StripeDirectiveName,
20
- event: argKey,
21
- defaultEvent: 'success',
22
- eventWhitelist: ['error', 'before', 'after', 'ready', 'focus', 'complete'],
23
- options: argOptions,
24
- optionBlacklist: ['window', 'document', 'outside'],
25
- })) {
26
- return;
27
- }
28
- let resolvedComponent = (component || FindComponentById(componentId)), elementScope = resolvedComponent === null || resolvedComponent === void 0 ? void 0 : resolvedComponent.FindElementScope(contextElement);
29
- if (!resolvedComponent || !elementScope) {
30
- return JournalError('Failed to retrieve element scope.', `InlineJS.${StripeDirectiveName}`, contextElement);
31
- }
32
- let localKey = `\$${StripeDirectiveName}`, detailsKey = `#${StripeDirectiveName}`;
33
- if (localKey in (elementScope.GetLocals() || {})) { //Already initialized
34
- return;
35
- }
36
- if (StripeSpecialKeys.includes(argKey)) { //No Stripe binding
37
- let details = resolvedComponent.FindElementLocalValue(contextElement, detailsKey, true);
38
- if (details) {
39
- details.specialMounts[argKey] = contextElement;
40
- elementScope.SetLocal(localKey, CreateInplaceProxy(BuildGetterProxyOptions({
41
- getter: (prop) => {
42
- var _a;
43
- let local = (contextElement.parentElement ? (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.FindElementLocalValue(contextElement.parentElement, localKey, true) : null);
44
- if (prop === 'parent') {
45
- return local;
46
- }
47
- return ((prop && local) ? local[prop] : null);
48
- },
49
- lookup: ['parent'],
50
- })));
51
- }
52
- return;
53
- }
54
- if (StripeKeys.hasOwnProperty(argKey)) { //Bind Stripe field
55
- return (_a = resolvedComponent.FindElementLocalValue(contextElement, detailsKey, true)) === null || _a === void 0 ? void 0 : _a.addField(argKey, contextElement, (field, details) => {
56
- let resolvedComponent = FindComponentById(componentId), elementScope = resolvedComponent === null || resolvedComponent === void 0 ? void 0 : resolvedComponent.FindElementScope(contextElement);
57
- let id = resolvedComponent === null || resolvedComponent === void 0 ? void 0 : resolvedComponent.GenerateUniqueId(`${StripeDirectiveName}_proxy_`);
58
- if (!field.element) {
59
- return;
60
- }
61
- elementScope === null || elementScope === void 0 ? void 0 : elementScope.SetLocal(localKey, CreateInplaceProxy(BuildGetterProxyOptions({
62
- getter: (prop) => {
63
- var _a, _b, _c, _d, _e;
64
- if (prop === 'complete') {
65
- (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
66
- return field.complete;
67
- }
68
- if (prop === 'focused') {
69
- (_b = FindComponentById(componentId)) === null || _b === void 0 ? void 0 : _b.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
70
- return field.focused;
71
- }
72
- if (prop === 'error') {
73
- (_c = FindComponentById(componentId)) === null || _c === void 0 ? void 0 : _c.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
74
- return field.error;
75
- }
76
- if (prop === 'parent') {
77
- return (contextElement.parentElement ? (_d = FindComponentById(componentId)) === null || _d === void 0 ? void 0 : _d.FindElementLocalValue(contextElement.parentElement, localKey, true) : null);
78
- }
79
- if (prop === 'clear') {
80
- return () => {
81
- if (field.element) {
82
- field.element.clear();
83
- }
84
- };
85
- }
86
- if (prop === 'focus') {
87
- return () => {
88
- if (field.element) {
89
- field.element.focus();
90
- }
91
- };
92
- }
93
- if (prop === 'blur') {
94
- return () => {
95
- if (field.element) {
96
- field.element.blur();
97
- }
98
- };
99
- }
100
- if (prop === 'disable') {
101
- return (state = true) => {
102
- if (field.element) {
103
- field.element.update({ disabled: state });
104
- }
105
- };
106
- }
107
- let local = (contextElement.parentElement ? (_e = FindComponentById(componentId)) === null || _e === void 0 ? void 0 : _e.FindElementLocalValue(contextElement.parentElement, localKey, true) : null);
108
- return ((prop && local) ? local[prop] : null);
109
- },
110
- lookup: ['complete', 'focused', 'error', 'parent', 'clear', 'focus', 'blur', 'disable'],
111
- })));
112
- field.element.on('ready', () => {
113
- if (!field.ready) {
114
- field.ready = true;
115
- field.mount.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.ready`));
116
- details.onReady();
117
- }
118
- });
119
- field.element.on('change', (e) => {
120
- var _a, _b;
121
- if ((e === null || e === void 0 ? void 0 : e.complete) === field.complete) {
122
- return;
123
- }
124
- field.complete = e === null || e === void 0 ? void 0 : e.complete;
125
- field.mount.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.complete`, {
126
- detail: {
127
- completed: e === null || e === void 0 ? void 0 : e.complete,
128
- },
129
- }));
130
- if (field.complete) {
131
- if (field.error) {
132
- field.error = undefined;
133
- AddChanges('set', `${id}.error`, 'error', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes);
134
- }
135
- if (details.options.autofocus) { //Focus next if any
136
- let index = details.fields.indexOf(field);
137
- if (index != -1 && index < (details.fields.length - 1)) {
138
- let nextField = details.fields[index + 1];
139
- if (nextField.element) {
140
- nextField.element.focus();
141
- }
142
- else if ('focus' in nextField.mount && typeof nextField.mount.focus === 'function') {
143
- nextField.mount.focus();
144
- }
145
- }
146
- else if (details.specialMounts.submit) {
147
- details.specialMounts.submit.focus();
148
- }
149
- }
150
- }
151
- else if ((e === null || e === void 0 ? void 0 : e.error) && e.error.message !== field.error) {
152
- field.error = e.error.message;
153
- AddChanges('set', `${id}.error`, 'error', (_b = FindComponentById(componentId)) === null || _b === void 0 ? void 0 : _b.GetBackend().changes);
154
- field.mount.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.error`, {
155
- detail: {
156
- message: e.error.message,
157
- },
158
- }));
159
- }
160
- details.onChange();
161
- });
162
- field.element.on('focus', () => {
163
- var _a;
164
- if (!field.focused) {
165
- field.focused = true;
166
- AddChanges('set', `${id}.focused`, 'focused', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes);
167
- field.mount.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.focus`, {
168
- detail: {
169
- focused: true,
170
- },
171
- }));
172
- }
173
- });
174
- field.element.on('blur', () => {
175
- var _a;
176
- if (field.focused) {
177
- field.focused = false;
178
- AddChanges('set', `${id}.focused`, 'focused', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes);
179
- field.mount.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.focus`, {
180
- detail: {
181
- focused: false,
182
- },
183
- }));
184
- }
185
- });
186
- });
187
- }
188
- let stripeInstance = null, elements = null, backlog = new Array(), init = () => {
189
- EvaluateLater({ componentId, contextElement, expression })((value) => {
190
- stripeInstance = Stripe(value || StripePublicKey);
191
- if (!stripeInstance) {
192
- return JournalError('Failed to initialize stripe', `InlineJS.${StripeDirectiveName}.Init`, contextElement);
193
- }
194
- elements = stripeInstance.elements();
195
- if (!elements) {
196
- return JournalError('Failed to initialize stripe', `InlineJS.${StripeDirectiveName}.Init`, contextElement);
197
- }
198
- backlog.splice(0).forEach(callback => JournalTry(callback, `InlineJS.${StripeDirectiveName}.Init`, contextElement));
199
- });
200
- };
201
- let fields = new Array(), specialMounts = {
202
- submit: null,
203
- save: null,
204
- name: null,
205
- email: null,
206
- phone: null,
207
- address: null,
208
- };
209
- let id = resolvedComponent.GenerateUniqueId(`${StripeDirectiveName}_proxy_`), options = ResolveOptions({
210
- options: {
211
- autofocus: false,
212
- nexttick: false,
213
- manual: false,
214
- },
215
- list: argOptions,
216
- });
217
- let active = false, complete = false, errorCount = 0, setComplete = (value) => {
218
- var _a;
219
- if (value != complete) {
220
- complete = value;
221
- AddChanges('set', `${id}.complete`, 'complete', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes);
222
- }
223
- };
224
- let details = { fields, specialMounts, options,
225
- addField(key, target, callback) {
226
- let add = () => {
227
- var _a, _b;
228
- let field = {
229
- name: key,
230
- mount: target,
231
- element: elements.create(StripeKeys[key], {
232
- style: (StripeStyles || undefined),
233
- classes: (StripeClasses || undefined),
234
- }),
235
- ready: false,
236
- complete: false,
237
- error: undefined,
238
- };
239
- field.element.mount(target);
240
- this.fields.push(field);
241
- (_b = (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.FindElementScope(target)) === null || _b === void 0 ? void 0 : _b.AddUninitCallback(() => {
242
- var _a;
243
- (_a = field.element) === null || _a === void 0 ? void 0 : _a.destroy();
244
- this.fields = this.fields.filter(item => (item !== field));
245
- });
246
- callback(field, this);
247
- };
248
- elements ? add() : backlog.push(add);
249
- },
250
- onReady: () => { var _a; return AddChanges('set', `${id}.readyCount`, 'readyCount', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes); },
251
- onChange: () => {
252
- var _a;
253
- setComplete(!fields.find(field => !field.complete));
254
- let currentErrorCount = fields.reduce((prev, field) => (prev + (field.error ? 1 : 0)), 0);
255
- if (currentErrorCount != errorCount) { //Error list changed
256
- errorCount = currentErrorCount;
257
- AddChanges('set', `${id}.errors`, 'errors', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes);
258
- }
259
- },
260
- };
261
- let setActive = (value) => {
262
- var _a;
263
- if (value != active) {
264
- active = value;
265
- AddChanges('set', `${id}.active`, 'active', (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes);
266
- }
267
- };
268
- let evaluate = EvaluateLater({ componentId, contextElement, expression }), onSuccess = (response) => {
269
- var _a;
270
- if (!response.error) {
271
- contextElement.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.success`, {
272
- detail: {
273
- intent: response.paymentIntent,
274
- },
275
- }));
276
- contextElement.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.after`));
277
- setActive(false);
278
- if (options.nexttick) {
279
- (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes.AddNextTickHandler(() => evaluate());
280
- }
281
- else { //Immediate evaluation
282
- evaluate();
283
- }
284
- }
285
- else { //Error
286
- onError(response.error.message || '');
287
- }
288
- };
289
- let onError = (err) => {
290
- contextElement.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.error`, {
291
- detail: {
292
- type: 'host',
293
- message: err,
294
- },
295
- }));
296
- reportError('host', err);
297
- contextElement.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.after`));
298
- setActive(false);
299
- };
300
- let reportError = (type, message) => {
301
- contextElement.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.error`, {
302
- detail: { type, message },
303
- }));
304
- };
305
- let getPaymentDetails = (paymentMethod, save) => {
306
- var _a;
307
- if (paymentMethod && typeof paymentMethod === 'string') {
308
- return {
309
- payment_method: paymentMethod,
310
- setup_future_usage: (save ? 'off_session' : undefined),
311
- };
312
- }
313
- let cardElement = (_a = fields.find(field => (field.name === 'number'))) === null || _a === void 0 ? void 0 : _a.element;
314
- if (!cardElement) {
315
- return null;
316
- }
317
- let billingDetails = {}, getBillingDetail = (key) => {
318
- if (paymentMethod) {
319
- return (paymentMethod[key] || (specialMounts[key] ? specialMounts[key].value : undefined));
320
- }
321
- return (specialMounts[key] ? specialMounts[key].value : undefined);
322
- };
323
- ['name', 'email', 'phone', 'address'].forEach((key) => {
324
- if (key === 'address') {
325
- billingDetails.address = {
326
- line1: getBillingDetail(key),
327
- };
328
- }
329
- else {
330
- billingDetails[key] = getBillingDetail(key);
331
- }
332
- });
333
- if (!save && specialMounts.save && specialMounts.save instanceof HTMLInputElement) {
334
- save = specialMounts.save.checked;
335
- }
336
- return {
337
- payment_method: {
338
- card: cardElement,
339
- billing_details: billingDetails,
340
- },
341
- setup_future_usage: (save ? 'off_session' : undefined),
342
- };
343
- };
344
- let payOrSetup = (callback, hasPaymentMethod = false) => {
345
- if (hasPaymentMethod || (complete && !fields.find(field => !!field.error))) {
346
- setActive(true);
347
- contextElement.dispatchEvent(new CustomEvent(`${StripeDirectiveName}.before`));
348
- callback();
349
- }
350
- else { //Error
351
- reportError('incomplete', 'Please fill in all required fields.');
352
- }
353
- };
354
- elementScope.SetLocal(detailsKey, details);
355
- elementScope.SetLocal(localKey, CreateInplaceProxy(BuildProxyOptions({
356
- getter: (prop) => {
357
- var _a, _b, _c, _d;
358
- if (prop === 'bind') {
359
- return () => {
360
- if (!stripeInstance) {
361
- bind();
362
- }
363
- };
364
- }
365
- if (prop === 'active') {
366
- (_a = FindComponentById(componentId)) === null || _a === void 0 ? void 0 : _a.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
367
- return active;
368
- }
369
- if (prop === 'readyCount') {
370
- (_b = FindComponentById(componentId)) === null || _b === void 0 ? void 0 : _b.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
371
- return fields.reduce((prev, field) => (prev + (field.ready ? 1 : 0)), 0);
372
- }
373
- if (prop === 'complete') {
374
- (_c = FindComponentById(componentId)) === null || _c === void 0 ? void 0 : _c.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
375
- return complete;
376
- }
377
- if (prop === 'errors') {
378
- (_d = FindComponentById(componentId)) === null || _d === void 0 ? void 0 : _d.GetBackend().changes.AddGetAccess(`${id}.${prop}`);
379
- return fields.filter(field => !!field.error).map(field => field.error);
380
- }
381
- if (prop === 'instance') {
382
- return stripeInstance;
383
- }
384
- if (prop === 'publicKey') {
385
- return StripePublicKey;
386
- }
387
- if (prop === 'styles') {
388
- return StripeStyles;
389
- }
390
- if (prop === 'classes') {
391
- return StripeClasses;
392
- }
393
- if (prop === 'url') {
394
- return StripeUrl;
395
- }
396
- if (prop === 'pay') {
397
- return (clientSecret, paymentMethod, save = false) => {
398
- payOrSetup(() => {
399
- let paymentDetails = getPaymentDetails(paymentMethod, save);
400
- if (paymentDetails) {
401
- stripeInstance === null || stripeInstance === void 0 ? void 0 : stripeInstance.confirmCardPayment(clientSecret, paymentDetails).then(onSuccess).catch(onError);
402
- }
403
- }, (!!paymentMethod && typeof paymentMethod === 'string'));
404
- };
405
- }
406
- if (prop === 'setup') {
407
- return (clientSecret, paymentMethod) => {
408
- payOrSetup(() => {
409
- let paymentDetails = getPaymentDetails(paymentMethod, false);
410
- if (paymentDetails) {
411
- stripeInstance === null || stripeInstance === void 0 ? void 0 : stripeInstance.confirmCardSetup(clientSecret, paymentDetails).then(onSuccess).catch(onError);
412
- }
413
- }, (!!paymentMethod && typeof paymentMethod === 'string'));
414
- };
415
- }
416
- },
417
- setter: (prop, value) => {
418
- if (prop === 'publicKey') {
419
- StripePublicKey = value;
420
- }
421
- else if (prop === 'styles') {
422
- StripeStyles = value;
423
- }
424
- else if (prop === 'classes') {
425
- StripeClasses = value;
426
- }
427
- else if (prop === 'url') {
428
- StripeUrl = value;
429
- }
430
- return true;
431
- },
432
- lookup: ['bind', 'active', 'readyCount', 'complete', 'errors', 'instance', 'publicKey', 'styles', 'classes', 'url', 'pay', 'setup'],
433
- })));
434
- let bind = () => {
435
- let resourceConcept = GetGlobal().GetConcept('resource');
436
- if (StripeUrl && resourceConcept) {
437
- resourceConcept.GetScript(StripeUrl).then(init);
438
- }
439
- else { //Resource not provided
440
- init();
441
- }
442
- };
443
- if (!options.manual) {
444
- bind();
445
- }
446
- });
447
- export function StripeDirectiveHandlerCompact() {
448
- AddDirectiveHandler(StripeDirectiveHandler);
449
- }