Users who have paid or otherwise have access to all content are now called 'premium', and user objects on client and server have an isPremium getter which checks all relevant properties.

This commit is contained in:
Scott Erickson 2014-12-05 17:19:44 -08:00
parent f4e3416918
commit 836d2905b9
6 changed files with 19 additions and 5 deletions

View file

@ -112,6 +112,13 @@ module.exports = class User extends CocoModel
application.tracker.identify gemPromptGroup: @gemPromptGroup unless me.isAdmin()
@gemPromptGroup
isPremium: ->
return false unless stripe = @get('stripe')
return true if stripe.subscriptionID
return true if stripe.free is true
return true if _.isString(stripe.free) and new Date() < new Date(stripe.free)
return false
tiersByLevel = [-1, 0, 0.05, 0.14, 0.18, 0.32, 0.41, 0.5, 0.64, 0.82, 0.91, 1.04, 1.22, 1.35, 1.48, 1.65, 1.78, 1.96, 2.1, 2.24, 2.38, 2.55, 2.69, 2.86, 3.03, 3.16, 3.29, 3.42, 3.58, 3.74, 3.89, 4.04, 4.19, 4.32, 4.47, 4.64, 4.79, 4.96,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15
]

View file

@ -24,7 +24,7 @@ module.exports = class PaymentsView extends RootView
c = super()
c.payments = @payments
c.subscribed = me.get('stripe')?.planID
c.active = me.get('stripe')?.subscriptionID
c.active = me.isPremium()
c
onClickStartSubscription: (e) ->

View file

@ -81,7 +81,7 @@ module.exports = class WorldMapView extends RootView
$('body').append($('<img src="http://code.org/api/hour/begin_codecombat.png" style="visibility: hidden;">'))
trackedHourOfCode = true
@requiresSubscription = @terrain isnt 'dungeon' and not me.get('stripe')?.subscriptionID
@requiresSubscription = @terrain isnt 'dungeon' and not me.isPremium()
@requiresSubscription = true
destroy: ->

View file

@ -72,7 +72,7 @@ LevelHandler = class LevelHandler extends Handler
Session.findOne(sessionQuery).exec (err, doc) =>
return @sendDatabaseError(res, err) if err
return @sendSuccess(res, doc) if doc?
return @sendPaymentRequiredError(res, err) if (not req.user.get('stripe')?.subscriptionID) and level.get('requiresSubscription')
return @sendPaymentRequiredError(res, err) if (not req.user.isPremium()) and level.get('requiresSubscription')
@createAndSaveNewSession sessionQuery, req, res
createAndSaveNewSession: (sessionQuery, req, res) =>

View file

@ -179,6 +179,13 @@ UserSchema.methods.register = (done) ->
delighted.addDelightedUser @
@saveActiveUser 'register'
UserSchema.methods.isPremium = ->
return false unless stripe = @get('stripe')
return true if stripe.subscriptionID
return true if stripe.free is true
return true if _.isString(stripe.free) and new Date() < new Date(stripe.free)
return false
UserSchema.statics.saveActiveUser = (id, event, done=null) ->
id = mongoose.Types.ObjectId id if _.isString id
@findById id, (err, user) ->

View file

@ -236,9 +236,9 @@ UserHandler = class UserHandler extends Handler
getStripeInfo: (req, res, handle) ->
@getDocumentForIdOrSlug handle, (err, user) =>
return @notFoundError(res) if not user
return @sendNotFoundError(res) if not user
return @sendForbiddenError(res) unless req.user and (req.user.isAdmin() or req.user.get('_id').equals(user.get('_id')))
return @notFoundError(res) if not customerID = user.get('stripe')?.customerID
return @sendNotFoundError(res) #if not customerID = user.get('stripe')?.customerID
stripe.customers.retrieve customerID, (err, customer) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res, JSON.stringify(customer, null, '\t'))