forked from mcohen01/quickbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
444 lines (395 loc) · 14.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* @file node.js client for Intuit's QuickBooks Payments and Financial Data API
* @name quickbooks
* @author Michael Cohen <[email protected]>
* @license ISC
* @copyright 2015 Michael Cohen
*/
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
// provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
// OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
var request = require('request'),
uuid = require('node-uuid'),
debug = require('request-debug'),
util = require('util'),
_ = require('underscore'),
version = require('./package.json').version
module.exports = Quickbooks
Quickbooks.REQUEST_TOKEN_URL = 'https://oauth.intuit.com/oauth/v1/get_request_token'
Quickbooks.ACCESS_TOKEN_URL = 'https://oauth.intuit.com/oauth/v1/get_access_token'
Quickbooks.APP_CENTER_BASE = 'https://appcenter.intuit.com'
Quickbooks.APP_CENTER_URL = Quickbooks.APP_CENTER_BASE + '/Connect/Begin?oauth_token='
Quickbooks.RECONNECT_URL = Quickbooks.APP_CENTER_BASE + '/api/v1/connection/reconnect'
Quickbooks.BASE_URL = 'https://sandbox.api.intuit.com/quickbooks/v4'
/**
* Node.js client encapsulating access to the QuickBooks Payments API. An instance
* of this class should be instantiated on behalf of each user accessing the api.
*
* @param consumerKey - application key
* @param consumerSecret - application password
* @param token - the OAuth generated user-specific key
* @param tokenSecret - the OAuth generated user-specific password
* @param realmId - QuickBooks companyId, returned as a request parameter when the user is redirected to the provided callback URL following authentication
* @param useSandbox - boolean - See https://developer.intuit.com/v2/blog/2014/10/24/intuit-developer-now-offers-quickbooks-sandboxes
* @param debug - boolean flag to turn on logging of HTTP requests, including headers and body
* @constructor
*/
function Quickbooks(consumerKey, consumerSecret, token, tokenSecret, realmId, useSandbox, debug) {
var prefix = _.isObject(consumerKey) ? 'consumerKey.' : ''
this.consumerKey = eval(prefix + 'consumerKey')
this.consumerSecret = eval(prefix + 'consumerSecret')
this.token = eval(prefix + 'token')
this.tokenSecret = eval(prefix + 'tokenSecret')
this.realmId = eval(prefix + 'realmId')
this.useSandbox = eval(prefix + 'useSandbox')
this.debug = eval(prefix + 'debug')
this.endpoint = this.useSandbox ? Quickbooks.BASE_URL : Quickbooks.BASE_URL.replace('sandbox.', '')
}
// ********************** Charge Api **********************
/**
* Create an opaque container that encapsulates a cardholder's credit card information or bank account information.
* @param card - the user's credit card information
* @param callback
*/
Quickbooks.prototype.createToken = function(card, callback) {
module.request(this, 'post', {
url: '/quickbooks/tokens',
headers: {
'Company-Id': this.realmId
}
}, card, callback)
}
/**
* Get details of charge.
*
* @param {string} chargeId - of previously created charge
* @param callback - Callback function which is called with any error or the Charge
*/
Quickbooks.prototype.getCharge = function(chargeId, callback) {
module.request(this, 'get', {
url: '/quickbooks/charges/' + chargeId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Process a credit card charge using card details or token.
* Can capture funds or just authorize.
*
* @param {object} charge - details, amount, currency etc. of charge to be processed
* @param callback - Callback function which is called with any error or the saved Charge
*/
Quickbooks.prototype.charge = function(charge, callback) {
module.request(this, 'post', {
url: '/quickbooks/charges',
headers: {
'Company-Id': this.realmId
}
}, charge, callback)
}
/**
* Allows you to capture funds for an existing charge that was intended to be captured at a later time.
*
* @param {string} chargeId - of previously created charge
* @param {object} charge - details, amount, currency to capture
* @param callback - Callback function which is called with any error or the capture description
*/
Quickbooks.prototype.capture = function(chargeId, charge, callback) {
module.request(this, 'post', {
url: '/quickbooks/charges/' + chargeId + '/capture',
headers: {
'Company-Id': this.realmId
}
}, charge, callback)
}
/**
* Retrieves the Refund for the given refund id
*
* @param {string} chargeId - id of previously created charge
* @param {string} refundId - id of previously created Refund
* @param callback - Callback function which is called with any error or the Refund
*/
Quickbooks.prototype.getChargeRefund = function(chargeId, refundId, callback) {
module.request(this, 'get', {
url: '/quickbooks/charges/' + chargeId + '/refunds/' + refundId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Allows you to refund an existing charge. Full and partial refund are supported.
*
* @param {string} chargeId - of previously created charge
* @param {object} refund - details, amount, currency to refund
* @param callback - Callback function which is called with any error or the refund description
*/
Quickbooks.prototype.refundCharge = function(chargeId, refund, callback) {
module.request(this, 'post', {
url: '/quickbooks/charges/' + chargeId + '/refunds',
headers: {
'Company-Id': this.realmId
}
}, refund, callback)
}
/**
* Retrieves a list of Bank Accounts for the customer
*
* @param {string} customerId - id of customer
* @param callback - Callback function which is called with any error or the list of Bank Accounts
*/
Quickbooks.prototype.bankAccounts = function(customerId, callback) {
module.request(this, 'get', {
url: '/quickbooks/customers/' + customerId + '/bank-accounts',
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Retrieves an individual Bank Account for a customer
*
* @param {string} customerId - id of customer
* @param {string} bankAccountId - id of Bank Account
* @param callback - Callback function which is called with any error or the Bank Account
*/
Quickbooks.prototype.bankAccount = function(customerId, bankAccountId, callback) {
module.request(this, 'get', {
url: '/quickbooks/customers/' + customerId + '/bank-accounts/' + bankAccountId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Create a new Bank Account for the customer
*
* @param {string} customerId - id of customer
* @param {string} bankAccount - Bank Account
* @param callback - Callback function which is called with any error or the Bank Account
*/
Quickbooks.prototype.createBankAccount = function(customerId, bankAccount, callback) {
module.request(this, 'post', {
url: '/quickbooks/customers/' + customerId + '/bank-accounts',
headers: {
'Company-Id': this.realmId
}
}, bankAccount, callback)
}
/**
* Create a new Bank Account for the customer
*
* @param {string} customerId - id of customer
* @param {string} bankAccount - Bank Account
* @param callback - Callback function which is called with any error or the Bank Account
*/
Quickbooks.prototype.createBankAccountFromToken = function(customerId, bankAccount, callback) {
module.request(this, 'post', {
url: '/quickbooks/customers/' + customerId + '/bank-accounts/createFromToken',
headers: {
'Company-Id': this.realmId
}
}, bankAccount, callback)
}
/**
* Deletes an individual Bank Account for a customer
*
* @param {string} customerId - id of customer
* @param {string} bankAccountId - id of Bank Account
* @param callback - Callback function which is called with any error or the Bank Account
*/
Quickbooks.prototype.deleteBankAccount = function(customerId, bankAccountId, callback) {
module.request(this, 'delete', {
url: '/quickbooks/customers/' + customerId + '/bank-accounts/' + bankAccountId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Retrieves a list of Cards for the customer
*
* @param {string} customerId - id of customer
* @param callback - Callback function which is called with any error or the list of Cards
*/
Quickbooks.prototype.cards = function(customerId, callback) {
module.request(this, 'get', {
url: '/quickbooks/customers/' + customerId + '/cards',
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Retrieves an individual Card for a customer
*
* @param {string} customerId - id of customer
* @param {string} cardId - id of Card
* @param callback - Callback function which is called with any error or the Card
*/
Quickbooks.prototype.bankAccount = function(customerId, cardId, callback) {
module.request(this, 'get', {
url: '/quickbooks/customers/' + customerId + '/cards/' + cardId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Create a new Card for the customer
*
* @param {string} customerId - id of customer
* @param {string} card - Card
* @param callback - Callback function which is called with any error or the Card
*/
Quickbooks.prototype.createCard = function(customerId, card, callback) {
module.request(this, 'post', {
url: '/quickbooks/customers/' + customerId + '/cards',
headers: {
'Company-Id': this.realmId
}
}, card, callback)
}
/**
* Create a new Card for the customer
*
* @param {string} customerId - id of customer
* @param {string} card - Card
* @param callback - Callback function which is called with any error or the Card
*/
Quickbooks.prototype.createCardFromToken = function(customerId, card, callback) {
module.request(this, 'post', {
url: '/quickbooks/customers/' + customerId + '/cards/createFromToken',
headers: {
'Company-Id': this.realmId
}
}, card, callback)
}
/**
* Deletes an individual Card for a customer
*
* @param {string} customerId - id of customer
* @param {string} cardId - id of Card
* @param callback - Callback function which is called with any error or the Card
*/
Quickbooks.prototype.deleteCard = function(customerId, cardId, callback) {
module.request(this, 'delete', {
url: '/quickbooks/customers/' + customerId + '/cards/' + cardId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Get details of Echeck.
*
* @param {string} echeckId - of previously created Echeck
* @param callback - Callback function which is called with any error or the Echeck
*/
Quickbooks.prototype.getEcheck = function(echeckId, callback) {
module.request(this, 'get', {
url: '/quickbooks/echecks/' + echeckId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Process an Echeck.
*
* @param {object} echeck - details, amount, currency etc. of charge to be processed
* @param callback - Callback function which is called with any error or the saved Echeck
*/
Quickbooks.prototype.echeck = function(echeck, callback) {
module.request(this, 'post', {
url: '/quickbooks/echecks',
headers: {
'Company-Id': this.realmId
}
}, echeck, callback)
}
/**
* Retrieves the Refund for the given refund id
*
* @param {string} echeckId - id of previously created echeck
* @param {string} refundId - id of previously created Refund
* @param callback - Callback function which is called with any error or the Refund
*/
Quickbooks.prototype.getEcheckRefund = function(echeckId, refundId, callback) {
module.request(this, 'get', {
url: '/quickbooks/echecks/' + echeckId + '/refunds/' + refundId,
headers: {
'Company-Id': this.realmId
}
}, null, callback)
}
/**
* Allows you to refund an existing echeck. Full and partial refund are supported.
*
* @param {string} echeckId - of previously created echeck
* @param {object} refund - details, amount, currency to refund
* @param callback - Callback function which is called with any error or the refund description
*/
Quickbooks.prototype.refundEcheck = function(echeckId, refund, callback) {
module.request(this, 'post', {
url: '/quickbooks/echecks/' + echeckId + '/refunds',
headers: {
'Company-Id': this.realmId
}
}, refund, callback)
}
module.request = function(context, verb, options, entity, callback) {
var url = context.endpoint + options.url
if (options.url === Quickbooks.RECONNECT_URL) {
url = options.url
}
var opts = {
url: url,
qs: options.qs || {},
headers: options.headers || {},
oauth: module.oauth(context),
json: true
}
opts.headers['Content-Type'] = 'application/json'
opts.headers['User-Agent'] = 'quickbooks4js: version ' + version
opts.headers['Request-Id'] = uuid.v1()
if (entity !== null) {
opts.body = entity
}
if ('production' !== process.env.NODE_ENV && context.debug) {
debug(request)
}
request[verb].call(context, opts, function (err, res, body) {
if ('production' !== process.env.NODE_ENV && context.debug) {
console.log('invoking endpoint: ' + url)
if (entity) console.log(entity)
console.log(util.inspect(err || body, {showHidden: false, depth: null}));
}
if (callback) {
if (err ||
res.statusCode >= 300 ||
(_.isObject(body) && body.Fault && body.Fault.Error && body.Fault.Error.length)) {
callback(err || body, body)
} else {
callback(null, body)
}
} else {
return
}
})
}
Quickbooks.prototype.reconnect = function(callback) {
module.request(this, 'get', {url: Quickbooks.RECONNECT_URL}, null, callback)
}
module.oauth = function(context) {
return {
consumer_key: context.consumerKey,
consumer_secret: context.consumerSecret,
token: context.token,
token_secret: context.tokenSecret
}
}