Class: Ishapi::PaymentsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #long_term_token, #vote

Instance Method Details

#createObject

this is for invoices on wasya.co, isn’t it? 20200712



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
44
# File 'app/controllers/ishapi/payments_controller.rb', line 13

def create
  authorize! :open_permission, ::Ishapi
  begin
    invoice = Ish::Invoice.where( :email => params[:email], :number => params[:number] ).first
    payment = Ish::Payment.new :invoice => invoice, :email => params[:email], :amount => params[:amount]
    amount_cents  = ( params[:amount].to_f * 100 ).to_i

    ::Stripe.api_key = STRIPE_SK
    acct = Stripe::Account.create(
      :country => 'US',
      :type => 'custom'
    )
    charge = ::Stripe::Charge.create(
      :amount => amount_cents,
      :currency => 'usd',
      :source => params[:token][:id],
      :destination => {
        :account => acct,
      }
    )

    payment.charge = JSON.parse( charge.to_json )
    if payment.save
      render :json => { :status => :ok }
    else
      render :status => 404, :json => {}
    end
  rescue Mongoid::Errors::DocumentNotFound => e
    puts! e, 'e'
    render :status => 404, :json => {}
  end
end

#create2Object

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

@TODO: cannot proceed if already is_purchasing?



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/ishapi/payments_controller.rb', line 52

def create2
  authorize! :create, ::Ish::Payment
  current_user.profile.update_attributes({ is_purchasing: true })

  begin
    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" },
    })

    payment = Ish::Payment.create!(
      client_secret: intent.client_secret,
      email: current_user.email,
      payment_intent_id: intent.id,
      profile_id: current_user.profile.id,
    )

    render json: { client_secret: intent.client_secret }
  rescue Mongoid::Errors::DocumentNotFound => e
    puts! e, '#create2 Mongoid::Errors::DocumentNotFound'
    render :status => 404, :json => e
  end
end

#stripe_confirmObject

webhook



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/ishapi/payments_controller.rb', line 83

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 => e
    puts! e, 'could not #stripe_confirm'
    render status: 400, json: { status: :not_ok }
    return
  end

  payment_intent = event.data.object

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

    payment.update_attributes( status: :confirmed )
    n_unlocks = payment.profile.n_unlocks + 1 # @TODO: it's not always 5? adjust

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

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

#unlockObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/ishapi/payments_controller.rb', line 109

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

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

  @current_user.profile.inc( n_unlocks: -item.premium_tier )

  purchase = ::Gameui::PremiumPurchase.create!( item: item, user_profile: @current_user.profile, )

  @profile = @current_user.profile
  render 'ishapi/users/account'
end