forked from liltimtim/mc-stripe-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
302 lines (280 loc) · 8.71 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
const Stripe = require('stripe');
const random = require('randomstring');
/**
* This library only supports USD at the moment.
*/
class StripeInterface {
constructor(stripeAPIKey) {
this.api = require('stripe')(stripeAPIKey);
}
findStripeConnect(connectId) {
return new Promise((resolve, reject) => {
this.api.accounts.retrieve(connectId, (err, account) => {
if (err) { return reject(err); }
return resolve(account);
});
});
}
createCustomer(userId, token) {
return new Promise((resolve, reject) => {
this.api.customers.create({
description: `Customer ${userId}`,
source: token
}, function(err, customer) {
if(err) return reject(err);
return resolve(customer);
});
});
}
findCustomer(customerId) {
return new Promise((resolve, reject) => {
this.api.customers.retrieve(customerId, (err, customer) => {
if(err) return reject(err);
return resolve(customer);
});
});
}
deleteCustomer(customerId) {
return new Promise((resolve, reject) => {
this.api.customers.del(customerId, (err, confirmation) => {
if(err) return reject(err);
return resolve(confirmation);
});
});
}
findSubscription(subscriptionId) {
return new Promise((resolve, reject) => {
this.api.subscriptions.retrieve(subscriptionId, (err, subscription) => {
if(err) return reject(err);
return resolve(subscription);
});
});
}
createSubscription(customerId, planId, quantity) {
return new Promise((resolve, reject) => {
if(quantity <= 0){
let err = new Error('quanitity must be greater than 0');
return reject(err);
}
this.api.subscriptions.create({
customer: customerId,
plan: planId,
quantity: quantity
}, (err, subscription) => {
if(err) return reject(err);
return resolve(subscription);
});
});
}
updateCustomer(customerId, token) {
return new Promise((resolve, reject) => {
this.api.customers.update(customerId, {
source: token
}, (err, customer) => {
if (err) return reject(err);
return resolve(customer);
});
});
}
updateSubscription(subscriptionId, quantity) {
return new Promise((resolve, reject) => {
if(quantity <= 0){
let err = new Error('Quantity must be greater than 0');
return reject(err);
}
this.api.subscriptions.update(subscriptionId, {
quantity: quantity
}, (err, subscription) => {
if(err) return reject(err);
return resolve(subscription);
});
});
}
updateSubscriptionPlan(subscriptionId, subscription, newPlanId) {
return new Promise((resolve, reject) => {
this.api.subscriptions.update(subscriptionId, {
items: [{
id: subscription.items.data[0].id,
plan: newPlanId,
}]
}, (err, subscription) => {
if(err) return reject(err);
return resolve(subscription);
});
});
}
removeSubscription(subscriptionId) {
return new Promise((resolve, reject) => {
this.api.subscriptions.del(subscriptionId, (err, confirmation) => {
if(err) return reject(err);
return resolve(confirmation);
});
});
}
removeSubscriptionAtEnd(subscriptionId) {
return new Promise((resolve, reject) => {
this.api.subscriptions.del(subscriptionId, {at_period_end: true}, (err, confirmation) => {
if(err) return reject(err);
return resolve(confirmation);
});
});
}
/**
* Gets the subscriptions statuses. Returns an array of objects with subscription id as key and its status.
* @param {String} subscriptionId
*/
subscriptionStatus(subscriptionId) {
return new Promise((resolve, reject) => {
this.api.subscriptions.retrieve(subscriptionId, (err, subscription) => {
if(err) return reject(err);
if(!subscription.status) {
let err = new Error('Issue occurred while retrieving the subscription');
return reject(err);
}
return resolve({status: subscription.status});
});
});
}
// Customer Card Functionality
/**
* Retrive all customer cards limit 100
* @param {String} customerId Stripe customer ID
*/
getCustomerCards(customerId) {
return new Promise((resolve, reject) => {
this.api.customers.listCards(customerId, (err, cards) => {
if(err) return reject(err);
return resolve(cards);
});
});
}
/**
* Deletes a specific on file card from stripe customer
* @param {String} cardId Stripe card id
* @param {String} customerId Stripe customer id
*/
deleteCustomerCard(cardId, customerId) {
return new Promise((resolve, reject) => {
this.api.customers.deleteCard(customerId, cardId, (err, confirmation) => {
if(err) return reject(err);
return resolve(confirmation);
});
});
}
/**
* Updates a specific customer card. All cardObj values are optional.
* @param {String} customerId
* @param {String} cardId
* @param {{address_city:string, address_country:string, address_line1:string, address_line2:string, address_state:string, address_zip:string, exp_month:number, exp_year:number, name:string}} cardObj
*/
updateCustomerCard(customerId, cardId, cardObj) {
return new Promise((resolve, reject) => {
this.api.customers.updateCard(customerId, cardId, cardObj, (err, card) => {
if(err) return reject(err);
return resolve(card);
});
});
}
createCustomerCard(customerId, token) {
return new Promise((resolve, reject) => {
this.api.customers.createSource(customerId, {
source: token
}, (err, card) => {
if(err) return reject(err);
return resolve(card);
});
});
}
// Customer Invoicing retrieval
getInvoices(customerId, startAfterObjectId = null) {
return new Promise((resolve, reject) => {
this.api.invoices.list({ limit: 100, customer: customerId }, (err, invoices) => {
if(err) return reject(err);
return resolve(invoices);
});
});
}
getUpcomingInvoices(customerId) {
return new Promise((resolve, reject) => {
this.api.invoices.retrieveUpcoming(customerId, (err, upcoming) => {
if(err) return reject(err);
return resolve(upcoming);
});
});
}
// Create new Plan objects
/**
* Creates a custom plan that will be billed at the interval scale and interval frequency. Default currency is USD.
* @param {String} name human readable name for plan.
* @param {Number} amount amount in cents
* @param {String} interval payment charge interval either year, month, day or week
* @param {Number} intervalCount frequency the charge will be made based on the interval. Example: interval = month interval count = 3 bills every 3 months.
*/
createPlan(name, amount, interval, intervalCount) {
return new Promise((resolve, reject) => {
let allowedIntervalTypes = ['year', 'month', 'day', 'week'];
if(!allowedIntervalTypes.includes(interval)) {
let err = new Error('interval must be year, month, day, or week');
return reject(err);
}
if(amount <= 0) {
let err = new Error('amount must be greater than 0');
return reject(err);
}
this.api.plans.create({
amount: amount,
interval: interval,
product: {
name: name
},
currency: 'usd',
id: random.generate({length: 12})
}, (err, plan) => {
if(err) return reject(err);
return resolve(plan);
});
});
}
deletePlan(planId) {
return new Promise((resolve, reject) => {
this.api.plans.del(planId, (err, confirmation) => {
if(err) return reject(err);
return resolve(confirmation);
});
});
}
listPlans(startingAfterPlanId = undefined) {
return new Promise((resolve, reject) => {
var query = {limit: 100}
if(startingAfterPlanId) {
query.starting_after = startingAfterPlanId;
}
this.api.plans.list(query, (err, plans) => {
if(err) return reject(err);
return resolve(plans);
});
});
}
getPlan(id) {
return new Promise((resolve, reject) => {
this.api.plans.retrieve(id, (err, plan) => {
if(err) return reject(err);
return resolve(plan);
});
});
}
/**
*
* @param {String} id
* @param {{metadata:Object, name:String, statement_descriptor:String}} options
*/
updatePlan(id, options) {
return new Promise((resolve, reject) => {
this.api.plans.update(id, options, (err, plan) => {
if(err) return reject(err);
return resolve(plan);
});
});
}
}
module.exports = StripeInterface;