2014-11-28 20:49:41 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2014-11-12 12:40:55 -05:00
|
|
|
template = require 'templates/play/modal/buy-gems-modal'
|
2014-11-28 20:49:41 -05:00
|
|
|
stripeHandler = require 'core/services/stripe'
|
|
|
|
utils = require 'core/utils'
|
2015-02-09 12:22:18 -05:00
|
|
|
SubscribeModal = require 'views/core/SubscribeModal'
|
2015-12-14 14:10:37 -05:00
|
|
|
Products = require 'collections/Products'
|
2014-11-12 12:40:55 -05:00
|
|
|
|
|
|
|
module.exports = class BuyGemsModal extends ModalView
|
|
|
|
id: 'buy-gems-modal'
|
|
|
|
template: template
|
|
|
|
plain: true
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2014-11-12 13:23:43 -05:00
|
|
|
subscriptions:
|
2014-11-12 13:37:09 -05:00
|
|
|
'ipad:products': 'onIPadProducts'
|
2014-11-12 13:23:43 -05:00
|
|
|
'ipad:iap-complete': 'onIAPComplete'
|
2014-11-17 18:15:02 -05:00
|
|
|
'stripe:received-token': 'onStripeReceivedToken'
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2014-11-12 13:23:43 -05:00
|
|
|
events:
|
2015-02-25 17:24:32 -05:00
|
|
|
'click .product button:not(.start-subscription-button)': 'onClickProductButton'
|
2015-02-08 12:06:03 -05:00
|
|
|
'click #close-modal': 'hide'
|
2015-02-09 12:22:18 -05:00
|
|
|
'click .start-subscription-button': 'onClickStartSubscription'
|
2014-11-12 13:23:43 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super(options)
|
2014-12-12 18:27:58 -05:00
|
|
|
@timestampForPurchase = new Date().getTime()
|
2014-11-17 18:15:02 -05:00
|
|
|
@state = 'standby'
|
2015-12-14 14:10:37 -05:00
|
|
|
@products = new Products()
|
|
|
|
@products.comparator = 'amount'
|
2014-11-12 13:37:09 -05:00
|
|
|
if application.isIPadApp
|
|
|
|
@products = []
|
2014-11-12 23:21:14 -05:00
|
|
|
Backbone.Mediator.publish 'buy-gems-modal:update-products'
|
|
|
|
else
|
2015-12-14 14:10:37 -05:00
|
|
|
@supermodel.loadCollection(@products, 'products')
|
2014-12-12 18:27:58 -05:00
|
|
|
$.post '/db/payment/check-stripe-charges', (something, somethingElse, jqxhr) =>
|
|
|
|
if jqxhr.status is 201
|
|
|
|
@state = 'recovered_charge'
|
|
|
|
@render()
|
2014-11-12 13:23:43 -05:00
|
|
|
|
2015-12-14 14:10:37 -05:00
|
|
|
onLoaded: ->
|
2016-01-11 12:52:46 -05:00
|
|
|
@basicProduct = @products.findWhere { name: 'basic_subscription' }
|
|
|
|
if countrySpecificProduct = @products.findWhere { name: "#{me.get('country')}_basic_subscription" }
|
|
|
|
@basicProduct = countrySpecificProduct
|
2015-12-14 14:10:37 -05:00
|
|
|
@products.reset @products.filter (product) -> _.string.startsWith(product.get('name'), 'gems_')
|
|
|
|
super()
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2015-08-15 17:42:57 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
return unless @supermodel.finished()
|
|
|
|
@playSound 'game-menu-open'
|
2016-01-11 12:52:46 -05:00
|
|
|
if @basicProduct
|
|
|
|
@$el.find('.subscription-gem-amount').text $.i18n.t('buy_gems.price').replace('{{gems}}', @basicProduct.get('gems'))
|
2015-08-15 17:42:57 -04:00
|
|
|
|
|
|
|
onHidden: ->
|
|
|
|
super()
|
|
|
|
@playSound 'game-menu-close'
|
|
|
|
|
2014-11-12 13:37:09 -05:00
|
|
|
onIPadProducts: (e) ->
|
2015-12-14 14:10:37 -05:00
|
|
|
# TODO: Update to handle new products collection
|
|
|
|
# newProducts = []
|
|
|
|
# for iapProduct in e.products
|
|
|
|
# localProduct = _.find @originalProducts, { id: iapProduct.id }
|
|
|
|
# continue unless localProduct
|
|
|
|
# localProduct.price = iapProduct.price
|
|
|
|
# newProducts.push localProduct
|
|
|
|
# @products = _.sortBy newProducts, 'gems'
|
|
|
|
# @render()
|
2014-11-12 12:40:55 -05:00
|
|
|
|
|
|
|
onClickProductButton: (e) ->
|
2014-11-26 09:58:23 -05:00
|
|
|
@playSound 'menu-button-click'
|
2014-11-17 18:15:02 -05:00
|
|
|
productID = $(e.target).closest('button').val()
|
2016-05-04 09:36:54 -04:00
|
|
|
# Don't throw error when product is not found
|
|
|
|
if productID.length == 0
|
|
|
|
return
|
2015-12-14 14:10:37 -05:00
|
|
|
product = @products.findWhere { name: productID }
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2014-11-12 12:40:55 -05:00
|
|
|
if application.isIPadApp
|
2014-11-12 13:23:43 -05:00
|
|
|
Backbone.Mediator.publish 'buy-gems-modal:purchase-initiated', { productID: productID }
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2014-11-12 12:40:55 -05:00
|
|
|
else
|
2015-02-26 17:24:00 -05:00
|
|
|
application.tracker?.trackEvent 'Started gem purchase', { productID: productID }
|
2014-11-17 18:15:02 -05:00
|
|
|
stripeHandler.open({
|
2015-12-14 14:10:37 -05:00
|
|
|
description: $.t(product.get('i18n'))
|
|
|
|
amount: product.get('amount')
|
2015-02-25 14:33:51 -05:00
|
|
|
bitcoin: true
|
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'
|
2014-11-17 18:15:02 -05:00
|
|
|
})
|
2014-11-26 09:58:23 -05:00
|
|
|
|
2014-11-17 18:15:02 -05:00
|
|
|
@productBeingPurchased = product
|
2014-11-26 09:58:23 -05:00
|
|
|
|
2014-11-17 18:15:02 -05:00
|
|
|
onStripeReceivedToken: (e) ->
|
|
|
|
data = {
|
2015-12-14 14:10:37 -05:00
|
|
|
productID: @productBeingPurchased.get('name')
|
2014-11-17 18:15:02 -05:00
|
|
|
stripe: {
|
|
|
|
token: e.token.id
|
|
|
|
timestamp: @timestampForPurchase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@state = 'purchasing'
|
|
|
|
@render()
|
|
|
|
jqxhr = $.post('/db/payment', data)
|
|
|
|
jqxhr.done(=>
|
2014-12-09 16:57:01 -05:00
|
|
|
application.tracker?.trackEvent 'Finished gem purchase',
|
2015-12-14 14:10:37 -05:00
|
|
|
productID: @productBeingPurchased.get('name')
|
|
|
|
value: @productBeingPurchased.get('amount')
|
2014-11-17 18:15:02 -05:00
|
|
|
document.location.reload()
|
|
|
|
)
|
|
|
|
jqxhr.fail(=>
|
|
|
|
if jqxhr.status is 402
|
|
|
|
@state = 'declined'
|
|
|
|
@stateMessage = arguments[2]
|
|
|
|
else if jqxhr.status is 500
|
|
|
|
@state = 'retrying'
|
|
|
|
f = _.bind @onStripeReceivedToken, @, e
|
|
|
|
_.delay f, 2000
|
|
|
|
else
|
|
|
|
@state = 'unknown_error'
|
2014-11-29 13:44:58 -05:00
|
|
|
@stateMessage = "#{jqxhr.status}: #{jqxhr.responseText}"
|
2014-11-17 18:15:02 -05:00
|
|
|
@render()
|
|
|
|
)
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2014-11-12 13:23:43 -05:00
|
|
|
onIAPComplete: (e) ->
|
2015-12-14 14:10:37 -05:00
|
|
|
product = @products.findWhere { name: e.productID }
|
2014-11-12 13:23:43 -05:00
|
|
|
purchased = me.get('purchased') ? {}
|
2014-11-12 15:14:47 -05:00
|
|
|
purchased = _.clone purchased
|
2014-11-12 13:23:43 -05:00
|
|
|
purchased.gems ?= 0
|
2014-11-12 15:14:47 -05:00
|
|
|
purchased.gems += product.gems
|
2014-11-12 13:23:43 -05:00
|
|
|
me.set('purchased', purchased)
|
2014-11-12 23:21:14 -05:00
|
|
|
@hide()
|
2015-02-09 12:22:18 -05:00
|
|
|
|
|
|
|
onClickStartSubscription: (e) ->
|
|
|
|
@openModalView new SubscribeModal()
|
|
|
|
window.tracker?.trackEvent 'Show subscription modal', category: 'Subscription', label: 'buy gems modal'
|