@danidoble/webserial 4.5.0-alpha.3 → 4.5.0-alpha.4

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/pinpax.cjs DELETED
@@ -1,4 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("./kernel-n1ma1Kh_.cjs"),c=require("./devices-CHRclG9I.cjs"),s=require("./relay-E3NCcSjS.cjs");class _ extends p.Kernel{__pinpax__={server:"DEV",businessId:null,encryptionKey:null,apiKey:null,asyncResponses:{voucher:null,sale:null},waiting:{voucher:!1,sale:!1,sale_init:"idle",sale_login:"idle"}};constructor({filters:i=null,config_port:e={baudRate:115200,dataBits:8,stopBits:1,parity:"none",bufferSize:32768,flowControl:"none"},no_device:t=1,device_listen_on_channel:a=1,socket:n=!1}={filters:null,config_port:{baudRate:115200,dataBits:8,stopBits:1,parity:"none",bufferSize:32768,flowControl:"none"},no_device:1,device_listen_on_channel:1,socket:!1}){if(super({filters:i,config_port:e,no_device:t,device_listen_on_channel:a,socket:n}),this.__internal__.device.type="pinpax",c.Devices.getCustom(this.typeDevice,t))throw new Error(`Device ${this.typeDevice} ${t} already exists`);this.__internal__.time.response_connection=4e3,this.__internal__.time.response_general=3e3,this.__internal__.serial.delay_first_connection=1e3,this.__internal__.serial.response.replacer="",this.__internal__.serial.response.limiter=`\r
2
- `,this.#t(),c.Devices.add(this),this.getResponseAsString()}#t(){const i=["buttons-status","init-app","connectMessage","voucher","info","keep-alive","reset-app","get-config","payment","error","refund","login"];for(const e of i)this.serialRegisterAvailableListener(e)}set businessId(i){this.__pinpax__.businessId=i}get businessId(){return this.__pinpax__.businessId}set encryptionKey(i){this.__pinpax__.encryptionKey=i}get encryptionKey(){return this.__pinpax__.encryptionKey}set apiKey(i){this.__pinpax__.apiKey=i}get apiKey(){return this.__pinpax__.apiKey}set server(i){this.__pinpax__.server=i}get server(){return this.__pinpax__.server}serialMessage(i){let e=null;try{e=JSON.parse(i),e.request||(e.request=this.lastAction),e.name||(e.name=this.lastAction)}catch(t){console.error("Error parsing response",t,i),this.dispatch("serial:message",i);return}switch(e.response){case"INIT":this.dispatch("init",{name:"INIT",request:this.lastAction,status:"ok"}),this.__pinpax__.waiting.sale_init==="waiting"&&(this.__pinpax__.waiting.sale_init="received");break;case"INITAPP":this.dispatch("init-app",{name:"INITAPP",request:this.lastAction,status:"started"}),this.__pinpax__.waiting.sale_init==="waiting"?this.__pinpax__.waiting.sale_init="received":this.#n().then(()=>{});break;case"CONNECT":this.dispatch("connectMessage",{name:"CONNECT",request:this.lastAction,status:"connected"}),this.#r().then(()=>{});break;case"LOGIN":e.name||(e.name="LOGIN"),this.__pinpax__.waiting.sale_login==="waiting"&&(this.__pinpax__.waiting.sale_login="received"),this.dispatch("login",e);break;case"LASTVOUCHER":e.name||(e.name="LASTVOUCHER"),this.dispatch("voucher",e),this.__pinpax__.waiting.voucher&&(this.__pinpax__.asyncResponses.voucher=e,this.__pinpax__.waiting.voucher=!1);break;case"DEVICEINFO":e.name||(e.name="DEVICEINFO"),this.dispatch("info",e);break;case"KEEPALIVE":this.dispatch("keep-alive",{name:"KEEPALIVE",request:this.lastAction,status:"alive"});break;case"RESETAPP":this.dispatch("reset-app",{name:"RESETAPP",request:this.lastAction,status:"accepted"});break;case"GETCONFIG":e.name||(e.name="GETCONFIG"),this.dispatch("get-config",e);break;case"HIDEBUTTONS":this.dispatch("buttons-status",{name:"HIDEBUTTONS",request:this.lastAction,hidden:!0});break;case"SHOWBUTTONS":this.dispatch("buttons-status",{name:"SHOWBUTTONS",request:this.lastAction,hidden:!1});break;case"PAYMENT_PROCESS":e.name||(e.name="PAYMENT_PROCESS"),this.dispatch("payment",{name:"PAYMENT_PROCESS",request:this.lastAction,status:"starting",amount:e.amount,reference:e.referecence});break;case"INSERT_CARD":this.dispatch("payment",{name:"INSERT_CARD",request:this.lastAction,status:"insert card"});break;case"CARD_DATA":e.name||(e.name="CARD_DATA"),this.dispatch("payment",{name:"CARD_DATA",request:this.lastAction,status:"card data",amount:e.amount,cardHolderName:e.cardHolderName,ccMark:e.ccMark,ccType:e.ccType,currency:e.currency,maskedPan:e.maskedPan,readingType:e.readingType});break;case"MERCHANT":e.name||(e.name="MERCHANT"),this.dispatch("payment",{request:this.lastAction,status:"merchant",afiliation:e.afiliation,alias:e.alias,amountCashBackCommission:e.amountCashBackCommission,currency:e.currency,description:e.description,hasCashBack:e.hasCashBack,limitCashBackAmount:e.limitCashBackAmount,merchant:e.merchant,months:e.months,name:e.name});break;case"TRANSACTION_RESULT":e.name||(e.name="TRANSACTION_RESULT"),this.#o({status:"result",approved:e.approved,acquirer:e.acquirer,address:e.address,amount:e.amount,amountCashback:e.amountCashback,appIDLabel:e.appIDLabel,appId:e.appId,arqc:e.arqc,auth:e.auth,ccBin:e.ccBin,ccExpirationDate:e.ccExpirationDate,ccName:e.ccName,ccNumber:e.ccNumber,ccType:e.ccType,comissionCashback:e.comissionCashback,currency:e.currency,date:e.date,errorCode:e.errorCode,errorDescription:e.errorDescription,folio:e.folio,id:e.id,merchantName:e.merchantName,msiLabel:e.msiLabel,operation:e.operation,pin:e.pin,qps:e.qps,reference:e.reference,response:e.response,source:e.source,sourceLabel:e.sourceLabel,time:e.time});break;case"ERROR":e.name||(e.name="ERROR"),this.__pinpax__.waiting.sale&&(this.__pinpax__.asyncResponses.sale={status:"error",approved:!1,response:e}),this.dispatch("error",{name:"ERROR",request:this.lastAction,status:"error",response:e}),this.dispatch("payment",{name:"ERROR",request:this.lastAction,status:"error",response:e});break;case"REFUND":this.dispatch("refund",{name:"ERROR",request:this.lastAction,status:"refund",response:e});break}this.dispatch("serial:message",e)}serialSetConnectionConstant(){return s.s.connection()}softReload(){super.softReload(),this.__pinpax__.waiting.sale=!1,this.__pinpax__.waiting.voucher=!1,this.__pinpax__.asyncResponses.sale=null,this.__pinpax__.asyncResponses.voucher=null}async sendCustomCode(i={}){if(typeof i!="object")throw new Error("Invalid object");if(i.constructor!==Object)throw new Error("Invalid object");if(Object.keys(i).length===0)throw new Error("Empty object");if(i.action===void 0||i.action===null)throw new Error("Invalid object add action");const e=JSON.stringify(i),t=this.parseStringToBytes(e,`\r
3
- `);await this.appendToQueue(this.stringArrayToUint8Array(t),"custom")}async connectMessage(){await this.appendToQueue(s.s.connect(),"connect:message")}async#e(){if(this.isDisconnected)throw new Error("Device is disconnected");return this.queue.length===0?!0:new Promise(i=>{const e=setInterval(()=>{this.queue.length===0&&(clearInterval(e),i(!0))},500)})}async#i({type:i="production"}={}){const e=s.s.readQR({type:i});return await this.appendToQueue(e,"read-qr")}async#s(){await this.appendToQueue(s.s.forceHide(),"force-hide")}async#a(){await this.appendToQueue(s.s.forceShow(),"force-show")}async#n(){await this.connectMessage()}async#r(){await this.hideButtons()}#o(i){this.dispatch("payment",i),this.__pinpax__.waiting.sale&&(this.__pinpax__.asyncResponses.sale=i)}cancelSaleRequestInProcess(){this.__pinpax__.waiting.sale&&(this.__pinpax__.asyncResponses.sale={status:"error",approved:!1,response:{}})}async#c(){return new Promise((i,e)=>{const t=setTimeout(()=>{this.__pinpax__.waiting.sale_login==="waiting"&&(this.__pinpax__.waiting.sale_login="timeout",e(new Error("Login timeout")))},1e4),a=setInterval(()=>{this.__pinpax__.waiting.sale_login==="received"&&(clearTimeout(t),clearInterval(a),i(!0))},100)})}async#p(){return new Promise((i,e)=>{const t=setTimeout(()=>{this.__pinpax__.waiting.sale_init==="waiting"&&(this.__pinpax__.waiting.sale_init="timeout",e(new Error("Init timeout")))},1e4),a=setInterval(()=>{this.__pinpax__.waiting.sale_init==="received"&&(clearTimeout(t),clearInterval(a),i(!0))},100)})}async makeSale({amount:i=0,reference:e=null}={amount:0,reference:null}){if(this.isDisconnected)throw new Error("Device is disconnected");if(this.__pinpax__.waiting.sale)throw new Error("Already waiting for sale response");const t=s.s.makeSale({amount:i,reference:e});this.__pinpax__.waiting.sale=!0,this.__pinpax__.asyncResponses.sale=null,this.queue.length>0&&await this.#e();let a=!1;try{this.__pinpax__.waiting.sale_login="waiting",await this.login(),await this.#c(),this.__pinpax__.waiting.sale_login="idle",this.__pinpax__.waiting.sale_init="waiting",await this.init(),await this.#p(),this.__pinpax__.waiting.sale_init="idle"}catch(n){a=!0,this.__pinpax__.waiting.sale_login="idle",this.__pinpax__.waiting.sale_init="idle",this.__pinpax__.waiting.sale=!1,this.dispatch("error",{name:"ERROR",request:this.lastAction,status:"error",response:"Error during login/init before sale",error:n})}return a?!1:(await this.appendToQueue(t,"make-sale"),new Promise(n=>{const r=setInterval(()=>{if(this.__pinpax__.asyncResponses.sale){const o=this.__pinpax__.asyncResponses.sale;this.__pinpax__.asyncResponses.sale=null,this.__pinpax__.waiting.sale=!1,clearInterval(r),n(o.approved)}},100)}))}async getVoucher({folio:i=null}={}){if(this.isDisconnected)throw new Error("Device is disconnected");if(this.__pinpax__.waiting.voucher)throw new Error("Already waiting for voucher");if(!i)throw new Error("Folio is required");this.__pinpax__.waiting.voucher=!0,this.__pinpax__.asyncResponses.voucher=null;const e=s.s.getVoucher({folio:i});return this.queue.length>0&&await this.#e(),await this.appendToQueue(e,"get-voucher"),new Promise((t,a)=>{const n=setTimeout(()=>{this.__pinpax__.waiting.voucher=!1,a("Timeout")},1e4),r=setInterval(()=>{if(this.__pinpax__.asyncResponses.voucher){const o=this.__pinpax__.asyncResponses.voucher;this.__pinpax__.asyncResponses.voucher=null,this.__pinpax__.waiting.voucher=!1,clearInterval(r),clearTimeout(n),t(o.voucher)}},100)})}async info(){await this.appendToQueue(s.s.info(),"info")}async keepAlive(){await this.appendToQueue(s.s.keepAlive(),"keep-alive")}async restartApp(){await this.appendToQueue(s.s.restartApp(),"reset-app")}async getConfig(){return await this.appendToQueue(s.s.getConfig(),"get-config")}async hideButtons(){return await this.appendToQueue(s.s.hideButtons(),"hide-buttons"),await this.#s()}async showButtons(){return await this.appendToQueue(s.s.showButtons(),"show-buttons"),await this.#a()}async demo(){return console.warn("RUNNING DEMO APP, BE CAREFUL"),await this.appendToQueue(s.s.demo(),"demo")}async refund({amount:i=0,folio:e=null,auth:t=null}={}){return await this.appendToQueue(s.s.refund({amount:i,folio:e,auth:t}),"refund")}async readProductionQR(){return await this.#i({type:"production"})}async readQualityAssuranceQR(){return this.#i({type:"QA"})}async exit(){return await this.appendToQueue(s.s.exit(),"exit-app")}async init(){await this.appendToQueue(s.s.init(),"init-app")}async login(){if(!this.apiKey||!this.businessId||!this.encryptionKey||!this.server)throw new Error("Invalid data to login check apiKey, businessId, encryptionKey, server");const i=JSON.stringify({action:"LOGIN_MIT",server:this.__pinpax__.server,business_id:this.__pinpax__.businessId,encryption_key:this.__pinpax__.encryptionKey,api_key:this.__pinpax__.apiKey}),e=this.parseStringToBytes(i,`\r
4
- `);await this.appendToQueue(e,"login")}}exports.PinPax=_;
package/dist/pinpax.js DELETED
@@ -1,408 +0,0 @@
1
- import { K as p } from "./kernel-DGZs5b7J.js";
2
- import { D as c } from "./devices-BGEK9inl.js";
3
- import { s } from "./relay-DP8PLsDP.js";
4
- class d extends p {
5
- __pinpax__ = {
6
- server: "DEV",
7
- businessId: null,
8
- encryptionKey: null,
9
- apiKey: null,
10
- asyncResponses: {
11
- voucher: null,
12
- sale: null
13
- },
14
- waiting: {
15
- voucher: !1,
16
- sale: !1,
17
- sale_init: "idle",
18
- sale_login: "idle"
19
- }
20
- };
21
- constructor({
22
- filters: i = null,
23
- config_port: e = {
24
- baudRate: 115200,
25
- dataBits: 8,
26
- stopBits: 1,
27
- parity: "none",
28
- bufferSize: 32768,
29
- flowControl: "none"
30
- },
31
- no_device: t = 1,
32
- device_listen_on_channel: a = 1,
33
- socket: n = !1
34
- } = {
35
- filters: null,
36
- config_port: {
37
- baudRate: 115200,
38
- dataBits: 8,
39
- stopBits: 1,
40
- parity: "none",
41
- bufferSize: 32768,
42
- flowControl: "none"
43
- },
44
- no_device: 1,
45
- device_listen_on_channel: 1,
46
- socket: !1
47
- }) {
48
- if (super({ filters: i, config_port: e, no_device: t, device_listen_on_channel: a, socket: n }), this.__internal__.device.type = "pinpax", c.getCustom(this.typeDevice, t))
49
- throw new Error(`Device ${this.typeDevice} ${t} already exists`);
50
- this.__internal__.time.response_connection = 4e3, this.__internal__.time.response_general = 3e3, this.__internal__.serial.delay_first_connection = 1e3, this.__internal__.serial.response.replacer = "", this.__internal__.serial.response.limiter = `\r
51
- `, this.#t(), c.add(this), this.getResponseAsString();
52
- }
53
- #t() {
54
- const i = [
55
- "buttons-status",
56
- "init-app",
57
- "connectMessage",
58
- "voucher",
59
- "info",
60
- "keep-alive",
61
- "reset-app",
62
- "get-config",
63
- "payment",
64
- "error",
65
- "refund",
66
- "login"
67
- ];
68
- for (const e of i)
69
- this.serialRegisterAvailableListener(e);
70
- }
71
- set businessId(i) {
72
- this.__pinpax__.businessId = i;
73
- }
74
- get businessId() {
75
- return this.__pinpax__.businessId;
76
- }
77
- set encryptionKey(i) {
78
- this.__pinpax__.encryptionKey = i;
79
- }
80
- get encryptionKey() {
81
- return this.__pinpax__.encryptionKey;
82
- }
83
- set apiKey(i) {
84
- this.__pinpax__.apiKey = i;
85
- }
86
- get apiKey() {
87
- return this.__pinpax__.apiKey;
88
- }
89
- set server(i) {
90
- this.__pinpax__.server = i;
91
- }
92
- get server() {
93
- return this.__pinpax__.server;
94
- }
95
- serialMessage(i) {
96
- let e = null;
97
- try {
98
- e = JSON.parse(i), e.request || (e.request = this.lastAction), e.name || (e.name = this.lastAction);
99
- } catch (t) {
100
- console.error("Error parsing response", t, i), this.dispatch("serial:message", i);
101
- return;
102
- }
103
- switch (e.response) {
104
- case "INIT":
105
- this.dispatch("init", { name: "INIT", request: this.lastAction, status: "ok" }), this.__pinpax__.waiting.sale_init === "waiting" && (this.__pinpax__.waiting.sale_init = "received");
106
- break;
107
- case "INITAPP":
108
- this.dispatch("init-app", { name: "INITAPP", request: this.lastAction, status: "started" }), this.__pinpax__.waiting.sale_init === "waiting" ? this.__pinpax__.waiting.sale_init = "received" : this.#n().then(() => {
109
- });
110
- break;
111
- case "CONNECT":
112
- this.dispatch("connectMessage", { name: "CONNECT", request: this.lastAction, status: "connected" }), this.#r().then(() => {
113
- });
114
- break;
115
- case "LOGIN":
116
- e.name || (e.name = "LOGIN"), this.__pinpax__.waiting.sale_login === "waiting" && (this.__pinpax__.waiting.sale_login = "received"), this.dispatch("login", e);
117
- break;
118
- case "LASTVOUCHER":
119
- e.name || (e.name = "LASTVOUCHER"), this.dispatch("voucher", e), this.__pinpax__.waiting.voucher && (this.__pinpax__.asyncResponses.voucher = e, this.__pinpax__.waiting.voucher = !1);
120
- break;
121
- case "DEVICEINFO":
122
- e.name || (e.name = "DEVICEINFO"), this.dispatch("info", e);
123
- break;
124
- case "KEEPALIVE":
125
- this.dispatch("keep-alive", { name: "KEEPALIVE", request: this.lastAction, status: "alive" });
126
- break;
127
- case "RESETAPP":
128
- this.dispatch("reset-app", { name: "RESETAPP", request: this.lastAction, status: "accepted" });
129
- break;
130
- case "GETCONFIG":
131
- e.name || (e.name = "GETCONFIG"), this.dispatch("get-config", e);
132
- break;
133
- case "HIDEBUTTONS":
134
- this.dispatch("buttons-status", { name: "HIDEBUTTONS", request: this.lastAction, hidden: !0 });
135
- break;
136
- case "SHOWBUTTONS":
137
- this.dispatch("buttons-status", { name: "SHOWBUTTONS", request: this.lastAction, hidden: !1 });
138
- break;
139
- case "PAYMENT_PROCESS":
140
- e.name || (e.name = "PAYMENT_PROCESS"), this.dispatch("payment", {
141
- name: "PAYMENT_PROCESS",
142
- request: this.lastAction,
143
- status: "starting",
144
- amount: e.amount,
145
- reference: e.referecence
146
- });
147
- break;
148
- case "INSERT_CARD":
149
- this.dispatch("payment", { name: "INSERT_CARD", request: this.lastAction, status: "insert card" });
150
- break;
151
- case "CARD_DATA":
152
- e.name || (e.name = "CARD_DATA"), this.dispatch("payment", {
153
- name: "CARD_DATA",
154
- request: this.lastAction,
155
- status: "card data",
156
- amount: e.amount,
157
- cardHolderName: e.cardHolderName,
158
- ccMark: e.ccMark,
159
- ccType: e.ccType,
160
- currency: e.currency,
161
- maskedPan: e.maskedPan,
162
- readingType: e.readingType
163
- });
164
- break;
165
- case "MERCHANT":
166
- e.name || (e.name = "MERCHANT"), this.dispatch("payment", {
167
- request: this.lastAction,
168
- status: "merchant",
169
- afiliation: e.afiliation,
170
- alias: e.alias,
171
- amountCashBackCommission: e.amountCashBackCommission,
172
- currency: e.currency,
173
- description: e.description,
174
- hasCashBack: e.hasCashBack,
175
- limitCashBackAmount: e.limitCashBackAmount,
176
- merchant: e.merchant,
177
- months: e.months,
178
- name: e.name
179
- });
180
- break;
181
- case "TRANSACTION_RESULT":
182
- e.name || (e.name = "TRANSACTION_RESULT"), this.#o({
183
- status: "result",
184
- approved: e.approved,
185
- acquirer: e.acquirer,
186
- address: e.address,
187
- amount: e.amount,
188
- amountCashback: e.amountCashback,
189
- appIDLabel: e.appIDLabel,
190
- appId: e.appId,
191
- arqc: e.arqc,
192
- auth: e.auth,
193
- ccBin: e.ccBin,
194
- ccExpirationDate: e.ccExpirationDate,
195
- ccName: e.ccName,
196
- ccNumber: e.ccNumber,
197
- ccType: e.ccType,
198
- comissionCashback: e.comissionCashback,
199
- currency: e.currency,
200
- date: e.date,
201
- errorCode: e.errorCode,
202
- errorDescription: e.errorDescription,
203
- folio: e.folio,
204
- id: e.id,
205
- merchantName: e.merchantName,
206
- msiLabel: e.msiLabel,
207
- operation: e.operation,
208
- pin: e.pin,
209
- qps: e.qps,
210
- reference: e.reference,
211
- response: e.response,
212
- source: e.source,
213
- sourceLabel: e.sourceLabel,
214
- time: e.time
215
- });
216
- break;
217
- case "ERROR":
218
- e.name || (e.name = "ERROR"), this.__pinpax__.waiting.sale && (this.__pinpax__.asyncResponses.sale = {
219
- status: "error",
220
- approved: !1,
221
- response: e
222
- }), this.dispatch("error", { name: "ERROR", request: this.lastAction, status: "error", response: e }), this.dispatch("payment", { name: "ERROR", request: this.lastAction, status: "error", response: e });
223
- break;
224
- case "REFUND":
225
- this.dispatch("refund", { name: "ERROR", request: this.lastAction, status: "refund", response: e });
226
- break;
227
- }
228
- this.dispatch("serial:message", e);
229
- }
230
- serialSetConnectionConstant() {
231
- return s.connection();
232
- }
233
- softReload() {
234
- super.softReload(), this.__pinpax__.waiting.sale = !1, this.__pinpax__.waiting.voucher = !1, this.__pinpax__.asyncResponses.sale = null, this.__pinpax__.asyncResponses.voucher = null;
235
- }
236
- async sendCustomCode(i = {}) {
237
- if (typeof i != "object") throw new Error("Invalid object");
238
- if (i.constructor !== Object) throw new Error("Invalid object");
239
- if (Object.keys(i).length === 0) throw new Error("Empty object");
240
- if (i.action === void 0 || i.action === null) throw new Error("Invalid object add action");
241
- const e = JSON.stringify(i), t = this.parseStringToBytes(e, `\r
242
- `);
243
- await this.appendToQueue(this.stringArrayToUint8Array(t), "custom");
244
- }
245
- async connectMessage() {
246
- await this.appendToQueue(s.connect(), "connect:message");
247
- }
248
- async #e() {
249
- if (this.isDisconnected) throw new Error("Device is disconnected");
250
- return this.queue.length === 0 ? !0 : new Promise((i) => {
251
- const e = setInterval(() => {
252
- this.queue.length === 0 && (clearInterval(e), i(!0));
253
- }, 500);
254
- });
255
- }
256
- async #i({ type: i = "production" } = {}) {
257
- const e = s.readQR({ type: i });
258
- return await this.appendToQueue(e, "read-qr");
259
- }
260
- async #s() {
261
- await this.appendToQueue(s.forceHide(), "force-hide");
262
- }
263
- async #a() {
264
- await this.appendToQueue(s.forceShow(), "force-show");
265
- }
266
- async #n() {
267
- await this.connectMessage();
268
- }
269
- async #r() {
270
- await this.hideButtons();
271
- }
272
- #o(i) {
273
- this.dispatch("payment", i), this.__pinpax__.waiting.sale && (this.__pinpax__.asyncResponses.sale = i);
274
- }
275
- cancelSaleRequestInProcess() {
276
- this.__pinpax__.waiting.sale && (this.__pinpax__.asyncResponses.sale = {
277
- status: "error",
278
- approved: !1,
279
- response: {}
280
- });
281
- }
282
- async #c() {
283
- return new Promise((i, e) => {
284
- const t = setTimeout(() => {
285
- this.__pinpax__.waiting.sale_login === "waiting" && (this.__pinpax__.waiting.sale_login = "timeout", e(new Error("Login timeout")));
286
- }, 1e4), a = setInterval(() => {
287
- this.__pinpax__.waiting.sale_login === "received" && (clearTimeout(t), clearInterval(a), i(!0));
288
- }, 100);
289
- });
290
- }
291
- async #p() {
292
- return new Promise((i, e) => {
293
- const t = setTimeout(() => {
294
- this.__pinpax__.waiting.sale_init === "waiting" && (this.__pinpax__.waiting.sale_init = "timeout", e(new Error("Init timeout")));
295
- }, 1e4), a = setInterval(() => {
296
- this.__pinpax__.waiting.sale_init === "received" && (clearTimeout(t), clearInterval(a), i(!0));
297
- }, 100);
298
- });
299
- }
300
- async makeSale({
301
- amount: i = 0,
302
- reference: e = null
303
- } = {
304
- amount: 0,
305
- reference: null
306
- }) {
307
- if (this.isDisconnected) throw new Error("Device is disconnected");
308
- if (this.__pinpax__.waiting.sale) throw new Error("Already waiting for sale response");
309
- const t = s.makeSale({ amount: i, reference: e });
310
- this.__pinpax__.waiting.sale = !0, this.__pinpax__.asyncResponses.sale = null, this.queue.length > 0 && await this.#e();
311
- let a = !1;
312
- try {
313
- this.__pinpax__.waiting.sale_login = "waiting", await this.login(), await this.#c(), this.__pinpax__.waiting.sale_login = "idle", this.__pinpax__.waiting.sale_init = "waiting", await this.init(), await this.#p(), this.__pinpax__.waiting.sale_init = "idle";
314
- } catch (n) {
315
- a = !0, this.__pinpax__.waiting.sale_login = "idle", this.__pinpax__.waiting.sale_init = "idle", this.__pinpax__.waiting.sale = !1, this.dispatch("error", {
316
- name: "ERROR",
317
- request: this.lastAction,
318
- status: "error",
319
- response: "Error during login/init before sale",
320
- error: n
321
- });
322
- }
323
- return a ? !1 : (await this.appendToQueue(t, "make-sale"), new Promise((n) => {
324
- const r = setInterval(() => {
325
- if (this.__pinpax__.asyncResponses.sale) {
326
- const o = this.__pinpax__.asyncResponses.sale;
327
- this.__pinpax__.asyncResponses.sale = null, this.__pinpax__.waiting.sale = !1, clearInterval(r), n(o.approved);
328
- }
329
- }, 100);
330
- }));
331
- }
332
- async getVoucher({ folio: i = null } = {}) {
333
- if (this.isDisconnected) throw new Error("Device is disconnected");
334
- if (this.__pinpax__.waiting.voucher) throw new Error("Already waiting for voucher");
335
- if (!i) throw new Error("Folio is required");
336
- this.__pinpax__.waiting.voucher = !0, this.__pinpax__.asyncResponses.voucher = null;
337
- const e = s.getVoucher({ folio: i });
338
- return this.queue.length > 0 && await this.#e(), await this.appendToQueue(e, "get-voucher"), new Promise((t, a) => {
339
- const n = setTimeout(() => {
340
- this.__pinpax__.waiting.voucher = !1, a("Timeout");
341
- }, 1e4), r = setInterval(() => {
342
- if (this.__pinpax__.asyncResponses.voucher) {
343
- const o = this.__pinpax__.asyncResponses.voucher;
344
- this.__pinpax__.asyncResponses.voucher = null, this.__pinpax__.waiting.voucher = !1, clearInterval(r), clearTimeout(n), t(o.voucher);
345
- }
346
- }, 100);
347
- });
348
- }
349
- async info() {
350
- await this.appendToQueue(s.info(), "info");
351
- }
352
- async keepAlive() {
353
- await this.appendToQueue(s.keepAlive(), "keep-alive");
354
- }
355
- async restartApp() {
356
- await this.appendToQueue(s.restartApp(), "reset-app");
357
- }
358
- async getConfig() {
359
- return await this.appendToQueue(s.getConfig(), "get-config");
360
- }
361
- async hideButtons() {
362
- return await this.appendToQueue(s.hideButtons(), "hide-buttons"), await this.#s();
363
- }
364
- async showButtons() {
365
- return await this.appendToQueue(s.showButtons(), "show-buttons"), await this.#a();
366
- }
367
- async demo() {
368
- return console.warn("RUNNING DEMO APP, BE CAREFUL"), await this.appendToQueue(s.demo(), "demo");
369
- }
370
- async refund({ amount: i = 0, folio: e = null, auth: t = null } = {}) {
371
- return await this.appendToQueue(
372
- s.refund({
373
- amount: i,
374
- folio: e,
375
- auth: t
376
- }),
377
- "refund"
378
- );
379
- }
380
- async readProductionQR() {
381
- return await this.#i({ type: "production" });
382
- }
383
- async readQualityAssuranceQR() {
384
- return this.#i({ type: "QA" });
385
- }
386
- async exit() {
387
- return await this.appendToQueue(s.exit(), "exit-app");
388
- }
389
- async init() {
390
- await this.appendToQueue(s.init(), "init-app");
391
- }
392
- async login() {
393
- if (!this.apiKey || !this.businessId || !this.encryptionKey || !this.server)
394
- throw new Error("Invalid data to login check apiKey, businessId, encryptionKey, server");
395
- const i = JSON.stringify({
396
- action: "LOGIN_MIT",
397
- server: this.__pinpax__.server,
398
- business_id: this.__pinpax__.businessId,
399
- encryption_key: this.__pinpax__.encryptionKey,
400
- api_key: this.__pinpax__.apiKey
401
- }), e = this.parseStringToBytes(i, `\r
402
- `);
403
- await this.appendToQueue(e, "login");
404
- }
405
- }
406
- export {
407
- d as PinPax
408
- };