-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtangocard.js
83 lines (65 loc) · 2.34 KB
/
tangocard.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
var request = require('request')
, fs = require('fs')
, path = require('path');
var certContent = fs.readFileSync(path.join(__dirname, 'ca_cert', 'digicert_chain.pem'));
module.exports = function(options) {
var token = new Buffer(options.name + ':' + options.key).toString('base64')
, domain = options.domain || 'https://api.tangocard.com';
var _request = function(method, uri, payload, callback) {
if ('undefined' === typeof callback ) {
callback = payload;
payload = null;
}
method = method.toUpperCase() || 'GET';
var options = {
uri: domain + uri
, method: method
, qs: 'GET' === method ? payload : null
, headers: { 'Authorization': 'Basic ' + token }
, json: true
, body: 'GET' !== method ? payload : null
, agentOptions: { ca: certContent }
};
return request(options, function(err, req, body) {
if (err) {
return callback(err);
}
if (!body) {
return callback(new Error('API Response is empty'));
}
if (false === body.success && 'string' === typeof body.error_message) {
return callback(new Error(body.error_message));
}
return callback(null, body);
});
};
return {
createAccount: function(payload, callback) {
return _request('POST', '/raas/v1/accounts', payload, callback);
},
getAccountInfo: function(customer, accountId, callback) {
return _request('GET', '/raas/v1/accounts/' + customer + '/' + accountId, callback);
},
registerCreditCard: function(payload, callback) {
return _request('POST', '/raas/v1/cc_register', payload, callback);
},
fundAccount: function(payload, callback) {
return _request('POST', '/raas/v1/cc_fund', payload, callback);
},
deleteCreditCard: function(payload, callback) {
return _request('POST', '/raas/v1/cc_unregister', payload, callback);
},
getRewards: function(callback) {
return _request('GET', '/raas/v1/rewards', callback);
},
placeOrder: function(payload, callback) {
return _request('POST', '/raas/v1/orders', payload, callback);
},
getOrderInfo: function(orderId, callback) {
return _request('GET', '/raas/v1/orders/' + orderId, callback);
},
getOrderHistory: function(qs, callback) {
return _request('GET', '/raas/v1/orders', qs, callback);
}
}
};