-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcharge.js
83 lines (80 loc) · 2.2 KB
/
charge.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
/*
Scanpay Node.js client library (Node >= v6.6.0)
Docs: https://docs.scanpay.dk/
[email protected] || irc.libera.chat:6697 #scanpay
*/
const apikey = '1153:YHZIUGQw6NkCIYa3mG6CWcgShnl13xuI7ODFUYuMy0j790Q6ThwBEjxfWFXwJZ0W';
const scanpay = require('../')(apikey);
const subscriberid = 5;
const options = {
hostname: 'api.test.scanpay.dk',
headers: {
'Idempotency-Key': scanpay.generateIdempotencyKey(),
}
};
const data = {
orderid: 'a766409',
items: [
{
name: 'Pink Floyd: The Dark Side Of The Moon',
quantity: 2,
price: '99.99 DKK',
sku: 'fadf23'
},
{
name: '巨人宏偉的帽子',
quantity: 2,
price: '420 DKK',
sku: '124'
}
],
billing: {
name: 'John Doe',
company: 'The Shop A/S',
email: '[email protected]',
phone: '+4512345678',
address: ['Langgade 23, 2. th'],
city: 'Havneby',
zip: '1234',
country: 'DK',
vatin: '35413308',
gln: '7495563456235'
},
shipping: {
name: 'Jan Dåh',
email: '[email protected]',
phone: '+45 87654321',
address: [
'Langgade 23, 1. th',
'C/O The Choppa'
],
city: 'Haveby',
zip: '1235',
country: 'DK'
}
};
async function docharge() {
let res = null;
let i = 0;
for (i = 0; i < 3; i++) {
await scanpay.charge(subscriberid, data, options).then(
(r) => res = r
).catch(e => {
if (e instanceof scanpay.IdempotentResponseError) {
/* Regenerate idempotency key */
options.headers['Idempotency-Key'] = scanpay.generateIdempotencyKey();
console.log('Idempotent exception: ' + e);
} else {
console.log('Exception (not idempotent:' + e);
}
});
if (res != null) { break; }
await new Promise(resolve => setTimeout(resolve, (i + 1) * 1000));
}
if (i == 3) {
console.log('Attempted charging 3 times and failed');
return;
}
console.log('Charge result: ' + JSON.stringify(res));
}
docharge();