-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
553 lines (489 loc) · 19.8 KB
/
app.js
File metadata and controls
553 lines (489 loc) · 19.8 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// ──────────────── State ────────────────
var piInitialized = false;
var piUser = null;
var currentReference = null;
var currentPiPaymentId = null;
// ──────────────── Config Management ────────────────
var STORAGE_KEY = 'pi_tester_configs';
var ACTIVE_CONFIG_KEY = 'pi_tester_active';
var ENDPOINTS = ['approve', 'complete', 'verify', 'cancel', 'status'];
var VARS = ['piPaymentId', 'reference', 'txid'];
function getConfigs() {
try { return JSON.parse(localStorage.getItem(STORAGE_KEY)) || {}; } catch (e) { return {}; }
}
function saveConfigs(configs) { localStorage.setItem(STORAGE_KEY, JSON.stringify(configs)); }
function getActiveConfigName() { return localStorage.getItem(ACTIVE_CONFIG_KEY) || ''; }
function setActiveConfigName(name) { localStorage.setItem(ACTIVE_CONFIG_KEY, name); }
function val(id) {
var el = document.getElementById(id);
return el ? (el.value || '').trim() : '';
}
function setVal(id, v) {
var el = document.getElementById(id);
if (el && v !== undefined && v !== null) el.value = v;
}
function isChecked(id) {
var el = document.getElementById(id);
return el ? el.checked : false;
}
function setChecked(id, v) {
var el = document.getElementById(id);
if (el) el.checked = !!v;
}
function readEndpointConfig(ep) {
var cfg = { route: val('ep_' + ep + '_route'), extra: val('ep_' + ep + '_extra'), vars: {} };
if (ep === 'status') cfg.method = val('ep_status_method');
VARS.forEach(function(v) {
cfg.vars[v] = {
on: isChecked('ep_' + ep + '_var_' + v + '_on'),
loc: val('ep_' + ep + '_var_' + v + '_loc'),
key: val('ep_' + ep + '_var_' + v + '_key'),
};
});
return cfg;
}
function applyEndpointConfig(ep, cfg) {
if (!cfg) return;
setVal('ep_' + ep + '_route', cfg.route);
setVal('ep_' + ep + '_extra', cfg.extra);
if (ep === 'status' && cfg.method) setVal('ep_status_method', cfg.method);
if (cfg.vars) {
VARS.forEach(function(v) {
if (cfg.vars[v]) {
setChecked('ep_' + ep + '_var_' + v + '_on', cfg.vars[v].on);
setVal('ep_' + ep + '_var_' + v + '_loc', cfg.vars[v].loc);
setVal('ep_' + ep + '_var_' + v + '_key', cfg.vars[v].key);
}
});
}
}
function readConfigFromForm() {
var cfg = {
baseUrl: val('cfgBaseUrl'),
accessToken: val('cfgAccessToken'),
authType: val('cfgAuthType'),
customHeader: val('cfgCustomHeader'),
initRoute: val('ep_init_route'),
initPayload: val('initPayload'),
responseRefPath: val('cfgResponseRefPath'),
responseStatusPath: val('cfgResponseStatusPath'),
piAmount: val('piAmount'),
piMemo: val('piMemo'),
endpoints: {},
};
ENDPOINTS.forEach(function(ep) { cfg.endpoints[ep] = readEndpointConfig(ep); });
return cfg;
}
function applyConfigToForm(cfg) {
setVal('cfgBaseUrl', cfg.baseUrl);
setVal('cfgAccessToken', cfg.accessToken);
setVal('cfgAuthType', cfg.authType || 'bearer');
setVal('cfgCustomHeader', cfg.customHeader);
setVal('ep_init_route', cfg.initRoute || '/payments/initialize');
if (cfg.initPayload) setVal('initPayload', cfg.initPayload);
setVal('cfgResponseRefPath', cfg.responseRefPath || 'data.reference');
setVal('cfgResponseStatusPath', cfg.responseStatusPath || 'data.status');
if (cfg.piAmount) setVal('piAmount', cfg.piAmount);
if (cfg.piMemo) setVal('piMemo', cfg.piMemo);
if (cfg.endpoints) {
ENDPOINTS.forEach(function(ep) { applyEndpointConfig(ep, cfg.endpoints[ep]); });
}
onAuthTypeChange();
}
function saveCurrentConfig() {
var name = prompt('Save config as:');
if (!name) return;
var configs = getConfigs();
configs[name] = readConfigFromForm();
saveConfigs(configs);
setActiveConfigName(name);
renderConfigChips();
log('Config "' + name + '" saved', 'success');
}
function loadConfig(name) {
var configs = getConfigs();
if (!configs[name]) return;
applyConfigToForm(configs[name]);
setActiveConfigName(name);
renderConfigChips();
log('Loaded config "' + name + '"', 'info');
}
function deleteConfig(name, evt) {
evt.stopPropagation();
if (!confirm('Delete config "' + name + '"?')) return;
var configs = getConfigs();
delete configs[name];
saveConfigs(configs);
if (getActiveConfigName() === name) setActiveConfigName('');
renderConfigChips();
log('Deleted config "' + name + '"', 'warn');
}
function renderConfigChips() {
var el = document.getElementById('savedConfigs');
var configs = getConfigs();
var active = getActiveConfigName();
var names = Object.keys(configs);
if (names.length === 0) {
el.innerHTML = '<span style="font-size:12px;color:var(--text-dim)">No saved configs yet</span>';
return;
}
el.innerHTML = names.map(function(n) {
return '<span class="config-chip' + (n === active ? ' active' : '') + '" onclick="loadConfig(\'' + n.replace(/'/g, "\\'") + '\')">'
+ n + '<span class="delete" onclick="deleteConfig(\'' + n.replace(/'/g, "\\'") + '\', event)">×</span></span>';
}).join('');
}
// ──────────────── Auth ────────────────
function onAuthTypeChange() {
var type = val('cfgAuthType');
document.getElementById('customHeaderField').style.display = type === 'custom' ? '' : 'none';
}
function buildAuthHeaders() {
var token = val('cfgAccessToken');
var type = val('cfgAuthType');
var headers = {};
switch (type) {
case 'apikey': headers['X-API-Key'] = token; break;
case 'basic': headers['Authorization'] = 'Basic ' + token; break;
case 'custom': headers[val('cfgCustomHeader') || 'Authorization'] = token; break;
default: headers['Authorization'] = 'Bearer ' + token; break;
}
return headers;
}
// ──────────────── Collapsible ────────────────
function toggleSection(id) {
var body = document.getElementById(id);
var arrow = document.getElementById(id + 'Arrow');
if (body.classList.contains('hidden')) {
body.classList.remove('hidden');
if (arrow) arrow.classList.add('open');
} else {
body.classList.add('hidden');
if (arrow) arrow.classList.remove('open');
}
}
// ──────────────── Logging ────────────────
function log(msg, type, data) {
type = type || 'info';
var el = document.getElementById('log');
var time = new Date().toLocaleTimeString();
var html = '<div class="log-entry"><span class="log-time">[' + time + ']</span> <span class="log-' + type + '">' + escapeHtml(msg) + '</span>';
if (data) {
var str = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
html += '<span class="log-data">' + escapeHtml(str) + '</span>';
}
html += '</div>';
el.innerHTML += html;
el.scrollTop = el.scrollHeight;
}
function escapeHtml(str) { return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); }
function clearLog() { document.getElementById('log').innerHTML = ''; }
// ──────────────── Steps ────────────────
function setStep(n, state) {
var el = document.getElementById('step' + n);
el.className = 'step';
if (state) el.classList.add(state);
}
function resetSteps() { for (var i = 1; i <= 5; i++) setStep(i, ''); }
// ──────────────── SDK Status ────────────────
function showSdkStatus(msg, type) {
var el = document.getElementById('sdkStatus');
el.className = 'status-bar ' + type;
el.classList.remove('hidden');
var colors = { info: 'blue', success: 'green', error: 'red', warning: 'yellow' };
el.innerHTML = '<div class="dot ' + (colors[type] || 'blue') + '"></div><span>' + msg + '</span>';
}
// ──────────────── Helpers ────────────────
function deepGet(obj, path) {
if (!path || !obj) return undefined;
var parts = path.split('.');
var cur = obj;
for (var i = 0; i < parts.length; i++) {
if (cur === null || cur === undefined) return undefined;
cur = cur[parts[i]];
}
return cur;
}
function buildRequest(epName, runtimeVars) {
var epCfg = readEndpointConfig(epName);
var method = epCfg.method || 'POST';
var route = epCfg.route;
var body = {};
var queryParams = [];
// Parse extra body fields
if (epCfg.extra) {
try {
var extra = JSON.parse(epCfg.extra);
for (var k in extra) { if (extra.hasOwnProperty(k)) body[k] = extra[k]; }
} catch (e) {
log('Invalid extra body JSON for ' + epName + ': ' + e.message, 'error');
}
}
// Apply variable mappings
VARS.forEach(function(v) {
var mapping = epCfg.vars[v];
if (!mapping || !mapping.on || !mapping.key) return;
var value = runtimeVars[v];
if (value === undefined || value === null) return;
switch (mapping.loc) {
case 'body':
body[mapping.key] = value;
break;
case 'query':
queryParams.push(encodeURIComponent(mapping.key) + '=' + encodeURIComponent(value));
break;
case 'url':
route = route.replace(/\/?$/, '/' + encodeURIComponent(value));
break;
}
});
var url = route;
if (queryParams.length > 0) url += '?' + queryParams.join('&');
var hasBody = Object.keys(body).length > 0;
return { method: method, url: url, body: hasBody ? body : null };
}
// ──────────────── API Helper ────────────────
async function api(method, endpoint, body) {
var baseUrl = val('cfgBaseUrl');
if (!baseUrl) throw new Error('Base URL is required');
if (!val('cfgAccessToken')) throw new Error('Access token is required');
var url = baseUrl.replace(/\/+$/, '') + endpoint;
var headers = Object.assign({ 'Content-Type': 'application/json' }, buildAuthHeaders());
var opts = { method: method, headers: headers };
if (body && method !== 'GET') opts.body = JSON.stringify(body);
log(method + ' ' + endpoint, 'info', body ? JSON.stringify(body) : null);
var res = await fetch(url, opts);
var json = await res.json();
if (!res.ok) {
log('ERR ' + res.status + ' — ' + (json.message || json.error || JSON.stringify(json)), 'error');
throw new Error(json.message || json.error || 'API Error ' + res.status);
}
log('OK ' + res.status, 'success', JSON.stringify(json));
return json;
}
async function callEndpoint(epName, runtimeVars) {
var req = buildRequest(epName, runtimeVars);
return await api(req.method, req.url, req.body);
}
// ──────────────── Pi SDK Init ────────────────
async function initPiSdk() {
var mode = val('piSdkMode');
var sandbox = mode.includes('Testnet');
log('Initializing Pi SDK in ' + (sandbox ? 'SANDBOX' : 'PRODUCTION') + ' mode...', 'warn');
showSdkStatus('Initializing...', 'warning');
try {
await Pi.init({ version: '2.0', sandbox: sandbox });
piInitialized = true;
showSdkStatus('Pi SDK ready (' + (sandbox ? 'Testnet' : 'Mainnet') + ')', 'success');
log('Pi SDK initialized', 'success');
document.getElementById('btnAuth').disabled = false;
document.getElementById('btnInit').textContent = 'SDK Initialized';
document.getElementById('btnInit').disabled = true;
} catch (err) {
showSdkStatus('SDK init failed: ' + err.message, 'error');
log('SDK init failed: ' + err.message, 'error');
}
}
// ──────────────── Pi Auth ────────────────
async function authenticatePi() {
if (!piInitialized) return log('Initialize Pi SDK first', 'error');
log('Requesting Pi authentication...', 'info');
try {
var scopes = ['payments', 'username'];
var authResult = await Pi.authenticate(scopes, onIncompletePaymentFound);
piUser = authResult.user;
document.getElementById('piUsername').textContent = piUser.username || '--';
document.getElementById('piUid').textContent = piUser.uid || '--';
document.getElementById('piUser').classList.remove('hidden');
log('Authenticated as ' + piUser.username + ' (uid: ' + piUser.uid + ')', 'success');
document.getElementById('btnAuth').textContent = 'Authenticated: ' + piUser.username;
document.getElementById('btnAuth').disabled = true;
} catch (err) {
log('Pi auth failed: ' + err.message, 'error');
}
}
function onIncompletePaymentFound(payment) {
log('Incomplete payment found!', 'warn', JSON.stringify(payment));
currentPiPaymentId = payment.identifier;
document.getElementById('manualPiId').value = payment.identifier;
if (payment.transaction && payment.transaction.txid) {
document.getElementById('manualTxId').value = payment.transaction.txid;
}
}
// ──────────────── Full Payment Flow ────────────────
async function startPayment() {
if (!piInitialized) return log('Initialize Pi SDK first', 'error');
var amount = parseFloat(val('piAmount'));
var memo = val('piMemo');
var payloadRaw = val('initPayload');
var initRoute = val('ep_init_route');
var refPath = val('cfgResponseRefPath');
if (!amount || amount <= 0) return log('Enter a valid Pi SDK amount', 'error');
if (!val('cfgAccessToken')) return log('Enter your access token', 'error');
if (!val('cfgBaseUrl')) return log('Enter your base URL', 'error');
var initBody;
try {
initBody = JSON.parse(payloadRaw);
} catch (e) {
return log('Invalid JSON in init payload: ' + e.message, 'error');
}
resetSteps();
document.getElementById('paymentResult').classList.add('hidden');
document.getElementById('btnPay').disabled = true;
document.getElementById('btnPay').textContent = 'Processing...';
try {
// Step 1: Initialize payment on backend
setStep(1, 'active');
log('Step 1/5: Initializing payment on backend...', 'info');
var initRes = await api('POST', initRoute, initBody);
currentReference = deepGet(initRes, refPath);
if (!currentReference) throw new Error('No reference found at path: ' + refPath);
document.getElementById('resRef').textContent = currentReference;
document.getElementById('manualRef').value = currentReference;
setStep(1, 'done');
log('Reference: ' + currentReference, 'success');
// Step 2: Create Pi SDK payment
setStep(2, 'active');
log('Step 2/5: Opening Pi SDK payment dialog...', 'info');
var piPaymentData = {
amount: amount,
memo: memo + ' [ref:' + currentReference + ']',
metadata: { reference: currentReference },
};
var runtimeVars = { reference: currentReference, piPaymentId: '', txid: '' };
var piCallbacks = {
onReadyForServerApproval: function(paymentId) {
currentPiPaymentId = paymentId;
runtimeVars.piPaymentId = paymentId;
document.getElementById('resPiId').textContent = paymentId;
document.getElementById('manualPiId').value = paymentId;
setStep(2, 'done');
setStep(3, 'active');
log('Step 3/5: Approving (piPaymentId: ' + paymentId + ')...', 'info');
callEndpoint('approve', runtimeVars).then(function() {
setStep(3, 'done');
log('Payment approved', 'success');
}).catch(function(err) {
setStep(3, 'error');
log('Approve failed: ' + err.message, 'error');
});
},
onReadyForServerCompletion: function(paymentId, txid) {
runtimeVars.piPaymentId = paymentId;
runtimeVars.txid = txid;
document.getElementById('resTxId').textContent = txid || '--';
document.getElementById('manualTxId').value = txid || '';
setStep(4, 'active');
log('Step 4/5: Completing (txid: ' + txid + ')...', 'info');
callEndpoint('complete', runtimeVars)
.then(function() {
setStep(4, 'done');
log('Payment completed', 'success');
setStep(5, 'active');
log('Step 5/5: Verifying...', 'info');
return callEndpoint('verify', runtimeVars);
})
.then(function(verifyRes) {
var statusPath = val('cfgResponseStatusPath');
var status = deepGet(verifyRes, statusPath) || 'unknown';
setStep(5, 'done');
updateResultStatus(status);
log('Verified — status: ' + status, 'success');
document.getElementById('paymentResult').classList.remove('hidden');
document.getElementById('btnPay').textContent = 'Payment Complete';
})
.catch(function(err) {
setStep(5, 'error');
log('Verify failed: ' + err.message, 'error');
resetPayButton();
});
},
onCancel: function(paymentId) {
log('Payment cancelled by user', 'warn');
setStep(2, 'error');
resetPayButton();
runtimeVars.piPaymentId = paymentId;
callEndpoint('cancel', runtimeVars).catch(function() {});
},
onError: function(err, payment) {
log('Pi SDK error: ' + (err.message || err), 'error', payment ? JSON.stringify(payment) : null);
resetPayButton();
},
};
Pi.createPayment(piPaymentData, piCallbacks);
} catch (err) {
log('Payment flow error: ' + err.message, 'error');
resetPayButton();
}
}
function resetPayButton() {
document.getElementById('btnPay').disabled = false;
document.getElementById('btnPay').textContent = 'Pay with Pi';
}
function updateResultStatus(status) {
var el = document.getElementById('resStatus');
var s = String(status).toLowerCase();
var map = {
successful: ['Successful', 'tag-green'],
success: ['Successful', 'tag-green'],
completed: ['Completed', 'tag-green'],
failed: ['Failed', 'tag-red'],
cancelled: ['Cancelled', 'tag-red'],
initialized: ['Initialized', 'tag-gold'],
pending: ['Pending', 'tag-gold'],
};
var entry = map[s] || [status, 'tag-purple'];
el.innerHTML = '<span class="tag ' + entry[1] + '">' + entry[0] + '</span>';
}
// ──────────────── Manual Controls ────────────────
function getManualVars() {
return {
reference: val('manualRef'),
piPaymentId: val('manualPiId'),
txid: val('manualTxId'),
};
}
async function manualApprove() {
var v = getManualVars();
if (!v.piPaymentId) return log('Pi Payment ID required', 'error');
if (!v.reference) return log('Reference required', 'error');
try { await callEndpoint('approve', v); } catch (e) {}
}
async function manualComplete() {
var v = getManualVars();
if (!v.piPaymentId) return log('Pi Payment ID required', 'error');
if (!v.txid) return log('TX ID required', 'error');
try { await callEndpoint('complete', v); } catch (e) {}
}
async function manualVerify() {
var v = getManualVars();
if (!v.reference) return log('Reference required', 'error');
try {
var res = await callEndpoint('verify', v);
var status = deepGet(res, val('cfgResponseStatusPath')) || 'unknown';
updateResultStatus(status);
document.getElementById('paymentResult').classList.remove('hidden');
} catch (e) {}
}
async function manualCancel() {
var v = getManualVars();
if (!v.piPaymentId) return log('Pi Payment ID required', 'error');
try { await callEndpoint('cancel', v); } catch (e) {}
}
async function manualGetStatus() {
var v = getManualVars();
if (!v.piPaymentId && !v.reference) return log('Pi Payment ID or Reference required', 'error');
try { await callEndpoint('status', v); } catch (e) {}
}
// ──────────────── Startup ────────────────
function init() {
renderConfigChips();
var activeName = getActiveConfigName();
var configs = getConfigs();
if (activeName && configs[activeName]) {
applyConfigToForm(configs[activeName]);
}
onAuthTypeChange();
log('Pi Payment Tester loaded', 'info');
log('Configure your backend connection and endpoints, then initialize the Pi SDK', 'info');
}
init();