@benbraide/inlinejs-stripe 2.0.1 → 2.0.3

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.
@@ -25,7 +25,10 @@ class StripeFieldElement extends inlinejs_element_1.CustomElement {
25
25
  super();
26
26
  this.stripeField_ = null;
27
27
  this.isReady_ = false;
28
+ this.isComplete_ = false;
29
+ this.lastError_ = null;
28
30
  this.readyWaiters_ = new Array();
31
+ this.changeListeners = new Array();
29
32
  this.stripe = null;
30
33
  this.options = null;
31
34
  this.type = '';
@@ -38,6 +41,13 @@ class StripeFieldElement extends inlinejs_element_1.CustomElement {
38
41
  this.isReady_ ? resolve() : this.readyWaiters_.push(() => resolve());
39
42
  });
40
43
  }
44
+ AddChangeListener(listener) {
45
+ this.changeListeners.push(listener);
46
+ }
47
+ RemoveChangeListener(listener) {
48
+ const index = this.changeListeners.indexOf(listener);
49
+ (index >= 0) && this.changeListeners.splice(index, 1);
50
+ }
41
51
  ToggleFocus(focused) {
42
52
  var _a, _b;
43
53
  (this.stripeField_ && (focused ? (_a = this.stripeField_) === null || _a === void 0 ? void 0 : _a.focus() : (_b = this.stripeField_) === null || _b === void 0 ? void 0 : _b.blur()));
@@ -83,23 +93,25 @@ class StripeFieldElement extends inlinejs_element_1.CustomElement {
83
93
  this.readyWaiters_.splice(0).forEach(waiter => (0, inlinejs_1.JournalTry)(waiter));
84
94
  });
85
95
  this.stripeField_.on('change', (event) => {
86
- var _a;
87
- if (event === null || event === void 0 ? void 0 : event.error) {
96
+ if (((event === null || event === void 0 ? void 0 : event.error) || null) !== this.lastError_) {
97
+ this.lastError_ = ((event === null || event === void 0 ? void 0 : event.error) || null);
88
98
  (0, inlinejs_1.EvaluateLater)({
89
99
  componentId: this.componentId_,
90
100
  contextElement: this,
91
101
  expression: this.onerrors,
92
102
  disableFunctionCall: false,
93
- })(undefined, [], { error: event.error });
103
+ })(undefined, [this.lastError_], { error: this.lastError_ });
104
+ this.changeListeners.forEach(listener => (0, inlinejs_1.JournalTry)(() => listener('error', this.lastError_)));
94
105
  }
95
- else if (event === null || event === void 0 ? void 0 : event.complete) {
106
+ if (((event === null || event === void 0 ? void 0 : event.complete) || false) != this.isComplete_) {
107
+ this.isComplete_ = ((event === null || event === void 0 ? void 0 : event.complete) || false);
96
108
  (0, inlinejs_1.EvaluateLater)({
97
109
  componentId: this.componentId_,
98
110
  contextElement: this,
99
111
  expression: this.oncomplete,
100
112
  disableFunctionCall: false,
101
- })();
102
- (_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.FocusNextField(this);
113
+ })(undefined, [this.isComplete_], { complete: this.isComplete_ });
114
+ this.changeListeners.forEach(listener => (0, inlinejs_1.JournalTry)(() => listener('complete', this.isComplete_)));
103
115
  }
104
116
  });
105
117
  this.stripeField_.mount(this);
@@ -1,10 +1,12 @@
1
1
  /// <reference types="stripe-v3" />
2
2
  import { CustomElement } from "@benbraide/inlinejs-element";
