@benbraide/inlinejs-stripe 1.0.8 → 2.0.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.
@@ -0,0 +1,136 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __rest = (this && this.__rest) || function (s, e) {
8
+ var t = {};
9
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
10
+ t[p] = s[p];
11
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
12
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
13
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
14
+ t[p[i]] = s[p[i]];
15
+ }
16
+ return t;
17
+ };
18
+ import { EvaluateLater, FindAncestor, JournalTry } from "@benbraide/inlinejs";
19
+ import { CustomElement, Property, RegisterCustomElement } from "@benbraide/inlinejs-element";
20
+ export class StripeFieldElement extends CustomElement {
21
+ constructor() {
22
+ super();
23
+ this.stripeField_ = null;
24
+ this.isReady_ = false;
25
+ this.readyWaiters_ = new Array();
26
+ this.stripe = null;
27
+ this.options = null;
28
+ this.type = '';
29
+ this.onready = '';
30
+ this.oncomplete = '';
31
+ this.onerrors = '';
32
+ }
33
+ WaitReady() {
34
+ return new Promise(resolve => {
35
+ this.isReady_ ? resolve() : this.readyWaiters_.push(() => resolve());
36
+ });
37
+ }
38
+ ToggleFocus(focused) {
39
+ var _a, _b;
40
+ (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()));
41
+ }
42
+ Reset() {
43
+ var _a;
44
+ this.stripeField_ && ((_a = this.stripeField_) === null || _a === void 0 ? void 0 : _a.clear());
45
+ }
46
+ AddDetails(details) {
47
+ (this.stripeField_ && (this.type === 'card' || this.type === 'number' || this.type === 'cardNumber')) && (details.method = this.stripeField_);
48
+ }
49
+ HandleElementScopeCreated_(_a, postAttributesCallback) {
50
+ var { scope } = _a, rest = __rest(_a, ["scope"]);
51
+ super.HandleElementScopeCreated_(Object.assign({ scope }, rest), () => {
52
+ var _a;
53
+ (_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.AddStripeField(this);
54
+ postAttributesCallback && postAttributesCallback();
55
+ });
56
+ scope.AddPostProcessCallback(() => {
57
+ var _a;
58
+ (_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.WaitInstance().then((stripe) => {
59
+ var _a;
60
+ if (!stripe) {
61
+ return;
62
+ }
63
+ let type = '';
64
+ if (['number', 'expiry', 'cvc'].includes(this.type)) {
65
+ type = `card${this.type.substring(0, 1).toUpperCase()}${this.type.substring(1)}`;
66
+ }
67
+ else if (['card', 'cardNumber', 'cardExpiry', 'cardCvc', 'postalCode', 'paymentRequestButton', 'iban', 'idealBank'].includes(this.type)) {
68
+ type = this.type;
69
+ }
70
+ if (type) {
71
+ this.stripeField_ = stripe.elements().create(type, (this.options || ((_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.options) || undefined));
72
+ this.stripeField_.on('ready', () => {
73
+ this.isReady_ = true;
74
+ this.onready && EvaluateLater({
75
+ componentId: this.componentId_,
76
+ contextElement: this,
77
+ expression: this.onready,
78
+ disableFunctionCall: false,
79
+ })();
80
+ this.readyWaiters_.splice(0).forEach(waiter => JournalTry(waiter));
81
+ });
82
+ this.stripeField_.on('change', (event) => {
83
+ var _a;
84
+ if (event === null || event === void 0 ? void 0 : event.error) {
85
+ EvaluateLater({
86
+ componentId: this.componentId_,
87
+ contextElement: this,
88
+ expression: this.onerrors,
89
+ disableFunctionCall: false,
90
+ })(undefined, [], { error: event.error });
91
+ }
92
+ else if (event === null || event === void 0 ? void 0 : event.complete) {
93
+ EvaluateLater({
94
+ componentId: this.componentId_,
95
+ contextElement: this,
96
+ expression: this.oncomplete,
97
+ disableFunctionCall: false,
98
+ })();
99
+ (_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.FocusNextField(this);
100
+ }
101
+ });
102
+ this.stripeField_.mount(this);
103
+ }
104
+ });
105
+ });
106
+ scope.AddUninitCallback(() => {
107
+ var _a;
108
+ (_a = this.GetStripe_()) === null || _a === void 0 ? void 0 : _a.RemoveStripeField(this);
109
+ this.stripeField_ = null;
110
+ });
111
+ }
112
+ GetStripe_() {
113
+ return (this.stripe || FindAncestor(this, ancestor => ('AddStripeField' in ancestor)));
114
+ }
115
+ }
116
+ __decorate([
117
+ Property({ type: 'object', checkStoredObject: true })
118
+ ], StripeFieldElement.prototype, "stripe", void 0);
119
+ __decorate([
120
+ Property({ type: 'object', checkStoredObject: true })
121
+ ], StripeFieldElement.prototype, "options", void 0);
122
+ __decorate([
123
+ Property({ type: 'string' })
124
+ ], StripeFieldElement.prototype, "type", void 0);
125
+ __decorate([
126
+ Property({ type: 'string' })
127
+ ], StripeFieldElement.prototype, "onready", void 0);
128
+ __decorate([
129
+ Property({ type: 'string' })
130
+ ], StripeFieldElement.prototype, "oncomplete", void 0);
131
+ __decorate([
132
+ Property({ type: 'string' })
133
+ ], StripeFieldElement.prototype, "onerrors", void 0);
134
+ export function StripeFieldElementCompact() {
135
+ RegisterCustomElement(StripeFieldElement, 'stripe-field');
136
+ }
@@ -0,0 +1,12 @@
1
+ /// <reference types="stripe-v3" />
2
+ import { CustomElement } from "@benbraide/inlinejs-element";
3
+ import { IStripeField, IStripePaymentDetails } from "../types";
4
+ export declare class StripePaymentMenthodElement extends CustomElement implements IStripeField {
5
+ value: stripe.elements.Element | string;
6
+ constructor();
7
+ WaitReady(): Promise<void>;
8
+ ToggleFocus(focused: boolean): void;
9
+ Reset(): void;
10
+ AddDetails(details: IStripePaymentDetails): void;
11
+ }
12
+ export declare function StripePaymentMenthodElementCompact(): void;
@@ -0,0 +1,30 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { CustomElement, Property, RegisterCustomElement } from "@benbraide/inlinejs-element";
8
+ export class StripePaymentMenthodElement extends CustomElement {
9
+ constructor() {
10
+ super({
11
+ isTemplate: true,
12
+ isHidden: true,
13
+ });
14
+ this.value = '';
15
+ }
16
+ WaitReady() {
17
+ return Promise.resolve();
18
+ }
19
+ ToggleFocus(focused) { }
20
+ Reset() { }
21
+ AddDetails(details) {
22
+ this.value && (details.method = this.value);
23
+ }
24
+ }
25
+ __decorate([
26
+ Property({ type: 'object', checkStoredObject: true })
27
+ ], StripePaymentMenthodElement.prototype, "value", void 0);
28
+ export function StripePaymentMenthodElementCompact() {
29
+ RegisterCustomElement(StripePaymentMenthodElement, 'stripe-payment-method');
30
+ }
@@ -0,0 +1,32 @@
1
+ /// <reference types="stripe-v3" />
2
+ import { IElementScopeCreatedCallbackParams } from "@benbraide/inlinejs";
3
+ import { CustomElement } from "@benbraide/inlinejs-element";
4
+ import { IStripeElement, IStripeField } from "../types";
5
+ export declare class StripeElement extends CustomElement implements IStripeElement {
6
+ protected stripe_: stripe.Stripe | null;
7
+ protected mounting_: boolean;
8
+ protected mounted_: boolean;
9
+ protected isReady_: boolean;
10
+ protected readyWaiters_: (() => void)[];
11
+ protected fields_: Array<IStripeField> | null;
12
+ protected readyFields_: Array<IStripeField> | null;
13
+ protected instanceWaiters_: (() => void)[];
14
+ options: stripe.elements.ElementsOptions | null;
15
+ publicKey: string;
16
+ onready: string;
17
+ defer: boolean;
18
+ autofocus: boolean;
19
+ constructor();
20
+ AddStripeField(field: IStripeField): void;
21
+ RemoveStripeField(field: IStripeField): void;
22
+ FocusNextField(field: IStripeField): void;
23
+ GetInstance(): stripe.Stripe | null;
24
+ WaitInstance(): Promise<stripe.Stripe | null>;
25
+ Mount(): void;
26
+ Pay(clientSecret: string, save?: boolean): Promise<false | stripe.PaymentIntentResponse>;
27
+ Setup(clientSecret: string): Promise<false | stripe.PaymentIntentResponse>;
28
+ WaitReady(): Promise<void>;
29
+ protected HandleElementScopeCreated_({ scope, ...rest }: IElementScopeCreatedCallbackParams, postAttributesCallback?: () => void): void;
30
+ protected PayOrSetup_(pay: boolean, clientSecret: string, save?: boolean): Promise<false | stripe.PaymentIntentResponse>;
31
+ }
32
+ export declare function StripeElementCompact(): void;
@@ -0,0 +1,157 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __rest = (this && this.__rest) || function (s, e) {
8
+ var t = {};
9
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
10
+ t[p] = s[p];
11
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
12
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
13
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
14
+ t[p[i]] = s[p[i]];
15
+ }
16
+ return t;
17
+ };
18
+ import { EvaluateLater, JournalTry } from "@benbraide/inlinejs";
19
+ import { CustomElement, Property, RegisterCustomElement } from "@benbraide/inlinejs-element";
20
+ export class StripeElement extends CustomElement {
21
+ constructor() {
22
+ super();
23
+ this.stripe_ = null;
24
+ this.mounting_ = false;
25
+ this.mounted_ = false;
26
+ this.isReady_ = false;
27
+ this.readyWaiters_ = new Array();
28
+ this.fields_ = null;
29
+ this.readyFields_ = null;
30
+ this.instanceWaiters_ = new Array();
31
+ this.options = null;
32
+ this.publicKey = '';
33
+ this.onready = '';
34
+ this.defer = false;
35
+ this.autofocus = false;
36
+ }
37
+ AddStripeField(field) {
38
+ this.fields_ = (this.fields_ || []);
39
+ this.fields_.push(field);
40
+ field.WaitReady().then(() => {
41
+ this.readyFields_ = (this.readyFields_ || []);
42
+ this.readyFields_.push(field);
43
+ (this.fields_ && this.readyFields_ && this.fields_.length <= this.readyFields_.length) && JournalTry(() => {
44
+ this.isReady_ = true;
45
+ this.onready && EvaluateLater({
46
+ componentId: this.componentId_,
47
+ contextElement: this,
48
+ expression: this.onready,
49
+ disableFunctionCall: false,
50
+ })();
51
+ this.readyWaiters_.splice(0).forEach(waiter => JournalTry(waiter));
52
+ });
53
+ });
54
+ }
55
+ RemoveStripeField(field) {
56
+ this.fields_ && (this.fields_ = this.fields_.filter(x => x !== field));
57
+ this.readyFields_ && (this.readyFields_ = this.readyFields_.filter(x => x !== field));
58
+ }
59
+ FocusNextField(field) {
60
+ if (this.autofocus && this.fields_) {
61
+ const index = this.fields_.indexOf(field);
62
+ (index >= 0 && index < this.fields_.length - 1) && this.fields_[index + 1].ToggleFocus(true);
63
+ }
64
+ }
65
+ GetInstance() {
66
+ return this.stripe_;
67
+ }
68
+ WaitInstance() {
69
+ return new Promise(resolve => {
70
+ this.mounted_ ? resolve(this.stripe_) : this.instanceWaiters_.push(() => resolve(this.stripe_));
71
+ });
72
+ }
73
+ Mount() {
74
+ if (this.mounted_ || this.mounting_) {
75
+ return;
76
+ }
77
+ this.mounting_ = true;
78
+ this.LoadResources().then(() => {
79
+ this.mounting_ = false;
80
+ this.mounted_ = true;
81
+ this.stripe_ = Stripe(this.publicKey);
82
+ this.instanceWaiters_.splice(0).forEach(waiter => JournalTry(waiter));
83
+ });
84
+ }
85
+ Pay(clientSecret, save = false) {
86
+ return this.PayOrSetup_(true, clientSecret, save);
87
+ }
88
+ Setup(clientSecret) {
89
+ return this.PayOrSetup_(false, clientSecret);
90
+ }
91
+ WaitReady() {
92
+ return new Promise(resolve => {
93
+ this.isReady_ ? resolve() : this.readyWaiters_.push(() => resolve());
94
+ });
95
+ }
96
+ HandleElementScopeCreated_(_a, postAttributesCallback) {
97
+ var { scope } = _a, rest = __rest(_a, ["scope"]);
98
+ super.HandleElementScopeCreated_(Object.assign({ scope }, rest), postAttributesCallback);
99
+ scope.AddPostProcessCallback(() => (!this.defer && this.Mount()));
100
+ scope.AddUninitCallback(() => (this.stripe_ = null));
101
+ }
102
+ PayOrSetup_(pay, clientSecret, save = false) {
103
+ return new Promise((resolve, reject) => {
104
+ this.WaitInstance().then((stripe) => {
105
+ var _a, _b;
106
+ if (!stripe) {
107
+ return resolve(false);
108
+ }
109
+ const details = {};
110
+ (_a = this.fields_) === null || _a === void 0 ? void 0 : _a.forEach(field => field.AddDetails(details));
111
+ if (!details.method) {
112
+ return resolve(false);
113
+ }
114
+ let cardDetails;
115
+ if (typeof details.method !== 'string') {
116
+ cardDetails = {
117
+ payment_method: {
118
+ card: details.method,
119
+ billing_details: details.billingDetails,
120
+ },
121
+ };
122
+ }
123
+ else {
124
+ cardDetails = {
125
+ payment_method: details.method,
126
+ };
127
+ }
128
+ if (pay) {
129
+ ((_b = details.billingDetails) === null || _b === void 0 ? void 0 : _b.email) && (cardDetails.receipt_email = details.billingDetails.email);
130
+ save && (cardDetails.setup_future_usage = 'off_session');
131
+ stripe.confirmCardPayment(clientSecret, cardDetails).then(resolve).catch(reject);
132
+ }
133
+ else {
134
+ stripe.confirmCardSetup(clientSecret, cardDetails).then(resolve).catch(reject);
135
+ }
136
+ }).catch(reject);
137
+ });
138
+ }
139
+ }
140
+ __decorate([
141
+ Property({ type: 'object', checkStoredObject: true })
142
+ ], StripeElement.prototype, "options", void 0);
143
+ __decorate([
144
+ Property({ type: 'string' })
145
+ ], StripeElement.prototype, "publicKey", void 0);
146
+ __decorate([
147
+ Property({ type: 'string' })
148
+ ], StripeElement.prototype, "onready", void 0);
149
+ __decorate([
150
+ Property({ type: 'boolean' })
151
+ ], StripeElement.prototype, "defer", void 0);
152
+ __decorate([
153
+ Property({ type: 'boolean' })
154
+ ], StripeElement.prototype, "autofocus", void 0);
155
+ export function StripeElementCompact() {
156
+ RegisterCustomElement(StripeElement, 'stripe');
157
+ }
package/lib/esm/entry.js CHANGED
@@ -1,5 +1,13 @@
1
1
  import { WaitForGlobal } from '@benbraide/inlinejs';
2
- import { StripeDirectiveHandlerCompact } from './directive/stripe';
2
+ import { StripeElementCompact } from './components/stripe';
3
+ import { StripeFieldElementCompact } from './components/field';
4
+ import { StripeDetailElementCompact } from './components/detail';
5
+ import { StripePaymentMenthodElementCompact } from './components/payment-method';
3
6
  export function InlineJSStripe() {
4
- WaitForGlobal().then(() => StripeDirectiveHandlerCompact());
7
+ WaitForGlobal().then(() => {
8
+ StripeElementCompact();
9
+ StripeFieldElementCompact();
10
+ StripeDetailElementCompact();
11
+ StripePaymentMenthodElementCompact();
12
+ });
5
13
  }
@@ -1 +1,5 @@
1
- export * from './directive/stripe';
1
+ export * from './components/stripe';
2
+ export * from './components/field';
3
+ export * from './components/detail';
4
+ export * from './components/payment-method';
5
+ export * from './entry';
package/lib/esm/index.js CHANGED
@@ -1 +1,5 @@
1
- export * from './directive/stripe';
1
+ export * from './components/stripe';
2
+ export * from './components/field';
3
+ export * from './components/detail';
4
+ export * from './components/payment-method';
5
+ export * from './entry';
@@ -0,0 +1,20 @@
1
+ /// <reference types="stripe-v3" />
2
+ export interface IStripePaymentDetails {
3
+ method?: stripe.elements.Element | string;
4
+ billingDetails?: stripe.BillingDetails;
5
+ }
6
+ export interface IStripeField {
7
+ WaitReady(): Promise<void>;
8
+ ToggleFocus(focused: boolean): void;
9
+ Reset(): void;
10
+ AddDetails(details: IStripePaymentDetails): void;
11
+ }
12
+ export interface IStripeElement {
13
+ options: stripe.elements.ElementsOptions | null;
14
+ AddStripeField(field: IStripeField): void;
15
+ RemoveStripeField(field: IStripeField): void;
16
+ FocusNextField(field: IStripeField): void;
17
+ GetInstance(): stripe.Stripe | null;
18
+ WaitInstance(): Promise<stripe.Stripe | null>;
19
+ Mount(): void;
20
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benbraide/inlinejs-stripe",
3
- "version": "1.0.8",
3
+ "version": "2.0.1",
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",
@@ -17,7 +17,8 @@
17
17
  "prepublishOnly": "npm run compile",
18
18
  "build": "webpack -c ./webpack.config.js && webpack -c ./webpack2.config.js",
19
19
  "upload": "npm run build && npm publish --access=public",
20
- "push": "npm i @benbraide/inlinejs && npm run upload"
20
+ "download": "npm i @benbraide/inlinejs @benbraide/inlinejs-element && npm audit fix",
21
+ "push": "npm run download && npm run upload"
21
22
  },
22
23
  "repository": {
23
24
  "type": "git",
@@ -52,7 +53,8 @@
52
53
  "webpack": "^5.41.0"
53
54
  },
54
55
  "dependencies": {
55
- "@benbraide/inlinejs": "^1.0.27",
56
+ "@benbraide/inlinejs": "^1.1.2",
57
+ "@benbraide/inlinejs-element": "^2.0.4",
56
58
  "webpack-cli": "^4.7.2"
57
59
  }
58
60
  }