-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
74 lines (63 loc) · 1.91 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
const request = require('superagent');
const setData = require('./resources/crypto');
const Token = require('./resources/token');
const Customers = require('./resources/customers');
const Plans = require('./resources/plans');
const Subscriptions = require('./resources/subscriptions');
const Bank = require('./resources/bank');
const Cash = require('./resources/cash');
const Charge = require('./resources/charge');
class Epayco {
/**
* [constructor description]
* @param {String} options.apiKey
* @param {String} options.privateKey
* @param {Boolean} options.test
* @param {Boolean} react
* @return {Object}
*/
constructor({ apiKey, privateKey, test }){
if (!(this instanceof Epayco)) {
return new Epayco({ apiKey, privateKey, test });
}
this.__apiKey = apiKey;
this.__privateKey = privateKey;
this.test = test ? 'TRUE' : 'FALSE';
this.token = new Token(this);
this.customers = new Customers(this);
this.plans = new Plans(this);
this.subscriptions = new Subscriptions(this);
this.bank = new Bank(this);
this.cash = new Cash(this);
this.charge = new Charge(this);
return this;
}
/**
* [__request description]
* @param {String} method
* @param {String} url
* @param {Object} data
* @param {Boolean} sw
* @return {Promise}
*/
__request (method, url, data={}, sw) {
if(this.ip){
data['ip'] = this.ip;
}
if (sw) {
data = setData(data, this.__privateKey, this.__apiKey, this.test);
url = Epayco.BASE_URL_SECURE + url;
} else {
url = Epayco.BASE_URL + url;
}
return new Promise((resolve, reject) => request(method, url)
.auth(this.__apiKey, '')
.set('type', 'sdk')
.query(method === 'get' && data || {})
.send(method !== 'get' && data || {})
.end((a, res) => res.ok ? resolve(res.body) : reject(res.error)));
}
};
Epayco.BASE_URL = 'https://api.secure.payco.co';
Epayco.BASE_URL_SECURE = 'https://secure.payco.co';
module.exports = Epayco;