@benbraide/inlinejs-stripe 2.2.0 → 2.3.0

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