2014-12-02 23:01:53 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2014-12-18 23:34:59 -05:00
|
|
|
template = require 'templates/core/subscribe-modal'
|
2014-12-02 23:01:53 -05:00
|
|
|
stripeHandler = require 'core/services/stripe'
|
|
|
|
utils = require 'core/utils'
|
2014-12-06 12:35:13 -05:00
|
|
|
AuthModal = require 'views/core/AuthModal'
|
2015-12-14 14:10:37 -05:00
|
|
|
Products = require 'collections/Products'
|
2014-12-02 23:01:53 -05:00
|
|
|
|
|
|
|
module.exports = class SubscribeModal extends ModalView
|
|
|
|
id: 'subscribe-modal'
|
|
|
|
template: template
|
|
|
|
plain: true
|
|
|
|
closesOnClickOutside: false
|
2015-12-14 14:10:37 -05:00
|
|
|
planID: 'basic'
|
2014-12-02 23:01:53 -05:00
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'stripe:received-token': 'onStripeReceivedToken'
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click #close-modal': 'hide'
|
2015-04-10 16:46:53 -04:00
|
|
|
'click .popover-content .parent-send': 'onClickParentSendButton'
|
|
|
|
'click .email-parent-complete button': 'onClickParentEmailCompleteButton'
|
2015-02-04 16:54:35 -05:00
|
|
|
'click .purchase-button': 'onClickPurchaseButton'
|
2015-08-21 14:10:06 -04:00
|
|
|
'click .sale-button': 'onClickSaleButton'
|
2014-12-02 23:01:53 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super(options)
|
|
|
|
@state = 'standby'
|
2015-12-14 14:10:37 -05:00
|
|
|
@products = new Products()
|
|
|
|
@supermodel.loadCollection(@products, 'products')
|
|
|
|
|
|
|
|
onLoaded: ->
|
|
|
|
@basicProduct = @products.findWhere { name: 'basic_subscription' }
|
|
|
|
@yearProduct = @products.findWhere { name: 'year_subscription' }
|
|
|
|
super()
|
2014-12-02 23:01:53 -05:00
|
|
|
|
2014-12-03 12:13:51 -05:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
2015-02-04 16:54:35 -05:00
|
|
|
@setupParentButtonPopover()
|
|
|
|
@setupParentInfoPopover()
|
2015-04-07 18:54:55 -04:00
|
|
|
@setupPaymentMethodsInfoPopover()
|
2015-02-04 16:54:35 -05:00
|
|
|
|
|
|
|
setupParentButtonPopover: ->
|
|
|
|
popoverTitle = $.i18n.t 'subscribe.parent_email_title'
|
|
|
|
popoverTitle += '<button type="button" class="close" onclick="$('.parent-button').popover('hide');">×</button>'
|
2015-04-10 16:46:53 -04:00
|
|
|
popoverContent = ->
|
|
|
|
$('.parent-button-popover-content').html()
|
2015-02-04 16:54:35 -05:00
|
|
|
@$el.find('.parent-button').popover(
|
|
|
|
animation: true
|
|
|
|
html: true
|
|
|
|
placement: 'top'
|
|
|
|
trigger: 'click'
|
|
|
|
title: popoverTitle
|
|
|
|
content: popoverContent
|
|
|
|
container: @$el
|
|
|
|
).on 'shown.bs.popover', =>
|
2015-02-27 19:07:41 -05:00
|
|
|
application.tracker?.trackEvent 'Subscription ask parent button click'
|
2015-02-04 16:54:35 -05:00
|
|
|
|
|
|
|
setupParentInfoPopover: ->
|
2014-12-03 12:13:51 -05:00
|
|
|
popoverTitle = $.i18n.t 'subscribe.parents_title'
|
2015-04-10 17:35:13 -04:00
|
|
|
levelsCompleted = me.get('stats')?.gamesCompleted or 'several'
|
|
|
|
popoverContent = "<p>" + $.i18n.t('subscribe.parents_blurb1', nLevels: levelsCompleted) + "</p>"
|
|
|
|
popoverContent += "<p>" + $.i18n.t('subscribe.parents_blurb1a') + "</p>"
|
2014-12-03 14:00:42 -05:00
|
|
|
popoverContent += "<p>" + $.i18n.t('subscribe.parents_blurb2') + "</p>"
|
|
|
|
popoverContent += "<p>" + $.i18n.t('subscribe.parents_blurb3') + "</p>"
|
2014-12-13 12:00:33 -05:00
|
|
|
#popoverContent = popoverContent.replace /9[.,]99/g, '3.99' # Sale
|
2014-12-03 12:13:51 -05:00
|
|
|
@$el.find('#parents-info').popover(
|
|
|
|
animation: true
|
|
|
|
html: true
|
|
|
|
placement: 'top'
|
|
|
|
trigger: 'hover'
|
|
|
|
title: popoverTitle
|
|
|
|
content: popoverContent
|
|
|
|
container: @$el
|
|
|
|
).on 'shown.bs.popover', =>
|
2015-02-27 19:07:41 -05:00
|
|
|
application.tracker?.trackEvent 'Subscription parent hover'
|
2014-12-03 12:13:51 -05:00
|
|
|
|
2015-04-07 18:54:55 -04:00
|
|
|
setupPaymentMethodsInfoPopover: ->
|
|
|
|
popoverTitle = $.i18n.t('subscribe.payment_methods_title')
|
2015-07-01 13:20:14 -04:00
|
|
|
popoverTitle += '<button type="button" class="close" onclick="$('#payment-methods-info').popover('hide');">×</button>'
|
2015-04-07 18:54:55 -04:00
|
|
|
popoverContent = "<p>" + $.i18n.t('subscribe.payment_methods_blurb1') + "</p>"
|
|
|
|
popoverContent += "<p>" + $.i18n.t('subscribe.payment_methods_blurb2') + " <a href='mailto:support@codecombat.com'>support@codecombat.com</a>."
|
|
|
|
@$el.find('#payment-methods-info').popover(
|
|
|
|
animation: true
|
|
|
|
html: true
|
|
|
|
placement: 'top'
|
|
|
|
trigger: 'click'
|
|
|
|
title: popoverTitle
|
|
|
|
content: popoverContent
|
|
|
|
container: @$el
|
|
|
|
).on 'shown.bs.popover', =>
|
|
|
|
application.tracker?.trackEvent 'Subscription payment methods hover'
|
|
|
|
|
2015-02-04 16:54:35 -05:00
|
|
|
onClickParentSendButton: (e) ->
|
|
|
|
# TODO: Popover sometimes dismisses immediately after send
|
|
|
|
|
2015-04-10 16:46:53 -04:00
|
|
|
email = @$el.find('.popover-content .parent-input').val()
|
2015-02-04 16:54:35 -05:00
|
|
|
unless /[\w\.]+@\w+\.\w+/.test email
|
2015-04-10 16:46:53 -04:00
|
|
|
@$el.find('.popover-content .parent-input').parent().addClass('has-error')
|
|
|
|
@$el.find('.popover-content .parent-email-validator').show()
|
2015-02-04 16:54:35 -05:00
|
|
|
return false
|
|
|
|
|
|
|
|
request = @supermodel.addRequestResource 'send_one_time_email', {
|
|
|
|
url: '/db/user/-/send_one_time_email'
|
|
|
|
data: {email: email, type: 'subscribe modal parent'}
|
|
|
|
method: 'POST'
|
|
|
|
}, 0
|
|
|
|
request.load()
|
|
|
|
|
2015-04-10 16:46:53 -04:00
|
|
|
@$el.find('.popover-content .email-parent-form').hide()
|
|
|
|
@$el.find('.popover-content .email-parent-complete').show()
|
2015-02-04 16:54:35 -05:00
|
|
|
false
|
|
|
|
|
2015-04-10 16:46:53 -04:00
|
|
|
onClickParentEmailCompleteButton: (e) ->
|
|
|
|
@$el.find('.parent-button').popover('hide')
|
|
|
|
|
2014-12-02 23:01:53 -05:00
|
|
|
onClickPurchaseButton: (e) ->
|
2015-12-14 14:10:37 -05:00
|
|
|
return unless @basicProduct and @yearProduct
|
2014-12-02 23:01:53 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-12-06 12:35:13 -05:00
|
|
|
return @openModalView new AuthModal() if me.get('anonymous')
|
2015-02-27 19:07:41 -05:00
|
|
|
application.tracker?.trackEvent 'Started subscription purchase'
|
2014-12-06 15:37:54 -05:00
|
|
|
options = {
|
2014-12-04 16:44:27 -05:00
|
|
|
description: $.i18n.t('subscribe.stripe_description')
|
2015-12-14 14:10:37 -05:00
|
|
|
amount: @basicProduct.get('amount')
|
2015-10-09 11:05:34 -04:00
|
|
|
alipay: if me.get('country') is 'china' or (me.get('preferredLanguage') or 'en-US')[...2] is 'zh' then true else 'auto'
|
2015-03-16 13:50:15 -04:00
|
|
|
alipayReusable: true
|
2014-12-06 15:37:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# SALE LOGIC
|
|
|
|
# overwrite amount with sale price
|
|
|
|
# maybe also put in another description with details about how long it lasts, etc
|
2014-12-09 02:43:52 -05:00
|
|
|
# NOTE: Do not change this price without updating the context.price in getRenderData
|
2014-12-11 21:39:03 -05:00
|
|
|
# NOTE: And, the popover content if necessary
|
2014-12-13 12:00:33 -05:00
|
|
|
#options = {
|
|
|
|
# description: 'Monthly Subscription (HoC sale)'
|
|
|
|
# amount: 399
|
|
|
|
#}
|
2014-12-06 15:37:54 -05:00
|
|
|
|
2014-12-09 16:57:01 -05:00
|
|
|
@purchasedAmount = options.amount
|
2015-08-21 14:10:06 -04:00
|
|
|
stripeHandler.open(options)
|
2014-12-09 16:57:01 -05:00
|
|
|
|
2015-08-21 14:10:06 -04:00
|
|
|
onClickSaleButton: (e) ->
|
|
|
|
@playSound 'menu-button-click'
|
|
|
|
return @openModalView new AuthModal() if me.get('anonymous')
|
|
|
|
application.tracker?.trackEvent 'Started 1 year subscription purchase'
|
|
|
|
options =
|
|
|
|
description: $.i18n.t('subscribe.stripe_description_year_sale')
|
2015-12-14 14:10:37 -05:00
|
|
|
amount: @yearProduct.get('amount')
|
2015-10-09 11:05:34 -04:00
|
|
|
alipay: if me.get('country') is 'china' or (me.get('preferredLanguage') or 'en-US')[...2] is 'zh' then true else 'auto'
|
2015-08-21 14:10:06 -04:00
|
|
|
alipayReusable: true
|
|
|
|
@purchasedAmount = options.amount
|
2014-12-06 15:37:54 -05:00
|
|
|
stripeHandler.open(options)
|
2014-12-02 23:01:53 -05:00
|
|
|
|
|
|
|
onStripeReceivedToken: (e) ->
|
|
|
|
@state = 'purchasing'
|
|
|
|
@render()
|
2014-12-03 00:27:12 -05:00
|
|
|
|
2015-12-14 14:10:37 -05:00
|
|
|
if @purchasedAmount is @basicProduct.get('amount')
|
2015-08-21 14:10:06 -04:00
|
|
|
stripe = _.clone(me.get('stripe') ? {})
|
2015-12-14 14:10:37 -05:00
|
|
|
stripe.planID = @basicProduct.get('planID')
|
2015-08-21 14:10:06 -04:00
|
|
|
stripe.token = e.token.id
|
|
|
|
me.set 'stripe', stripe
|
|
|
|
@listenToOnce me, 'sync', @onSubscriptionSuccess
|
|
|
|
@listenToOnce me, 'error', @onSubscriptionError
|
|
|
|
me.patch({headers: {'X-Change-Plan': 'true'}})
|
2015-12-14 14:10:37 -05:00
|
|
|
else if @purchasedAmount is @yearProduct.get('amount')
|
2015-08-21 14:10:06 -04:00
|
|
|
# Purchasing a year
|
|
|
|
data =
|
|
|
|
stripe:
|
|
|
|
token: e.token.id
|
|
|
|
timestamp: new Date().getTime()
|
|
|
|
jqxhr = $.post('/db/subscription/-/year_sale', data)
|
|
|
|
jqxhr.done (data, textStatus, jqXHR) =>
|
|
|
|
application.tracker?.trackEvent 'Finished 1 year subscription purchase', value: @purchasedAmount
|
|
|
|
me.set 'stripe', data?.stripe if data?.stripe?
|
|
|
|
Backbone.Mediator.publish 'subscribe-modal:subscribed', {}
|
|
|
|
@playSound 'victory'
|
|
|
|
@hide()
|
|
|
|
jqxhr.fail (xhr, textStatus, errorThrown) =>
|
|
|
|
console.error 'We got an error subscribing with Stripe from our server:', textStatus, errorThrown
|
|
|
|
application.tracker?.trackEvent 'Failed to finish 1 year subscription purchase', status: textStatus, value: @purchasedAmount
|
|
|
|
stripe = me.get('stripe') ? {}
|
|
|
|
delete stripe.token
|
|
|
|
delete stripe.planID
|
|
|
|
if xhr.status is 402
|
|
|
|
@state = 'declined'
|
|
|
|
else
|
|
|
|
@state = 'unknown_error'
|
|
|
|
@stateMessage = "#{xhr.status}: #{xhr.responseText}"
|
|
|
|
@render()
|
|
|
|
else
|
|
|
|
console.error "Unexpected purchase amount received", @purchasedAmount, e
|
|
|
|
@state = 'unknown_error'
|
|
|
|
@stateMessage = "Uknown problem occurred while processing Stripe request"
|
2014-12-03 00:27:12 -05:00
|
|
|
|
2014-12-03 12:13:51 -05:00
|
|
|
onSubscriptionSuccess: ->
|
2015-03-24 18:02:57 -04:00
|
|
|
application.tracker?.trackEvent 'Finished subscription purchase', value: @purchasedAmount
|
2014-12-03 14:19:10 -05:00
|
|
|
Backbone.Mediator.publish 'subscribe-modal:subscribed', {}
|
2014-12-03 12:13:51 -05:00
|
|
|
@playSound 'victory'
|
2014-12-03 00:27:12 -05:00
|
|
|
@hide()
|
|
|
|
|
2014-12-03 12:13:51 -05:00
|
|
|
onSubscriptionError: (user, response, options) ->
|
|
|
|
console.error 'We got an error subscribing with Stripe from our server:', response
|
2015-08-21 14:10:06 -04:00
|
|
|
application.tracker?.trackEvent 'Failed to finish subscription purchase', status: options.xhr?.status, value: @purchasedAmount
|
2014-12-03 00:27:12 -05:00
|
|
|
stripe = me.get('stripe') ? {}
|
|
|
|
delete stripe.token
|
|
|
|
delete stripe.planID
|
2015-03-19 18:02:45 -04:00
|
|
|
# TODO: Need me.set('stripe', stripe) here?
|
2014-12-03 12:13:51 -05:00
|
|
|
xhr = options.xhr
|
|
|
|
if xhr.status is 402
|
|
|
|
@state = 'declined'
|
|
|
|
else
|
|
|
|
@state = 'unknown_error'
|
|
|
|
@stateMessage = "#{xhr.status}: #{xhr.responseText}"
|
|
|
|
@render()
|