3
- import { IStripeField, IStripePaymentDetails } from "../types";
3
+ import { IStripeField, IStripePaymentDetails, StripeFieldChangeHandlerType } from "../types";
4
4
  export declare class StripePaymentMenthodElement extends CustomElement implements IStripeField {
5
5
  value: stripe.elements.Element | string;
6
6
  constructor();
7
7
  WaitReady(): Promise<void>;
8
+ AddChangeListener(listener: StripeFieldChangeHandlerType): void;
9
+ RemoveChangeListener(listener: StripeFieldChangeHandlerType): void;
8
10
  ToggleFocus(focused: boolean): void;
9
11
  Reset(): void;
10
12
  AddDetails(details: IStripePaymentDetails): void;
@@ -19,6 +19,8 @@ class StripePaymentMenthodElement extends inlinejs_element_1.CustomElement {
19
19
  WaitReady() {
20
20
  return Promise.resolve();
21
21
  }
22
+ AddChangeListener(listener) { }
23
+ RemoveChangeListener(listener) { }
22
24
  ToggleFocus(focused) { }
23
25
  Reset() { }
24
26
  AddDetails(details) {
@@ -11,11 +11,15 @@ export declare class StripeElement extends CustomElement implements IStripeEleme
11
11
  protected fields_: Array<IStripeField> | null;
12
12
  protected readyFields_: Array<IStripeField> | null;
13
13
  protected instanceWaiters_: (() => void)[];
14
+ protected completeFields_: Array<IStripeField> | null;
15
+ protected errorFields_: Array<IStripeField> | null;
14
16
  options: stripe.elements.ElementsOptions | null;
15
17
  publicKey: string;
16
18
  onready: string;
19
+ oncomplete: string;
20
+ onerrors: string;
17
21
  defer: boolean;
18
- autofocus: boolean;
22
+ focusnext: boolean;
19
23
  constructor();
20
24
  AddStripeField(field: IStripeField): void;
21
25
  RemoveStripeField(field: IStripeField): void;
@@ -31,11 +31,15 @@ class StripeElement extends inlinejs_element_1.CustomElement {
31
31
  this.fields_ = null;
32
32
  this.readyFields_ = null;
33
33
  this.instanceWaiters_ = new Array();
34
+ this.completeFields_ = null;
35
+ this.errorFields_ = null;
34
36
  this.options = null;
35
37
  this.publicKey = '';
36
38
  this.onready = '';
39
+ this.oncomplete = '';
40
+ this.onerrors = '';
37
41
  this.defer = false;
38
- this.autofocus = false;
42
+ this.focusnext = false;
39
43
  }
40
44
  AddStripeField(field) {
41
45
  this.fields_ = (this.fields_ || []);
@@ -54,13 +58,54 @@ class StripeElement extends inlinejs_element_1.CustomElement {
54
58
  this.readyWaiters_.splice(0).forEach(waiter => (0, inlinejs_1.JournalTry)(waiter));
55
59
  });
56
60
  });
61
+ field.AddChangeListener((type, data) => {
62
+ if (type === 'complete') {
63
+ let changed = false;
64
+ this.completeFields_ = (this.completeFields_ || []);
65
+ if (data && !this.completeFields_.includes(field)) {
66
+ this.completeFields_.push(field);
67
+ changed = !!(this.readyFields_ && this.completeFields_.length == this.readyFields_.length);
68
+ }
69
+ else if (!data && this.completeFields_.includes(field)) {
70
+ this.completeFields_ = this.completeFields_.filter(x => x !== field);
71
+ changed = (this.completeFields_.length == 0);
72
+ }
73
+ changed && this.oncomplete && (0, inlinejs_1.EvaluateLater)({
74
+ componentId: this.componentId_,
75
+ contextElement: this,
76
+ expression: this.oncomplete,
77
+ disableFunctionCall: false,
78
+ })(undefined, [!!data], { complete: !!data });
79
+ !!data && this.focusnext && this.FocusNextField(field);
80
+ }
81
+ else if (type === 'error') {
82
+ let changed = false;
83
+ this.errorFields_ = (this.errorFields_ || []);
84
+ this.errorFields_.push(field);
85
+ this.errorFields_ = (this.errorFields_ || []);
86
+ if (data && !this.errorFields_.includes(field)) {
87
+ this.errorFields_.push(field);
88
+ changed = (this.errorFields_.length == 1);
89
+ }
90
+ else if (!data && this.errorFields_.includes(field)) {
91
+ this.errorFields_ = this.errorFields_.filter(x => x !== field);
92
+ changed = (this.errorFields_.length == 0);
93
+ }
94
+ changed && this.onerrors && (0, inlinejs_1.EvaluateLater)({
95
+ componentId: this.componentId_,
96
+ contextElement: this,
97
+ expression: this.onerrors,
98
+ disableFunctionCall: false,
99
+ })(undefined, [data], { error: data });
100
+ }
101
+ });
57
102
  }
