forked from cubyn/payline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (52 loc) · 1.61 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
const soap = require("soap");
const wsdl = require("soap/lib/wsdl");
const { promisify } = require("util");
const productionWsdl = require("./WebPaymentAPI.v4.66.1.wsdl");
const homologationWsdl = require("./WebPaymentAPI.v4.66.1.homologation.wsdl");
module.exports = class Payline {
constructor(user, pass, environment = "production", customWsdl) {
const wsdl =
customWsdl ||
(environment === "production" ? productionWsdl : homologationWsdl);
if (!user || !pass) {
throw new Error("All of user / pass should be defined");
}
this.user = user;
this.pass = pass;
this.wsdl = wsdl;
}
async initialize() {
if (this.client) {
return this.client;
}
const parsedWsdl = new wsdl.WSDL(this.wsdl, "https://example.com", {});
await new Promise((resolve, reject) => {
parsedWsdl.onReady((err) => {
if (err) return reject(err);
resolve();
});
});
this.client = new soap.Client(parsedWsdl);
this.client.setSecurity(new soap.BasicAuthSecurity(this.user, this.pass));
return this.client;
}
async runAction(action, args) {
try {
const client = await this.initialize();
const call = promisify(client[action].bind(client));
return call(args);
} catch (error) {
if (error.message === "client[action] is not a function") {
throw new Error(
"Action not found. See the methods payline.describe()."
);
} else {
throw new Error(error.message);
}
}
}
async describe() {
const client = await this.initialize();
return client.describe();
}
};