-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (108 loc) · 3.82 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
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
/**
* GladePay API wrapper
* @author Abubakar Hassan < @itssadon >
*/
const request = require('request-promise');
const endpoint = 'https://api.gladepay.com';
const demoEndpoint = 'https://demo.api.gladepay.com';
const Events = require("./resources/events");
function GladePay(merchantId, merchantKey, mode = false) {
if (!(this instanceof GladePay)) {
return new GladePay(merchantId, merchantKey);
}
this.endpoint = (mode) ? endpoint : demoEndpoint;
this.mid = merchantId;
this.key = merchantKey;
this.importResources();
this.Events = new Events(this.key);
}
const resources = {
payment: require("./resources/payment"),
disbursement: require("./resources/disbursement"),
resources: require("./resources/resources"),
loansAndInvestments: require("./resources/loans_and_investments")
};
GladePay.prototype = {
extend: function (func) {
const me = this;
return function () {
const data = arguments[0] || {};
const method = ["post", "get", "put"].includes(func.method) ?
func.method :
(function () {
throw new Error("Method not Allowed! - Resource declaration error");
})();
var endpoint = me.endpoint + func.route,
qs = {};
var argsInEndpoint = endpoint.match(/{[^}]+}/g);
if (argsInEndpoint) {
argsInEndpoint.map(arg => {
arg = arg.replace(/\W/g, "");
if (!(arg in data)) {
throw new Error(`Argument '${arg}' is required`);
} else {
endpoint = endpoint.replace(`{${arg}}`, data[`${arg}`]);
delete data[arg];
}
});
}
if (func.params) {
func.params.filter(param => {
if (typeof param === 'string') {
if (!param.includes("*")) return;
param = param.replace("*", "");
if (!(param in data)) {
throw new Error(`Parameter '${param}' is required`);
}
return;
}
return;
});
}
if (func.args) {
func.args.filter(a => {
if (!a.includes("*")) {
if (a in data) {
qs[`${a}`] = data[`${a}`];
}
return;
}
a = a.replace("*", "");
if (!(a in data)) {
throw new Error(`Argument '${a}' is required`);
} else {
qs[`${a}`] = data[`${a}`];
}
return;
});
}
const options = {
url: endpoint,
json: true,
method: method.toUpperCase(),
headers: {
'Content-Type': 'application/json',
key: `${me.key}`,
mid: `${me.mid}`
}
};
if (method == "post" || method == "put") {
const reqData = Object.assign(func.data, data);
options.body = reqData;
} else {
options.qs = qs;
}
return request(options);
};
},
importResources: function () {
for (var i in resources) {
const anon = function () {};
for (var j in resources[i]) {
anon.prototype[j] = this.extend(resources[i][j]);
}
GladePay.prototype[i] = new anon();
}
}
};
module.exports = GladePay;