mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-30 10:56:53 -05:00
Remove courses from purchasing course prepaids
This commit is contained in:
parent
6b5e13501e
commit
b7843b59c8
3 changed files with 24 additions and 71 deletions
|
@ -135,21 +135,13 @@ PrepaidHandler = class PrepaidHandler extends Handler
|
|||
@sendSuccess(res, prepaid.toObject())
|
||||
|
||||
else if req.body.type is 'course'
|
||||
courseID = req.body.courseID
|
||||
|
||||
maxRedeemers = parseInt(req.body.maxRedeemers)
|
||||
timestamp = req.body.stripe?.timestamp
|
||||
token = req.body.stripe?.token
|
||||
|
||||
return @sendBadInputError(res) unless isNaN(maxRedeemers) is false and maxRedeemers > 0
|
||||
|
||||
query = if courseID? then {_id: courseID} else {}
|
||||
Course.find query, (err, courses) =>
|
||||
if err
|
||||
@logError(user, "Find courses error: #{JSON.stringify(err)}")
|
||||
return done(err)
|
||||
|
||||
@purchasePrepaidCourse req.user, courses, maxRedeemers, timestamp, token, (err, prepaid) =>
|
||||
@purchasePrepaidCourse req.user, maxRedeemers, timestamp, token, (err, prepaid) =>
|
||||
# TODO: this badinput detection is fragile, in course instance handler as well
|
||||
return @sendBadInputError(res, err) if err is 'Missing required Stripe token'
|
||||
return @sendDatabaseError(res, err) if err
|
||||
|
@ -157,18 +149,16 @@ PrepaidHandler = class PrepaidHandler extends Handler
|
|||
else
|
||||
@sendForbiddenError(res)
|
||||
|
||||
purchasePrepaidCourse: (user, courses, maxRedeemers, timestamp, token, done) ->
|
||||
purchasePrepaidCourse: (user, maxRedeemers, timestamp, token, done) ->
|
||||
type = 'course'
|
||||
|
||||
courseIDs = (c.get('_id') for c in courses)
|
||||
coursePrices = (c.get('pricePerSeat') for c in courses)
|
||||
amount = utils.getCourseBundlePrice(coursePrices, maxRedeemers)
|
||||
amount = maxRedeemers * 200 # TODO: Actual price
|
||||
if amount > 0 and not (token or user.isAdmin())
|
||||
@logError(user, "Purchase prepaid courses missing required Stripe token #{amount}")
|
||||
return done('Missing required Stripe token')
|
||||
|
||||
if amount is 0 or user.isAdmin()
|
||||
@createPrepaid(user, type, maxRedeemers, courseIDs: courseIDs, done)
|
||||
@createPrepaid(user, type, maxRedeemers, {}, done)
|
||||
|
||||
else
|
||||
StripeUtils.getCustomer user, token, (err, customer) =>
|
||||
|
@ -180,10 +170,8 @@ PrepaidHandler = class PrepaidHandler extends Handler
|
|||
type: type
|
||||
userID: user.id
|
||||
timestamp: parseInt(timestamp)
|
||||
description: if courses.length is 1 then courses[0].get('name') else 'All Courses'
|
||||
maxRedeemers: maxRedeemers
|
||||
productID: "prepaid #{type}"
|
||||
courseIDs: courseIDs
|
||||
|
||||
StripeUtils.createCharge user, amount, metadata, (err, charge) =>
|
||||
if err
|
||||
|
@ -194,9 +182,9 @@ PrepaidHandler = class PrepaidHandler extends Handler
|
|||
if err
|
||||
@logError(user, "createPayment error: #{JSON.stringify(err)}")
|
||||
return done(err)
|
||||
msg = "Prepaid code purchased: #{type} seats=#{maxRedeemers} courseIDs=#{courseIDs} #{user.get('email')}"
|
||||
msg = "Prepaid code purchased: #{type} seats=#{maxRedeemers} #{user.get('email')}"
|
||||
hipchat.sendHipChatMessage msg, ['tower']
|
||||
@createPrepaid(user, type, maxRedeemers, courseIDs: courseIDs, done)
|
||||
@createPrepaid(user, type, maxRedeemers, {}, done)
|
||||
|
||||
purchasePrepaidTerminalSubscription: (user, description, maxRedeemers, months, timestamp, token, done) ->
|
||||
type = 'terminal_subscription'
|
||||
|
|
|
@ -164,8 +164,6 @@ GLOBAL.purchasePrepaid = (type, properties, maxRedeemers, token, done) ->
|
|||
options.json.stripe.token = token if token?
|
||||
if type is 'terminal_subscription'
|
||||
options.json.months = properties.months
|
||||
else if type is 'course'
|
||||
options.json.courseID = properties.courseID if properties?.courseID
|
||||
request.post options, done
|
||||
|
||||
GLOBAL.subscribeWithPrepaid = (ppc, done) =>
|
||||
|
|
|
@ -19,7 +19,6 @@ describe '/db/prepaid', ->
|
|||
expect(prepaid.type).toEqual('course')
|
||||
expect(prepaid.maxRedeemers).toBeGreaterThan(0)
|
||||
expect(prepaid.code).toMatch(/^\w{8}$/)
|
||||
expect(prepaid.properties?.courseIDs?.length).toBeGreaterThan(0)
|
||||
done()
|
||||
|
||||
verifySubscriptionPrepaid = (user, prepaid, done) ->
|
||||
|
@ -229,67 +228,35 @@ describe '/db/prepaid', ->
|
|||
done() unless found
|
||||
|
||||
describe 'Purchase course', ->
|
||||
it 'Standard user purchases a prepaid for one course, 0 seats', (done) ->
|
||||
it 'Standard user purchases a prepaid for 0 seats', (done) ->
|
||||
stripe.tokens.create {
|
||||
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
||||
}, (err, token) ->
|
||||
loginNewUser (user1) ->
|
||||
createCourse 700, (err, course) ->
|
||||
expect(err).toBeNull()
|
||||
purchasePrepaid 'course', courseID: course.id, 0, token.id, (err, res, prepaid) ->
|
||||
purchasePrepaid 'course', {}, 0, token.id, (err, res, prepaid) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe(422)
|
||||
done()
|
||||
it 'Standard user purchases a prepaid for one course, 1 seat', (done) ->
|
||||
|
||||
it 'Standard user purchases a prepaid for 1 seat', (done) ->
|
||||
stripe.tokens.create {
|
||||
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
||||
}, (err, token) ->
|
||||
loginNewUser (user1) ->
|
||||
createCourse 700, (err, course) ->
|
||||
expect(err).toBeNull()
|
||||
purchasePrepaid 'course', courseID: course.id, 1, token.id, (err, res, prepaid) ->
|
||||
purchasePrepaid 'course', {}, 1, token.id, (err, res, prepaid) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe(200)
|
||||
verifyCoursePrepaid(user1, prepaid, done)
|
||||
it 'Standard user purchases a prepaid for one course, 3 seats', (done) ->
|
||||
stripe.tokens.create {
|
||||
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
||||
}, (err, token) ->
|
||||
loginNewUser (user1) ->
|
||||
createCourse 700, (err, course) ->
|
||||
expect(err).toBeNull()
|
||||
purchasePrepaid 'course', courseID: course.id, 3, token.id, (err, res, prepaid) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe(200)
|
||||
verifyCoursePrepaid(user1, prepaid, done)
|
||||
it 'Standard user purchases a prepaid for all courses, 10 seats', (done) ->
|
||||
clearModels [Course, CourseInstance, Payment, Prepaid, User], (err) ->
|
||||
throw err if err
|
||||
stripe.tokens.create {
|
||||
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
||||
}, (err, token) ->
|
||||
loginNewUser (user1) ->
|
||||
createCourse 700, (err, course) ->
|
||||
expect(err).toBeNull()
|
||||
createCourse 700, (err, course) ->
|
||||
expect(err).toBeNull()
|
||||
purchasePrepaid 'course', null, 10, token.id, (err, res, prepaid) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe(200)
|
||||
expect(prepaid.properties?.courseIDs?.length).toEqual(2)
|
||||
verifyCoursePrepaid(user1, prepaid, done)
|
||||
|
||||
it 'Standard user purchases a prepaid course for 3', (done) ->
|
||||
it 'Standard user purchases a prepaid for 3 seats', (done) ->
|
||||
stripe.tokens.create {
|
||||
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
|
||||
}, (err, token) ->
|
||||
loginNewUser (user1) ->
|
||||
createCourse 700, (err, course) ->
|
||||
expect(err).toBeNull()
|
||||
purchasePrepaid 'course', courseID: course.id, 3, token.id, (err, res, prepaid) ->
|
||||
purchasePrepaid 'course', {}, 3, token.id, (err, res, prepaid) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe(200)
|
||||
done()
|
||||
verifyCoursePrepaid(user1, prepaid, done)
|
||||
|
||||
describe 'Purchase terminal_subscription', ->
|
||||
it 'Anonymous submits a prepaid purchase', (done) ->
|
||||
|
|
Loading…
Reference in a new issue