File size: 6,481 Bytes
cf0f72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';
var BBPromise = require('bluebird');
var OAuth = require('@hoist/oauth').OAuth;
var _ = require('lodash');
var logger = require('@hoist/logger');
var config = require('config');

function QBOConnector(settings) {
  logger.info({
    settings: settings
  }, 'constructed qbo-connector');
  this.settings = settings;

  this.auth = BBPromise.promisifyAll(new OAuth(
    'https://oauth.intuit.com/oauth/v1/get_request_token',
    'https://oauth.intuit.com/oauth/v1/get_access_token',
    settings.consumerKey,
    settings.consumerSecret,
    '1.0',
    'https://' + config.get('Hoist.domains.bouncer') + '/bounce',
    'HMAC-SHA1'
  ), {
    multiArgs: true
  });
  this.auth._originalCreateClient = this.auth._createClient;
  this.auth._createClient = function (port, hostname, method, path, headers, sslEnabled) {
    headers['user-agent'] = 'Hoist';
    headers.Accept = 'application/json';
    return this._originalCreateClient(port, hostname, method, path, headers, sslEnabled);
  };
  this.auth._performSecureRequestAsync = BBPromise.promisify(this.auth._performSecureRequest, this.auth, {
    multiArgs: true
  });
  _.bindAll(this);
}


/* istanbul ignore next */
QBOConnector.prototype.authorize = function (authorization) {
  this.authorization = authorization;
  this.settings.accessKey = authorization.get('AccessToken');
  this.settings.accessSecret = authorization.get('AccessTokenSecret');
};

QBOConnector.prototype.swapRequestToken = function (requestToken, requestTokenSecret, verifier) {
  return this.auth.getOAuthAccessTokenAsync(requestToken, requestTokenSecret, verifier);
};

QBOConnector.prototype.generateRequestToken = function () {
  logger.info('requesting auth token');
  return this.auth.getOAuthRequestTokenAsync();
};


QBOConnector.prototype.receiveBounce = function (bounce) {
  logger.info(this.settings);
  if (bounce.get('RequestToken')) {
    /*jshint camelcase: false */
    return this.swapRequestToken(bounce.get('RequestToken'), bounce.get('RequestTokenSecret'), bounce.query.oauth_verifier)
      .spread(function (accessToken, accessTokenSecret, auth_headers) {
        logger.info(auth_headers);
        logger.info('got request token');
        return bounce.delete('RequestToken')
          .then(function () {
            return bounce.delete('RequestTokenSecret');
          }).then(function () {
            return bounce.set('AccessToken', accessToken);
          }).then(function () {
            return bounce.set('AccessTokenSecret', accessTokenSecret);
          }).then(function () {
            return bounce.set('CompanyId', bounce.query.realmId);
          }).then(function () {
            bounce.done();
          });
      });
  } else if (bounce.get('IntuitSetup')) {
    return this.generateRequestToken()
      .bind(this)
      .spread(function (requestToken, requestTokenSecret) {
        logger.info('got request token');
        return bounce.set('RequestToken', requestToken)
          .bind(this)
          .then(function () {
            return bounce.set('RequestTokenSecret', requestTokenSecret);
          })
          .bind(this)
          .then(function () {
            bounce.redirect('https://appcenter.intuit.com/Connect/Begin?oauth_token=' + requestToken);
          });
      });
  } else {
    return this.generateSetupUrl(bounce)
      .bind(this)
      .then(function (qboSetupUrl) {
        return bounce.set('IntuitSetup', true)
          .then(function () {
            bounce.redirect(qboSetupUrl);
          });
      });
  }
  bounce.done();
};

QBOConnector.prototype.generateSetupUrl = function (bounce) {
  var datasources = [];
  return BBPromise.try(function () {
    /* istanbul ignore else */
    if (bounce.query.quickbooks || (!(bounce.query.quickbooks) && !(bounce.query.payments))) {
      datasources.push('quckbooks');
    }
    /* istanbul ignore else */
    if (bounce.query.payments) {
      datasources.push('payments');
    }
    return 'https://appcenter.intuit.com/Connect/SessionStart?grantUrl=' + encodeURIComponent('https://' + config.get('Hoist.domains.bouncer') + '/bounce') + '&datasources=' + encodeURIComponent(datasources.join());
  });
};

QBOConnector.prototype.get = function get(url, extraHeader) {
  logger.info('inside hoist-connector-qbo.get');
  return this.request('GET', url, null, extraHeader);
};

QBOConnector.prototype.update = function put(url, data) {
  logger.info('inside hoist-connector-qbo.put');
  var uri = require('url').parse(url, true);
  uri.query.operation = 'update';
  delete uri.search;
  delete uri.path;
  return this.request('POST', require('url').format(uri), data);
};
QBOConnector.prototype.delete = function put(url, data) {
  logger.info('inside hoist-connector-qbo.put');
  var uri = require('url').parse(url, true);
  uri.query.operation = 'delete';
  delete uri.search;
  delete uri.path;
  return this.request('POST', require('url').format(uri), data);
};

QBOConnector.prototype.create = function post(url, data) {
  logger.info('inside hoist-connector-qbo.post');
  return this.request('POST', url, data);
};

QBOConnector.prototype.authorize = function (authorization) {
  this.authorization = authorization;
  this.settings.accessKey = authorization.get('AccessToken');
  this.settings.accessSecret = authorization.get('AccessTokenSecret');
  this.settings.companyId = authorization.get('CompanyId');

};
QBOConnector.prototype.getRootUrl = function () {
  var domain = 'quickbooks.api.intuit.com';
  if (this.settings.isSandbox) {
    domain = 'sandbox--quickbooks-api-intuit-com-s404txkcwglf.runscope.net';
    var originalDomain = 'sandbox-quickbooks.api.intuit.com';
    this.auth._createSignatureBaseOriginal = this.auth._createSignatureBase;
    this.auth._createSignatureBase = function (method, u, parameters) {
      return this._createSignatureBaseOriginal(method, u.replace(domain, originalDomain), parameters);
    };
  }
  return 'https://' + domain + '/v3/company/' + this.settings.companyId;
};
QBOConnector.prototype.request = function request(method, path, data) {
  var contentType = 'application/json';
  data = data ? data : null;

  logger.info({
    method: method,
    path: path
  }, 'inside hoist-connector-qbo.request');
  return this.auth._performSecureRequestAsync(this.settings.accessKey, this.settings.accessSecret, method, this.getRootUrl() + path, null, data, contentType).spread(function (data) {
    return JSON.parse(data);
  });
};


module.exports = QBOConnector;