From b7843b59c82e738f990d243fb59bbd69dbec222d Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Thu, 5 Nov 2015 14:20:24 -0800 Subject: [PATCH] Remove courses from purchasing course prepaids --- server/prepaids/prepaid_handler.coffee | 32 ++++-------- test/server/common.coffee | 2 - test/server/functional/prepaid.spec.coffee | 61 +++++----------------- 3 files changed, 24 insertions(+), 71 deletions(-) diff --git a/server/prepaids/prepaid_handler.coffee b/server/prepaids/prepaid_handler.coffee index 3fc43b85f..30cd7553f 100644 --- a/server/prepaids/prepaid_handler.coffee +++ b/server/prepaids/prepaid_handler.coffee @@ -135,40 +135,30 @@ 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) => - # 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 - @sendSuccess(res, prepaid.toObject()) + @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 + @sendSuccess(res, prepaid.toObject()) 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' diff --git a/test/server/common.coffee b/test/server/common.coffee index 432e098d9..0635cbc45 100644 --- a/test/server/common.coffee +++ b/test/server/common.coffee @@ -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) => diff --git a/test/server/functional/prepaid.spec.coffee b/test/server/functional/prepaid.spec.coffee index 9a0f39e25..f299e5dda 100644 --- a/test/server/functional/prepaid.spec.coffee +++ b/test/server/functional/prepaid.spec.coffee @@ -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,68 +228,36 @@ 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) -> + purchasePrepaid 'course', {}, 0, token.id, (err, res, prepaid) -> expect(err).toBeNull() - purchasePrepaid 'course', courseID: course.id, 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) -> + expect(res.statusCode).toBe(422) + 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) -> + purchasePrepaid 'course', {}, 1, token.id, (err, res, prepaid) -> expect(err).toBeNull() - purchasePrepaid 'course', courseID: course.id, 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) + expect(res.statusCode).toBe(200) + 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) -> + purchasePrepaid 'course', {}, 3, token.id, (err, res, prepaid) -> expect(err).toBeNull() - purchasePrepaid 'course', courseID: course.id, 3, token.id, (err, res, prepaid) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(200) - done() - + expect(res.statusCode).toBe(200) + verifyCoursePrepaid(user1, prepaid, done) + describe 'Purchase terminal_subscription', -> it 'Anonymous submits a prepaid purchase', (done) -> stripe.tokens.create {