@healnow/payments-ui 1.0.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/dist/payments-ui.js +1 -0
- package/lib/payments/card.js +177 -0
- package/lib/payments.js +27 -0
- package/package.json +30 -0
- package/src/payments/card.js +90 -0
- package/src/payments.js +5 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.PaymentsUI=t():e.PaymentsUI=t()}(self,(()=>(()=>{"use strict";var e={625:(e,t,n)=>{t.Z=void 0;var r,i,o,a,s,c,l,u,f,h,p,d,y,v=(r=n(100))&&r.__esModule?r:{default:r};function w(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function b(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?w(Object(n),!0).forEach((function(t){j(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):w(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function m(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function g(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function k(e,t){E(e,t),t.add(e)}function O(e,t,n){E(e,t),t.set(e,n)}function E(e,t){if(t.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")}function j(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(e,t){return function(e,t){return t.get?t.get.call(e):t.value}(e,C(e,t,"get"))}function C(e,t,n){if(!t.has(e))throw new TypeError("attempted to "+n+" private field on non-instance");return t.get(e)}function S(e,t,n){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return n}var W=(o=new WeakMap,a=new WeakSet,s=new WeakSet,c=new WeakSet,l=new WeakSet,u=new WeakSet,f=new WeakSet,h=new WeakSet,p=new WeakSet,d=new WeakSet,y=new WeakSet,i=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m(this,e),k(this,y),k(this,d),k(this,p),k(this,h),k(this,f),k(this,u),k(this,l),k(this,c),k(this,s),k(this,a),O(this,o,{writable:!0,value:null}),this.options=b({timeout:5e3,ack:!1},t),this.messageBuffer=[],this.listeners={global:[],ack:{}},this.connected=!1}var t,n;return t=e,n=[{key:"connect",value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"*",r=new MessageChannel,i=S(this,y,z).call(this,null,e.opcodes.CONNECT);return t.contentWindow.postMessage(i,n,[r.port2]),S(this,l,A).call(this,r.port1),S(this,c,x).call(this,i)}},{key:"close",value:function(){var t=this;if(!this.connected)throw new Error("Cannot close connection. Not connected.");return this.send(null,e.opcodes.CLOSE).then((function(e){return S(t,u,_).call(t),e}))}},{key:"send",value:function(e,t){var n=S(this,y,z).call(this,e,t);return S(this,a,M).call(this,n)}},{key:"waitForConnection",value:function(e){var t=this;return new Promise((function(n){e.addEventListener("message",(function(e){t.connected||(S(t,f,N).call(t,e),t.connected&&n())}))}))}},{key:"onMessage",value:function(e){this.listeners.global.push(e)}},{key:"onError",value:function(e){var t,n;n=e,function(e,t,n){if(t.set)t.set.call(e,n);else{if(!t.writable)throw new TypeError("attempted to set read only private field");t.value=n}}(t=this,C(t,o,"set"),n)}}],n&&g(t.prototype,n),Object.defineProperty(t,"prototype",{writable:!1}),e}(),j(i,"opcodes",{CONNECT:"messenger::connect",CLOSE:"messenger::close",ACK:"messenger::ack",MESSAGE:"messenger::message"}),i);function M(e){return this.connected?this.port.postMessage(e):this.messageBuffer.push(e),S(this,c,x).call(this,e)}function T(e){this.options.ack&&this.send(e,W.opcodes.ACK)}function x(e){var t=this;return new Promise((function(n,r){if(!t.options.ack||e.opcode===W.opcodes.ACK)return n(e);var i=setTimeout((function(){delete t.listeners.ack[e.id],r("ACK not received")}),t.options.timeout);t.listeners.ack[e.id]=function(){clearTimeout(i),delete t.listeners.ack[e.id],n(e)}}))}function A(e){for(this.port=e,this.port.onmessage=S(this,f,N).bind(this),this.port.onmessageerror=S(this,h,B).bind(this),this.connected=!0;this.messageBuffer.length>0;)S(this,a,M).call(this,this.messageBuffer.shift()).catch(S(this,h,B))}function _(){this.port.close(),this.connected=!1}function N(e){var t=e.data;switch(t.opcode){case W.opcodes.CONNECT:S(this,l,A).call(this,e.ports[0]),S(this,s,T).call(this,t.id);break;case W.opcodes.CLOSE:S(this,s,T).call(this,t.id),S(this,u,_).call(this);break;case W.opcodes.ACK:S(this,p,L).call(this,t.payload);break;default:S(this,s,T).call(this,t.id),S(this,d,R).call(this,t)}}function B(e){if(P(this,o)){var t=e.data;P(this,o).call(this,t)}}function L(e){this.listeners.ack[e]&&this.listeners.ack[e](e)}function R(e){this.listeners.global.forEach((function(t){return t(e)}))}function z(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:W.opcodes.MESSAGE;return{id:(0,v.default)(),opcode:t,payload:e}}var K=W;t.Z=K},100:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;t.default=function(){var e=new Uint32Array(4);return crypto.getRandomValues(e),Array.from(e).concat([performance.now()]).map((function(e){return e.toString(36)})).join("")}}},t={};function n(r){var i=t[r];if(void 0!==i)return i.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var r={};return(()=>{n.d(r,{default:()=>S});var e,t,i,o,a,s,c,l,u,f=n(625);function h(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function p(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function d(e,t){v(e,t),t.add(e)}function y(e,t,n){v(e,t),t.set(e,n)}function v(e,t){if(t.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")}function w(e,t,n){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return n}function b(e,t){return function(e,t){return t.get?t.get.call(e):t.value}(e,g(e,t,"get"))}function m(e,t,n){return function(e,t,n){if(t.set)t.set.call(e,n);else{if(!t.writable)throw new TypeError("attempted to set read only private field");t.value=n}}(e,g(e,t,"set"),n),n}function g(e,t,n){if(!t.has(e))throw new TypeError("attempted to "+n+" private field on non-instance");return t.get(e)}function k(){var e=document.createElement("iframe");return e.hidden=!1,e.style.border="none",e.height="250px",e.src="".concat(b(this,i),"/payments/ui"),e.width="100%",e}function O(e){"error"===e.payload.type&&("function"==typeof b(this,c)&&b(this,c).call(this,e.payload),"function"==typeof b(this,s)&&b(this,s).call(this,e.payload)),"token"===e.payload.type&&"function"==typeof b(this,a)&&b(this,a).call(this,e.payload.value),m(this,a,null),m(this,s,null)}const E=(e=new WeakMap,t=new WeakMap,i=new WeakMap,o=new WeakMap,a=new WeakMap,s=new WeakMap,c=new WeakMap,l=new WeakSet,u=new WeakSet,function(){function n(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",p=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";h(this,n),d(this,u),d(this,l),y(this,e,{writable:!0,value:!1}),y(this,t,{writable:!0,value:null}),y(this,i,{writable:!0,value:null}),y(this,o,{writable:!0,value:null}),y(this,a,{writable:!0,value:null}),y(this,s,{writable:!0,value:null}),y(this,c,{writable:!0,value:null}),m(this,t,new f.Z),m(this,i,r),m(this,o,p),b(this,t).onMessage(w(this,u,O).bind(this))}var r,v;return r=n,(v=[{key:"onError",value:function(e){m(this,c,e)}},{key:"tokenize",value:function(){var e=this;return b(this,a)?new Error("Card is already being tokenized"):new Promise((function(n,r){b(e,t).send({type:"tokenize"}).catch(r),m(e,a,n),m(e,s,r)}))}},{key:"mount",value:function(n){var r=this;if("string"==typeof n&&(n=document.querySelector(n)),!n||!n instanceof HTMLElement)throw new Error("Invalid mount target");return new Promise((function(i,a){var s=w(r,l,k).call(r);n.appendChild(s),m(r,e,!0),s.addEventListener("load",(function(){setTimeout((function(){b(r,t).connect(s).then(b(r,t).send({type:"init",key:b(r,o)}).then(i).catch(a)).catch(a)}),300)}))}))}}])&&p(r.prototype,v),Object.defineProperty(r,"prototype",{writable:!1}),n}());function j(e,t,n){return j=P()?Reflect.construct.bind():function(e,t,n){var r=[null];r.push.apply(r,t);var i=new(Function.bind.apply(e,r));return n&&C(i,n.prototype),i},j.apply(null,arguments)}function P(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}function C(e,t){return C=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},C(e,t)}const S={card:function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return j(E,t)}}})(),r.default})()));
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _channelMessenger = _interopRequireDefault(require("@mladenilic/channel-messenger"));
|
|
9
|
+
|
|
10
|
+
var _mounted, _messenger, _baseUrl, _clientKey, _tokenResolve, _tokenReject, _errorHandler, _buildIframe, _onMessage;
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
|
|
14
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
15
|
+
|
|
16
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
17
|
+
|
|
18
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
19
|
+
|
|
20
|
+
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
|
|
21
|
+
|
|
22
|
+
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
23
|
+
|
|
24
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
25
|
+
|
|
26
|
+
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
|
|
27
|
+
|
|
28
|
+
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
29
|
+
|
|
30
|
+
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
31
|
+
|
|
32
|
+
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
33
|
+
|
|
34
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
35
|
+
|
|
36
|
+
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
|
|
37
|
+
|
|
38
|
+
var Card = (_mounted = /*#__PURE__*/new WeakMap(), _messenger = /*#__PURE__*/new WeakMap(), _baseUrl = /*#__PURE__*/new WeakMap(), _clientKey = /*#__PURE__*/new WeakMap(), _tokenResolve = /*#__PURE__*/new WeakMap(), _tokenReject = /*#__PURE__*/new WeakMap(), _errorHandler = /*#__PURE__*/new WeakMap(), _buildIframe = /*#__PURE__*/new WeakSet(), _onMessage = /*#__PURE__*/new WeakSet(), /*#__PURE__*/function () {
|
|
39
|
+
function Card() {
|
|
40
|
+
var baseUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
41
|
+
var clientKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
42
|
+
|
|
43
|
+
_classCallCheck(this, Card);
|
|
44
|
+
|
|
45
|
+
_classPrivateMethodInitSpec(this, _onMessage);
|
|
46
|
+
|
|
47
|
+
_classPrivateMethodInitSpec(this, _buildIframe);
|
|
48
|
+
|
|
49
|
+
_classPrivateFieldInitSpec(this, _mounted, {
|
|
50
|
+
writable: true,
|
|
51
|
+
value: false
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
_classPrivateFieldInitSpec(this, _messenger, {
|
|
55
|
+
writable: true,
|
|
56
|
+
value: null
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
_classPrivateFieldInitSpec(this, _baseUrl, {
|
|
60
|
+
writable: true,
|
|
61
|
+
value: null
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
_classPrivateFieldInitSpec(this, _clientKey, {
|
|
65
|
+
writable: true,
|
|
66
|
+
value: null
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
_classPrivateFieldInitSpec(this, _tokenResolve, {
|
|
70
|
+
writable: true,
|
|
71
|
+
value: null
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
_classPrivateFieldInitSpec(this, _tokenReject, {
|
|
75
|
+
writable: true,
|
|
76
|
+
value: null
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
_classPrivateFieldInitSpec(this, _errorHandler, {
|
|
80
|
+
writable: true,
|
|
81
|
+
value: null
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
_classPrivateFieldSet(this, _messenger, new _channelMessenger["default"]());
|
|
85
|
+
|
|
86
|
+
_classPrivateFieldSet(this, _baseUrl, baseUrl);
|
|
87
|
+
|
|
88
|
+
_classPrivateFieldSet(this, _clientKey, clientKey);
|
|
89
|
+
|
|
90
|
+
_classPrivateFieldGet(this, _messenger).onMessage(_classPrivateMethodGet(this, _onMessage, _onMessage2).bind(this));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_createClass(Card, [{
|
|
94
|
+
key: "onError",
|
|
95
|
+
value: function onError(cb) {
|
|
96
|
+
_classPrivateFieldSet(this, _errorHandler, cb);
|
|
97
|
+
}
|
|
98
|
+
}, {
|
|
99
|
+
key: "tokenize",
|
|
100
|
+
value: function tokenize() {
|
|
101
|
+
var _this = this;
|
|
102
|
+
|
|
103
|
+
if (_classPrivateFieldGet(this, _tokenResolve)) return new Error('Card is already being tokenized');
|
|
104
|
+
return new Promise(function (resolve, reject) {
|
|
105
|
+
_classPrivateFieldGet(_this, _messenger).send({
|
|
106
|
+
type: 'tokenize'
|
|
107
|
+
})["catch"](reject);
|
|
108
|
+
|
|
109
|
+
_classPrivateFieldSet(_this, _tokenResolve, resolve);
|
|
110
|
+
|
|
111
|
+
_classPrivateFieldSet(_this, _tokenReject, reject);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}, {
|
|
115
|
+
key: "mount",
|
|
116
|
+
value: function mount(target) {
|
|
117
|
+
var _this2 = this;
|
|
118
|
+
|
|
119
|
+
if (typeof target === 'string') {
|
|
120
|
+
target = document.querySelector(target);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!target || !target instanceof HTMLElement) throw new Error('Invalid mount target');
|
|
124
|
+
return new Promise(function (resolve, reject) {
|
|
125
|
+
var iframe = _classPrivateMethodGet(_this2, _buildIframe, _buildIframe2).call(_this2);
|
|
126
|
+
|
|
127
|
+
target.appendChild(iframe);
|
|
128
|
+
|
|
129
|
+
_classPrivateFieldSet(_this2, _mounted, true);
|
|
130
|
+
|
|
131
|
+
iframe.addEventListener('load', function () {
|
|
132
|
+
setTimeout(function () {
|
|
133
|
+
_classPrivateFieldGet(_this2, _messenger).connect(iframe).then(_classPrivateFieldGet(_this2, _messenger).send({
|
|
134
|
+
type: 'init',
|
|
135
|
+
key: _classPrivateFieldGet(_this2, _clientKey)
|
|
136
|
+
}).then(resolve)["catch"](reject))["catch"](reject);
|
|
137
|
+
}, 300);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}]);
|
|
142
|
+
|
|
143
|
+
return Card;
|
|
144
|
+
}());
|
|
145
|
+
|
|
146
|
+
function _buildIframe2() {
|
|
147
|
+
var iframe = document.createElement('iframe');
|
|
148
|
+
iframe.hidden = false;
|
|
149
|
+
iframe.style.border = 'none';
|
|
150
|
+
iframe.height = '250px';
|
|
151
|
+
iframe.src = "".concat(_classPrivateFieldGet(this, _baseUrl), "/payments/ui");
|
|
152
|
+
iframe.width = '100%';
|
|
153
|
+
return iframe;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function _onMessage2(message) {
|
|
157
|
+
if (message.payload.type === 'error') {
|
|
158
|
+
if (typeof _classPrivateFieldGet(this, _errorHandler) === 'function') {
|
|
159
|
+
_classPrivateFieldGet(this, _errorHandler).call(this, message.payload);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (typeof _classPrivateFieldGet(this, _tokenReject) === 'function') {
|
|
163
|
+
_classPrivateFieldGet(this, _tokenReject).call(this, message.payload);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (message.payload.type === 'token' && typeof _classPrivateFieldGet(this, _tokenResolve) === 'function') {
|
|
168
|
+
_classPrivateFieldGet(this, _tokenResolve).call(this, message.payload.value);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
_classPrivateFieldSet(this, _tokenResolve, null);
|
|
172
|
+
|
|
173
|
+
_classPrivateFieldSet(this, _tokenReject, null);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
var _default = Card;
|
|
177
|
+
exports["default"] = _default;
|
package/lib/payments.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _card = _interopRequireDefault(require("./payments/card"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
|
+
|
|
12
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
13
|
+
|
|
14
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
15
|
+
|
|
16
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
17
|
+
|
|
18
|
+
var _default = {
|
|
19
|
+
card: function card() {
|
|
20
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
21
|
+
args[_key] = arguments[_key];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return _construct(_card["default"], args);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
exports["default"] = _default;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@healnow/payments-ui",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/payments-ui.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "webpack serve --mode development",
|
|
8
|
+
"build:dist": "webpack",
|
|
9
|
+
"build:lib": "babel src -d lib",
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"author": "Mladen Ilic <mladen@healnow.io>",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@mladenilic/channel-messenger": "^1.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@babel/cli": "^7.18.10",
|
|
23
|
+
"@babel/core": "^7.19.1",
|
|
24
|
+
"@babel/preset-env": "^7.19.1",
|
|
25
|
+
"babel-loader": "^8.2.5",
|
|
26
|
+
"webpack": "^5.74.0",
|
|
27
|
+
"webpack-cli": "^4.10.0",
|
|
28
|
+
"webpack-dev-server": "^4.11.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import ChannelMessenger from '@mladenilic/channel-messenger';
|
|
2
|
+
|
|
3
|
+
const Card = class {
|
|
4
|
+
#mounted = false;
|
|
5
|
+
#messenger = null;
|
|
6
|
+
#baseUrl = null;
|
|
7
|
+
#clientKey = null;
|
|
8
|
+
#tokenResolve = null;
|
|
9
|
+
#tokenReject = null;
|
|
10
|
+
#errorHandler = null;
|
|
11
|
+
|
|
12
|
+
constructor(baseUrl = '', clientKey = '') {
|
|
13
|
+
this.#messenger = new ChannelMessenger();
|
|
14
|
+
this.#baseUrl = baseUrl;
|
|
15
|
+
this.#clientKey = clientKey;
|
|
16
|
+
|
|
17
|
+
this.#messenger.onMessage(this.#onMessage.bind(this));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
onError(cb) {
|
|
21
|
+
this.#errorHandler = cb;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
tokenize() {
|
|
25
|
+
if (this.#tokenResolve) return new Error('Card is already being tokenized');
|
|
26
|
+
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
this.#messenger.send({ type: 'tokenize' }).catch(reject);
|
|
29
|
+
|
|
30
|
+
this.#tokenResolve = resolve;
|
|
31
|
+
this.#tokenReject = reject;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
mount(target) {
|
|
36
|
+
if (typeof target === 'string') {
|
|
37
|
+
target = document.querySelector(target);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!target || !target instanceof HTMLElement) throw new Error('Invalid mount target');
|
|
41
|
+
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const iframe = this.#buildIframe();
|
|
44
|
+
target.appendChild(iframe);
|
|
45
|
+
this.#mounted = true;
|
|
46
|
+
|
|
47
|
+
iframe.addEventListener('load', () => {
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
this.#messenger.connect(iframe).then(
|
|
50
|
+
this.#messenger.send({ type: 'init', key: this.#clientKey })
|
|
51
|
+
.then(resolve).catch(reject)
|
|
52
|
+
).catch(reject)
|
|
53
|
+
}, 300);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#buildIframe() {
|
|
59
|
+
const iframe = document.createElement('iframe');
|
|
60
|
+
|
|
61
|
+
iframe.hidden = false;
|
|
62
|
+
iframe.style.border = 'none';
|
|
63
|
+
iframe.height = '250px';
|
|
64
|
+
iframe.src = `${this.#baseUrl}/payments/ui`;
|
|
65
|
+
iframe.width = '100%';
|
|
66
|
+
|
|
67
|
+
return iframe;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#onMessage(message) {
|
|
71
|
+
if (message.payload.type === 'error') {
|
|
72
|
+
if (typeof this.#errorHandler === 'function') {
|
|
73
|
+
this.#errorHandler(message.payload);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof this.#tokenReject === 'function') {
|
|
77
|
+
this.#tokenReject(message.payload);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (message.payload.type === 'token' && typeof this.#tokenResolve === 'function') {
|
|
82
|
+
this.#tokenResolve(message.payload.value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.#tokenResolve = null;
|
|
86
|
+
this.#tokenReject = null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default Card;
|