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'
|
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
|
|
|
|
|
|
|
originalProducts: [
|
2014-11-17 18:15:02 -05:00
|
|
|
{ price: '$4.99', gems: 5000, amount: 499, id: 'gems_5', i18n: 'buy_gems.few_gems' }
|
|
|
|
{ price: '$9.99', gems: 11000, amount: 999, id: 'gems_10', i18n: 'buy_gems.pile_gems' }
|
|
|
|
{ price: '$19.99', gems: 25000, amount: 1999, id: 'gems_20', i18n: 'buy_gems.chest_gems' }
|
2014-11-12 13:23:43 -05:00
|
|
|
]
|
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:
|
2014-11-12 20:16:34 -05:00
|
|
|
'click .product button': 'onClickProductButton'
|
2014-11-12 13:23:43 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super(options)
|
2014-11-17 18:15:02 -05:00
|
|
|
@state = 'standby'
|
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
|
|
|
|
@products = @originalProducts
|
2014-11-12 13:23:43 -05:00
|
|
|
|
2014-11-12 12:40:55 -05:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
2014-11-12 13:37:09 -05:00
|
|
|
c.products = @products
|
2014-11-17 18:15:02 -05:00
|
|
|
c.state = @state
|
2014-11-29 13:44:58 -05:00
|
|
|
c.stateMessage = @stateMessage
|
2014-11-12 12:40:55 -05:00
|
|
|
return c
|
2014-11-12 23:21:14 -05:00
|
|
|
|
2014-11-12 13:37:09 -05:00
|
|
|
onIPadProducts: (e) ->
|
2014-11-12 15:14:47 -05:00
|
|
|
newProducts = []
|
|
|
|
for iapProduct in e.products
|
2014-11-12 23:21:14 -05:00
|
|
|
localProduct = _.find @originalProducts, { id: iapProduct.id }
|
2014-11-12 15:14:47 -05:00
|
|
|
continue unless localProduct
|
|
|
|
localProduct.price = iapProduct.price
|
|
|
|
newProducts.push localProduct
|
2014-11-12 23:21:14 -05:00
|
|
|
@products = _.sortBy newProducts, 'gems'
|
2014-11-12 13:37:09 -05:00
|
|
|
@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()
|
|
|
|
product = _.find @products, { id: 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
|
2014-12-02 23:01:53 -05:00
|
|
|
application.tracker?.trackEvent 'Started purchase', { productID: productID }
|
2014-11-17 18:15:02 -05:00
|
|
|
stripeHandler.open({
|
|
|
|
description: $.t(product.i18n)
|
|
|
|
amount: product.amount
|
|
|
|
})
|
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) ->
|
|
|
|
@timestampForPurchase = new Date().getTime()
|
|
|
|
data = {
|
|
|
|
productID: @productBeingPurchased.id
|
|
|
|
stripe: {
|
|
|
|
token: e.token.id
|
|
|
|
timestamp: @timestampForPurchase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@state = 'purchasing'
|
|
|
|
@render()
|
|
|
|
jqxhr = $.post('/db/payment', data)
|
|
|
|
jqxhr.done(=>
|
|
|
|
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) ->
|
2014-11-12 15:14:47 -05:00
|
|
|
product = _.find @products, { id: 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()
|