feat(subscriptions): allow one month after sub start-date before it's terminated. Fixes #3180

This commit is contained in:
Tyler Renelle 2014-05-05 23:18:27 -06:00
parent 25c0b3e32a
commit b0eb12bb5f
4 changed files with 68 additions and 16 deletions

View file

@ -44,7 +44,8 @@ function createSubscription(user, data) {
customerId: data.customerId, customerId: data.customerId,
dateUpdated: new Date, dateUpdated: new Date,
gemsBought: 0, gemsBought: 0,
paymentMethod: data.paymentMethod paymentMethod: data.paymentMethod,
dateTerminated: null
}) })
.defaults({ // allow non-override if a plan was previously used .defaults({ // allow non-override if a plan was previously used
dateCreated: new Date, dateCreated: new Date,
@ -56,9 +57,15 @@ function createSubscription(user, data) {
ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + '-subscription', data.paymentMethod + " > Stripe").send(); ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + '-subscription', data.paymentMethod + " > Stripe").send();
} }
/**
* Sets their subscription to be cancelled later
*/
function cancelSubscription(user, data){ function cancelSubscription(user, data){
_.merge(user.purchased.plan, {planId:null, customerId:null, paymentMethod:null}); var du = user.purchased.plan.dateUpdated, now = moment();
user.markModified('purchased.plan'); user.purchased.plan.dateTerminated =
moment( now.format('MM') + '/' + moment(du).format('DD') + '/' + now.format('YYYY') )
.add('month',1)
.toDate();
ga.event('unsubscribe', 'Stripe').send(); ga.event('unsubscribe', 'Stripe').send();
} }
@ -69,6 +76,12 @@ function buyGems(user, data) {
ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send(); ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send();
} }
// Expose some functions for tests
if (nconf.get('NODE_ENV')==='testing') {
api.cancelSubscription = cancelSubscription;
api.createSubscription = createSubscription;
}
/* /*
Setup Stripe response when posting payment Setup Stripe response when posting payment
*/ */

View file

@ -89,6 +89,7 @@ var UserSchema = new Schema({
paymentMethod: String, //enum: ['Paypal','Stripe', '']} paymentMethod: String, //enum: ['Paypal','Stripe', '']}
customerId: String, customerId: String,
dateCreated: Date, dateCreated: Date,
dateTerminated: Date,
dateUpdated: Date, dateUpdated: Date,
gemsBought: {type: Number, 'default': 0}, gemsBought: {type: Number, 'default': 0},
mysteryItems: {type: Array, 'default': []} mysteryItems: {type: Array, 'default': []}

View file

@ -27,6 +27,7 @@ var Challenge = require('../src/models/challenge').model;
var app = require('../src/server'); var app = require('../src/server');
var shared = require('habitrpg-shared'); var shared = require('habitrpg-shared');
var payments = require('../src/controllers/payments');
// ###### Helpers & Variables ###### // ###### Helpers & Variables ######
var model, uuid, taskPath, var model, uuid, taskPath,
@ -118,6 +119,7 @@ describe('API', function () {
}); });
}); });
describe('Todos', function(){ describe('Todos', function(){
it('Archives old todos',function(done){ it('Archives old todos',function(done){
request.post(baseURL + "/user/batch-update?_v=999") request.post(baseURL + "/user/batch-update?_v=999")
@ -506,8 +508,7 @@ describe('API', function () {
],done); ],done);
// You see all these freaking })s? This is why I prefer coffeescript!! I keep hitting stupid errors // See all these })s? This is why CoffeeScript is better.
// where they're mis-matched and I'm counting )'s on my fingers for 10m just to fix them. Gaahhh..
//}) //})
}) })
}) })
@ -517,5 +518,41 @@ describe('API', function () {
}); });
}); });
describe('Subscriptions', function(){
var user;
before(function(done){
User.findOne({_id: _id}, function (err, _user) {
expect(err).to.not.be.ok();
user = _user;
done();
});
})
})
it('Handles unsubscription', function(done){
var cron = function(){
user.lastCron = moment().subtract('d',1);
user.fns.cron();
}
expect(user.purchased.plan.customerId).to.not.be.ok();
payments.createSubscription(user,{customerId:'123',paymentMethod:'Stripe'});
expect(user.purchased.plan.customerId).to.be.ok();
shared.wrap(user);
cron();
expect(user.purchased.plan.customerId).to.be.ok();
payments.cancelSubscription(user);
cron();
expect(user.purchased.plan.customerId).to.be.ok();
expect(user.purchased.plan.dateTerminated).to.be.ok();
user.purchased.plan.dateTerminated = moment().subtract('d',2);
cron();
expect(user.purchased.plan.customerId).to.not.be.ok();
payments.createSubscription(user,{customerId:'123',paymentMethod:'Stripe'});
expect(user.purchased.plan.dateTerminated).to.not.be.ok();
done();
})
}); });
}); });

View file

@ -193,16 +193,17 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template')
p p
small.muted Payment Methods: small.muted Payment Methods:
.btn.btn-primary(ng-click='showStripe(true)') Card .btn.btn-primary(ng-click='showStripe(true)') Card
//-small.muted PayPal coming soon. //a.btn.btn-warning(ng-click='paypalSubscribe()') PayPal
//a.btn.btn-warning(ng-click='paypalSubscribe()') PayPal
a.btn.btn-warning(href='/paypal/subscribe?_id={{user._id}}&apiToken={{user.apiToken}}') PayPal a.btn.btn-warning(href='/paypal/subscribe?_id={{user._id}}&apiToken={{user.apiToken}}') PayPal
div(ng-if='user.purchased.plan.customerId') div(ng-if='user.purchased.plan.customerId')
.well p.alert.alert-warning(ng-if='user.purchased.plan.dateTerminated')
p.lead i.glyphicon.glyphicon-time
=env.t('subscribed') |  
|   =env.t('subCanceled')
span.glyphicon.glyphicon-ok | <strong>{{moment(user.purchased.plan.dateTerminated).format('MM/DD/YYYY')}}</strong>
div(ng-include="'partials/options.settings.subscription.perks.html'") p.lead
.btn.btn-sm.btn-danger(ng-click='cancelSubscription()')=env.t('cancelSub') =env.t('subscribed')
p | &nbsp;
small.muted Unsubscribing removes subscriber perks (<a href='https://github.com/HabitRPG/habitrpg/issues/3180' target='_blank'>read more here</a>). We're working on persisting subscriber perks through a canceled plan's billing cycle, but for now be sure to open your mystery box before unsubscribing! span.glyphicon.glyphicon-ok
div(ng-include="'partials/options.settings.subscription.perks.html'")
.btn.btn-sm.btn-danger(ng-if='!user.purchased.plan.dateTerminated', ng-click='cancelSubscription()')=env.t('cancelSub')