-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAgenteClisitefAPI.js
107 lines (90 loc) · 3.46 KB
/
AgenteClisitefAPI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class AgenteClisitefAPI {
constructor() {
this.baseUrl = 'https://127.0.0.1/agente/clisitef'
this.timeout = 10000
}
ConfigurarSitef(configSitef) {
this.ConfiguracoesSitef = configSitef
}
async requisicaoBase(requestMethod, requestUrl, parameters = null) {
let requisicao = {
url: `${this.baseUrl}/${requestUrl}`,
method: requestMethod,
data: parameters,
timeout: this.timeout
}
return await $.ajax(requisicao)
.then(response => {
if(requestUrl != 'session') console.warn(`${requestUrl}: `, response)
return response
})
.catch((error) => {
error = !error.responseJSON ? error : error.responseJSON.serviceMessage
throw new Error(`Falha na requisição "${requestUrl}":\n ${error}`)
})
}
async sitefState() {
return await this.requisicaoBase('GET', 'state')
}
async createSession(storeId, terminalId) {
const parametros = {
sitefIp: this.ConfiguracoesSitef.ip,
storeId: storeId,
terminalId: terminalId,
trnInitParameters: this.ConfiguracoesSitef.parametrosAdicionaisTransacao
}
return await this.requisicaoBase('POST', 'session', parametros)
}
async getSession() {
return await this.requisicaoBase('GET', 'session')
}
async deleteSession() {
return await this.requisicaoBase('delete', 'session')
}
async startTransaction(sessionId, transacao) {
const parametros = {
sessionId: sessionId,
functionId: transacao.Funcao,
trnAmount: transacao.Valor,
taxInvoiceNumber: transacao.CupomFiscal,
taxInvoiceDate: transacao.DataFiscal,
taxInvoiceTime: transacao.HoraFiscal,
sitefIp: this.ConfiguracoesSitef.ip,
cashierOperator: this.ConfiguracoesSitef.operador,
trnAdditionalParameters: this.ConfiguracoesSitef.parametrosAdicionaisTransacao,
trnInitParameters: this.ConfiguracoesSitef.parametrosAdicionaisConfiguracao
}
return await this.requisicaoBase('POST', 'startTransaction', parametros)
}
async continueTransaction(sessionId, params, cod) {
const parametros = {
sessionId: sessionId,
continue: cod,
data: params
}
return await this.requisicaoBase('POST', 'continueTransaction', parametros)
}
async finishTransaction(sessionId, transacao, confirma) {
const parametros = {
sessionId: sessionId,
taxInvoiceNumber: transacao.CupomFiscal,
taxInvoiceDate: transacao.DataFiscal,
taxInvoiceTime: transacao.HoraFiscal,
confirm: confirma
}
return await this.requisicaoBase('POST', 'finishTransaction', parametros)
}
async finishOutTransaction(sessionId, transacao, confirma) {
const parametros = {
sessionId: sessionId,
sitefIp: this.ConfiguracoesSitef.sitefIp,
storeId: this.ConfiguracoesSitef.storeId,
terminalId: this.ConfiguracoesSitef.terminalId,
taxInvoiceNumber: transacao.CupomFiscal,
taxInvoiceDate: transacao.DataFiscal,
taxInvoiceTime: transacao.HoraFiscal,
confirm: confirma
}
return await this.requisicaoBase('POST', 'finishTransaction', parametros)
}
}