summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-01-28 19:18:27 -0500
committerJules Laplace <jules@okfoc.us>2015-01-28 20:21:49 -0500
commit5adac681bdb43b8b709795fa501689fb9ae8a4e1 (patch)
tree635ad1f56013840f60dba46e4ba8fd0261353f3b
parente3ff5315f7ea7421431658077253c4d71f0f5731 (diff)
many methods
-rw-r--r--package.json1
-rw-r--r--server/lib/api/subscription.js124
-rw-r--r--server/lib/middleware.js2
-rw-r--r--server/lib/schemas/Subscription.js6
-rw-r--r--server/lib/views/staff.js2
-rw-r--r--server/lib/webhook/webhook.js2
-rw-r--r--views/about/brochure.ejs15
-rw-r--r--views/partials/header.ejs1
-rw-r--r--views/staff/_users.ejs1
-rw-r--r--views/staff/plans/_form.ejs5
-rw-r--r--views/staff/subscriptions/index.ejs2
-rw-r--r--views/staff/users/show.ejs1
12 files changed, 135 insertions, 27 deletions
diff --git a/package.json b/package.json
index 8afb96e..e89fcd9 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,7 @@
"express-subdomains": "0.0.5",
"html-entities": "~1.0.10",
"intro.js": "^0.9.0",
+ "js2xml": "^1.0.0",
"knox": "~0.8.10",
"lodash": "~2.4.1",
"marked": "~0.3.2",
diff --git a/server/lib/api/subscription.js b/server/lib/api/subscription.js
index 83644cf..40aa715 100644
--- a/server/lib/api/subscription.js
+++ b/server/lib/api/subscription.js
@@ -9,7 +9,22 @@ var _ = require('lodash'),
Layout = require('../schemas/Layout'),
Subscription = require('../schemas/Subscription');
+var plan_levels = {
+ free: 0,
+ basic: 1,
+ pro: 2,
+}
+
var subscription = module.exports = {
+ middleware: {
+ ensureSubscription: function(req, res, next){
+ Subscription.findOne({ user_id: req.user._id }, function(err, data){
+ if (err) { return next() }
+ req.subscription = data
+ next()
+ })
+ },
+ }
/*
index: function(req, res){
@@ -18,30 +33,115 @@ var subscription = module.exports = {
})
},
*/
- middleware: {
- fetchAccount: function(req, res, next){
- recurly.subscriptions.listByAccount(req.user._id, function(data){
- })
- },
- },
// synchronise an account with recurly..
- // useful when testing locally (if webhooks do not fire)
+ // useful when testing locally (where webhooks cannot be received)
sync: function(req, res){
- // fetch req.user._id
+ var subscriber = req.subscription || new Subscription ()
+ var user = req.user
+ recurly.subscriptions.listByAccount(req.user._id, function(data){
+ if (data.description !== 200) {
+ res.json({ error: "no account" })
+ return
+ }
+ if (! data.subscriptions.subscription) {
+ res.json({ error: "account error" })
+ return
+ }
+
+ var plan = data.subscriptions.subscription.plan.plan_code.split("-")
+ var plan_type = plan[0]
+ var plan_period = plan[1]
+
+ user.plan_type = plan_type
+ user.plan_level = plan_levels[plan_type]
+
+ subscriber.uuid = data.subscriptions.subscription.uuid
+ subscriber.user_id = user._id
+ subscriber.plan_type = plan_type
+ subscriber.plan_period = plan_period
+ subscriber.plan_level = plan_levels[plan_type]
+
+ var add_ons = data.subscriptions.subscription.subscription_add_ons.subscription_add_on
+ if (add_ons) {
+ if (add_ons.add_on_code) {
+ add_ons = [ add_ons ]
+ }
+ // TODO: handle multiple add-ons.. presumably this will work
+ add_ons.forEach(function(add_on){
+ var add_on_type = add_on.add_on_code.split("-")[1]
+ if (add_on_type == "basic") {
+ subscriber.basic_layouts = parseInt( add_on.quantity._ )
+ }
+ if (add_on_type == "pro") {
+ subscriber.pro_layouts = parseInt( add_on.quantity._ )
+ }
+ })
+ }
+ else {
+ subscriber.basic_layouts = 0
+ subscriber.pro_layouts = 0
+ }
+
+ subscriber.save(function(){
+ user.save(function(){
+ res.render(subscriber)
+ })
+ })
+ })
},
show: function(req, res){
- // fetch from recurly
+ res.json(req.subscription || { error: "no subscription" })
},
update: function(req, res){
- // update plan_type on recurly
- // update add_ons on recurly
+ if (! req.subscription ) {
+ return res.json({ error: "no subscription" })
+ }
+ if (! (req.body.plan_type in plan_levels)) {
+ return res.json({ error: "bad input" })
+ }
+ var subscriber = req.subscription
+
+ // change..
+ // data.plan_code
+ // data.subscription_add_ons = []
+ // add_on.add_on_code
+ // add_on.quantity
+ var basic_layouts = max(0, parseInt(req.body.basic_layouts))
+ var pro_layouts = max(0, parseInt(req.body.pro_layouts))
+
+ var data = {}
+ data.plan_code = req.body.plan_type + "_monthly"
+ data.subscription_add_ons = []
+
+ if (plan_levels[req.body.plan_type]) {
+ data.subscription_add_ons.push({ add_on_code: "extra-basic-layout", quantity: basic_layouts })
+ }
+
+ if (req.body.plan_type == "pro") {
+ data.subscription_add_ons.push({ add_on_code: "extra-pro-layout", quantity: pro_layouts })
+ }
+
+
+ recurly.subscriptions.update(subscriber.uuid, data, function(){
+ return res.json(subscriber)
+ })
},
destroy: function(req, res){
- // destroy on recurly
+ if (! req.subscription ) {
+ return res.json({ error: "no subscription" })
+ }
+ var subscriber = req.subscription
+
+ recurly.subscriptions.cancel(subscriber.uuid, function(){
+ subscriber.remove(function(){
+ req.user.plan_code = 0
+ req.user.plan_type = "free"
+ })
+ })
},
}; \ No newline at end of file
diff --git a/server/lib/middleware.js b/server/lib/middleware.js
index 797d677..7dfe821 100644
--- a/server/lib/middleware.js
+++ b/server/lib/middleware.js
@@ -59,7 +59,7 @@ var middleware = {
res.locals.opt = {}
next()
},
-
+
ensureProject: function (req, res, next) {
if (req.params.slug) {
Project.findOne({ slug: req.params.slug }, function(err, project){
diff --git a/server/lib/schemas/Subscription.js b/server/lib/schemas/Subscription.js
index b766555..24c5096 100644
--- a/server/lib/schemas/Subscription.js
+++ b/server/lib/schemas/Subscription.js
@@ -13,10 +13,8 @@ var SubscriptionSchema = new mongoose.Schema({
plan_period: { type: String, default: "monthly" },
subscription_uuid: { type: String },
- subscription_add_ons: [{
- name: { type: String },
- quantity: { type: Number },
- }],
+ basic_layouts: { type: Number, default: 0 },
+ pro_layouts: { type: Number, default: 0 },
history: [{
action: { type: String },
diff --git a/server/lib/views/staff.js b/server/lib/views/staff.js
index 74dd7cd..67193fe 100644
--- a/server/lib/views/staff.js
+++ b/server/lib/views/staff.js
@@ -154,7 +154,7 @@ var staff = module.exports = {
},
ensurePlans: function(req, res, next){
- Plan.find(function (err, plans) {
+ Plan.find({}).sort({ 'level': -1 }).exec(function (err, plans) {
res.locals.plans = (plans || []).map(staff.helpers.plan)
res.locals.plans.sort(function(a,b){ return a.monthly_price })
next()
diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js
index 4f23d0b..a871a3a 100644
--- a/server/lib/webhook/webhook.js
+++ b/server/lib/webhook/webhook.js
@@ -149,6 +149,6 @@ var subscribe = module.exports = {
route: function(app){
app.post('/subscribe/webhook', subscribe.handle);
- app.get('/subscribe/list/:id', subscribe.list);
+ app.get('/subscribe/list/:id', subscribe.middleware.ensureSubscription, subscribe.list);
},
}
diff --git a/views/about/brochure.ejs b/views/about/brochure.ejs
index 49b03db..1dad763 100644
--- a/views/about/brochure.ejs
+++ b/views/about/brochure.ejs
@@ -34,12 +34,11 @@
<li> VValls logo appears when embedding an exhibition on a web page
<li>
[[ if (! logged_in) { ]]
- <button href="/signup">Sign Up</button>
- [[ } else if (! user.plan_level || user.plan_level < plan.level) { ]]
- <button href="https://vvalls.recurly.com/subscribe/basic/[[- user._id ]]/[[- user.username ]]">Buy Now</button>
+ <button data-role="show-signup-modal">Sign Up</button>
+ [[ } else if (! user.plan_level) { ]]
+ <a href="https://vvalls.recurly.com/subscribe/basic-monthly/[[- user._id ]]/[[- user.username ]]"><button>Buy Now</button></a>
[[ } else if (user.plan_level == plan.level) { ]]
Current Level
- [[ } else { ]]
[[ } ]]
</ul>
</div>
@@ -56,11 +55,13 @@
<li> No VValls logo on embed
<li>
[[ if (! logged_in) { ]]
- <button href="/signup">Sign Up</button>
- [[ } else if (! user.plan_level || user.plan_level < plan.level) { ]]
- <button href="https://vvalls.recurly.com/subscribe/pro/[[- user._id ]]/[[- user.username ]]">Buy Now</button>
+ <button data-role="show-signup-modal">Sign Up</button>
+ [[ } else if (! user.plan_level) { ]]
+ <a href="https://vvalls.recurly.com/subscribe/pro-monthly/[[- user._id ]]/[[- user.username ]]"><button>Buy Now</button></a>
[[ } else if (user.plan_level == plan.level) { ]]
Current Level
+ [[ } else if (user.plan_level < plan.level) { ]]
+ <a href="/profile/billing"><button>Upgrade Now</button></a>
[[ } ]]
</ul>
</div>
diff --git a/views/partials/header.ejs b/views/partials/header.ejs
index ce5bab9..2acf2bc 100644
--- a/views/partials/header.ejs
+++ b/views/partials/header.ejs
@@ -64,6 +64,7 @@
[[ if (profile && String(user._id) == String(profile._id)) { ]]
<a href="/profile" data-role="edit-profile-modal" class="topLink editProfile">Settings</a>
+ <a href="/profile" data-role="edit-subscription-modal" class="topLink editSubscription">Subscription</a>
[[ } else if (! profile) { ]]
<a href="/profile" class="topLink profileLink">Profile</a>
[[ } ]]
diff --git a/views/staff/_users.ejs b/views/staff/_users.ejs
index d46058f..053e289 100644
--- a/views/staff/_users.ejs
+++ b/views/staff/_users.ejs
@@ -12,6 +12,7 @@
</td>
<td class="editLinks">
<a href="/profile/[[- user.username ]]">[view profile]</a>
+ <a href="https://vvalls.recurly.com/accounts/[[- profile._id ]]">[recurly]</a>
</td>
<td>
[[- user.last_seen ]]
diff --git a/views/staff/plans/_form.ejs b/views/staff/plans/_form.ejs
index ae5ca5a..b55c5cd 100644
--- a/views/staff/plans/_form.ejs
+++ b/views/staff/plans/_form.ejs
@@ -16,6 +16,11 @@
<div><input id="plan_slug" name="slug" type="text" value="[[- plan.slug ]]"></div>
</li>
+<li>
+ <label for="plan_level">Level</label>
+ <div><input id="plan_level" name="level" type="number" value="[[- plan.level ]]"></div>
+</li>
+
<!-- - - - - -->
<li class="section_break">
diff --git a/views/staff/subscriptions/index.ejs b/views/staff/subscriptions/index.ejs
index d1c0588..3efffb5 100644
--- a/views/staff/subscriptions/index.ejs
+++ b/views/staff/subscriptions/index.ejs
@@ -1,6 +1,6 @@
[[ include ../_header ]]
- <h1>Users</h1>
+ <h1>Subscriptions</h1>
[[ include ../_nav ]]
diff --git a/views/staff/users/show.ejs b/views/staff/users/show.ejs
index 4ce1d9a..d6a21d5 100644
--- a/views/staff/users/show.ejs
+++ b/views/staff/users/show.ejs
@@ -19,6 +19,7 @@
<td class="editLinks">
<a href="/profile/[[- profile.username ]]">[view profile]</a>
<a href="/staff/users/[[- profile.username ]]/media">[view media]</a>
+ <a href="https://vvalls.recurly.com/accounts/[[- profile._id ]]">[view on recurly]</a>
</td>
</tr>
</table>