Class: Ishapi::PaymentsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#exception, #home, #long_term_token, #vote

Instance Method Details

#createObject

vp 2020-07-21 This is for guyd vp 2022-03-01 It’s been a while! vp 2022-09-04 continue vp 2023-03-29 Continue

@TODO: cannot proceed if already is_purchasing? @TODO: and this doesn’t say what you’re buying! herehere



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
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/ishapi/payments_controller.rb', line 19

def create
  authorize! :create, Ish::Payment.new

  @current_profile.update_attributes({ is_purchasing: true })

  amount_cents  = params[:amount_cents].to_i # @TODO: change

  ::Stripe.api_key = ::STRIPE_SK
  intent = Stripe::PaymentIntent.create({
    amount: amount_cents,
    currency: 'usd',
    metadata: { integration_check: "accept_a_payment" },
  })

  item_type = case params[:item_type]
  when 'Ish::Location'
    'Gameui::Map'
  else
    throw "item_type not implemented: #{params[:item_type]}"
  end
  item = item_type.constantize.find params[:item_id]

  payment = Ish::Payment.create!(
    client_secret: intent.client_secret,
    email: @current_profile.email,
    item: item,
    payment_intent_id: intent.id,
    profile_id: @current_profile.id,
  )
  render json: {
    client_secret: intent.client_secret,
    clientSecret: intent.client_secret,
  }

end

#stripe_confirmObject

webhook vp 2023-03-29 Continue



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/ishapi/payments_controller.rb', line 59

def stripe_confirm
  authorize! :open_permission, ::Ishapi
  payload = request.body.read
  event = nil
  begin
    event = Stripe::Event.construct_from(JSON.parse(payload, symbolize_names: true))
  rescue StandardError => err
    puts! err, 'could not #stripe_confirm'
    render status: 400, json: { status: :not_ok }
    return
  end

  payment_intent = event.data.object
  puts! payment_intent, 'payment_intent'

  payment = Ish::Payment.where( payment_intent_id: payment_intent.id ).first
  if payment && payment_intent['status'] == 'succeeded'

    payment.update_attributes( status: Ish::Payment::STATUS_CONFIRMED )
    n_unlocks = payment.profile.n_unlocks + 1 # @TODO: it's not always 5, adjust! herehere

    payment.profile.update_attributes!( n_unlocks: n_unlocks, is_purchasing: false )
  end

  render status: 200, json: { status: :ok }
end

#unlockObject

Spend an unlock without spending money. vp 2022-03-01



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/ishapi/payments_controller.rb', line 89

def unlock
  authorize! :unlock, ::Ish::Payment
  item = Object::const_get(params['kind']).find params['id']

  existing = Ish::Payment.where( profile: @current_profile, item: item ).first
  if existing
    render status: 200, json: { status: :ok, message: 'already purchased' }
    return
  end

  @current_profile.inc( n_unlocks: -item.premium_tier )

  purchase = ::Ish::Payment.create!( item: item, profile: @current_profile, )

  @profile = @current_profile
  render 'ishapi/user_profiles/account'
end