-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
73 lines (58 loc) · 2.03 KB
/
main.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
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const CryptlexApi = require('./Cryptlex/CryptlexApi');
const app = new Koa();
const router = new Router();
const port = process.env.PORT || 3000;
// add exception handler
app.use(async function (ctx, next) {
try {
await next();
} catch (error) {
console.error(error);
ctx.status = error.status || 500;
ctx.body = { message: error.message || 'Oops! Something is broken!' }
}
});
// add body parser
app.use(bodyParser());
router.post('/license', async (ctx) => {
const productId = 'PASTE_YOUR_PRODUCT_ID';
// get post params from request body
const { email, first_name, last_name, order_id } = ctx.request.body;
// create license user
const userBody = {
email: email,
firstName: first_name,
lastName: last_name,
password: 'top_secret', // make sure you change logic for setting the password
role: 'user'
};
const user = await CryptlexApi.createUser(userBody);
// create license
const licenseBody = {
productId: productId,
userId: user.id,
metadata: []
};
licenseBody.metadata.push({ key: 'order_id', value: order_id, visible: false });
const license = await CryptlexApi.createLicense(licenseBody);
console.log("license created successfully!")
// return license key to payment processor
ctx.body = license.key;
});
router.post('/renew-license', async (ctx) => {
const productId = 'PASTE_YOUR_PRODUCT_ID';
// get post params from request body
const { order_id } = ctx.request.body;
// renew license expiry
const license = await CryptlexApi.renewLicense(productId, 'order_id', order_id);
// return new expiry date
ctx.body = { message: `License new expiry date: ${license.expiresAt}` }
});
// add the routes
app.use(router.routes()).use(router.allowedMethods());
// start the server
app.listen(process.env.PORT || 3000);
console.log(`Listening on port: ${port}`);