Class: Ishapi::PaymentsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#test

Instance Method Details

#createObject



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

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,
      }
    )
    # puts! charge, 'charge'

    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