-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
153 lines (97 loc) · 4.08 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var request = require("request")
var url = require("url")
module.exports = function(api_secret, marketplace_id) {
if (api_secret === undefined){throw new Error("missing required api_secret")}
if (marketplace_id === undefined){throw new Error("missing required marketplace_id")}
var client = function(method, uri, json, cb) {
//make json param optional
if(typeof json === 'function' && cb === undefined){cb = json; json = null}
request({
method: method,
uri: url.format({protocol: "https", host: "api.balancedpayments.com", auth: api_secret+":", pathname: uri}),
encoding: "utf-8",
json: json || true
}, function(err, response, body) {
if (response.statusCode >= 400){
if(body !== undefined){
err = new Error("Balanced call failed: "+response.statusCode+" - "+body.response)
}else{
err = new Error("Balanced call failed: "+response.statusCode)
}
}
cb(err, body)
})
}
return {
account: {
//creates a new balanced account
create: function(account, cb){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts", account, cb)
},
//adds a card to their account
add_card: function(account_id, card_info_or_id, cb){
if(typeof card_info_or_id === "object"){
var card = {card:card_info_or_id}
}else{
var card = {card_uri:"/v1/marketplaces/"+marketplace_id+"/cards/"+card_info_or_id}
}
client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, card, cb)
},
//debits the accounts card
debit: function(account_id, debit, cb){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/debits", debit, cb)
},
//puts a hold on the accounts card
hold: function(account_id, hold, cb){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/holds", hold, cb)
},
//adds a bank account to this account
add_bank: function(account_id, bank_info_or_id, cb){
if(typeof bank_info_or_id === "object"){
var bank = {bank_account:bank_info_or_id}
}else{
var bank = {bank_account_uri:"/v1/marketplaces/"+marketplace_id+"/bank_accounts/"+bank_info_or_id}
}
client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, bank, cb)
},
//credits accounts bank account
credit: function(account_id, credit, cb){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/credits", credit, cb)
},
//adds extra deatils for underwriting purposes
underwrite: function(account_id, underwriting_info, cb){
client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, {merchant: underwriting_info}, cb)
},
//returns account details
get: function(account_id, cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, cb)
},
//returns object of recent credits and debits for the account
transactions: function(account_id, cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/transactions", cb)
}
},
marketplace: {
//returns list of accounts for the marketplace
accounts: function(cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts", cb)
},
//returns list of debits for the marketplace
debits: function(cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/debits", cb)
},
//returns list of credits for the marketplace
credits: function(cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/credits", cb)
},
//returns list of refunds for the marketplace
refunds: function(cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/refunds", cb)
},
//returns list of holds for the marketplace
holds: function(cb){
client("GET", "/v1/marketplaces/"+marketplace_id+"/holds", cb)
}
}
}
}