-
Notifications
You must be signed in to change notification settings - Fork 3
/
zuora.js
146 lines (132 loc) · 4.92 KB
/
zuora.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
const BPromise = require('bluebird');
const got = require('got');
const _ = require('lodash');
const accountsLib = require('./lib/accounts.js');
const actionLib = require('./lib/action.js');
const amendmentLib = require('./lib/amendments.js');
const attachmentsLib = require('./lib/attachments.js');
const billRunLib = require('./lib/billRun.js');
const contactsLib = require('./lib/contacts.js');
const exportsLib = require('./lib/exports.js');
const filesLib = require('./lib/files.js');
const invoicesLib = require('./lib/invoices.js');
const invoiceItemsLib = require('./lib/invoiceItems.js');
const productRatePlansLib = require('./lib/productRatePlans.js');
const productRatePlanChargesLib = require('./lib/productRatePlanCharges.js');
const ratePlanChargesLib = require('./lib/ratePlanCharges.js');
const subscriptionsLib = require('./lib/subscriptions.js');
const taxationItemsLib = require('./lib/taxationItems.js');
const paymentMethodsLib = require('./lib/paymentMethods');
const rsaSignaturesLib = require('./lib/rsaSignatures');
const paymentsLib = require('./lib/payments');
const usagesLib = require('./lib/usages.js');
function Zuora(config) {
this.serverUrl = config.url;
this.oauthType = config.oauthType || 'cookie';
this.apiAccessKeyId = config.apiAccessKeyId;
this.apiSecretAccessKey = config.apiSecretAccessKey;
this.client_id = config.client_id;
this.client_secret = config.client_secret;
this.entityId = config.entityId;
this.entityName = config.entityName;
this.accounts = accountsLib(this);
this.action = actionLib(this);
this.amendments = amendmentLib(this);
this.attachments = attachmentsLib(this);
this.billRun = billRunLib(this);
this.contacts = contactsLib(this);
this.exports = exportsLib(this);
this.files = filesLib(this);
this.invoices = invoicesLib(this);
this.invoiceItems = invoiceItemsLib(this);
this.productRatePlans = productRatePlansLib(this);
this.productRatePlanCharges = productRatePlanChargesLib(this);
this.ratePlanCharges = ratePlanChargesLib(this);
this.subscriptions = subscriptionsLib(this);
this.taxationItems = taxationItemsLib(this);
this.paymentMethods = paymentMethodsLib(this);
this.rsaSignatures = rsaSignaturesLib(this);
this.payments = paymentsLib(this);
this.usages = usagesLib(this);
}
module.exports = Zuora;
Zuora.prototype.authenticate = function() {
const oauthV2 = () => {
if (this.access_token === undefined || Date.now() > this.renewal_time) {
const url = `${this.serverUrl}/oauth/token`;
const auth_params = {
form: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
client_id: this.client_id,
client_secret: this.client_secret,
grant_type: 'client_credentials',
},
};
return got.post(url, auth_params).then((res) => {
const responseBody = JSON.parse(res.body);
this.access_token = responseBody.access_token;
this.renewal_time = Date.now() + responseBody.expires_in * 1000 - 60000;
return { Authorization: `Bearer ${this.access_token}` };
});
} else {
return BPromise.resolve({ Authorization: `Bearer ${this.access_token}` });
}
};
const oauthCookie = () => {
if (this.authCookie === undefined) {
const url = this.serverUrl + '/v1/connections';
const query = {
headers: {
'user-agent': 'zuorajs',
apiAccessKeyId: this.apiAccessKeyId,
apiSecretAccessKey: this.apiSecretAccessKey,
},
json: true,
};
return got.post(url, query).then((res) => {
this.authCookie = res.headers['set-cookie'][0];
return { cookie: this.authCookie };
});
} else {
return BPromise.resolve({ cookie: this.authCookie });
}
};
if (this.oauthType === 'oauth_v2') {
return oauthV2();
} else {
return oauthCookie();
}
};
Zuora.prototype.getObject = function(url) {
return this.authenticate().then((headers) => {
const fullUrl = this.serverUrl + url;
const query = {
headers,
json: true,
};
return got.get(fullUrl, query).then((res) => res.body);
});
};
Zuora.prototype.queryFirst = function(queryString) {
return this.action.query(queryString).then((queryResult) => (queryResult.size > 0 ? queryResult.records[0] : null));
};
Zuora.prototype.queryFull = function(queryString) {
const fullQueryMore = (queryLocator) =>
this.action
.queryMore(queryLocator)
.then((result) =>
result.done ? result.records : fullQueryMore(result.queryLocator).then((more) => _.concat(result.records, more))
);
return this.action
.query(queryString)
.then((result) =>
result.done ? result.records : fullQueryMore(result.queryLocator).then((more) => _.concat(result.records, more))
);
};
Zuora.prototype.downloadFile = async function(url) {
const headers = await this.authenticate();
return got.stream(this.serverUrl + url, { headers });
};