Skip to content
Permalink
Browse files

Consolidated logic for changing stripe subscription

  • Loading branch information...
davidmerfield committed Mar 12, 2019
1 parent 66c390b commit 8578d98b2e415862183276d3cc762ab8583f24ae
Showing with 28 additions and 16 deletions.
  1. +28 −16 app/dashboard/routes/account/delete.js
@@ -44,8 +44,9 @@ Delete.route("/blog/:handle")
function(req, res, next) {
deleteBlog(req.blogToDelete.id, next);
},
calculateSubscriptionChange,
decreaseSubscription,
function(req, res, next) {
function(req, res) {
res.message("/account", "Deleted " + req.blogToDelete.title);
}
);
@@ -68,14 +69,31 @@ function loadBlogToDelete(req, res, next) {
}

function calculateSubscriptionChange(req, res, next) {
var subscription = req.user.subscription;

// The user does not have an active subscription
// so proceed to the next middleware
if (!subscription || !subscription.status || subscription.status !== "active")
return next();

var currentQuantity = req.user.subscription.quantity;
var newQuantity = req.user.blogs.length - 1;

// Quantity cannot go below 1
// You must pay for at least one blog to keep an account open
if (
req.user.subscription.plan &&
req.user.subscription.quantity > 1 &&
req.user.subscription.plan.amount
) {
res.locals.reduction = pretty(req.user.subscription.plan.amount);
}
// We only decrease the quantity on the Stripe plan if you are
// paying for all of your blogs.
if (newQuantity < 1) newQuantity = 1;

// You can't increase your bill by deleting a blog
// Some early users have free blogs for various
// reasons. Handle this case here.
if (newQuantity >= currentQuantity) return next();

res.locals.reduction = pretty(
(currentQuantity - newQuantity) * req.user.subscription.plan.amount
);
req.newQuantity = newQuantity;

return next();
}
@@ -156,15 +174,9 @@ function deleteUser(req, res, next) {

function decreaseSubscription(req, res, next) {
var subscription = req.user.subscription;
var quantity = req.user.blogs.length - 1;

// Quantity cannot go below 1
if (!quantity) quantity = 1;
var quantity = req.newQuantity;

// The user does not have an active subscription
// so proceed to the next middleware
if (!subscription || !subscription.status || subscription.status !== "active")
return next();
if (!quantity || !subscription) return next();

stripe.customers.updateSubscription(
subscription.customer,

0 comments on commit 8578d98

Please sign in to comment.
You can’t perform that action at this time.