-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentWindow.qml
223 lines (177 loc) · 6.77 KB
/
PaymentWindow.qml
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import Qt.labs.platform
import QtQuick 2.12
import Qt.labs.qmlmodels 1.0
import QtQuick.Controls 2.5 as MyControls
import QtQuick.Controls
import QtQuick.Layouts 1.12
import QtQuick.Dialogs
import "functions.js" as Functions
import io.qt.libki_jamex.backend 1.0
RowLayout {
id: paymentWindow
property double currentJamexMachineBalance: 0
// https://doc.qt.io/qt-5/qml-qtquick-controls2-dialog.html
MyControls.Dialog {
id: paymentWindowMessageDialog
modal: true
focus: true
standardButtons: MyControls.Dialog.Ok
parent: Overlay.overlay
x: Math.round((parent.width - width) / 2)
y: Math.round((parent.height - height) / 2)
Text {
id: paymentWindowMessageDialogText
}
}
onVisibleChanged: function() {
transferFundsButton.enabled
}
Timer {
interval: 1000
running: true
repeat: true
onTriggered: updateJamexBalanceAmount()
}
function updateJamexBalanceAmount() {
var jbalance_float = parseFloat(backend.jamexBalance)
let balance_changed = jbalance_float != currentJamexMachineBalance
currentJamexMachineBalance = jbalance_float
var jbalance = jbalance_float.toFixed(2)
if (jamexBalanceAmount.text.substring(1) != jbalance) {
jamexBalanceAmount.text = qsTr("$") + jbalance
amountToTransferSpinbox.to = jbalance * 100
var balanceForLibki = amountToTransferSpinbox.value / 100
var remainder = jbalance - balanceForLibki
balanceToReturn.text = qsTr("$") + remainder.toFixed(2)
}
if ( balance_changed ) mainWindow.balanceChanged()
}
function deductAmount(amount) {
let a = amount.toFixed(2)
backend.jamexDeductAmount = a
const success = backend.jamexDeductAmount
return success
}
function transferFunds() {
var username = backend.userName
var funds = amountToTransferSpinbox.value / 100
var api_key = backend.serverApiKey
var server_address = backend.serverAddress
var path = '/api/public/user_funds/'
let url = Functions.build_add_user_funds_url(server_address, api_key,
username, funds)
if (amountToTransferSpinbox.value == 0) {
return
}
transferFundsButton.enabled = false
var balanceForLibki = amountToTransferSpinbox.value / 100
var amount_to_deduct = balanceForLibki.toFixed(2)
backend.jamexDeductAmount = amount_to_deduct
var success
success = backend.jamexDeductAmount
if (success === "false") {
paymentWindowMessageDialogText.text = qsTr(
"Unable to deduct amount from coinbox. Please ask staff for help")
transferFundsButton.enabled = true
} else {
//backend.jamexDisableChangeCardReturn;
waitDialog.open()
Functions.request(url, function (o) {
waitDialog.close()
// translate response into an object
var d = eval('new Object(' + o.responseText + ')')
let messageText
if (d.success) {
messageText = qsTr("Funds have been transferred!")
} else {
if (d.error === "INVALID_API_KEY") {
messageText = qsTr(
"Unable to authenticate. API key is invalid.")
} else if (d.error === "INVALID_USER") {
messageText = qsTr("Unable to find user.")
} else if ( d && d.error ) {
messageText = qsTr(
"Unable to add funds. Error code: ") + d.error
} else {
messageText = qsTr("Unable to connect to server.")
}
// Return the funds, they did not get applied to their Libki funds balance
backend.jamexAddAmount = amount_to_deduct //FIXME:
success = backend.jamexAddAmount
}
amountToTransferSpinbox.value = 0
transferFundsButton.enabled = true
paymentWindowMessageDialogText.text = messageText
paymentWindowMessageDialog.open()
success = backend.jamexReturnBalance
success = backend.jamexEnableChangeCardReturn
}, 'POST')
}
}
GridLayout {
id: transferFundsGrid
rows: 4
columns: 2
Text {
text: qsTr("Balance in machine:")
}
Text {
id: jamexBalanceAmount
text: qsTr("$") + parseFloat(backend.jamexBalance).toFixed(2)
}
Label {
id: labelAmountToTransfer
text: qsTr("Amount to transfer:")
}
SpinBox {
id: amountToTransferSpinbox
from: 0
value: 0
to: backend.jamexBalance * 100
stepSize: 1
editable: true
property int decimals: 2
validator: DoubleValidator {
bottom: Math.min(amountToTransferSpinbox.from,
amountToTransferSpinbox.to)
top: Math.max(amountToTransferSpinbox.from,
amountToTransferSpinbox.to)
}
textFromValue: function (value, locale) {
return Number(value / 100).toLocaleString(
locale, 'f', amountToTransferSpinbox.decimals)
}
valueFromText: function (text, locale) {
return Number.fromLocaleString(locale, text) * 100
}
onValueModified: {
var jamexBalance = jamexBalanceAmount.text.substring(1)
var balanceForLibki = amountToTransferSpinbox.value / 100
var remainder = jamexBalance - balanceForLibki
balanceToReturn.text = qsTr("$") + remainder.toFixed(2)
}
Keys.onEnterPressed: {
amountToTransferSpinbox.valueModified()
transferFundsButton.clicked()
}
Keys.onReturnPressed: {
amountToTransferSpinbox.valueModified()
transferFundsButton.clicked()
}
}
Text {
text: qsTr("Balance to return:")
}
Text {
id: balanceToReturn
text: "$0.00"
}
Text {}
Button {
id: transferFundsButton
text: qsTr("Transfer funds to Libki account")
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
onClicked: transferFunds(true)
}
}
}