From e0debd761bc24cd42f4140d2b6cc32a4271fe975 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 23 Jan 2015 02:35:46 -0500 Subject: getting webhooks in order.. didnt need as many as i thought --- server/lib/webhook/webhook.js | 118 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 server/lib/webhook/webhook.js (limited to 'server/lib/webhook/webhook.js') diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js new file mode 100644 index 0000000..067af30 --- /dev/null +++ b/server/lib/webhook/webhook.js @@ -0,0 +1,118 @@ +// // where should this live? +// app.get('/subscribe/webhook', views.subscription.webhook); + +/* +app.use(express.basicAuth(function(user, pass, callback) { + var result = (user === 'testUser' && pass === 'testPass'); + callback(null, result); +})); +*/ + + +/* jshint node: true */ + +var User = require('../schemas/User'), + Subscription = require('../schemas/Subscription'), + config = require('../../../config'), + middleware = require('../middleware'), + util = require('../util'), + _ = require('lodash'), + moment = require('moment'), + xml2js = require('xml2js'), + Recurly = require('node-recurly'), + recurly = new Recurly(require('./config')); + +var parser = new xml2js.Parser(); + +var subscription = module.exports = { + plan_level: { + free: 0, + basic: 1, + pro: 2, + }, + + callbacks: { + // accounts + new_account_notification: function(data){ + // fires on successful signup + // use the account_code to find the user, + }, + canceled_account_notification: function(data){ + }, + billing_info_updated_notification: function(data){ + }, + reactivated_account_notification: function(data){ + }, + + // invoices + new_invoice_notification: function(data){ + }, + closed_invoice_notification: function(data){ + }, + past_due_invoice_notification: function(data){ + }, + + // subscriptions + new_subscription_notification: function(data){ + var account = data.account + User.findOne({ _id: account.account_code }, function(err, user){ + if (err) return; + var subscription = data.subscription + + var plan = subscription.plan.plan_code.split("_") + var plan_type = plan[0] + var plan_period = plan[1] + + user.plan_type = plan_type + user.plan_level = subscription.plan_level[plan_type] + + // subscription.uuid + // subscription.subscription_add_ons[] + // subscription.subscription_add_ons[0].quantity + user.save(function(){ + }) + + }) + }, + updated_subscription_notification: function(data){ + }, + canceled_subscription_notification: function(data){ + }, + expired_subscription_notification: function(data){ + }, + renewed_subscription_notification: function(data){ + }, + + // payments + successful_payment_notification: function(data){ + var account = data.account + User.findOne({ _id: account.account_code }, function(err, user){ + if (err) return; + user.last_charged = new Date(data.transaction.date) + user.save(function(){ + }) + }) + }, + failed_payment_notification: function(data){ + }, + successful_refund_notification: function(data){ + }, + void_payment_notification: function(data){ + }, + + }, + + // need a route for the webhook, + // then calls to get appropriate info from the recurly api + webhook: function(req, res){ + res.status(200).end() + parser.parseString(data, function (err, result) { + console.log(inspect(result, { colors: true, depth: Infinity })); + for (var i in data) { + if (subscription.callbacks[i]) { + subscription.callbacks[i](data[i]) + } + } + }); + }, +} -- cgit v1.2.3-70-g09d2 From 74fb7a313b4d9ad3517e97133febff9cada96fe0 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 23 Jan 2015 14:46:44 -0500 Subject: handle subscribe and payment events with webhook --- server/lib/schemas/Subscription.js | 17 +++--- server/lib/schemas/User.js | 1 - server/lib/webhook/index.js | 4 +- server/lib/webhook/webhook.js | 108 ++++++++++++++++++++++--------------- 4 files changed, 76 insertions(+), 54 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/server/lib/schemas/Subscription.js b/server/lib/schemas/Subscription.js index 7f2579b..99e4ebf 100644 --- a/server/lib/schemas/Subscription.js +++ b/server/lib/schemas/Subscription.js @@ -8,14 +8,15 @@ var mongoose = require('mongoose'), var SubscriptionSchema = new mongoose.Schema({ user_id: { type: mongoose.Schema.ObjectId, index: true }, - - monthly_total: { type: Number }, - yearly_total: { type: Number }, - - plans: [{ - tier: { type: String }, - monthly: { type: Boolean }, - projects: [{ type: mongoose.Schema.ObjectId }], + + plan_level: { type: Number, default: 0 }, + plan_type: { type: String, default: "free" }, + last_charged: { type: Date, default: null }, + + subscription_uuid: { type: String }, + subscription_add_ons: [{ + name: { type: String }, + quantity: { type: Number }, }], created_at: { type: Date, default: Date.now }, diff --git a/server/lib/schemas/User.js b/server/lib/schemas/User.js index 77de2d4..06ad33d 100644 --- a/server/lib/schemas/User.js +++ b/server/lib/schemas/User.js @@ -58,7 +58,6 @@ var UserSchema = new mongoose.Schema({ plan_level: { type: Number, default: 0 }, plan_type: { type: String, default: "free" }, last_charged: { type: Date, default: null }, - subscription_id: { type: mongoose.Schema.ObjectId }, location: { type: String, default: "" }, photo: { type: String, default: "" }, diff --git a/server/lib/webhook/index.js b/server/lib/webhook/index.js index 7dd68e6..11419c2 100644 --- a/server/lib/webhook/index.js +++ b/server/lib/webhook/index.js @@ -13,6 +13,8 @@ var http = require('http'), path = require('path'), mongoose = require('mongoose'); +var webhook = require('./webhook'); + var app = express() var server var DATABASE_URI = process.env.MONGOLAB_URI || ('mongodb://' + config.databaseHost + '/vvalls') @@ -36,7 +38,7 @@ site.ready = function(){ }); app.get('/', function(req,res){ res.send('HI THERE') }) - app.get('/subscribe/webhook', require('./webhook').webhook); + webhook.route(app) } site.init() diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 067af30..2e5e627 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -24,7 +24,7 @@ var User = require('../schemas/User'), var parser = new xml2js.Parser(); -var subscription = module.exports = { +var subscribe = module.exports = { plan_level: { free: 0, basic: 1, @@ -32,87 +32,107 @@ var subscription = module.exports = { }, callbacks: { +/* // accounts - new_account_notification: function(data){ + new_account_notification: function(data, user){ // fires on successful signup - // use the account_code to find the user, }, - canceled_account_notification: function(data){ + canceled_account_notification: function(data, user){ }, - billing_info_updated_notification: function(data){ + billing_info_updated_notification: function(data, user){ }, - reactivated_account_notification: function(data){ + reactivated_account_notification: function(data, user){ }, // invoices - new_invoice_notification: function(data){ + new_invoice_notification: function(data, user){ }, - closed_invoice_notification: function(data){ + closed_invoice_notification: function(data, user){ }, - past_due_invoice_notification: function(data){ + past_due_invoice_notification: function(data, user){ }, - +*/ + // subscriptions - new_subscription_notification: function(data){ + new_subscription_notification: function(data, user){ var account = data.account - User.findOne({ _id: account.account_code }, function(err, user){ - if (err) return; - var subscription = data.subscription - - var plan = subscription.plan.plan_code.split("_") + Subscription.findOne({ "uuid": data.subscription.uuid }, function(err, subscription){ + if (err || subscription) return; + + var plan = data.subscription.plan.plan_code.split("-") var plan_type = plan[0] var plan_period = plan[1] user.plan_type = plan_type - user.plan_level = subscription.plan_level[plan_type] - - // subscription.uuid - // subscription.subscription_add_ons[] - // subscription.subscription_add_ons[0].quantity - user.save(function(){ - }) + user.plan_period = plan_period + user.plan_level = subscribe.plan_level[plan_type] + var subscriber = new Subscription () + subscriber.user_id = user._id + subscriber.uuid = data.subscription.uuid + subscriber.add_ons = subscription.add_ons.map(function(add_on){ + return { + name: add_on.plan, + quantity: add_on.quantity, + } + }) + subscriber.save(function(err, data){ + if (err) return; + user.save(function(err){ + // saved! + }) + }) }) }, - updated_subscription_notification: function(data){ + +/* + updated_subscription_notification: function(data, user){ }, - canceled_subscription_notification: function(data){ + canceled_subscription_notification: function(data, user){ }, - expired_subscription_notification: function(data){ + expired_subscription_notification: function(data, user){ }, - renewed_subscription_notification: function(data){ + renewed_subscription_notification: function(data, user){ }, - +*/ // payments - successful_payment_notification: function(data){ + successful_payment_notification: function(data, user){ var account = data.account - User.findOne({ _id: account.account_code }, function(err, user){ - if (err) return; - user.last_charged = new Date(data.transaction.date) - user.save(function(){ - }) - }) + user.last_charged = new Date(data.transaction.date) + user.save(function(){ + }) }, - failed_payment_notification: function(data){ +/* + failed_payment_notification: function(data, user){ }, - successful_refund_notification: function(data){ + successful_refund_notification: function(data, user){ }, - void_payment_notification: function(data){ + void_payment_notification: function(data, user){ }, - +*/ }, - // need a route for the webhook, + execute: function(action, data){ + User.findOne({ _id: data.account.account_code }, function(err, user){ + if (err) { return } + subscribe.callbacks[action](data, user) + }) + }, + // then calls to get appropriate info from the recurly api - webhook: function(req, res){ + handle: function(req, res){ res.status(200).end() parser.parseString(data, function (err, result) { console.log(inspect(result, { colors: true, depth: Infinity })); - for (var i in data) { - if (subscription.callbacks[i]) { - subscription.callbacks[i](data[i]) + for (var action in result) { + if (subscribe.callbacks[action]) { + subscribe.execute(action, result[action]); } } }); }, + + route: function(app){ + app.post('/subscribe/webhook', subscribe.handle); + }, } -- cgit v1.2.3-70-g09d2 From 5efb0ed941ed80136e63014c4f615574b2b613d7 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 23 Jan 2015 17:58:41 -0500 Subject: edit subscription partial stub --- server/lib/api/index.js | 1 + server/lib/api/subscription.js | 22 ++++++++++ server/lib/schemas/Subscription.js | 3 +- server/lib/webhook/config.js | 6 --- server/lib/webhook/recurly-config.js | 6 +++ server/lib/webhook/webhook.js | 8 ++-- views/about/brochure.ejs | 12 +++--- views/partials/edit-subscription.ejs | 33 +++++++++++++++ views/profile.ejs | 82 ++++++++++++++++++++---------------- 9 files changed, 119 insertions(+), 54 deletions(-) create mode 100644 server/lib/api/subscription.js delete mode 100644 server/lib/webhook/config.js create mode 100644 server/lib/webhook/recurly-config.js create mode 100644 views/partials/edit-subscription.ejs (limited to 'server/lib/webhook/webhook.js') diff --git a/server/lib/api/index.js b/server/lib/api/index.js index 11e13fc..9478d9b 100644 --- a/server/lib/api/index.js +++ b/server/lib/api/index.js @@ -8,6 +8,7 @@ var api = { projects: require('./projects'), rooms: require('./rooms'), collaborator: require('./collaborator'), + subscription: require('./subscription'), } module.exports = api diff --git a/server/lib/api/subscription.js b/server/lib/api/subscription.js new file mode 100644 index 0000000..6fe8c61 --- /dev/null +++ b/server/lib/api/subscription.js @@ -0,0 +1,22 @@ +/* jshint node: true */ + +var _ = require('lodash'), + util = require('../util'), + upload = require('../upload'), + config = require('../../../config.json'), + User = require('../schemas/User'), + Project = require('../schemas/Project'), + Layout = require('../schemas/Layout'), + Subscription = require('../schemas/Subscription'); + +var subscription = module.exports = { + +/* + index: function(req, res){ + Project.find({ user_id: req.user._id }, function(err, docs){ + res.json(docs) + }) + }, +*/ + +}; \ No newline at end of file diff --git a/server/lib/schemas/Subscription.js b/server/lib/schemas/Subscription.js index 99e4ebf..2f49ea1 100644 --- a/server/lib/schemas/Subscription.js +++ b/server/lib/schemas/Subscription.js @@ -9,9 +9,8 @@ var mongoose = require('mongoose'), var SubscriptionSchema = new mongoose.Schema({ user_id: { type: mongoose.Schema.ObjectId, index: true }, - plan_level: { type: Number, default: 0 }, plan_type: { type: String, default: "free" }, - last_charged: { type: Date, default: null }, + plan_period: { type: String, default: "monthly" }, subscription_uuid: { type: String }, subscription_add_ons: [{ diff --git a/server/lib/webhook/config.js b/server/lib/webhook/config.js deleted file mode 100644 index 3d7e1c5..0000000 --- a/server/lib/webhook/config.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - API_KEY: process.env['VVALLS_RECURLY_SECRET'], - SUBDOMAIN: 'vvalls', - ENVIRONMENT: 'sandbox', - DEBUG: true, -}; diff --git a/server/lib/webhook/recurly-config.js b/server/lib/webhook/recurly-config.js new file mode 100644 index 0000000..3d7e1c5 --- /dev/null +++ b/server/lib/webhook/recurly-config.js @@ -0,0 +1,6 @@ +module.exports = { + API_KEY: process.env['VVALLS_RECURLY_SECRET'], + SUBDOMAIN: 'vvalls', + ENVIRONMENT: 'sandbox', + DEBUG: true, +}; diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 2e5e627..e9a7925 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -20,7 +20,7 @@ var User = require('../schemas/User'), moment = require('moment'), xml2js = require('xml2js'), Recurly = require('node-recurly'), - recurly = new Recurly(require('./config')); + recurly = new Recurly(require('./recurly-config')); var parser = new xml2js.Parser(); @@ -64,12 +64,14 @@ var subscribe = module.exports = { var plan_period = plan[1] user.plan_type = plan_type - user.plan_period = plan_period user.plan_level = subscribe.plan_level[plan_type] var subscriber = new Subscription () - subscriber.user_id = user._id subscriber.uuid = data.subscription.uuid + subscriber.user_id = user._id + subscriber.plan_type = plan_type + subscriber.plan_period = plan_period + subscriber.plan_level = subscribe.plan_level[plan_type] subscriber.add_ons = subscription.add_ons.map(function(add_on){ return { name: add_on.plan, diff --git a/views/about/brochure.ejs b/views/about/brochure.ejs index cffa51f..1c808f8 100644 --- a/views/about/brochure.ejs +++ b/views/about/brochure.ejs @@ -35,9 +35,9 @@
  • [[ if (! logged_in) { ]] - [[ } else if (! profile.plan_level || profile.plan_level < plan.level) { ]] - - [[ } else if (profile.plan_level == plan.level) { ]] + [[ } else if (! user.plan_level || user.plan_level < plan.level) { ]] + + [[ } else if (user.plan_level == plan.level) { ]] Current Level [[ } else { ]] [[ } ]] @@ -57,9 +57,9 @@
  • [[ if (! logged_in) { ]] - [[ } else if (! profile.plan_level || profile.plan_level < plan.level) { ]] - - [[ } else if (profile.plan_level == plan.level) { ]] + [[ } else if (! user.plan_level || user.plan_level < plan.level) { ]] + + [[ } else if (user.plan_level == plan.level) { ]] Current Level [[ } ]] diff --git a/views/partials/edit-subscription.ejs b/views/partials/edit-subscription.ejs new file mode 100644 index 0000000..0aa5281 --- /dev/null +++ b/views/partials/edit-subscription.ejs @@ -0,0 +1,33 @@ +
    + X +
    +
    + +
      +
    • +

      Edit Subscription

      +
    • +
    • + [[ if (! user.plan_level) { ]] + You are currently using the free plan. For access to all of Vvalls features, + consider upgrading to a paid plan. +

      + View the Plans + [[ } else { ]] + Your current plan level is XXX + You have been a member since XXX + $cost/month OR $cost/year + + You are using N basic layouts + Buy more + + You are using N pro layouts + Buy more / Upgrade your account + + Cancel your subscription + [[ } ]] +

    • +
    +
    +
    +
    diff --git a/views/profile.ejs b/views/profile.ejs index 88af6b0..e149847 100644 --- a/views/profile.ejs +++ b/views/profile.ejs @@ -9,48 +9,53 @@ [[- include partials/header ]]
    - [[ if (profile.photo && profile.photo.length) { ]] -
    -
    - [[ } else { ]] -
    - - - [[ if (isOwnProfile) { ]] -
    click to add profile pic
    - - [[ } ]] -
    -
    + [[ if (profile.photo && profile.photo.length) { ]] +
    +
    + [[ } else { ]] +
    + + + [[ if (isOwnProfile) { ]] +
    click to add profile pic
    + [[ } ]] -
    -
    -

    [[- profile.displayName ]]

    - [[ if (profile.location) { ]] - - [[- profile.location ]] - - [[ } ]] - [[ if (profile.website && profile.website.length) { ]] - - [[- profile.website ]] - - [[ } ]] - [[ if (profile.twitterName && profile.twitterName.length) { ]] - - @[[- profile.twitterName ]] - - [[ } ]] -
    -
    - +
    +
    + [[ } ]] +
    +
    +

    [[- profile.displayName ]]

    + [[ if (profile.location) { ]] + + [[- profile.location ]] + + [[ } ]] + [[ if (profile.website && profile.website.length) { ]] + + [[- profile.website ]] + + [[ } ]] + [[ if (profile.twitterName && profile.twitterName.length) { ]] + + @[[- profile.twitterName ]] + + [[ } ]] + [[ if (profile.plan_level == 1) { ]] + PREMIUM + [[ } else if (profile.plan_level == 2) { ]] + PRO + [[ } ]] +
    +
    [[ if (projects.length) { ]] +

    [[- profile.username ]] has [[- projectCount ]] project[[- projectCount != 1 ? "s" : "" ]]

    - [[ include projects/list-projects ]] + [[ } else { ]] - +

    Welcome to VVALLS

    @@ -69,8 +74,11 @@

    This person has no projects.

    [[ } ]]
    + [[ } ]] -
    + + + [[ include partials/edit-subscription ]] [[ include partials/edit-profile ]] [[ include projects/layouts-modal ]] [[ include projects/edit-project ]] -- cgit v1.2.3-70-g09d2 From 3059c3203d2cec4e2e745be8c21c6d3fbddb0c14 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 28 Jan 2015 01:39:28 -0500 Subject: rigging EditSubscriptionModal --- Gruntfile.js | 1 + config.json.example | 1 - public/assets/javascripts/ui/_router.js | 10 ++++++++++ server/index.js | 1 + server/lib/api/subscription.js | 19 ++++++++++++++++--- server/lib/webhook/webhook.js | 14 ++++++++++++++ views/about/brochure.ejs | 3 +++ views/partials/edit-subscription.ejs | 1 - views/partials/scripts.ejs | 1 + 9 files changed, 46 insertions(+), 5 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/Gruntfile.js b/Gruntfile.js index f62cc82..ed236c5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -99,6 +99,7 @@ module.exports = function(grunt) { "public/assets/javascripts/ui/site/LayoutsModal.js", "public/assets/javascripts/ui/site/EditProjectModal.js", "public/assets/javascripts/ui/site/EditProfileModal.js", + "public/assets/javascripts/ui/site/EditSubscriptionModal.js", "public/assets/javascripts/ui/site/DocumentModal.js", "public/assets/javascripts/ui/site/HomeView.js", diff --git a/config.json.example b/config.json.example index b8c310c..dc79edd 100644 --- a/config.json.example +++ b/config.json.example @@ -5,6 +5,5 @@ "socketPort": 1337, "webhookPort": 5000, "databaseHost": "lvh.me", - "pageSize": 10, "env": { "development": 1 } } diff --git a/public/assets/javascripts/ui/_router.js b/public/assets/javascripts/ui/_router.js index 3532428..9e7ce75 100644 --- a/public/assets/javascripts/ui/_router.js +++ b/public/assets/javascripts/ui/_router.js @@ -9,6 +9,7 @@ var SiteRouter = Router.extend({ "click [data-role='new-project-modal']": 'newProject', "click [data-role='edit-project-modal']": 'editProject', "click [data-role='edit-profile-modal']": 'editProfile', + "click [data-role='edit-subscription-modal']": 'editSubscription', "click [data-role='new-document-modal']": 'newDocument', "click [data-role='edit-document-modal']": 'editDocument', "click [data-role='destroy-document-modal']": 'destroyDocument', @@ -29,6 +30,7 @@ var SiteRouter = Router.extend({ "/profile": 'profile', "/profile/edit": 'editProfile', + "/profile/billing": 'editSubscription', "/profile/:name": 'profile', "/about/:name/edit": 'editDocument', "/about/new": 'newDocument', @@ -56,6 +58,7 @@ var SiteRouter = Router.extend({ "/profile": 'profile', "/profile/edit": 'editProfile', + "/profile/billing": 'editSubscription', "/profile/:name": 'profile', "/project/:name": 'projectViewer', @@ -69,6 +72,7 @@ var SiteRouter = Router.extend({ this.newProjectModal = new NewProjectModal() this.editProjectModal = new EditProjectModal() this.editProfileModal = new EditProfileModal() + this.editSubscriptionModal = new EditSubscriptionModal() this.passwordForgotModal = new PasswordForgot() this.documentModal = new DocumentModal() this.profileView = new ProfileView() @@ -195,6 +199,12 @@ var SiteRouter = Router.extend({ this.editProfileModal.load() }, + editSubscription: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/profile/billing") + + this.editSubscriptionModal.load() + }, newDocument: function(e){ diff --git a/server/index.js b/server/index.js index 02fea3c..475054d 100644 --- a/server/index.js +++ b/server/index.js @@ -102,6 +102,7 @@ site.route = function () { app.get('/profile', views.profile) app.get('/profile/edit', views.profile) + app.get('/profile/billing', views.profile) app.get('/profile/:username', views.profile) app.get('/about', views.docs); diff --git a/server/lib/api/subscription.js b/server/lib/api/subscription.js index bd19127..83644cf 100644 --- a/server/lib/api/subscription.js +++ b/server/lib/api/subscription.js @@ -18,16 +18,29 @@ var subscription = module.exports = { }) }, */ - show: function(req,res){ + 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) + sync: function(req, res){ + // fetch req.user._id + }, + + show: function(req, res){ // fetch from recurly }, - update: function(req,res){ + update: function(req, res){ // update plan_type on recurly // update add_ons on recurly }, - destroy: function(req,res){ + destroy: function(req, res){ // destroy on recurly }, diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index e9a7925..4f23d0b 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -134,7 +134,21 @@ var subscribe = module.exports = { }); }, + list: function(req, res){ + recurly.subscriptions.listByAccount(req.params.id, function(data){ + if (data.data != 404) { + res.json(data) + return + } + else { + res.json(data) + return + } + }) + }, + route: function(app){ app.post('/subscribe/webhook', subscribe.handle); + app.get('/subscribe/list/:id', subscribe.list); }, } diff --git a/views/about/brochure.ejs b/views/about/brochure.ejs index 1c808f8..49b03db 100644 --- a/views/about/brochure.ejs +++ b/views/about/brochure.ejs @@ -123,6 +123,9 @@ text-align: center; margin-bottom: 10px; } +.about_plan ul { + margin-bottom: 60px; +} .planbox li { list-style-type: none; margin-bottom: 5px; diff --git a/views/partials/edit-subscription.ejs b/views/partials/edit-subscription.ejs index 1f8db62..adc3f71 100644 --- a/views/partials/edit-subscription.ejs +++ b/views/partials/edit-subscription.ejs @@ -44,7 +44,6 @@ - [[ } ]]
  • diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index fc94992..04bd945 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -95,6 +95,7 @@ + -- cgit v1.2.3-70-g09d2 From 5adac681bdb43b8b709795fa501689fb9ae8a4e1 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 28 Jan 2015 19:18:27 -0500 Subject: many methods --- package.json | 1 + server/lib/api/subscription.js | 124 ++++++++++++++++++++++++++++++++---- server/lib/middleware.js | 2 +- server/lib/schemas/Subscription.js | 6 +- server/lib/views/staff.js | 2 +- server/lib/webhook/webhook.js | 2 +- views/about/brochure.ejs | 15 +++-- views/partials/header.ejs | 1 + views/staff/_users.ejs | 1 + views/staff/plans/_form.ejs | 5 ++ views/staff/subscriptions/index.ejs | 2 +- views/staff/users/show.ejs | 1 + 12 files changed, 135 insertions(+), 27 deletions(-) (limited to 'server/lib/webhook/webhook.js') 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 @@
  • VValls logo appears when embedding an exhibition on a web page
  • [[ if (! logged_in) { ]] - - [[ } else if (! user.plan_level || user.plan_level < plan.level) { ]] - + + [[ } else if (! user.plan_level) { ]] + [[ } else if (user.plan_level == plan.level) { ]] Current Level - [[ } else { ]] [[ } ]] @@ -56,11 +55,13 @@
  • No VValls logo on embed
  • [[ if (! logged_in) { ]] - - [[ } else if (! user.plan_level || user.plan_level < plan.level) { ]] - + + [[ } else if (! user.plan_level) { ]] + [[ } else if (user.plan_level == plan.level) { ]] Current Level + [[ } else if (user.plan_level < plan.level) { ]] + [[ } ]] 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)) { ]] Settings + Subscription [[ } else if (! profile) { ]] Profile [[ } ]] 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 @@ [view profile] + [recurly] [[- 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 @@
  • +
  • + +
    +
  • +
  • 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 ]] -

    Users

    +

    Subscriptions

    [[ 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 @@ [view profile] [view media] + [view on recurly] -- cgit v1.2.3-70-g09d2 From fdcc335d7e95139b71083928602713b82f29aba1 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 28 Jan 2015 20:22:53 -0500 Subject: lol --- server/lib/webhook/webhook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index a871a3a..4f23d0b 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.middleware.ensureSubscription, subscribe.list); + app.get('/subscribe/list/:id', subscribe.list); }, } -- cgit v1.2.3-70-g09d2 From 466e56bbdcf1b713a91e77081237b3adf0b86ba0 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 2 Feb 2015 13:24:03 -0500 Subject: using xml middleware parser for webhook --- package.json | 1 + server/lib/webhook/index.js | 2 +- server/lib/webhook/webhook.js | 191 +++++++++++++++++++++--------------------- 3 files changed, 99 insertions(+), 95 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/package.json b/package.json index 56ffcdf..7a504af 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "express": "~3.4.8", "express-subdomain-handler": "~0.1.0", "express-subdomains": "0.0.5", + "express-xml-bodyparser": "0.0.6", "html-entities": "~1.0.10", "intro.js": "^0.9.0", "js2xml": "^1.0.0", diff --git a/server/lib/webhook/index.js b/server/lib/webhook/index.js index 11419c2..eb6a6b3 100644 --- a/server/lib/webhook/index.js +++ b/server/lib/webhook/index.js @@ -27,7 +27,7 @@ site.init = function(){ site.ready = function(){ app.set('port', config.webhookPort); - app.use(bodyParser()); + // app.use(bodyParser()); app.use(express.query()); app.set('env', config.env.production ? "production" : "development") app.get('env') === 'development' && app.use(express.errorHandler()); diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 4f23d0b..21ff23d 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -12,15 +12,17 @@ app.use(express.basicAuth(function(user, pass, callback) { /* jshint node: true */ var User = require('../schemas/User'), - Subscription = require('../schemas/Subscription'), - config = require('../../../config'), - middleware = require('../middleware'), - util = require('../util'), - _ = require('lodash'), - moment = require('moment'), - xml2js = require('xml2js'), - Recurly = require('node-recurly'), - recurly = new Recurly(require('./recurly-config')); + Subscription = require('../schemas/Subscription'), + config = require('../../../config'), + middleware = require('../middleware'), + util = require('../util'), + _ = require('lodash'), + moment = require('moment'), + xml2js = require('xml2js'), + Recurly = require('node-recurly'), + recurly = new Recurly(require('./recurly-config')); + +var xml_bodyparser = require('express-xml-bodyparser'); var parser = new xml2js.Parser(); @@ -30,34 +32,34 @@ var subscribe = module.exports = { basic: 1, pro: 2, }, - + callbacks: { /* - // accounts - new_account_notification: function(data, user){ - // fires on successful signup - }, - canceled_account_notification: function(data, user){ - }, - billing_info_updated_notification: function(data, user){ - }, - reactivated_account_notification: function(data, user){ - }, - - // invoices - new_invoice_notification: function(data, user){ - }, - closed_invoice_notification: function(data, user){ - }, - past_due_invoice_notification: function(data, user){ - }, + // accounts + new_account_notification: function(data, user){ + // fires on successful signup + }, + canceled_account_notification: function(data, user){ + }, + billing_info_updated_notification: function(data, user){ + }, + reactivated_account_notification: function(data, user){ + }, + + // invoices + new_invoice_notification: function(data, user){ + }, + closed_invoice_notification: function(data, user){ + }, + past_due_invoice_notification: function(data, user){ + }, */ - // subscriptions - new_subscription_notification: function(data, user){ - var account = data.account - Subscription.findOne({ "uuid": data.subscription.uuid }, function(err, subscription){ - if (err || subscription) return; + // subscriptions + new_subscription_notification: function(data, user){ + var account = data.account + Subscription.findOne({ "uuid": data.subscription.uuid }, function(err, subscription){ + if (err || subscription) return; var plan = data.subscription.plan.plan_code.split("-") var plan_type = plan[0] @@ -66,75 +68,76 @@ var subscribe = module.exports = { user.plan_type = plan_type user.plan_level = subscribe.plan_level[plan_type] - var subscriber = new Subscription () + var subscriber = new Subscription () subscriber.uuid = data.subscription.uuid - subscriber.user_id = user._id + subscriber.user_id = user._id subscriber.plan_type = plan_type subscriber.plan_period = plan_period subscriber.plan_level = subscribe.plan_level[plan_type] subscriber.add_ons = subscription.add_ons.map(function(add_on){ - return { - name: add_on.plan, - quantity: add_on.quantity, - } + return { + name: add_on.plan, + quantity: add_on.quantity, + } }) subscriber.save(function(err, data){ - if (err) return; - user.save(function(err){ - // saved! - }) + if (err) return; + user.save(function(err){ + // saved! + }) }) - }) - }, + }) + }, /* - updated_subscription_notification: function(data, user){ - }, - canceled_subscription_notification: function(data, user){ - }, - expired_subscription_notification: function(data, user){ - }, - renewed_subscription_notification: function(data, user){ - }, -*/ - // payments - successful_payment_notification: function(data, user){ - var account = data.account - user.last_charged = new Date(data.transaction.date) - user.save(function(){ - }) - }, + updated_subscription_notification: function(data, user){ + }, + canceled_subscription_notification: function(data, user){ + }, + expired_subscription_notification: function(data, user){ + }, + renewed_subscription_notification: function(data, user){ + }, +*/ + // payments + successful_payment_notification: function(data, user){ + var account = data.account + user.last_charged = new Date(data.transaction.date) + user.save(function(){ + }) + }, /* - failed_payment_notification: function(data, user){ - }, - successful_refund_notification: function(data, user){ - }, - void_payment_notification: function(data, user){ - }, + failed_payment_notification: function(data, user){ + }, + successful_refund_notification: function(data, user){ + }, + void_payment_notification: function(data, user){ + }, */ }, - - execute: function(action, data){ - User.findOne({ _id: data.account.account_code }, function(err, user){ - if (err) { return } - subscribe.callbacks[action](data, user) - }) - }, - + + execute: function(action, data){ + User.findOne({ _id: data.account.account_code }, function(err, user){ + if (err) { return } + subscribe.callbacks[action](data, user) + }) + }, + // then calls to get appropriate info from the recurly api - handle: function(req, res){ - res.status(200).end() - parser.parseString(data, function (err, result) { - console.log(inspect(result, { colors: true, depth: Infinity })); - for (var action in result) { - if (subscribe.callbacks[action]) { - subscribe.execute(action, result[action]); - } - } - }); - }, - - list: function(req, res){ + handle: function(req, res){ + console.log(req.body) + // parser.parseString(req.body, function (err, result) { + var result = req.body + console.log(inspect(result, { colors: true, depth: Infinity })); + for (var action in result) { + if (subscribe.callbacks[action]) { + subscribe.execute(action, result[action]); + return res.status(200).end() + } + } + }, + + list: function(req, res){ recurly.subscriptions.listByAccount(req.params.id, function(data){ if (data.data != 404) { res.json(data) @@ -145,10 +148,10 @@ var subscribe = module.exports = { return } }) - }, - - route: function(app){ - app.post('/subscribe/webhook', subscribe.handle); - app.get('/subscribe/list/:id', subscribe.list); - }, + }, + + route: function(app){ + app.post('/subscribe/webhook', xml_bodyparser(), subscribe.handle); + app.get('/subscribe/list/:id', subscribe.list); + }, } -- cgit v1.2.3-70-g09d2 From 19b74bf938aef3b0c3a35c185d9d1563e773a972 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 2 Feb 2015 13:50:09 -0500 Subject: webhook firing --- package.json | 1 + server/lib/webhook/webhook.js | 35 ++++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/package.json b/package.json index 7a504af..69766ce 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "express-subdomains": "0.0.5", "express-xml-bodyparser": "0.0.6", "html-entities": "~1.0.10", + "inspect": "0.0.2", "intro.js": "^0.9.0", "js2xml": "^1.0.0", "knox": "~0.8.10", diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 21ff23d..25e3fb8 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -57,11 +57,13 @@ var subscribe = module.exports = { // subscriptions new_subscription_notification: function(data, user){ - var account = data.account - Subscription.findOne({ "uuid": data.subscription.uuid }, function(err, subscription){ + var account = data.account[0].account_code[0] + var subscrip = data.subscription[0] + var uuid = subscrip.uuid[0] + Subscription.findOne({ "uuid": uuid }, function(err, subscription){ if (err || subscription) return; - var plan = data.subscription.plan.plan_code.split("-") + var plan = subscrip.plan[0].plan_code[0].split("-") var plan_type = plan[0] var plan_period = plan[1] @@ -69,17 +71,25 @@ var subscribe = module.exports = { user.plan_level = subscribe.plan_level[plan_type] var subscriber = new Subscription () - subscriber.uuid = data.subscription.uuid + subscriber.uuid = uuid subscriber.user_id = user._id subscriber.plan_type = plan_type subscriber.plan_period = plan_period subscriber.plan_level = subscribe.plan_level[plan_type] - subscriber.add_ons = subscription.add_ons.map(function(add_on){ - return { - name: add_on.plan, - quantity: add_on.quantity, - } - }) + subscriber.add_ons = [] + var add_ons = subscrip.subscription_add_ons[0].subscription_add_on + if (add_ons) { + add_ons.forEach(function(add_on){ + switch (add_on.add_on_code[0]) { + case 'extra-basic-layout': + subscriber.basic_layouts = parseInt(add_on.quantity[0]._, 10) || 0 + break + case 'extra-pro-layout': + subscriber.pro_layouts = parseInt(add_on.quantity[0]._, 10) || 0 + break + } + }) + } subscriber.save(function(err, data){ if (err) return; user.save(function(err){ @@ -117,7 +127,7 @@ var subscribe = module.exports = { }, execute: function(action, data){ - User.findOne({ _id: data.account.account_code }, function(err, user){ + User.findOne({ _id: data.account[0].account_code[0] }, function(err, user){ if (err) { return } subscribe.callbacks[action](data, user) }) @@ -128,13 +138,12 @@ var subscribe = module.exports = { console.log(req.body) // parser.parseString(req.body, function (err, result) { var result = req.body - console.log(inspect(result, { colors: true, depth: Infinity })); for (var action in result) { if (subscribe.callbacks[action]) { subscribe.execute(action, result[action]); - return res.status(200).end() } } + return res.status(200).end() }, list: function(req, res){ -- cgit v1.2.3-70-g09d2 From 07f648798676d565914dd8e7b1cd78ebfc3b7309 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 2 Feb 2015 13:57:49 -0500 Subject: dont make a million subscriber --- server/lib/webhook/webhook.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 25e3fb8..9e3a4d3 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -60,8 +60,8 @@ var subscribe = module.exports = { var account = data.account[0].account_code[0] var subscrip = data.subscription[0] var uuid = subscrip.uuid[0] - Subscription.findOne({ "uuid": uuid }, function(err, subscription){ - if (err || subscription) return; + Subscription.findOne({ "uuid": uuid }, function(err, old_subscriber){ + // if (err) return; var plan = subscrip.plan[0].plan_code[0].split("-") var plan_type = plan[0] @@ -70,7 +70,7 @@ var subscribe = module.exports = { user.plan_type = plan_type user.plan_level = subscribe.plan_level[plan_type] - var subscriber = new Subscription () + var subscriber = old_subscriber || new Subscription () subscriber.uuid = uuid subscriber.user_id = user._id subscriber.plan_type = plan_type -- cgit v1.2.3-70-g09d2 From 72754ed88f7dfed9b80f5a46cd11550aac11cf1e Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 2 Feb 2015 15:28:48 -0500 Subject: fix successful_payment_notification --- server/lib/webhook/webhook.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 9e3a4d3..58a13ca 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -1,5 +1,4 @@ // // where should this live? -// app.get('/subscribe/webhook', views.subscription.webhook); /* app.use(express.basicAuth(function(user, pass, callback) { @@ -111,8 +110,8 @@ var subscribe = module.exports = { */ // payments successful_payment_notification: function(data, user){ - var account = data.account - user.last_charged = new Date(data.transaction.date) + var account = data.account[0] + user.last_charged = new Date(data.transaction[0].date[0]._) user.save(function(){ }) }, -- cgit v1.2.3-70-g09d2 From dd4f0178c7bcb5d14e1308e3877c5ab02eddf000 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 12 Aug 2015 12:37:49 -0400 Subject: modify webhook to support custom plans --- .../javascripts/ui/site/EditSubscriptionModal.js | 1 + server/lib/api/subscription.js | 1 + server/lib/schemas/Subscription.js | 1 + server/lib/schemas/User.js | 1 + server/lib/views/staff/middleware.js | 2 +- server/lib/webhook/webhook.js | 24 ++++++++++++++++------ views/staff/subscriptions/index.ejs | 3 +++ 7 files changed, 26 insertions(+), 7 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/public/assets/javascripts/ui/site/EditSubscriptionModal.js b/public/assets/javascripts/ui/site/EditSubscriptionModal.js index 3a20234..1033114 100644 --- a/public/assets/javascripts/ui/site/EditSubscriptionModal.js +++ b/public/assets/javascripts/ui/site/EditSubscriptionModal.js @@ -96,6 +96,7 @@ var EditSubscriptionModal = ModalView.extend({ free: 0, basic: 1, pro: 2, + custom: 3, }, loaded: false, diff --git a/server/lib/api/subscription.js b/server/lib/api/subscription.js index 9c2d6ef..c2fff4c 100644 --- a/server/lib/api/subscription.js +++ b/server/lib/api/subscription.js @@ -16,6 +16,7 @@ var plan_levels = { free: 0, basic: 1, pro: 2, + custom: 3, } var subscription = module.exports = { diff --git a/server/lib/schemas/Subscription.js b/server/lib/schemas/Subscription.js index 355bbe2..bf43e8b 100644 --- a/server/lib/schemas/Subscription.js +++ b/server/lib/schemas/Subscription.js @@ -9,6 +9,7 @@ var mongoose = require('mongoose'), var SubscriptionSchema = new mongoose.Schema({ user_id: { type: mongoose.Schema.ObjectId, index: true }, + plan_code: { type: String, default: "" }, plan_type: { type: String, default: "free" }, plan_period: { type: String, default: "monthly" }, diff --git a/server/lib/schemas/User.js b/server/lib/schemas/User.js index 4b6ff39..e6e7f03 100644 --- a/server/lib/schemas/User.js +++ b/server/lib/schemas/User.js @@ -55,6 +55,7 @@ var UserSchema = new mongoose.Schema({ default: "", }, + plan_code: { type: String, default: "" }, plan_level: { type: Number, default: 0 }, plan_type: { type: String, default: "free" }, last_charged: { type: Date, default: null }, diff --git a/server/lib/views/staff/middleware.js b/server/lib/views/staff/middleware.js index 5c74d0b..1ea98e9 100644 --- a/server/lib/views/staff/middleware.js +++ b/server/lib/views/staff/middleware.js @@ -40,7 +40,7 @@ var middleware = module.exports = { } if (initial) { if (initial == "?") { - criteria.username = new RegExp('^[$a-zA-Z]', "i") + criteria.username = new RegExp('^[^a-zA-Z]', "i") } else { criteria.username = new RegExp('^' + initial, "i") diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index 58a13ca..bd51dac 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -26,10 +26,11 @@ var xml_bodyparser = require('express-xml-bodyparser'); var parser = new xml2js.Parser(); var subscribe = module.exports = { - plan_level: { + plan_levels: { free: 0, basic: 1, pro: 2, + custom: 3, }, callbacks: { @@ -62,19 +63,30 @@ var subscribe = module.exports = { Subscription.findOne({ "uuid": uuid }, function(err, old_subscriber){ // if (err) return; - var plan = subscrip.plan[0].plan_code[0].split("-") - var plan_type = plan[0] - var plan_period = plan[1] + var plan, plan_type, plan_code + var plan_code = subscrip.plan[0].plan_code[0] + if (plan_code.indexOf("-") !== -1) { + plan = plan_code.split("-") + plan_type = plan[0] + plan_period = plan[1] + } + else { + plan_type = "custom" + plan_period = "monthly" + } + + user.plan_code = plan_code user.plan_type = plan_type - user.plan_level = subscribe.plan_level[plan_type] + user.plan_level = subscribe.plan_levels[plan_type] var subscriber = old_subscriber || new Subscription () subscriber.uuid = uuid subscriber.user_id = user._id + subscriber.plan_code = plan_code subscriber.plan_type = plan_type subscriber.plan_period = plan_period - subscriber.plan_level = subscribe.plan_level[plan_type] + subscriber.plan_level = subscribe.plan_levels[plan_type] subscriber.add_ons = [] var add_ons = subscrip.subscription_add_ons[0].subscription_add_on if (add_ons) { diff --git a/views/staff/subscriptions/index.ejs b/views/staff/subscriptions/index.ejs index 3efffb5..adf148c 100644 --- a/views/staff/subscriptions/index.ejs +++ b/views/staff/subscriptions/index.ejs @@ -26,6 +26,9 @@ [[- subscription.user.last_seen ]] + + [[- subscription.plan_code ]] + [[ }) ]] -- cgit v1.2.3-70-g09d2 From cc3d0bf036dc934494bf517ebae88bd8544b9b06 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 12 Aug 2015 13:27:07 -0400 Subject: add artist privilege --- .../javascripts/ui/site/EditSubscriptionModal.js | 1 + public/assets/javascripts/ui/site/StaffView.js | 23 +++++++++++++++++++++- server/lib/schemas/User.js | 1 + server/lib/views/staff/index.js | 16 ++++++++++++++- server/lib/webhook/webhook.js | 1 + views/staff/users/show.ejs | 11 ++++++++++- 6 files changed, 50 insertions(+), 3 deletions(-) (limited to 'server/lib/webhook/webhook.js') diff --git a/public/assets/javascripts/ui/site/EditSubscriptionModal.js b/public/assets/javascripts/ui/site/EditSubscriptionModal.js index 1033114..c1dc9f8 100644 --- a/public/assets/javascripts/ui/site/EditSubscriptionModal.js +++ b/public/assets/javascripts/ui/site/EditSubscriptionModal.js @@ -97,6 +97,7 @@ var EditSubscriptionModal = ModalView.extend({ basic: 1, pro: 2, custom: 3, + artist: 4, }, loaded: false, diff --git a/public/assets/javascripts/ui/site/StaffView.js b/public/assets/javascripts/ui/site/StaffView.js index 59649e3..97f86c2 100644 --- a/public/assets/javascripts/ui/site/StaffView.js +++ b/public/assets/javascripts/ui/site/StaffView.js @@ -5,12 +5,14 @@ var StaffView = View.extend({ "click #toggle-staff": "toggleStaff", "click #toggle-featured": "toggleFeatured", "click #toggle-stock": "toggleStock", + "click #toggle-artist": "toggleArtist", }, initialize: function() { this.$toggleStaff = $("#toggle-staff") this.$toggleFeatured = $("#toggle-featured") this.$toggleStock = $("#toggle-stock") + this.$toggleArtist = $("#toggle-artist") this.$mediaEmbed = $("#media-embed") if (this.$toggleStaff.length && this.$toggleStaff.data().isstaff) { this.$toggleStaff.html("Is Staff") @@ -21,6 +23,9 @@ var StaffView = View.extend({ if (this.$toggleStock.length && this.$toggleStock.data().stock) { this.$toggleStock.html("Layout is Stock") } + if (this.$toggleArtist.length && this.$toggleArtist.data().isartist) { + this.$toggleArtist.html("Is Artist") + } if (this.$mediaEmbed.length) { var media = this.$mediaEmbed.data() this.$mediaEmbed.html( Parser.tag( media ) ) @@ -75,7 +80,6 @@ var StaffView = View.extend({ }, toggleStock: function(){ - console.log("stock") var state = ! this.$toggleStock.data().stock $.ajax({ type: "put", @@ -93,4 +97,21 @@ var StaffView = View.extend({ }) }, + toggleArtist: function(){ + var state = ! this.$toggleArtist.data().isartist + $.ajax({ + type: "put", + dataType: "json", + url: window.location.href + "/artist", + data: { + state: state, + _csrf: $("#_csrf").val(), + }, + success: function(data){ + this.$toggleArtist.data("stock", data.state) + this.$toggleArtist.html(data.state ? "Is Artist" : "Make Artist") + $("#isArtist").html(data.state ? "yes" : "no") + }.bind(this) + }) + }, }) diff --git a/server/lib/schemas/User.js b/server/lib/schemas/User.js index e6e7f03..829b360 100644 --- a/server/lib/schemas/User.js +++ b/server/lib/schemas/User.js @@ -67,6 +67,7 @@ var UserSchema = new mongoose.Schema({ twitterName: { type: String, default: "" }, facebookUrl: { type: String, default: "" }, isStaff: { type: Boolean, default: false }, + isArtist: { type: Boolean, default: false }, subscription_id: { type: mongoose.Schema.ObjectId }, created_at: { type: Date }, updated_at: { type: Date }, diff --git a/server/lib/views/staff/index.js b/server/lib/views/staff/index.js index 6a56238..033fc88 100644 --- a/server/lib/views/staff/index.js +++ b/server/lib/views/staff/index.js @@ -75,7 +75,15 @@ var staff = module.exports = { staff.users.bless ); - + app.put('/staff/users/:username/artist', + middleware.ensureAuthenticated, + middleware.ensureIsStaff, + + staff.middleware.ensureProfile, + + staff.users.make_artist + ); + if (app.get('env') === 'development') { app.get('/staff/authorize', middleware.ensureAuthenticated, @@ -304,6 +312,12 @@ var staff = module.exports = { res.json({ state: user.isStaff }) }) }, + make_artist: function(req, res){ + res.locals.profile.isArtist = req.body.state == "true" + res.locals.profile.save(function(err, user){ + res.json({ state: user.isArtist }) + }) + }, }, // /staff/projects/ diff --git a/server/lib/webhook/webhook.js b/server/lib/webhook/webhook.js index bd51dac..896d836 100644 --- a/server/lib/webhook/webhook.js +++ b/server/lib/webhook/webhook.js @@ -31,6 +31,7 @@ var subscribe = module.exports = { basic: 1, pro: 2, custom: 3, + artist: 4, }, callbacks: { diff --git a/views/staff/users/show.ejs b/views/staff/users/show.ejs index e441109..a434b57 100644 --- a/views/staff/users/show.ejs +++ b/views/staff/users/show.ejs @@ -35,7 +35,7 @@ plan - [[- profile.plan_type ]] + [[- profile.plan_code ]] @@ -101,12 +101,21 @@ [[- profile.isStaff ? "yes" : "no" ]] + + + is artist? + + + [[- profile.isArtist ? "yes" : "no" ]] + +

    [[ if (String(user._id) != String(profile._id)) { ]] + [[ } ]]
    -- cgit v1.2.3-70-g09d2