2014-08-27 12:51:10 -04:00
|
|
|
async = require("async")
|
2014-02-12 05:23:40 -05:00
|
|
|
RecurlyWrapper = require("./RecurlyWrapper")
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
User = require('../../models/User').User
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
SubscriptionUpdater = require("./SubscriptionUpdater")
|
|
|
|
LimitationsManager = require('./LimitationsManager')
|
2014-03-05 09:56:52 -05:00
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
2015-02-05 13:20:25 -05:00
|
|
|
Events = require "../../infrastructure/Events"
|
2016-10-10 10:29:54 -04:00
|
|
|
Analytics = require("../Analytics/AnalyticsManager")
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2014-12-19 06:17:35 -05:00
|
|
|
createSubscription: (user, subscriptionDetails, recurly_token_id, callback)->
|
2014-02-12 05:23:40 -05:00
|
|
|
self = @
|
2014-12-18 13:59:29 -05:00
|
|
|
clientTokenId = ""
|
2014-12-19 06:17:35 -05:00
|
|
|
RecurlyWrapper.createSubscription user, subscriptionDetails, recurly_token_id, (error, recurlySubscription)->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
SubscriptionUpdater.syncSubscription recurlySubscription, user._id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
2014-08-27 12:51:10 -04:00
|
|
|
updateSubscription: (user, plan_code, coupon_code, callback)->
|
2014-08-28 13:14:31 -04:00
|
|
|
logger.log user:user, plan_code:plan_code, coupon_code:coupon_code, "updating subscription"
|
2014-02-12 05:23:40 -05:00
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
|
2014-08-27 12:51:10 -04:00
|
|
|
if !hasSubscription
|
|
|
|
return callback()
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
2014-08-27 12:51:10 -04:00
|
|
|
async.series [
|
|
|
|
(cb)->
|
|
|
|
return cb() if !coupon_code?
|
2014-08-28 13:14:31 -04:00
|
|
|
logger.log user_id:user._id, plan_code:plan_code, coupon_code:coupon_code, "updating subscription with coupon code applied first"
|
2014-08-27 12:51:10 -04:00
|
|
|
RecurlyWrapper.getSubscription subscription.recurlySubscription_id, includeAccount: true, (err, usersSubscription)->
|
|
|
|
return callback(err) if err?
|
|
|
|
account_code = usersSubscription.account.account_code
|
|
|
|
RecurlyWrapper.redeemCoupon account_code, coupon_code, cb
|
|
|
|
(cb)->
|
|
|
|
RecurlyWrapper.updateSubscription subscription.recurlySubscription_id, {plan_code: plan_code, timeframe: "now"}, (error, recurlySubscription) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
SubscriptionUpdater.syncSubscription recurlySubscription, user._id, cb
|
|
|
|
], callback
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
cancelSubscription: (user, callback) ->
|
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
|
|
|
|
if hasSubscription
|
|
|
|
RecurlyWrapper.cancelSubscription subscription.recurlySubscription_id, (error) ->
|
|
|
|
return callback(error) if error?
|
2014-03-05 09:56:52 -05:00
|
|
|
emailOpts =
|
|
|
|
to: user.email
|
|
|
|
first_name: user.first_name
|
2014-09-16 09:25:12 -04:00
|
|
|
ONE_HOUR_IN_MS = 1000 * 60 * 60
|
|
|
|
setTimeout (-> EmailHandler.sendEmail "canceledSubscription", emailOpts
|
|
|
|
), ONE_HOUR_IN_MS
|
2015-02-05 13:20:25 -05:00
|
|
|
Events.emit "cancelSubscription", user._id
|
2016-10-10 10:29:54 -04:00
|
|
|
Analytics.recordEvent user._id, "subscription-canceled"
|
2015-02-05 13:20:25 -05:00
|
|
|
callback()
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
|
|
|
reactivateSubscription: (user, callback) ->
|
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
|
|
|
|
if hasSubscription
|
|
|
|
RecurlyWrapper.reactivateSubscription subscription.recurlySubscription_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
|
|
|
recurlyCallback: (recurlySubscription, callback) ->
|
|
|
|
RecurlyWrapper.getSubscription recurlySubscription.uuid, includeAccount: true, (error, recurlySubscription) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
User.findById recurlySubscription.account.account_code, (error, user) ->
|
|
|
|
return callback(error) if error?
|
2015-10-21 06:27:13 -04:00
|
|
|
if !user?
|
|
|
|
return callback("no user found")
|
2015-10-19 17:29:11 -04:00
|
|
|
SubscriptionUpdater.syncSubscription recurlySubscription, user?._id, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-12-08 13:23:28 -05:00
|
|
|
extendTrial: (subscription, daysToExend, callback)->
|
|
|
|
RecurlyWrapper.extendTrial subscription.recurlySubscription_id, daysToExend, callback
|