Class: CatarseStripe::Payment::StripeController

Inherits:
ApplicationController show all
Defined in:
app/controllers/catarse_stripe/payment/stripe_controller.rb

Constant Summary collapse

SCOPE =
"projects.backers.checkout"
AUTH_SCOPE =
"users.auth"

Instance Method Summary collapse

Instance Method Details

#authObject

Makes the call to @client.auth_code.authorize_url from auth.html.erg



22
23
24
25
26
27
28
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 22

def auth
  @stripe_user = current_user
  respond_to do |format|
    format.html
    format.js
  end
end

#callbackObject

Brings back the authcode from Stripe and makes another call to Stripe to convert to a authtoken



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 31

def callback
  @stripe_user = current_user
  code = params[:code]

  @response = @client.auth_code.get_token(code, {
  :headers => {'Authorization' => "Bearer(::Configuration['stripe_secret_key'])"} #Platform Secret Key
  })
  
  #Save PROJECT owner's new keys
  @stripe_user.stripe_access_token = @response.token
  @stripe_user.stripe_key = @response.params['stripe_publishable_key']
  @stripe_user.stripe_userid = @response.params['stripe_user_id']
  @stripe_user.save

  
  return redirect_to payment_stripe_auth_path(@stripe_user)
rescue Stripe::AuthenticationError => e
  ::Airbrake.notify({ :error_class => "Stripe #Pay Error", :error_message => "Stripe #Pay Error: #{e.inspect}", :parameters => params}) rescue nil
  Rails.logger.info "-----> #{e.inspect}"
  flash[:error] = e.message
  return redirect_to main_app.user_path(@stripe_user)
end

#cancelObject



171
172
173
174
175
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 171

def cancel
  backer = current_user.backs.find params[:id]
  flash[:failure] = t('stripe_cancel', scope: SCOPE)
  redirect_to main_app.new_project_backer_path(backer.project)
end

#chargeObject



89
90
91
92
93
94
95
96
97
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 89

def charge
  @backer = current_user.backs.find params[:id]
  access_token = @backer.project.stripe_access_token #Project Owner SECRET KEY

  respond_to do |format|
    format.html
    format.js
  end
end

#ipnObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 58

def ipn
  backer = Backer.where(:payment_id => details.id).first
  if backer
    notification = backer.payment_notifications.new({
      extra_data: JSON.parse(params.to_json.force_encoding(params['charset']).encode('utf-8'))
    })
    notification.save!
    backer.update_attribute :payment_service_fee => details.fee
  end
  return render status: 200, nothing: true
rescue Stripe::CardError => e
  ::Airbrake.notify({ :error_class => "Stripe Notification Error", :error_message => "Stripe Notification Error: #{e.inspect}", :parameters => params}) rescue nil
  return render status: 200, nothing: true
end

#notificationsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 73

def notifications
  backer = Backer.find params[:id]
  details = Stripe::Charge.retrieve(
      id: backer.payment_id
      )
  if details.paid = true
    build_notification(backer, details)
    render status: 200, nothing: true
  else
    render status: 404, nothing: true
  end
rescue Stripe::CardError => e
  ::Airbrake.notify({ :error_class => "Stripe Notification Error", :error_message => "Stripe Notification Error: #{e.inspect}", :parameters => params}) rescue nil
  render status: 404, nothing: true
end

#payObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 99

def pay
  @backer = current_user.backs.find params[:id]
  access_token = @backer.project.stripe_access_token #Project Owner SECRET KEY
  begin
    customer = Stripe::Customer.create(
    {
       email: @backer.payer_email,
       card: params[:stripeToken]
       },
    access_token
    )
    
    @backer.update_attributes(:payment_token => customer.id)
    @backer.save
    flash[:notice] = "Stripe Customer ID Saved!"

    response = Stripe::Charge.create(
      {
      customer: @backer.payment_token,
      amount: @backer.price_in_cents,
      currency: 'usd',
      description: t('stripe_description', scope: SCOPE, :project_name => @backer.project.name, :value => @backer.display_value),
      application_fee: @backer.platform_fee.to_i
      },
      access_token #ACCESS_TOKEN (Stripe Secret Key of Connected Project Owner NOT platform)
    )

    @backer.update_attributes({
      :payment_method => 'Stripe',
      :payment_token => response.customer, #Stripe Backer Customer_id
      :payment_id => response.id, #Stripe Backer Payment Id
      :confirmed => response.paid #Paid = True, Confirmed =  true
    })
    @backer.save

    build_notification(@backer, response)
  
    redirect_to payment_success_stripe_url(id: @backer.id)
  rescue Stripe::CardError => e
    ::Airbrake.notify({ :error_class => "Stripe #Pay Error", :error_message => "Stripe #Pay Error: #{e.inspect}", :parameters => params}) rescue nil
    Rails.logger.info "-----> #{e.inspect}"
    flash[:error] = e.message
    return redirect_to main_app.new_project_backer_path(@backer.project)
  end
end

#reviewObject



54
55
56
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 54

def review

end

#successObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/catarse_stripe/payment/stripe_controller.rb', line 145

def success
  backer = current_user.backs.find params[:id]
  access_token = backer.project.stripe_access_token #Project Owner SECRET KEY
  begin
    details = Stripe::Charge.retrieve(
    {
      id: backer.payment_id
      },
      access_token
      )

    build_notification(backer, details)

    if details.id
      backer.update_attribute :payment_id, details.id
    end
    stripe_flash_success
    redirect_to main_app.thank_you_project_backer_path(project_id: backer.project.id, id: backer.id)
  rescue Stripe::CardError => e
    ::Airbrake.notify({ :error_class => "Stripe Error", :error_message => "Stripe Error: #{e.message}", :parameters => params}) rescue nil
    Rails.logger.info "-----> #{e.inspect}"
    flash[:error] = e.message
    return redirect_to main_app.new_project_backer_path(backer.project)
  end
end