mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 17:45:40 -05:00
Tweaked the subscription handler to modify the actual target user, not necessarily the user that is making the changes.
This commit is contained in:
parent
6344b0c1a3
commit
e8bb5b824d
1 changed files with 23 additions and 23 deletions
|
@ -13,17 +13,17 @@ subscriptions = {
|
|||
}
|
||||
|
||||
class SubscriptionHandler extends Handler
|
||||
logSubscriptionError: (req, msg) ->
|
||||
console.warn "Subscription Error: #{req.user.get('slug')} (#{req.user._id}): '#{msg}'"
|
||||
logSubscriptionError: (user, msg) ->
|
||||
console.warn "Subscription Error: #{user.get('slug')} (#{user._id}): '#{msg}'"
|
||||
|
||||
subscribeUser: (req, user, done) ->
|
||||
if (not req.user) or req.user.isAnonymous()
|
||||
if (not req.user) or req.user.isAnonymous() or user.isAnonymous()
|
||||
return done({res: 'You must be signed in to subscribe.', code: 403})
|
||||
|
||||
token = req.body.stripe.token
|
||||
customerID = user.get('stripe')?.customerID
|
||||
if not (token or customerID)
|
||||
@logSubscriptionError(req, 'Missing stripe token or customer ID.')
|
||||
@logSubscriptionError(user, 'Missing stripe token or customer ID.')
|
||||
return done({res: 'Missing stripe token or customer ID.', code: 422})
|
||||
|
||||
if token
|
||||
|
@ -31,15 +31,15 @@ class SubscriptionHandler extends Handler
|
|||
stripe.customers.update customerID, { card: token }, (err, customer) =>
|
||||
if err or not customer
|
||||
# should not happen outside of test and production polluting each other
|
||||
@logSubscriptionError(req, 'Cannot find customer: ', +customer.id + '\n\n' + err)
|
||||
@logSubscriptionError(user, 'Cannot find customer: ', +customer.id + '\n\n' + err)
|
||||
return done({res: 'Cannot find customer.', code: 404})
|
||||
@checkForExistingSubscription(req, user, customer, done)
|
||||
|
||||
else
|
||||
newCustomer = {
|
||||
card: token
|
||||
email: req.user.get('email')
|
||||
metadata: { id: req.user._id + '', slug: req.user.get('slug') }
|
||||
email: user.get('email')
|
||||
metadata: { id: user._id + '', slug: user.get('slug') }
|
||||
}
|
||||
|
||||
stripe.customers.create newCustomer, (err, customer) =>
|
||||
|
@ -47,22 +47,22 @@ class SubscriptionHandler extends Handler
|
|||
if err.type in ['StripeCardError', 'StripeInvalidRequestError']
|
||||
return done({res: 'Card error', code: 402})
|
||||
else
|
||||
@logSubscriptionError(req, 'Stripe customer creation error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe customer creation error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
|
||||
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
||||
stripeInfo = _.cloneDeep(user.get('stripe') ? {})
|
||||
stripeInfo.customerID = customer.id
|
||||
req.user.set('stripe', stripeInfo)
|
||||
req.user.save (err) =>
|
||||
user.set('stripe', stripeInfo)
|
||||
user.save (err) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe customer id save db error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe customer id save db error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
@checkForExistingSubscription(req, user, customer, done)
|
||||
|
||||
else
|
||||
stripe.customers.retrieve(customerID, (err, customer) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe customer creation error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe customer creation error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
@checkForExistingSubscription(req, user, customer, done)
|
||||
)
|
||||
|
@ -79,14 +79,14 @@ class SubscriptionHandler extends Handler
|
|||
# subscription a trial period that ends when the cancelled subscription would have ended.
|
||||
stripe.customers.cancelSubscription subscription.customer, subscription.id, (err) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe cancel subscription error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe cancel subscription error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
|
||||
options = { plan: 'basic', trial_end: subscription.current_period_end }
|
||||
options.coupon = couponID if couponID
|
||||
stripe.customers.update req.user.get('stripe').customerID, options, (err, customer) =>
|
||||
stripe.customers.update user.get('stripe').customerID, options, (err, customer) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe customer plan setting error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe customer plan setting error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
|
||||
@updateUser(req, user, customer, false, done)
|
||||
|
@ -98,9 +98,9 @@ class SubscriptionHandler extends Handler
|
|||
else
|
||||
options = { plan: 'basic' }
|
||||
options.coupon = couponID if couponID
|
||||
stripe.customers.update req.user.get('stripe').customerID, options, (err, customer) =>
|
||||
stripe.customers.update user.get('stripe').customerID, options, (err, customer) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe customer plan setting error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe customer plan setting error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
|
||||
@updateUser(req, user, customer, true, done)
|
||||
|
@ -123,25 +123,25 @@ class SubscriptionHandler extends Handler
|
|||
|
||||
user.save (err) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe user plan saving error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe user plan saving error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
req.user?.saveActiveUser 'subscribe'
|
||||
user?.saveActiveUser 'subscribe'
|
||||
return done()
|
||||
|
||||
unsubscribeUser: (req, user, done) ->
|
||||
stripeInfo = _.cloneDeep(user.get('stripe'))
|
||||
stripe.customers.cancelSubscription stripeInfo.customerID, stripeInfo.subscriptionID, { at_period_end: true }, (err) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'Stripe cancel subscription error. '+err)
|
||||
@logSubscriptionError(user, 'Stripe cancel subscription error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
delete stripeInfo.planID
|
||||
user.set('stripe', stripeInfo)
|
||||
req.body.stripe = stripeInfo
|
||||
user.save (err) =>
|
||||
if err
|
||||
@logSubscriptionError(req, 'User save unsubscribe error. '+err)
|
||||
@logSubscriptionError(user, 'User save unsubscribe error. '+err)
|
||||
return done({res: 'Database error.', code: 500})
|
||||
req.user?.saveActiveUser 'unsubscribe'
|
||||
user?.saveActiveUser 'unsubscribe'
|
||||
return done()
|
||||
|
||||
module.exports = new SubscriptionHandler()
|
||||
|
|
Loading…
Reference in a new issue