-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-sale.html
More file actions
335 lines (327 loc) · 15.7 KB
/
Copy pathquick-sale.html
File metadata and controls
335 lines (327 loc) · 15.7 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quick Sales / Venta Rápida</title>
<script src="i18n.js"></script>
<script src="auth-utils.js"></script>
<meta name="theme-color" content="#1C1917">
<link rel="apple-touch-icon" href="images/sweetplace logo.png">
<link rel="stylesheet" href="theme.css">
<script src="theme-loader.js"></script>
<style>
.container { max-width: 420px; margin: 2.5em auto; background: var(--surface); border-radius: var(--r-xl); box-shadow: var(--sh-lg); padding: 2em 1.5em 2.5em 1.5em; border: 1px solid var(--border); }
h1 { color: var(--text); font-size: 2em; margin-bottom: 0.2em; text-align: center; }
.subtitle { color: var(--text-2); font-size: 1em; text-align: center; margin-bottom: 2em; }
.row { display: flex; align-items: center; gap: 0.7em; margin-bottom: 1.2em; }
input[type=text], input[type=number] { font-size: 1.1em; }
.form-control { width: 100%; }
.current-account { font-size: 0.98em; color: var(--text-2); margin-bottom: 1.2em; text-align: left; }
.amount-input { font-size: 1.6em; text-align: center; width: 100%; margin-bottom: 1.2em; }
.generate-btn { width: 100%; margin-bottom: 1.5em; }
#qrContainer { display: flex; flex-direction: column; align-items: center; margin-top: 2em; }
#qrCode { margin-bottom: 1.2em; }
/* Provide a clear white quiet zone around the QR for better scanning */
.qr-box { background: #fff; padding: 16px; border-radius: var(--r-lg); box-shadow: var(--sh); }
#qrCode canvas { image-rendering: pixelated; display: block; }
#paymentStatus { font-size: 1.05em; color: var(--green); margin-bottom: 1.2em; text-align: center; }
.reset-btn { width: auto; }
@media (max-width: 600px) {
.container { padding: 1em 0.5em; }
h1 { font-size: 1.6em; }
.subtitle { font-size: 0.95em; }
.amount-input { font-size: 1.3em; }
}
</style>
<link rel="manifest" href="manifest.json">
<script src="pwa.js"></script>
</head>
<body>
<div class="container">
<h1 data-i18n="quicksale.title">Quick Sales</h1>
<div class="subtitle" data-i18n="quicksale.subtitle">Generate QR Payments quickly - Powered by Distriator</div>
<label for="hiveAccountInput" data-i18n="quicksale.accountLabel">Receiving Hive Account</label>
<div class="row">
<input type="text" id="hiveAccountInput" class="form-control" data-i18n-placeholder="quicksale.accountPlaceholder" placeholder="e.g. myshop.hive" autocomplete="off" autocapitalize="none" autocorrect="off" spellcheck="false" style="text-transform: lowercase;" />
<button class="btn btn-primary" id="updateAccountBtn" data-i18n="quicksale.updateBtn">Update</button>
</div>
<div class="current-account" id="currentAccountDisplay">
<span id="currentAccountAvatar" style="vertical-align:middle;display:inline-block;width:32px;height:32px;border-radius:50%;background:var(--border);overflow:hidden;margin-right:0.5em;"></span>
Current: <span id="currentAccount">none</span>
</div>
<label for="amountInput" data-i18n="quicksale.amountLabel">Amount (HBD)</label>
<input type="number" id="amountInput" class="amount-input form-control" min="0.01" step="0.01" placeholder="0.00" />
<button class="btn btn-success generate-btn" id="generateQrBtn" data-i18n="quicksale.generateBtn">Generate QR</button>
<div id="qrContainer" style="display:none;">
<div id="qrCode"></div>
<div id="qrDetails"></div>
<div id="paymentStatus"></div>
<button class="btn btn-secondary reset-btn" id="resetBtn" style="display:none;" data-i18n="common.newSale">New Sale</button>
</div>
<!-- Recent Incoming Payments -->
<div class="card" id="recentPaymentsCard" style="margin-top: 2rem;">
<div class="card-header">
<h2 data-i18n="quicksale.recentTitle">Recent Incoming Payments</h2>
</div>
<div class="card-body">
<div id="recentStatus" class="text-muted" style="margin-bottom: 0.75rem;"></div>
<ul id="recentList" style="list-style: none; padding: 0; margin: 0;"></ul>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
// --- Memo generator (reuse from pos.html) ---
function generateKcsHposMemo() {
function randGroup() {
return Math.random().toString(36).substring(2, 6);
}
return `kcs-hpos-${randGroup()}-${randGroup()}`;
}
// --- LocalStorage helpers ---
function getStoredAccount() {
return localStorage.getItem('quick_hive_account') || '';
}
function setStoredAccount(acc) {
localStorage.setItem('quick_hive_account', acc);
}
document.addEventListener('DOMContentLoaded', () => i18n.init());
// --- UI Elements ---
const hiveAccountInput = document.getElementById('hiveAccountInput');
const updateAccountBtn = document.getElementById('updateAccountBtn');
const currentAccount = document.getElementById('currentAccount');
const currentAccountAvatar = document.getElementById('currentAccountAvatar');
const amountInput = document.getElementById('amountInput');
const generateQrBtn = document.getElementById('generateQrBtn');
const qrContainer = document.getElementById('qrContainer');
const qrCodeDiv = document.getElementById('qrCode');
const qrDetails = document.getElementById('qrDetails');
const paymentStatus = document.getElementById('paymentStatus');
const resetBtn = document.getElementById('resetBtn');
// --- Render current account ---
function renderCurrentAccount() {
const acc = getStoredAccount();
currentAccount.textContent = acc ? acc : 'none';
if (acc) {
const imgUrl = `https://images.hive.blog/u/${acc.toLowerCase()}/avatar`;
currentAccountAvatar.innerHTML = `<img src="${imgUrl}" alt="avatar" style="width:32px;height:32px;border-radius:50%;object-fit:cover;vertical-align:middle;" onerror=\"this.onerror=null;this.src='https://images.hive.blog/u/ecency/avatar';\">`;
} else {
currentAccountAvatar.innerHTML = '';
}
}
renderCurrentAccount();
// Force lowercase (and strip leading @) as the user types
hiveAccountInput.addEventListener('input', () => {
const pos = hiveAccountInput.selectionStart;
const val = hiveAccountInput.value;
const sanitized = val.toLowerCase().replace(/^@+/, '');
if (val !== sanitized) {
hiveAccountInput.value = sanitized;
if (typeof pos === 'number') {
const newPos = Math.max(0, pos - (val.length - sanitized.length));
hiveAccountInput.setSelectionRange(newPos, newPos);
}
}
});
// --- Update account logic ---
updateAccountBtn.onclick = function() {
const acc = hiveAccountInput.value.trim().toLowerCase().replace(/^@+/, '');
if (!acc) { alert(t('quicksale.enterAccount')); return; }
setStoredAccount(acc);
renderCurrentAccount();
updateRecentTransactions();
hiveAccountInput.value = '';
};
// --- Generate QR logic ---
generateQrBtn.onclick = function() {
const acc = getStoredAccount();
const amt = parseFloat(amountInput.value);
if (!acc) { alert(t('quicksale.noAccount')); return; }
if (!amt || amt < 0.01) { alert(t('quicksale.enterAmount')); return; }
// Memo
const memo = generateKcsHposMemo();
window.lastPaymentMemo = memo;
window.lastPaymentAmount = amt.toFixed(3) + ' HBD';
// Hive op
const op = [
"transfer",
{
to: acc,
amount: amt.toFixed(3) + ' HBD',
memo: memo
}
];
const json = JSON.stringify(op);
const base64 = btoa(json).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
const uri = `hive://sign/op/${base64}`;
// Show QR
qrContainer.style.display = 'flex';
// Ensure a white padded box around the QR (quiet zone)
qrCodeDiv.innerHTML = '<div id="qrCanvasBox" class="qr-box"></div>';
new QRCode(document.getElementById('qrCanvasBox'), {
text: uri,
width: 320,
height: 320,
colorDark: '#000',
colorLight: '#fff',
correctLevel: QRCode.CorrectLevel.H
});
qrDetails.innerHTML = `<b>To:</b> @${acc}<br><b>Amount:</b> ${amt.toFixed(3)} HBD<br><b>Memo:</b> <span style='font-family:monospace;'>${memo}</span>
<div style="margin-top:0.75em;word-break:break-all;font-size:0.92em;background:#f4f4f4;color:#000;padding:0.5em 0.6em;border-radius:6px;font-family:monospace;">${uri}</div>`;
paymentStatus.textContent = t('quicksale.waitingPayment');
resetBtn.style.display = 'none';
startPaymentListener(acc, amt.toFixed(3) + ' HBD', memo);
};
// --- Payment Listener (reuse logic from pos.html) ---
let paymentListenerInterval = null;
function startPaymentListener(hiveAccount, expectedAmount, expectedMemo) {
if (paymentListenerInterval) clearInterval(paymentListenerInterval);
let attempts = 0;
paymentListenerInterval = setInterval(async function() {
attempts++;
try {
const response = await fetch('https://api.hive.blog', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'condenser_api.get_account_history',
params: [hiveAccount, -1, 20],
id: 1
})
});
const data = await response.json();
const history = data.result;
for (let i = history.length - 1; i >= 0; i--) {
const op = history[i][1].op;
if (op[0] === 'transfer' &&
op[1].to === hiveAccount &&
op[1].amount === expectedAmount &&
op[1].memo === expectedMemo) {
paymentStatus.textContent = t('quicksale.paymentReceived');
clearInterval(paymentListenerInterval);
paymentListenerInterval = null;
resetBtn.style.display = 'block';
// Refresh recent transactions after a confirmed payment
updateRecentTransactions();
alert(t('quicksale.paymentConfirmed'));
return;
}
}
paymentStatus.textContent = t('quicksale.waitingPayment');
} catch (e) {
paymentStatus.textContent = t('quicksale.paymentError');
}
if (attempts > 100) {
paymentStatus.textContent = t('quicksale.paymentTimeout');
clearInterval(paymentListenerInterval);
paymentListenerInterval = null;
resetBtn.style.display = 'block';
}
}, 3000);
}
// --- Reset logic ---
resetBtn.onclick = function() {
qrContainer.style.display = 'none';
qrCodeDiv.innerHTML = '';
qrDetails.innerHTML = '';
paymentStatus.textContent = '';
amountInput.value = '';
resetBtn.style.display = 'none';
};
// --- Recent Incoming Payments ---
function formatAmount(amountStr) {
return amountStr; // already like '1.000 HBD'
}
function truncateMemo(memo, max = 60) {
if (!memo) return '';
return memo.length > max ? memo.slice(0, max - 1) + '…' : memo;
}
async function fetchRecentIncoming(hiveAccount, desired = 10) {
try {
const res = await fetch('https://api.hive.blog', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'condenser_api.get_account_history',
params: [hiveAccount, -1, 100],
id: 2
})
});
const data = await res.json();
const history = data.result || [];
const incoming = [];
for (let i = history.length - 1; i >= 0; i--) {
const rec = history[i][1];
const op = rec.op;
if (op && op[0] === 'transfer') {
const details = op[1];
if (details && details.to && details.to.toLowerCase() === hiveAccount.toLowerCase()) {
incoming.push({
from: details.from,
to: details.to,
amount: details.amount,
memo: details.memo,
timestamp: rec.timestamp
});
if (incoming.length >= desired) break;
}
}
}
return incoming;
} catch (e) {
console.error('Failed to fetch recent incoming payments:', e);
return null;
}
}
function renderRecentTransactions(list) {
const status = document.getElementById('recentStatus');
const ul = document.getElementById('recentList');
if (!list) {
status.textContent = t('quicksale.loadFail');
ul.innerHTML = '';
return;
}
if (list.length === 0) {
status.textContent = t('quicksale.noRecent');
ul.innerHTML = '';
return;
}
status.textContent = '';
ul.innerHTML = list.map(tx => {
const when = new Date(tx.timestamp + 'Z');
const timeStr = isNaN(when.getTime()) ? tx.timestamp : when.toLocaleString();
const memo = tx.memo ? ` • <span class="text-muted">${truncateMemo(tx.memo)}</span>` : '';
return `
<li style="padding: 0.6rem 0; border-bottom: 1px solid var(--border);">
<div style="display:flex; justify-content: space-between; gap: 0.75rem;">
<div>
<strong>${formatAmount(tx.amount)}</strong> from @${tx.from}${memo}
</div>
<div class="text-muted" style="white-space: nowrap;">${timeStr}</div>
</div>
</li>
`;
}).join('');
}
async function updateRecentTransactions() {
const acc = getStoredAccount();
const status = document.getElementById('recentStatus');
const ul = document.getElementById('recentList');
if (!acc) {
status.textContent = t('quicksale.setAccount');
ul.innerHTML = '';
return;
}
status.textContent = t('quicksale.loadingPayments');
ul.innerHTML = '';
const list = await fetchRecentIncoming(acc, 10);
renderRecentTransactions(list);
}
// Initial load
updateRecentTransactions();
</script>
</body>
</html>