58
103
  RemoveStripeField(field) {
59
104
  this.fields_ && (this.fields_ = this.fields_.filter(x => x !== field));
60
105
  this.readyFields_ && (this.readyFields_ = this.readyFields_.filter(x => x !== field));
61
106
  }
62
107
  FocusNextField(field) {
63
- if (this.autofocus && this.fields_) {
108
+ if (this.fields_) {
64
109
  const index = this.fields_.indexOf(field);
65
110
  (index >= 0 && index < this.fields_.length - 1) && this.fields_[index + 1].ToggleFocus(true);
66
111
  }
@@ -149,12 +194,18 @@ __decorate([
149
194
  __decorate([
150
195
  (0, inlinejs_element_1.Property)({ type: 'string' })
151
196
  ], StripeElement.prototype, "onready", void 0);
197
+ __decorate([
198
+ (0, inlinejs_element_1.Property)({ type: 'string' })
199
+ ], StripeElement.prototype, "oncomplete", void 0);
200
+ __decorate([
201
+ (0, inlinejs_element_1.Property)({ type: 'string' })
202
+ ], StripeElement.prototype, "onerrors", void 0);
152
203
  __decorate([
153
204
  (0, inlinejs_element_1.Property)({ type: 'boolean' })
154
205
  ], StripeElement.prototype, "defer", void 0);
155
206
  __decorate([
156
207
  (0, inlinejs_element_1.Property)({ type: 'boolean' })
157
- ], StripeElement.prototype, "autofocus", void 0);
208
+ ], StripeElement.prototype, "focusnext", void 0);
158
209
  exports.StripeElement = StripeElement;
159
210
  function StripeElementCompact() {
160
211
  (0, inlinejs_element_1.RegisterCustomElement)(StripeElement, 'stripe');
@@ -3,8 +3,12 @@ export interface IStripePaymentDetails {
3
3
  method?: stripe.elements.Element | string;
4
4
  billingDetails?: stripe.BillingDetails;
5
5
  }
6
+ export declare type StripeFieldChangeType = 'error' | 'complete';
7
+ export declare type StripeFieldChangeHandlerType = (type: StripeFieldChangeType, data: any) => void;
6
8
  export interface IStripeField {
7
9
  WaitReady(): Promise<void>;
10
+ AddChangeListener(listener: StripeFieldChangeHandlerType): void;
11
+ RemoveChangeListener(listener: StripeFieldChangeHandlerType): void;
8
12
  ToggleFocus(focused: boolean): void;
9
13
  Reset(): void;
10
14
  AddDetails(details: IStripePaymentDetails): void;
@@ -1,11 +1,13 @@
1
1
  import { CustomElement } from "@benbraide/inlinejs-element";
2
- import { IStripeField, IStripePaymentDetails } from "../types";
2
+ import { IStripeField, IStripePaymentDetails, StripeFieldChangeHandlerType } from "../types";
3
3
  export declare class StripeDetailElement extends CustomElement implements IStripeField {
4
4
  input: HTMLInputElement | null;
5
5
  name: string;
6
6
  value: string;
7
7
  constructor();
8
8
  WaitReady(): Promise<void>;
9
+ AddChangeListener(listener: StripeFieldChangeHandlerType): void;
10
+ RemoveChangeListener(listener: StripeFieldChangeHandlerType): void;
9
11
  ToggleFocus(focused: boolean): void;
10
12
  Reset(): void;
11
13
  AddDetails(details: IStripePaymentDetails): void;
@@ -15,6 +15,8 @@ export class StripeDetailElement extends CustomElement {
15
15
  WaitReady() {
16
16
  return Promise.resolve();
17
17
  }
18
+ AddChangeListener(listener) { }
19
+ RemoveChangeListener(listener) { }
18
20
  ToggleFocus(focused) {
19
21
  const input = this.GetInput_();
20
22
  input && (focused ? input.focus() : input.blur());
@@ -25,7 +27,7 @@ export class StripeDetailElement extends CustomElement {
25
27
  }
26
28
  AddDetails(details) {
27
29
  const input = this.GetInput_();
28
- if (!this.name || (!input && !this.value)) {
30
+ if (!this.name || (!(input === null || input === void 0 ? void 0 : input.value) && !this.value)) {
29
31
  return;
30
32
  }
31
33
  details.billingDetails = (details.billingDetails || {});
@@ -1,11 +1,14 @@
1
1
  /// <reference types="stripe-v3" />
2
2
  import { IElementScopeCreatedCallbackParams } from "@benbraide/inlinejs";
3
3
  import { CustomElement } from "@benbraide/inlinejs-element";
4
- import { IStripeElement, IStripeField, IStripePaymentDetails } from "../types";
4
+ import { IStripeElement, IStripeField, IStripePaymentDetails, StripeFieldChangeHandlerType } from "../types";
5
5
  export declare class StripeFieldElement extends CustomElement implements IStripeField {
6
6
  protected stripeField_: stripe.elements.Element | null;
7
7
  protected isReady_: boolean;
8
+ protected isComplete_: boolean;
9
+ protected lastError_: stripe.Error | null;
8
10
  protected readyWaiters_: (() => void)[];
11
+ protected changeListeners: StripeFieldChangeHandlerType[];
9
12
  stripe: IStripeElement | null;
10
13
  options: stripe.elements.ElementsOptions | null;
11
14
  type: string;
@@ -14,6 +17,8 @@ export declare class StripeFieldElement extends CustomElement implements IStripe
14
17
  onerrors: string;
15
18
  constructor();
16
19
  WaitReady(): Promise<void>;
20
+ AddChangeListener(listener: StripeFieldChangeHandlerType): void;
21
+ RemoveChangeListener(listener: StripeFieldChangeHandlerType): void;
17
22
  ToggleFocus(focused: boolean): void;
18
23
  Reset(): void;
19
24
  AddDetails(details: IStripePaymentDetails): void;
@@ -22,7 +22,10 @@ export class StripeFieldElement extends CustomElement {
22
22
  super();
23
23
  this.stripeField_ = null;
24
24
  this.isReady_ = false;
25
+ this.isComplete_ = false;
26
+ this.lastError_ = null;
25
27
  this.readyWaiters_ = new Array();
28
+ this.changeListeners = new Array();
26
29
  this.stripe = null;
27
30
  this.options = null;
28
31
  this.type = '';
@@ -35,6 +38,13 @@ export class StripeFieldElement extends CustomElement {
35
38
  this.isReady_ ? resolve() : this.readyWaiters_.push(() => resolve());
36
39
  });
37
40
  }
41
+ AddChangeListener(listener) {
42
+ this.changeListeners.push(listener);
43
+ }
44
+ RemoveChangeListener(listener) {
45
+ const index = this.changeListeners.indexOf(listener);
46
+ (index >= 0) && this.changeListeners.splice(index, 1);
47
+ }
38
48
  ToggleFocus(focused) {
39
49
  var _a, _b;
40
50
  (this.stripeField_ && (focused ? (_a = this.stripeField_) === null || _a === void 0 ? void 0 : _a.focus() : (_b = this.stripeField_) === null || _b === void 0 ? void 0 : _b.blur()));
@@ -80,23 +90,25 @@ export class StripeFieldElement extends CustomElement {
80
90
  this.readyWaiters_.splice(0).forEach(waiter => JournalTry(waiter));
81
91
  });
82
92
  this.stripeField_.on('change', (event) => {
83
- var _a;
84
- if (event === null || event === void 0 ? void 0 : event.error) {
93
+ if (((event === null || event === void 0 ? void 0 : event.error) || null) !== this.lastError_) {
94
+ this.lastError_ = ((event === null || event === void 0 ? void 0 : event.error) || null);
85
95
  EvaluateLater({
86
96
  componentId: this.componentId_,
87
97
  contextElement: this,
88
98
  expression: this.onerrors,
89
99
  disableFunctionCall: false,
90
- })(undefined, [], { error: event.error });
100
+ })(undefined, [this.lastError_], { error: this.lastError_ });
101
+ this.changeListeners.forEach(listener => JournalTry(() => listener('error', this.lastError_)));
91
102
  }
92
- else if (event === null || event === void 0 ? void 0 : event.complete) {
103
+ if (((event === null || event === void 0 ? void 0 : event.complete) || false) != this.isComplete_) {
104
+ this.isComplete_ = ((event === null || event === void 0 ? void 0 : event.complete) || false);
93
105
  EvaluateLater({
94
106
  componentId: this.componentId_,
95
107
  contextElement: this,
96
108
  expression: this.oncomplete,
97
109
  disableFunctionCall: false,
98
- })();
99
- (_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.FocusNextField(this);
110
+ })(undefined, [this.isComplete_], { complete: this.isComplete_ });
111
+ this.changeListeners.forEach(listener => JournalTry(() => listener('complete', this.isComplete_)));
100
112
  }
101
113
  });
102
114
  this.stripeField_.mount(this);
@@ -1,10 +1,12 @@
1
1
  /// <reference types="stripe-v3" />
2
2
  import { CustomElement } from "@benbraide/inlinejs-element";
3
- import { IStripeField, IStripePaymentDetails } from "../types";
3
+ import { IStripeField, IStripePaymentDetails, StripeFieldChangeHandlerType } from "../types";
4
4
  export declare class StripePaymentMenthodElement extends CustomElement implements IStripeField {
5
5
  value: stripe.elements.Element | string;
6
6
  constructor();
7
7
  WaitReady(): Promise<void>;
8
+ AddChangeListener(listener: StripeFieldChangeHandlerType): void;
9
+ RemoveChangeListener(listener: StripeFieldChangeHandlerType): void;
8
10
  ToggleFocus(focused: boolean): void;
9
11
  Reset(): void;
10
12
  AddDetails(details: IStripePaymentDetails): void;
@@ -16,6 +16,8 @@ export class StripePaymentMenthodElement extends CustomElement {
16
16
  WaitReady() {
17
17
  return Promise.resolve();
18
18
  }
19
+ AddChangeListener(listener) { }
20
+ RemoveChangeListener(listener) { }
19
21
  ToggleFocus(focused) { }
20
22
  Reset() { }
21
23
  AddDetails(details) {
@@ -11,11 +11,15 @@ export declare class StripeElement extends CustomElement implements IStripeEleme
11
11
  protected fields_: Array<IStripeField> | null;
12
12
  protected readyFields_: Array<IStripeField> | null;
13
13
  protected instanceWaiters_: (() => void)[];
14
+ protected completeFields_: Array<IStripeField> | null;
15
+ protected errorFields_: Array<IStripeField> | null;
14
16
  options: stripe.elements.ElementsOptions | null;
15
17
  publicKey: string;
16
18
  onready: string;
19
+ oncomplete: string;
20
+ onerrors: string;
17
21
  defer: boolean;
18
- autofocus: boolean;
22
+ focusnext: boolean;
19
23
  constructor();
20
24
  AddStripeField(field: IStripeField): void;
21
25
  RemoveStripeField(field: IStripeField): void;
@@ -28,11 +28,15 @@ export class StripeElement extends CustomElement {
28
28
  this.fields_ = null;
29
29
  this.readyFields_ = null;
30
30
  this.instanceWaiters_ = new Array();
31
+ this.completeFields_ = null;
32
+ this.errorFields_ = null;
31
33
  this.options = null;
32
34
  this.publicKey = '';
33
35
  this.onready = '';
36
+ this.oncomplete = '';
37
+ this.onerrors = '';
34
38
  this.defer = false;
35
- this.autofocus = false;
39
+ this.focusnext = false;
36
40
  }
37
41
  AddStripeField(field) {
38
42
  this.fields_ = (this.fields_ || []);
@@ -51,13 +55,54 @@ export class StripeElement extends CustomElement {
51
55
  this.readyWaiters_.splice(0).forEach(waiter => JournalTry(waiter));
52
56
  });
53
57
  });
58
+ field.AddChangeListener((type, data) => {
59
+ if (type === 'complete') {
60
+ let changed = false;
61
+ this.completeFields_ = (this.completeFields_ || []);
62
+ if (data && !this.completeFields_.includes(field)) {
63
+ this.completeFields_.push(field);
64
+ changed = !!(this.readyFields_ && this.completeFields_.length == this.readyFields_.length);
65
+ }
66
+ else if (!data && this.completeFields_.includes(field)) {
67
+ this.completeFields_ = this.completeFields_.filter(x => x !== field);
68
+ changed = (this.completeFields_.length == 0);
69
+ }
70
+ changed && this.oncomplete && EvaluateLater({
71
+ componentId: this.componentId_,
72
+ contextElement: this,
73
+ expression: this.oncomplete,
74
+ disableFunctionCall: false,
75
+ })(undefined, [!!data], { complete: !!data });
76
+ !!data && this.focusnext && this.FocusNextField(field);
77
+ }
78
+ else if (type === 'error') {
79
+ let changed = false;
80
+ this.errorFields_ = (this.errorFields_ || []);
81
+ this.errorFields_.push(field);
82
+ this.errorFields_ = (this.errorFields_ || []);
83
+ if (data && !this.errorFields_.includes(field)) {
84
+ this.errorFields_.push(field);
85
+ changed = (this.errorFields_.length == 1);
86
+ }
87
+ else if (!data && this.errorFields_.includes(field)) {
88
+ this.errorFields_ = this.errorFields_.filter(x => x !== field);
89
+ changed = (this.errorFields_.length == 0);
90
+ }
91
+ changed && this.onerrors && EvaluateLater({
92
+ componentId: this.componentId_,
93
+ contextElement: this,
94
+ expression: this.onerrors,
95
+ disableFunctionCall: false,
96
+ })(undefined, [data], { error: data });
97
+ }
98
+ });
54
99
  }
55
100
  RemoveStripeField(field) {
56
101
  this.fields_ && (this.fields_ = this.fields_.filter(x => x !== field));
57
102
  this.readyFields_ && (this.readyFields_ = this.readyFields_.filter(x => x !== field));
58
103
  }
59
104
  FocusNextField(field) {
60
- if (this.autofocus && this.fields_) {
105
+ if (this.fields_) {
61
106
  const index = this.fields_.indexOf(field);
62
107
  (index >= 0 && index < this.fields_.length - 1) && this.fields_[index + 1].ToggleFocus(true);
63
108
  }
@@ -146,12 +191,18 @@ __decorate([
146
191
  __decorate([
147
192
  Property({ type: 'string' })
148
193
  ], StripeElement.prototype, "onready", void 0);
194
+ __decorate([
195
+ Property({ type: 'string' })
196
+ ], StripeElement.prototype, "oncomplete", void 0);
197
+ __decorate([
198
+ Property({ type: 'string' })
199
+ ], StripeElement.prototype, "onerrors", void 0);
149
200
  __decorate([
150
201
  Property({ type: 'boolean' })
151
202
  ], StripeElement.prototype, "defer", void 0);
152
203
  __decorate([
153
204
  Property({ type: 'boolean' })
154
- ], StripeElement.prototype, "autofocus", void 0);
205
+ ], StripeElement.prototype, "focusnext", void 0);
155
206
  export function StripeElementCompact() {
156
207
  RegisterCustomElement(StripeElement, 'stripe');
157
208
  }
@@ -3,8 +3,12 @@ export interface IStripePaymentDetails {
3
3
  method?: stripe.elements.Element | string;
4
4
  billingDetails?: stripe.BillingDetails;
5
5
  }
6
+ export declare type StripeFieldChangeType = 'error' | 'complete';
7
+ export declare type StripeFieldChangeHandlerType = (type: StripeFieldChangeType, data: any) => void;
6
8
  export interface IStripeField {
7
9
  WaitReady(): Promise<void>;
10
+ AddChangeListener(listener: StripeFieldChangeHandlerType): void;
11
+ RemoveChangeListener(listener: StripeFieldChangeHandlerType): void;
8
12
  ToggleFocus(focused: boolean): void;
9
13
  Reset(): void;
10
14
  AddDetails(details: IStripePaymentDetails): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benbraide/inlinejs-stripe",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
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",