2015-11-05 18:30:57 -05:00
|
|
|
app = require 'core/application'
|
2015-12-06 17:26:32 -05:00
|
|
|
Classroom = require 'models/Classroom'
|
2015-11-05 18:30:57 -05:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
Course = require 'models/Course'
|
2015-12-06 17:26:32 -05:00
|
|
|
Prepaids = require 'collections/Prepaids'
|
2015-11-05 18:30:57 -05:00
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
stripeHandler = require 'core/services/stripe'
|
|
|
|
template = require 'templates/courses/purchase-courses-view'
|
2015-12-06 17:26:32 -05:00
|
|
|
User = require 'models/User'
|
2015-11-05 18:30:57 -05:00
|
|
|
utils = require 'core/utils'
|
2015-12-14 14:10:37 -05:00
|
|
|
Products = require 'collections/Products'
|
2015-11-05 18:30:57 -05:00
|
|
|
|
|
|
|
module.exports = class PurchaseCoursesView extends RootView
|
|
|
|
id: 'purchase-courses-view'
|
|
|
|
template: template
|
|
|
|
numberOfStudents: 30
|
2015-12-14 14:10:37 -05:00
|
|
|
pricePerStudent: 0
|
2015-12-06 17:26:32 -05:00
|
|
|
|
2015-11-05 18:30:57 -05:00
|
|
|
initialize: (options) ->
|
|
|
|
@listenTo stripeHandler, 'received-token', @onStripeReceivedToken
|
2015-11-30 14:14:27 -05:00
|
|
|
@fromClassroom = utils.getQueryVariable('from-classroom')
|
2015-12-06 17:26:32 -05:00
|
|
|
@members = new CocoCollection([], { model: User })
|
|
|
|
@listenTo @members, 'sync', @membersSync
|
|
|
|
@classrooms = new CocoCollection([], { url: "/db/classroom", model: Classroom })
|
|
|
|
@classrooms.comparator = '_id'
|
|
|
|
@listenToOnce @classrooms, 'sync', @onceClassroomsSync
|
|
|
|
@supermodel.loadCollection(@classrooms, 'classrooms', {data: {ownerID: me.id}})
|
|
|
|
@prepaids = new Prepaids()
|
|
|
|
@prepaids.comparator = '_id'
|
|
|
|
@prepaids.fetchByCreator(me.id)
|
|
|
|
@supermodel.loadCollection(@prepaids, 'prepaids')
|
2015-12-14 14:10:37 -05:00
|
|
|
@products = new Products()
|
|
|
|
@supermodel.loadCollection(@products, 'products')
|
2015-11-05 18:30:57 -05:00
|
|
|
super(options)
|
2015-12-06 17:26:32 -05:00
|
|
|
|
2015-11-05 18:30:57 -05:00
|
|
|
events:
|
|
|
|
'input #students-input': 'onInputStudentsInput'
|
|
|
|
'click #purchase-btn': 'onClickPurchaseButton'
|
2016-02-02 15:48:19 -05:00
|
|
|
|
2015-12-14 14:10:37 -05:00
|
|
|
onLoaded: ->
|
|
|
|
@pricePerStudent = @products.findWhere({name: 'course'}).get('amount')
|
|
|
|
super()
|
2015-11-05 18:30:57 -05:00
|
|
|
|
2015-12-14 14:10:37 -05:00
|
|
|
getPriceString: -> '$' + (@getPrice()/100).toFixed(2)
|
2015-11-05 18:30:57 -05:00
|
|
|
getPrice: -> @pricePerStudent * @numberOfStudents
|
|
|
|
|
2015-12-06 17:26:32 -05:00
|
|
|
onceClassroomsSync: ->
|
|
|
|
for classroom in @classrooms.models
|
|
|
|
@members.fetch({
|
|
|
|
remove: false
|
|
|
|
url: "/db/classroom/#{classroom.id}/members"
|
|
|
|
})
|
|
|
|
|
|
|
|
membersSync: ->
|
|
|
|
@memberEnrolledMap = {}
|
|
|
|
for user in @members.models
|
|
|
|
@memberEnrolledMap[user.id] = user.get('coursePrepaidID')?
|
|
|
|
@classroomNotEnrolledMap = {}
|
|
|
|
@totalNotEnrolled = 0
|
|
|
|
for classroom in @classrooms.models
|
|
|
|
@classroomNotEnrolledMap[classroom.id] = 0
|
|
|
|
for memberID in classroom.get('members')
|
|
|
|
@classroomNotEnrolledMap[classroom.id]++ unless @memberEnrolledMap[memberID]
|
|
|
|
@totalNotEnrolled += @classroomNotEnrolledMap[classroom.id]
|
|
|
|
@numberOfStudents = @totalNotEnrolled
|
|
|
|
@render?()
|
|
|
|
|
2015-11-05 18:30:57 -05:00
|
|
|
onInputStudentsInput: ->
|
2015-12-02 19:10:57 -05:00
|
|
|
@numberOfStudents = Math.max(parseInt(@$('#students-input').val()) or 0, 0)
|
2015-11-05 18:30:57 -05:00
|
|
|
@updatePrice()
|
|
|
|
|
|
|
|
updatePrice: ->
|
|
|
|
@renderSelectors '#price-form-group'
|
2016-02-02 15:48:19 -05:00
|
|
|
|
|
|
|
numberOfStudentsIsValid: -> @numberOfStudents > 0 and @numberOfStudents < 100000
|
2015-11-05 18:30:57 -05:00
|
|
|
|
|
|
|
onClickPurchaseButton: ->
|
2016-03-09 17:40:52 -05:00
|
|
|
return application.router.navigate('/teachers/signup', {trigger: true}) if me.isAnonymous()
|
2016-01-07 12:54:02 -05:00
|
|
|
unless @numberOfStudentsIsValid()
|
2015-11-05 18:30:57 -05:00
|
|
|
alert("Please enter the maximum number of students needed for your class.")
|
|
|
|
return
|
|
|
|
|
|
|
|
@state = undefined
|
|
|
|
@stateMessage = undefined
|
|
|
|
@render()
|
|
|
|
|
|
|
|
# Show Stripe handler
|
|
|
|
application.tracker?.trackEvent 'Started course prepaid purchase', {
|
2015-12-06 19:21:08 -05:00
|
|
|
price: @pricePerStudent, students: @numberOfStudents}
|
2015-11-05 18:30:57 -05:00
|
|
|
stripeHandler.open
|
2015-12-14 14:10:37 -05:00
|
|
|
amount: @numberOfStudents * @pricePerStudent
|
2015-11-05 18:30:57 -05:00
|
|
|
description: "Full course access for #{@numberOfStudents} students"
|
|
|
|
bitcoin: true
|
|
|
|
alipay: if me.get('country') is 'china' or (me.get('preferredLanguage') or 'en-US')[...2] is 'zh' then true else 'auto'
|
|
|
|
|
|
|
|
onStripeReceivedToken: (e) ->
|
|
|
|
@state = 'purchasing'
|
|
|
|
@render?()
|
2015-12-06 17:26:32 -05:00
|
|
|
|
2015-11-05 18:30:57 -05:00
|
|
|
data =
|
|
|
|
maxRedeemers: @numberOfStudents
|
|
|
|
type: 'course'
|
|
|
|
stripe:
|
|
|
|
token: e.token.id
|
|
|
|
timestamp: new Date().getTime()
|
2015-12-06 17:26:32 -05:00
|
|
|
|
2015-11-05 18:30:57 -05:00
|
|
|
$.ajax({
|
|
|
|
url: '/db/prepaid/-/purchase',
|
|
|
|
data: data,
|
|
|
|
method: 'POST',
|
|
|
|
context: @
|
|
|
|
success: ->
|
|
|
|
application.tracker?.trackEvent 'Finished course prepaid purchase', {price: @pricePerStudent, seats: @numberOfStudents}
|
|
|
|
@state = 'purchased'
|
|
|
|
@render?()
|
2015-12-06 17:26:32 -05:00
|
|
|
|
2015-11-05 18:30:57 -05:00
|
|
|
error: (jqxhr, textStatus, errorThrown) ->
|
|
|
|
application.tracker?.trackEvent 'Failed course prepaid purchase', status: textStatus
|
|
|
|
if jqxhr.status is 402
|
|
|
|
@state = 'error'
|
|
|
|
@stateMessage = arguments[2]
|
|
|
|
else
|
|
|
|
@state = 'error'
|
|
|
|
@stateMessage = "#{jqxhr.status}: #{jqxhr.responseText}"
|
|
|
|
@render?()
|
|
|
|
})
|