forked from sergeiliski/bankid-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (80 loc) · 2.32 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
// const fs = require('fs');
const axios = require('axios');
const https = require('https');
class BankID {
constructor(connectionOptions) {
if (!connectionOptions) {
throw Error('Must include options')
}
this.options = Object.assign({}, connectionOptions);
if (!this.options.pfx || !this.options.passphrase) {
throw Error('Certificate and passphrase are required')
}
this.baseUrl = this.options.baseUrl;
if (this.baseUrl.substr(-1) != '/') {
this.baseUrl += '/'
}
this.axios = this._createAxiosInstance();
this.authUrl = 'auth';
this.signUrl = 'sign';
this.collectUrl = 'collect';
this.cancelUrl = 'cancel'
}
auth(data) {
if (!data.endUserIp) {
throw Error('User ip address is required')
}
const params = {
endUserIp: data.endUserIp
};
if (data.personalNumber) {
params.personalNumber = data.personalNumber;
}
return this.axios.post(this.baseUrl.concat(this.authUrl), params)
}
sign(data) {
if (!data.endUserIp || !data.userVisibleData) {
throw Error('User ip and visible data are required')
}
let params = {
endUserIp: data.endUserIp,
personalNumber: data.personalNumber,
userVisibleData: data.userVisibleData
};
if (data.userNonVisibleData) {
params = Object.assign({}, params, { userNonVisibleData: data.userNonVisibleData })
}
return this.axios.post(this.baseUrl.concat(this.signUrl), params)
}
collect(orderRef) {
if (!orderRef) {
throw Error('Order reference value is required')
}
const params = {
orderRef
};
return this.axios.post(this.baseUrl.concat(this.collectUrl), params)
}
cancel(orderRef) {
if (!orderRef) {
throw Error('Order reference value is required')
}
const params = {
orderRef
};
return this.axios.post(this.baseUrl.concat(this.cancelUrl), params)
}
_createAxiosInstance() {
const ca = this.options.ca; //fs.readFileSync(this.options.ca, 'utf-8');
const pfx = this.options.pfx; //fs.readFileSync(this.options.pfx);
const passphrase = this.options.passphrase;
return axios.create({
httpsAgent: new https.Agent({ pfx, passphrase, ca }),
headers: {
'Content-Type': 'application/json'
}
})
}
}
module.exports = BankID;