codecombat/app/views/core/SubscribeModal.coffee

234 lines
9.3 KiB
CoffeeScript
Raw Normal View History

2014-12-02 23:01:53 -05:00
ModalView = require 'views/core/ModalView'
template = require 'templates/core/subscribe-modal'
2014-12-02 23:01:53 -05:00
stripeHandler = require 'core/services/stripe'
utils = require 'core/utils'
CreateAccountModal = require 'views/core/CreateAccountModal'
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'
i18nData:
levelsCount: '145'
worldsCount: '5'
heroesCount: '14'
bonusLevelsCount: '95'
2014-12-02 23:01:53 -05:00
subscriptions:
'stripe:received-token': 'onStripeReceivedToken'
events:
'click #close-modal': 'hide'
'click .popover-content .parent-send': 'onClickParentSendButton'
'click .email-parent-complete button': 'onClickParentEmailCompleteButton'
'click .purchase-button': 'onClickPurchaseButton'
'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')
2015-12-23 13:07:32 -05:00
2015-12-14 14:10:37 -05:00
onLoaded: ->
@basicProduct = @products.findWhere { name: 'basic_subscription' }
@yearProduct = @products.findWhere { name: 'year_subscription' }
2016-01-11 12:52:46 -05:00
if countrySpecificProduct = @products.findWhere { name: "#{me.get('country')}_basic_subscription" }
@basicProduct = countrySpecificProduct
@yearProduct = @products.findWhere { name: "#{me.get('country')}_year_subscription" } # probably null
2015-12-14 14:10:37 -05:00
super()
2014-12-02 23:01:53 -05:00
2014-12-03 12:13:51 -05:00
afterRender: ->
super()
@setupParentButtonPopover()
@setupParentInfoPopover()
@setupPaymentMethodsInfoPopover()
2016-01-11 12:52:46 -05:00
if @basicProduct
@$el.find('.gem-amount').html $.i18n.t('subscribe.feature4').replace('{{gems}}', @basicProduct.get('gems'))
2016-01-16 06:38:31 -05:00
@playSound 'game-menu-open'
setupParentButtonPopover: ->
popoverTitle = $.i18n.t 'subscribe.parent_email_title'
popoverTitle += '<button type="button" class="close" onclick="$(&#39;.parent-button&#39;).popover(&#39;hide&#39;);">&times;</button>'
popoverContent = ->
$('.parent-button-popover-content').html()
@$el.find('.parent-button').popover(
animation: true
html: true
placement: 'top'
trigger: 'click'
title: popoverTitle
content: popoverContent
container: @$el
).on 'shown.bs.popover', =>
application.tracker?.trackEvent 'Subscription ask parent button click'
setupParentInfoPopover: ->
2015-12-15 16:24:02 -05:00
return unless @products.size()
2014-12-03 12:13:51 -05:00
popoverTitle = $.i18n.t 'subscribe.parents_title'
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>"
2015-12-23 13:07:32 -05:00
popoverContent += "<p>" + $.i18n.t('subscribe.parents_blurb2') + "</p>"
2016-01-11 12:52:46 -05:00
price = (@basicProduct.get('amount') / 100).toFixed(2)
2015-12-15 16:24:02 -05:00
# TODO: Update i18next and use its own interpolation system instead
popoverContent = popoverContent.replace('{{price}}', price)
2014-12-03 14:00:42 -05:00
popoverContent += "<p>" + $.i18n.t('subscribe.parents_blurb3') + "</p>"
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', =>
application.tracker?.trackEvent 'Subscription parent hover'
2014-12-03 12:13:51 -05:00
setupPaymentMethodsInfoPopover: ->
2015-12-15 16:24:02 -05:00
return unless @products.size()
popoverTitle = $.i18n.t('subscribe.payment_methods_title')
2016-01-11 12:52:46 -05:00
threeMonthPrice = (@basicProduct.get('amount') * 3 / 100).toFixed(2)
if @yearProduct
yearPrice = (@yearProduct.get('amount') / 100).toFixed(2)
else
yearPrice = (@basicProduct.get('amount') * 12 / 100).toFixed(2)
2015-07-01 13:20:14 -04:00
popoverTitle += '<button type="button" class="close" onclick="$(&#39;#payment-methods-info&#39;).popover(&#39;hide&#39;);">&times;</button>'
2015-12-23 13:07:32 -05:00
popoverContent = "<p>" + $.i18n.t('subscribe.payment_methods_blurb1') + "</p>"
2015-12-15 16:24:02 -05:00
# TODO: Update i18next and use its own interpolation system instead
2016-01-11 12:52:46 -05:00
popoverContent = popoverContent.replace('{{three_month_price}}', threeMonthPrice)
popoverContent = popoverContent.replace('{{year_price}}', yearPrice)
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'
onClickParentSendButton: (e) ->
# TODO: Popover sometimes dismisses immediately after send
email = @$el.find('.popover-content .parent-input').val()
unless /[\w\.]+@\w+\.\w+/.test email
@$el.find('.popover-content .parent-input').parent().addClass('has-error')
@$el.find('.popover-content .parent-email-validator').show()
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()
@$el.find('.popover-content .email-parent-form').hide()
@$el.find('.popover-content .email-parent-complete').show()
false
onClickParentEmailCompleteButton: (e) ->
@$el.find('.parent-button').popover('hide')
2014-12-02 23:01:53 -05:00
onClickPurchaseButton: (e) ->
2016-01-11 12:52:46 -05:00
return unless @basicProduct
2014-12-02 23:01:53 -05:00
@playSound 'menu-button-click'
return @openModalView new CreateAccountModal() if me.get('anonymous')
application.tracker?.trackEvent 'Started subscription purchase'
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')
alipay: if me.get('country') is 'china' or (me.get('preferredLanguage') or 'en-US')[...2] is 'zh' then true else 'auto'
alipayReusable: true
}
2014-12-09 16:57:01 -05:00
@purchasedAmount = options.amount
stripeHandler.open(options)
2014-12-09 16:57:01 -05:00
onClickSaleButton: (e) ->
@playSound 'menu-button-click'
return @openModalView new CreateAccountModal() if me.get('anonymous')
application.tracker?.trackEvent 'Started 1 year subscription purchase'
discount = @basicProduct.get('amount') * 12 - @yearProduct.get('amount')
discountString = (discount/100).toFixed(2)
options =
2015-12-23 13:07:32 -05:00
description: $.i18n.t('subscribe.stripe_description_year_sale').replace('{{discount}}', discountString)
2015-12-14 14:10:37 -05:00
amount: @yearProduct.get('amount')
alipay: if me.get('country') is 'china' or (me.get('preferredLanguage') or 'en-US')[...2] is 'zh' then true else 'auto'
alipayReusable: true
@purchasedAmount = options.amount
stripeHandler.open(options)
2014-12-02 23:01:53 -05:00
onStripeReceivedToken: (e) ->
@state = 'purchasing'
@render()
2015-12-14 14:10:37 -05:00
if @purchasedAmount is @basicProduct.get('amount')
stripe = _.clone(me.get('stripe') ? {})
2015-12-14 14:10:37 -05:00
stripe.planID = @basicProduct.get('planID')
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')
# 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"
onSubscriptionSuccess: ->
application.tracker?.trackEvent 'Finished subscription purchase', value: @purchasedAmount
Backbone.Mediator.publish 'subscribe-modal:subscribed', {}
2014-12-03 12:13:51 -05:00
@playSound 'victory'
@hide()
onSubscriptionError: (user, response, options) ->
2014-12-03 12:13:51 -05:00
console.error 'We got an error subscribing with Stripe from our server:', response
application.tracker?.trackEvent 'Failed to finish subscription purchase', status: options.xhr?.status, value: @purchasedAmount
stripe = me.get('stripe') ? {}
delete stripe.token
delete stripe.planID
# 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()
2016-01-16 06:38:31 -05:00
onHidden: ->
super()
@playSound 'game-menu-close'