-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
208 lines (175 loc) Β· 6.4 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
const generateAPIKey = require('./lib/hashing');
const crypto = require('crypto');
require('colors');
module.exports = function(schema, options) {
console.log('[System] I cannot check if your stripe key is valid until a user try to use a stripe service, make sure that it is'.yellow)
options = options || {};
options.successUrl = options.successUrl || 'http://localhost:3000/';
options.cancelUrl = options.cancelUrl || 'http://localhost:3000/';
options.stripeSecret = options.stripeSecret || null;
options.webhookSign = options.webhookSign || null;
options.priceId = options.priceId || null;
options.showUsage = options.showUsage || false;
options.apiKeyField = options.apiKeyField || 'apiKey';
options.saltField = options.saltField || 'salt'
options.customerIdField = options.customerIdField || 'customerId';
options.subscriptionIdField = options.subscriptionIdField || 'subscriptionId';
options.itemIdField = options.itemIdField || 'ItemId';
options.bytesApiKey = options.bytesApiKey || 16;
options.iterations = options.iterations || 25000;
options.salten = options.salten || 'f019832affcc06784b0f8ce25c1c2fd914d8c0261722d9e2bba0cb602a8a4d15';
options.keylen = options.keylen || 512;
options.digest = options.digest || 'sha256';
if(!options.stripeSecret){
throw Error('MissingStripeSecretKey')
}
if(!options.webhookSign){
throw Error('MissingStripeSignKey')
}
const stripe = require('stripe')(options.stripeSecret)
// adding apiKey field to the schema
const schemaFields = {};
schemaFields[options.apiKeyField] = {type: String, select: true};
schemaFields[options.customerIdField] = {type: String, select: true};
schemaFields[options.subscriptionIdField] = {type: String, select: true};
schemaFields[options.itemIdField] = {type: String, select: true};
schema.add(schemaFields);
schema.statics.subscribeUser = async function(user, res) {
if (!(user instanceof this)) {
try{
user = await new this(user);
user.set(options.apiKeyField, 'null');
user.set(options.customerIdField, 'null');
user.set(options.subscriptionIdField, 'null');
user.set(options.itemIdField, 'null')
await user.save()
} catch(e) {
throw new Error(`InvalidUserError: ${e}`)
}
}else{
user.set(options.apiKeyField, 'null');
await user.save()
}
try {
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
payment_method_types: ['card'],
line_items: [
{
price: options.priceId
}
],
client_reference_id: user._id.toString(),
success_url: options.successUrl,
cancel_url: options.cancelUrl
})
res.redirect(session.url);
return user;
} catch(e) {
throw new Error(`InvalidStripeOptions: ${e}`)
}
}
schema.statics.webhook = async function(req, res) {
// Retrieve the event by verifying the signature using the raw body and secret.
let event;
let signature = req.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
req['rawBody'],
signature,
options.webhookSign
);
} catch (err) {
res.sendStatus(400);
throw new Error('InvalidStripeOptions: ' + err)
}
// Extract the object from the event.
let data = event.data;
let eventType = event.type;
switch (eventType) {
case 'checkout.session.completed':
// Check if the user exist
let clientReferenceId = event.data.object.client_reference_id;
const user = await this.findById(clientReferenceId);
if(user.length === 0){
res.sendStatus(403);
return;
}
// Data included in the event object:
const customerId = data.object.customer;
const subscriptionId = data.object.subscription;
// Get the subscription. The first item is the plan the user subscribed to.
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
const itemId = subscription.items.data[0].id;
// Generate API key
const apiKeys = await generateAPIKey(this, options);
// Store the API key in your database.
user[options.apiKeyField] = apiKeys.encryptedApiKey;
user[options.customerIdField] = customerId;
user[options.subscriptionIdField] = subscriptionId;
user[options.itemIdField] = itemId;
await user.save()
console.log(
`[System] Customer ${customerId} subscribed to plan ${subscriptionId}, the generated api Key is ${apiKeys.apiKey}`.yellow
);
break;
case 'invoice.created':
if(event.data.object.status === 'draft'){
const user = await this.updateOne({[options.subscriptionIdField]: event.data.object.subscription},
{
[options.apiKeyField]:'null',
[options.customerIdField]: 'null',
[options.subscriptionIdField]: 'null',
[options.itemIdField]: 'null'
});
}
break;
default:
}
res.sendStatus(200);
}
schema.statics.api = async function(res, dataToSend, api) {
if(!api){
res.sendStatus(400); // bad request
return;
}
let encryptedApiKey;
try{
encryptedApiKey = crypto.pbkdf2Sync(api, options.salten, options.iterations, options.keylen, options.digest).toString('hex');
}catch(e) {
throw new Error('InvalidHashingOptions: ' + e)
}
const user = await this.findOne({[options.apiKey]: encryptedApiKey})
if(user.length === 0){
res.sendStatus(403);
return;
}
const record = await stripe.subscriptionItems.createUsageRecord(
user[options.itemIdField],
{
quantity: 1,
timestamp: 'now',
action: 'increment'
}
)
if(options.showUsage){
dataToSend = {...dataToSend, usage: record}
}
res.json(JSON.stringify(dataToSend));
}
schema.methods.customerRecords = async function(res){
const invoice = await stripe.invoices.retrieveUpcoming({
customer: this[options.customerIdField]
})
res.json(JSON.stringify(invoice))
}
schema.statics.changeApiKey = async function(user) {
if(user[options.apiKeyField] !== 'null') {
const apiKeys = await generateAPIKey(this, options);
user[options.apiKeyField] = apiKeys.encryptedApiKey;
await user.save()
return apiKeys;
}
return 'user.api.failed';
}
}