-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathseq.js
More file actions
52 lines (48 loc) · 1.6 KB
/
seq.js
File metadata and controls
52 lines (48 loc) · 1.6 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
/*
Scanpay Node.js client library (Node >= v6.6.0)
Docs: https://docs.scanpay.dk/
help@scanpay.dk || irc.libera.chat:6697 #scanpay
*/
const apikey = '1153:YHZIUGQw6NkCIYa3mG6CWcgShnl13xuI7ODFUYuMy0j790Q6ThwBEjxfWFXwJZ0W';
const scanpay = require('../')(apikey);
const options = {
hostname: 'api.test.scanpay.dk'
};
// Second test: Apply changes since last seq call
let dbseq = 5; // Stored in the shop database after last seq.
async function applyChanges() {
// Loop through all changes
while (1) {
let res;
try {
res = await scanpay.seq(dbseq, options);
} catch (e) {
console.log(e);
return;
}
// Apply some changes ... and update dbseq after
for (const change of res.changes) {
console.log(JSON.stringify(change, null, 4));
switch (change.type) {
case 'transaction':
/* fallthrough */
case 'charge':
console.log('order #' + change.orderid + ' updated to revision ' + change.rev);
break;
case 'subscriber':
console.log('subscriber #' + change.ref + ' updated to revision ' + change.rev);
break;
}
}
if (res.seq > dbseq) {
console.log('Updating seq to ' + res.seq);
dbseq = res.seq;
}
// Break when there are no more changes
if (res.changes.length === 0) {
console.log('Done applying changes, new seq is ' + dbseq);
return;
}
}
}
applyChanges();