Class: Ishapi::GameuiController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ishapi/gameui_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #long_term_token, #test

Instance Method Details

#buy_starsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/ishapi/gameui_controller.rb', line 7

def buy_stars
  authorize! :buy_stars, @profile
  puts! @current_user, 'current_user'
  puts! @profile, 'profile'

  payment = Ish::Payment.new :email => @profile.email, :amount => params[:amount],
    profile: @profile

  ::Stripe.api_key = STRIPE_SK
  acct = Stripe::Account.create(
    :country => 'US',
    :type => 'custom'
  )
  charge = ::Stripe::Charge.create(
    :amount => params[:amount],
    :currency => 'usd',
    :source => params[:stripeToken],
    :destination => {
      :account => acct,
    }
  )
  puts! charge, 'charge'
  payment.charge = JSON.parse( charge.to_json )
  payment.save
  if payment.persisted?
    # add the star
    @profile.n_stars += 1
    @profile.save
    if !@profile.persisted?
      raise 'could not save profile, somehow'
    end
  else
    puts! payment.errors.messages
  end

  render json: { status: 'ok', n_stars: @profile.n_stars }
end

#do_purchaseObject

spend the star



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/ishapi/gameui_controller.rb', line 46

def do_purchase
  authorize! :do_purchase, ::Gameui
  item = params[:className].constantize.find_by_slug( params[:slug] )

  raise 'no such item'     if !item
  raise 'too little funds' if @profile.n_stars < item.premium_tier

  ::IshModels::UserProfile.with_session do
    @profile.update_attributes( n_stars: @profile.n_stars - item.premium_tier )
    @purchase = ::Gameui::PremiumPurchase.create! user_profile: @profile, item: item
  end

  render json: @purchase
rescue ::Exception => e
  render json: e
end