-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (105 loc) · 3.04 KB
/
app.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
const express = require('express')
const path = require("path");
require('dotenv').config()
var bodyParser = require('body-parser')
const moesif = require('moesif-nodejs');
const Stripe = require('stripe');
// npm i --save [email protected]
const fetch = require('node-fetch');
const app = express();
app.use(express.static(path.join(__dirname)));
const port = 5000;
const stripe = Stripe(process.env.STRIPE_KEY);
var jsonParser = bodyParser.json();
const moesifMiddleware = moesif({
applicationId: process.env.MOESIF_APPLICATION_ID
});
app.use(moesifMiddleware);
app.post('/register', jsonParser,
async (req, res) => {
// create Stripe customer
const customer = await stripe.customers.create({
email: req.body.email,
name: `${req.body.firstname} ${req.body.lastname}`,
description: 'Customer created through /register endpoint',
});
// create Stripe subscription
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [
{ price: process.env.STRIPE_PRICE_KEY },
],
});
// create user, company, and subscription in Moesif
var company = { companyId: customer.id };
moesifMiddleware.updateCompany(company);
var user = {
userId: customer.id,
companyId: customer.id,
metadata: {
email: req.body.email,
firstName: req.body.firstname,
lastName: req.body.lastname,
}
};
moesifMiddleware.updateUser(user);
var subscription = {
subscriptionId: subscription.id,
companyId: customer.id,
status: "active",
}
moesifMiddleware.updateSubscription(subscription).then((result) => {
console.log("subscription updated successfully");
}).catch((err) => {
console.error("Error updating subscription", err);
} );
// send back a new API key for use
var body = {
alias: customer.id,
last_check: 0,
allowance: 1000,
rate: 1000,
per: 60,
expires: 0,
quota_max: 10000,
quota_renews: 1424543479,
quota_remaining: 10000,
quota_renewal_rate: 2520000,
access_rights: {
[process.env.TYK_API_ID]: {
api_id: process.env.TYK_API_ID,
api_name: process.env.TYK_API_NAME,
versions: [
"Default"
]
}
}
}
var response = await fetch(`${process.env.TYK_URL}/api/keys`, {
method: 'post',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
"Authorization": process.env.TYK_AUTH_KEY
}
});
var data = await response.json();
var tykAPIKey = data.key_id;
var user = {
userId: customer.id,
metadata: {
apikey: tykAPIKey,
}
};
moesifMiddleware.updateUser(user);
res.status(200)
res.send({ apikey: tykAPIKey });
}
)
app.get("/", function (_req, res) {
res.sendFile(path.join(__dirname, "index.html"));
res.sendFile(path.join(__dirname, "index.js"));
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
})