WIP chargebee integration sketch

This commit is contained in:
Lukas F. Hartmann
2021-05-23 23:53:41 +02:00
parent 6ef1f8fba6
commit 0a1bd2a107
6 changed files with 140 additions and 60 deletions

13
package-lock.json generated
View File

@@ -1035,6 +1035,14 @@
"supports-color": "^5.3.0" "supports-color": "^5.3.0"
} }
}, },
"chargebee": {
"version": "2.6.5",
"resolved": "https://registry.npmjs.org/chargebee/-/chargebee-2.6.5.tgz",
"integrity": "sha512-h11cxGXN+6I47F0Ncj7pemnYO05kLm/vOKPK9mcvdU6K/HByNI/ArlvOLLhpFqjrePfTyCmBTqDYWgYjMP3uJg==",
"requires": {
"q": ">=1.0.1"
}
},
"cheerio": { "cheerio": {
"version": "0.22.0", "version": "0.22.0",
"resolved": "https://registry.npmjs.org/cheerio/-/cheerio-0.22.0.tgz", "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-0.22.0.tgz",
@@ -5267,6 +5275,11 @@
} }
} }
}, },
"q": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz",
"integrity": "sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc="
},
"qs": { "qs": {
"version": "6.7.0", "version": "6.7.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",

View File

@@ -15,6 +15,7 @@
"async": "2.3.0", "async": "2.3.0",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"chargebee": "2.6.5",
"cheerio": "0.22.0", "cheerio": "0.22.0",
"config": "1.25.1", "config": "1.25.1",
"cookie-parser": "~1.4.3", "cookie-parser": "~1.4.3",

View File

@@ -130,9 +130,27 @@ SpacedeckUsers = {
$event.stopPropagation(); $event.stopPropagation();
} }
create_user(name, email, password, password_confirmation, invite_code, function(session) { create_user(name, email, password, password_confirmation, invite_code, function(res) {
this.creating_user = false; this.creating_user = false;
if (res.chargebee_checkout) {
var chargebeeInstance = Chargebee.init({
site: "spacedeck-test",
enableRedirectMode: true,
enableFriendbuyTracking: false
});
chargebeeInstance.openCheckout({
hostedPage: function() {
return new Promise(function(resolve, reject) {
resolve(res.chargebee_checkout);
})
}
});
} else {
// user created, login
this.login_submit(email, password, null, on_success); this.login_submit(email, password, null, on_success);
}
}.bind(this), function(req) { }.bind(this), function(req) {
this.creating_user = false; this.creating_user = false;
try { try {

View File

@@ -23,6 +23,8 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
var glob = require('glob'); var glob = require('glob');
var chargebee = require('chargebee');
router.get('/current', function(req, res, next) { router.get('/current', function(req, res, next) {
if (req.user) { if (req.user) {
var u = _.clone(req.user.dataValues); var u = _.clone(req.user.dataValues);
@@ -39,35 +41,32 @@ router.get('/current', function(req, res, next) {
} }
}); });
// create user function createChargebeeCheckout(req, res, plan_id, email, name, company) {
router.post('/', function(req, res) { var nameParts = name.split(" ");
if (!req.body["email"] || !req.body["password"]) { var firstName = nameParts.shift();
res.status(400).json({"error":"email or password missing"}); var lastName = nameParts.join(" ");
return;
chargebee.hosted_page.checkout_new({
subscription: {
plan_id: plan_id
},
customer: {
first_name: firstName,
last_name: lastName,
email: email,
company: company,
} }
}).request(function(error,result) {
var email = req.body["email"].toLowerCase(); if (error) {
var nickname = req.body["nickname"]; console.log(error);
var password = req.body["password"]; res.status(400).json({"error":error+""});
var password_confirmation = req.body["password_confirmation"]; } else {
var invite_code = req.body["invite_code"]; res.status(200).json({"chargebee_checkout":result.hosted_page});
if (password_confirmation != password) {
res.status(400).json({"error":"password_confirmation"});
return;
} }
});
};
if (config.invite_code && invite_code != config.invite_code) { function createUser(req, res, email, nickname, password) {
res.status(400).json({"error":"Invalid Invite Code."});
return;
}
if (!validator.isEmail(email)) {
res.status(400).json({"error":"email_invalid"});
return;
}
var createUser = function() {
bcrypt.genSalt(10, function(err, salt) { bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(password, salt, function(err, hash) { bcrypt.hash(password, salt, function(err, hash) {
crypto.randomBytes(16, function(ex, buf) { crypto.randomBytes(16, function(ex, buf) {
@@ -124,10 +123,46 @@ router.post('/', function(req, res) {
}); });
}; };
// create user
router.post('/', function(req, res) {
if (!req.body["email"] || !req.body["password"]) {
res.status(400).json({"error":"email or password missing"});
return;
}
var email = req.body["email"].toLowerCase();
var nickname = req.body["nickname"];
var password = req.body["password"];
var password_confirmation = req.body["password_confirmation"];
var invite_code = req.body["invite_code"];
var company = req.body["company"] || "";
if (password_confirmation != password) {
res.status(400).json({"error":"password_confirmation"});
return;
}
if (config.invite_code && invite_code != config.invite_code) {
res.status(400).json({"error":"Invalid Invite Code."});
return;
}
if (!validator.isEmail(email)) {
res.status(400).json({"error":"email_invalid"});
return;
}
db.User.findAll({where: {email: email}}) db.User.findAll({where: {email: email}})
.then(users => { .then(users => {
if (users.length == 0) { if (users.length == 0) {
createUser(); if (config.get('chargebee_integration')) {
// sign up via paid plan trial
createChargebeeCheckout(req, res, config.get('chargebee_default_plan_id'), email, nickname, company);
// TODO: createUser after chargebee checkout
}
else {
createUser(req, res, email, nickname, password);
}
} else { } else {
res.status(400).json({"error":"user_email_already_used"}); res.status(400).json({"error":"user_email_already_used"});
} }

View File

@@ -23,6 +23,8 @@ const express = require('express');
const app = express(); const app = express();
const serveStatic = require('serve-static'); const serveStatic = require('serve-static');
const chargebee = require('chargebee');
const isProduction = app.get('env') === 'production'; const isProduction = app.get('env') === 'production';
// workaround for libssl_conf.so error triggered by phantomjs // workaround for libssl_conf.so error triggered by phantomjs
@@ -110,6 +112,14 @@ if (app.get('env') == 'development') {
module.exports = app; module.exports = app;
// ChargeBee Integration (Optional)
if (config.get('chargebee_integration')) {
chargebee.configure({
site: config.get('chargebee_site'),
api_key: config.get('chargebee_api_key')
});
}
// CONNECT TO DATABASE // CONNECT TO DATABASE
db.init(); db.init();

View File

@@ -57,6 +57,9 @@
<script src="/javascripts/spacedeck_directives.js"></script> <script src="/javascripts/spacedeck_directives.js"></script>
<script src="/javascripts/spacedeck_vue.js"></script> <script src="/javascripts/spacedeck_vue.js"></script>
<!-- TODO: only include based on config -->
<script src="https://js.chargebee.com/v2/chargebee.js"></script>
<script>if (window.module) module = window.module;</script> <script>if (window.module) module = window.module;</script>
</head> </head>